一、前言:
前面已经和SurfaceFlinger
建立好了连接,那么,本章介绍下如何去向SurfaceFlinger
申请我们的画布(也就是Surface)。
二、获取Surface流程:
上面创建好了Client,也就是创建好了连接,我们要绘制一张图片,首先得拿到画布,这个由CreateSurface
完成:
status_t BootAnimation::readyToRun() {// 。。。// 创建SurfaceControl, 参数指定名称 ("BootAnimation")、宽度、高度和像素格式 (PIXEL_FORMAT_RGB_565)sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);SurfaceComposerClient::Transaction t;t.setLayer(control, 0x40000000).apply();// 获得对应的Surface(画布)sp<Surface> s = control->getSurface();// 。。。
}
可以看出,先获取SurfaceControl
,然后从SurfaceControl
当中取出了Surface
,那么,这个API调用,后面都有谁在配合呢?
1、APP侧开始请求:
这个session()里面其实就是返回一个mSession
,我们都知道mSession
是sp<SurfaceComposerClient>
类型,进去看看:
sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,PixelFormat format, uint32_t flags,SurfaceControl* parent,LayerMetadata metadata) {sp<SurfaceControl> s;createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));return s;
}
再进去看看:
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,PixelFormat format,sp<SurfaceControl>* outSurface, uint32_t flags,SurfaceControl* parent,LayerMetadata metadata) {sp<SurfaceControl> sur;status_t err = mStatus;if (mStatus == NO_ERROR) {sp<IBinder> handle; // Surfacesp<IBinder> parentHandle;sp<IGraphicBufferProducer> gbp; // SF端Layer的生产者对象if (parent != nullptr) {parentHandle = parent->getHandle();}// 这个mClienterr = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),&handle, &gbp);ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));if (err == NO_ERROR) {// 创建SurfaceControl*outSurface = new SurfaceControl(this, handle, gbp, true