This source file includes following definitions.
- lock
- buffer
- yuvView
#include "LockedSurface.h"
#define IMAGE_FORMAT_YV12 842094169
#define ALIGN(x, mask) (((x) + (mask)-1) & ~((mask)-1))
LockedSurface *LockedSurface::lock(JNIEnv *env, jobject surface) {
LockedSurface *output = new LockedSurface;
output->window_ = ANativeWindow_fromSurface(env, surface);
if (int err = ANativeWindow_lock(output->window_, &(output->buffer_), NULL)) {
ANativeWindow_release(output->window_);
delete output;
output = nullptr;
}
return output;
}
LockedSurface::~LockedSurface() {
ANativeWindow_unlockAndPost(window_);
ANativeWindow_release(window_);
window_ = nullptr;
}
const ANativeWindow_Buffer &LockedSurface::buffer() const {
return buffer_;
}
YuvBufferT LockedSurface::yuvView() const {
if (buffer_.format != IMAGE_FORMAT_YV12) {
return YuvBufferT();
}
uint8_t *lumaPtr = reinterpret_cast<uint8_t *>(buffer_.bits);
uint32_t lumaRowStrideBytes = buffer_.stride;
uint32_t lumaSizeBytes = lumaRowStrideBytes * buffer_.height;
uint32_t chromaRowStrideBytes = ALIGN(buffer_.stride / 2, 16);
uint32_t chromaSizeBytes = chromaRowStrideBytes * buffer_.height / 2;
uint8_t *chromaVPtr = lumaPtr + lumaSizeBytes;
uint8_t *chromaUPtr = lumaPtr + lumaSizeBytes + chromaSizeBytes;
return YuvBufferT(lumaPtr,
buffer_.width, buffer_.height,
1 , lumaRowStrideBytes,
chromaUPtr,
buffer_.width / 2, buffer_.height / 2,
1 , chromaRowStrideBytes,
chromaVPtr,
buffer_.width / 2, buffer_.height / 2,
1 , chromaRowStrideBytes
);
}