admin 管理员组

文章数量: 887021


2023年12月17日发(作者:native手办图片)

我们先来看VirtualDisplaySurfaceclass VirtualDisplaySurface : public DisplaySurface, public BnGraphicBufferProducer, private ConsumerBase {厉害了,我的VirtualDisplaySurface,继承了BnGraphicBufferProducer和ConsumerBase。这是即做Producer,也做Consumer。先来看VirtualDisplaySurface的构造函数:

* frameworks/native/services/surfaceflinger/DisplayHardware/tualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, const sp& sink, const sp& bqProducer, const sp& bqConsumer, const String8& name): ConsumerBase(bqConsumer), mHwc(hwc), mDisplayId(dispId), mDisplayName(name), mSource{}, mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED), mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED), mOutputUsage(GRALLOC_USAGE_HW_COMPOSER), mProducerSlotSource(0), mProducerBuffers(), mQueueBufferOutput(), mSinkBufferWidth(0), mSinkBufferHeight(0), mCompositionType(COMPOSITION_UNKNOWN), mFbFence(Fence::NO_FENCE), mOutputFence(Fence::NO_FENCE), mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT), mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT), mDbgState(DBG_STATE_IDLE), mDbgLastCompositionType(COMPOSITION_UNKNOWN), mMustRecompose(false), mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv){ mSource[SOURCE_SINK] = sink; mSource[SOURCE_SCRATCH] = bqProducer; resetPerFrameState(); int sinkWidth, sinkHeight; sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth); sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight); mSinkBufferWidth = sinkWidth; mSinkBufferHeight = sinkHeight; // Pick the buffer format to request from the sink when not rendering to it // with GLES. If the consumer needs CPU access, use the default format // set by the consumer. Otherwise allow gralloc to decide the format based // on usage bits. int sinkUsage; sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage); if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { int sinkFormat; sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat); mDefaultOutputFormat = sinkFormat; } else { mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; } mOutputFormat = mDefaultOutputFormat; ConsumerBase::mName = String8::format("VDS: %s", ()); mConsumer->setConsumerName(ConsumerBase::mName); mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER); mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight); sink->setAsyncMode(true); IGraphicBufferProducer::QueueBufferOutput output; mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);}ImageReader那边过来的Surface,被保存在mSource[SOURCE_SINK] 中新创建的BufferQueue的Producer保存在mSource[SOURCE_SCRATCH] 中新的BufferQueue的Consumer给到mConsumer。

private int acquireNextSurfaceImage(SurfaceImage si) { synchronized (mCloseLock) { // A null image will eventually be returned if ImageReader is already closed. int status = ACQUIRE_NO_BUFS; if (mIsReaderValid) { status = nativeImageSetup(si); } switch (status) { case ACQUIRE_SUCCESS: geValid = true; case ACQUIRE_NO_BUFS: case ACQUIRE_MAX_IMAGES: break; default: throw new AssertionError("Unknown nativeImageSetup return code " + status); } // Only keep track the successfully acquired image, as the native buffer is only mapped // for such case. if (status == ACQUIRE_SUCCESS) { (si); } return status; } }这类终于调到关键的了,nativeImageSetup函数。对应的JNI函数为ImageReader_imageSetup。ImageReader_imageSetup函数:static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz, jobject image) { ALOGV("%s:", __FUNCTION__); JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz); if (ctx == NULL) { jniThrowException(env, "java/lang/IllegalStateException", "ImageReader is not initialized or was already closed"); return -1; } BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer(); BufferItem* buffer = ctx->getBufferItem(); if (buffer == NULL) { ALOGW("Unable to acquire a buffer item, very likely client tried to acquire more than" " maxImages buffers"); return ACQUIRE_MAX_IMAGES; } status_t res = bufferConsumer->acquireBuffer(buffer, 0); if (res != OK) { ... ... } // Add some extra checks for non-opaque formats. if (!isFormatOpaque(ctx->getBufferFormat())) { ... ... } // Set SurfaceImage instance member variables Image_setBufferItem(env, image, buffer); env->SetLongField(image, tamp, static_cast(buffer->mTimestamp)); return ACQUIRE_SUCCESS;}


本文标签: 对应 函数