4.3_APP申请创建Surface的过程

本文深入探讨了Android应用程序如何通过SurfaceComposerClient创建Surface的过程。首先创建SurfaceControl,然后通过与SurfaceFlinger交互创建Surface。涉及的关键步骤包括客户端创建handle和gbp,以及服务端的Layer创建和BufferQueue的使用,最终实现Surface的生成与管理。
摘要由CSDN通过智能技术生成

4.3_APP申请创建Surface的过程

先创建surfaceControl再创建surface

Client

在测试程序SurfaceTest.cpp 中如下:

创建client,surface大小。

sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),

 surfaceControl

 向BnSurfaceComposerClient发送CREATE_SURFACE,最终得到 handlegbp(BpGraphicBufferProducer)

sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
	sp<IBinder> handle;//创建变量handle
    sp<IGraphicBufferProducer> gbp;//创建变量gdb
	mClient->createSurface(&handle, &gbp
	mClient//mClient在 onFirstRef 方法中创建ISurfaceComposerClient.cpp创建
		 virtual status_t createSurface(sp<IBinder>* handle,sp<IGraphicBufferProducer>* gbp// bp***中构造、发送数据   ISurfaceComposerClient.cpp
			//构造数据
			remote()->transact(CREATE_SURFACE //发送数据
            //接收数据并构建handle与gbp 
			*handle = reply.readStrongBinder();
			*gbp = interface_cast<IGraphicBufferProducer>(reply.readStrongBinder());//根据BpBinder创建BpGraphicBufferProducer
                new BpBinder(handle); 
            sur = new SurfaceControl(this, handle, gbp);
            return sur;//根据从Binder获取到的数据创建SurfaceControl

根据得到的数据创建 new SurfaceControl,  对其构造参数赋值如下:

new SurfaceControl(this, handle, gbp);
	SurfaceControl::SurfaceControl(
        const sp<SurfaceComposerClient>& client,
        const sp<IBinder>& handle,
        const sp<IGraphicBufferProducer>& gbp)
    : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)

server

BnSurfaceComposerClient收到CREATE_SURFACE后解析可知是需要创建一个Layer ,这里也创建了handlegbp。

status_t BnSurfaceComposerClient::onTransact//ISurfaceComposerClient.cpp
	case CREATE_SURFACE:
		sp<IBinder> handle;
        sp<IGraphicBufferProducer> gbp;
		status_t result = createSurface(...&handle, &gbp);
			createSurface //Client.cpp
				sp<MessageBase> msg = new MessageCreateLayer(... handle, gbp,//创建
				mFlinger->postMessageSync(msg);//提交
					handler() {//导致message中的handler函数被调用
						flinger->createLayer(handle, gbp ... //SurfaceFlinger.cpp
							createNormalLayer(...,gbp, &layer
								outLayer= new Layer(this,//outLayer从哪里来
								status_t err = (*outLayer)->setBuffers(w, h, format, flags);
									*handle = (*outLayer)->getHandle();
									*gbp = (*outLayer)->getProducer();

handle其中就是sufaceFlinger

sp<IBinder> Layer::getHandle() {
    return new Handle(mFlinger, this);
}

gpd就是mProducer生产者;

sp<IGraphicBufferProducer> Layer::getProducer() const {
    return mProducer;
}

new Layer 创建了buff,并指向了生产者与消费者。

new Layer(this,//创建new Layer的过程 Client.cpp
	void Layer::onFirstRef()//第一次引用时调用Layer.cpp
		 BufferQueue::createBufferQueue(&producer, &consumer);
			sp<BufferQueueCore> core(new BufferQueueCore(allocator));
				 BufferQueueCore类成员mSlots(),其中类型为typedef BufferSlot SlotsType[NUM_BUFFER_SLOTS];
				 mAllocator = composer->createGraphicBufferAlloc();//BufferQueueCore.cpp
			sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core));
				mCore(core),
				mSlots(core->mSlots),为那一块数组
			sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
			    mCore(core),
				mSlots(core->mSlots),为那一块数组
			这个producer与consumer都指向了同一块内存。
		mProducer = new MonitoredProducer(producer, mFlinger);//创建生产者给到了mFlinger
			
		mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);//创建消费者给到 
			SurfaceFlingerConsumer: GLConsumer(consumer, tex, GLConsumer::TEXTURE_//构造函数
				GLConsumer::GLConsumer(c//调用其父类的构造函数 GLConsumer.cpp
					 ConsumerBase(bq, isControlledByApp)//再调用更高父类的构造函数
						mConsumer(bufferQueue)成员mConsumer指向consumer

surface

紧接着创建surface让其受surfaceControl管理。

sp<Surface> surface = surfaceControl->getSurface();
	mSurfaceData = new Surface(mGraphicBufferProducer, false);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值