static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) { ALOGV("%s", __FUNCTION__); GraphicBuffer* buffer; int fenceFd = -1; Image_getNativeContext(env, thiz, &buffer, &fenceFd); if (buffer == NULL) { jniThrowException(env, "java/lang/IllegalStateException", "Image is not initialized"); return; } void* pData = NULL; android_ycbcr ycbcr = android_ycbcr(); status_t res; int format = Image_getFormat(env, thiz); int flexFormat = format; if (isPossiblyYUV(format)) { // ImageWriter doesn't use crop by itself, app sets it, use the no crop version. res = buffer->lockAsyncYCbCr(GRALLOC_USAGE_SW_WRITE_OFTEN, &ycbcr, fenceFd); // Clear the fenceFd as it is already consumed by lock call. Image_setFenceFd(env, thiz, /*fenceFd*/-1); if (res != OK) { jniThrowRuntimeException(env, "lockAsyncYCbCr failed for YUV buffer"); return; } pData = ycbcr.y; flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888; } // lockAsyncYCbCr for YUV is unsuccessful. if (pData == NULL) { res = buffer->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, &pData, fenceFd); if (res != OK) { jniThrowRuntimeException(env, "lockAsync failed"); return; } } image->data = reinterpret_cast<uint8_t*>(pData); image->width = buffer->getWidth(); image->height = buffer->getHeight(); image->format = format; image->flexFormat = flexFormat; image->stride = (ycbcr.y != NULL) ? static_cast<uint32_t>(ycbcr.ystride) : buffer->getStride(); image->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb); image->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr); image->chromaStride = static_cast<uint32_t>(ycbcr.cstride); image->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step); ALOGV("Successfully locked the image"); // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer, // and we don't set them here. }
static GraphicBuffer* create_composible_buffer(int width, int height) { uint32_t bufferFlags = GraphicBuffer::USAGE_SW_WRITE_RARELY | GraphicBuffer::USAGE_HW_COMPOSER; GraphicBuffer* buffer = new GraphicBuffer(width, height, PIXEL_FORMAT_RGBA_8888, bufferFlags); status_t err = buffer->initCheck(); if (err) { LOGE("error creating GraphicBuffer: %s", strerror(-err)); return NULL; } Rect rect = buffer->getBounds(); LOGE("GraphicBuffer information"); LOGE("width : %d", buffer->getWidth()); LOGE("height: %d", buffer->getHeight()); LOGE("stride: %d", buffer->getStride()); LOGE("usage : 0x%08x", buffer->getUsage()); LOGE("format: %d", buffer->getPixelFormat()); LOGE("bounds: (%d,%d) - (%d,%d)", rect.left, rect.top, rect.right, rect.bottom); return buffer; }