前言 在谈及 Android GUI 框架前,先从底层视角梳理一下显示的流程。在 Android 系统中,引入了状态栏、导航栏、壁纸和背景的图层概念,需要把这些图层进行 alpha blending 后显示。
在嵌入式 linux 系统下显示界面,可以直接操作 framebuffer 的内存;但 Android 应用并不能直接操作 framebuffer,应用需要借助 SurfaceFlinger。
SurfaceFlinger 通过 Gralloc HAL 向 ashmem 申请内存,供应用使用。同时使用 OpenGL 和 HardwareComposer 来合成 Surface。
SurfaceFlinger内部机制 可以把应用理解为客户端,SurfaceFlinger 理解为服务端,在 SurfaceFlinger 中用 Client 对象表示应用,有多少个应用就有多少个 Client 对象。
Client 对象中包含了 Layer 对象,用来对应应用层的 SurfaceControl ;其中 Layer 对象包含消费者 mSurfaceFlingerConsumer 和 生产者 mBufferQueue 成员变量,并在 onFirstRef() 方法中构建。
1 2 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer; sp<BufferQueue> mBufferQueue;
与之对应的, Surface 对象中包含生产者 mGraphicBufferProducer 和 BufferSlot ,后者是 Surface 内部存储 buffer 的地方,最多有32个,仅当 dequeueBuffer 时才会分配真正的空间。这里基本可以看出 Surface 对象是通过 mGraphicBufferProducer 来获取 buffer ,并将它记录在 mSlots 数组中供后续使用。
1 2 3 4 5 6 7 sp<IGraphicBufferProducer> mGraphicBufferProducer; BufferSlot mSlots[NUM_BUFFER_SLOTS]; struct BufferSlot { sp<GraphicBuffer> buffer; Region dirtyRegion; };
通过系统的测试用例 resize 了解应用的基本流程,追踪代码分析 SurfaceFlinger 的工作方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @ frameworks/native/services/surfaceflinger/tests/resize/resize.cpp int main(int argc, char** argv) { // set up the thread-pool sp<ProcessState> proc(ProcessState::self()); ProcessState::self()->startThreadPool(); // create a client to surfaceflinger sp<SurfaceComposerClient> client = new SurfaceComposerClient(); //<1> sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"), 160, 240, PIXEL_FORMAT_RGB_565, 0); //<2> sp<Surface> surface = surfaceControl->getSurface(); //<3> SurfaceComposerClient::openGlobalTransaction(); surfaceControl->setLayer(100000); SurfaceComposerClient::closeGlobalTransaction(); ANativeWindow_Buffer outBuffer; surface->lock(&outBuffer, NULL); //<4> ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height); surface->unlockAndPost(); <5> surface->lock(&outBuffer); android_memset16((uint16_t*)outBuffer.bits, 0x07E0, bpr*outBuffer.height); surface->unlockAndPost(); SurfaceComposerClient::openGlobalTransaction(); surfaceControl->setSize(320, 240); SurfaceComposerClient::closeGlobalTransaction(); IPCThreadState::self()->joinThreadPool(); return 0; }
对应的流程图如下:
SurfaceComposerClient 当应用程序请求 SurfaceFlinger 服务时,首先需要构造 SurfaceComposerClient 对象,通过 SurfaceComposerClient 对象访问 SurfaceFlinger 服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @ frameworks/native/include/gui/SurfaceComposerClient.h class SurfaceComposerClient : public RefBase { private: virtual void onFirstRef(); Composer& getComposer(); mutable Mutex mLock; status_t mStatus; sp<ISurfaceComposerClient> mClient; Composer& mComposer; }
因为 SurfaceComposerClient 继承于 RefBase 类,在创建 SurfaceComposerClient 对象后,第一次引用对象时,调用 onFirstRef() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @ frameworks/native/libs/gui/SurfaceComposerClient.cpp SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT), mComposer(Composer::getInstance()) { } void SurfaceComposerClient::onFirstRef() { //获取 BpSurfaceComposer 的实例化对象 sp<ISurfaceComposer> sm(ComposerService::getComposerService()); if (sm != 0) { //获取 BpSurfaceComposerClient 实例化对象 sp<ISurfaceComposerClient> conn = sm->createConnection(); if (conn != 0) { mClient = conn; mStatus = NO_ERROR; } } }
onFirstRef() 方法首先调用 ComposerService 类的 getComposerService() 方法获取 SurfaceFlinger 的代理对象 BpSurfaceComposer。
1 2 3 4 5 6 7 8 9 10 11 12 @ frameworks/native/libs/gui/SurfaceComposerClient.cpp /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() { ComposerService& instance = ComposerService::getInstance(); Mutex::Autolock _l(instance.mLock); if (instance.mComposerService == NULL) { ComposerService::getInstance().connectLocked(); assert(instance.mComposerService != NULL); ALOGD("ComposerService reconnected"); } return instance.mComposerService; }
通过单例模式调用 getInstance() 方法构造 ComposerService 对象,期间会调用 connectLocked() 方法获取 BpServiceComposer 服务的代理对象 ,并保存到成员变量 mComposerService 中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @ frameworks/native/libs/gui/SurfaceComposerClient.cpp ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService); ComposerService::ComposerService() : Singleton<ComposerService>() { Mutex::Autolock _l(mLock); connectLocked(); } void ComposerService::connectLocked() { const String16 name("SurfaceFlinger"); while (getService(name, &mComposerService) != NO_ERROR) { usleep(250000); } assert(mComposerService != NULL); // Create the death listener. class DeathObserver : public IBinder::DeathRecipient { ComposerService& mComposerService; virtual void binderDied(const wp<IBinder>& who) { ALOGW("ComposerService remote (surfaceflinger) died [%p]", who.unsafe_get()); mComposerService.composerServiceDied(); } public: DeathObserver(ComposerService& mgr) : mComposerService(mgr) { } }; mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this)); mComposerService->asBinder()->linkToDeath(mDeathObserver); }
上面已经获取到 BpSurfaceComposer 对象,调用它的 createConnection() 方法定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @ frameworks/native/libs/gui/ISurfaceComposer.cpp class BpSurfaceComposer : public BpInterface<ISurfaceComposer> { public: BpSurfaceComposer(const sp<IBinder>& impl) : BpInterface<ISurfaceComposer>(impl) { } virtual sp<ISurfaceComposerClient> createConnection() { uint32_t n; Parcel data, reply; data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor()); remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply); return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder()); } }
createConnection() 方法中调用 remote()->transact 会导致 SurfaceFlinger 类的 onTransact()方法被调用,进而调用 BnSurfaceComposer::onTransact() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp status_t SurfaceFlinger::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch (code) { case CREATE_CONNECTION: case CREATE_DISPLAY: case SET_TRANSACTION_STATE: case BOOT_FINISHED: case BLANK: case UNBLANK: ... } status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); } @ frameworks/native/libs/gui/ISurfaceComposer.cpp status_t BnSurfaceComposer::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case CREATE_CONNECTION: { CHECK_INTERFACE(ISurfaceComposer, data, reply); sp<IBinder> b = createConnection()->asBinder(); reply->writeStrongBinder(b); return NO_ERROR; } ... default: { return BBinder::onTransact(code, data, reply, flags); } } return NO_ERROR; }
SurfaceFlinger 继承 BnSurfaceComposer,作为 Binder 的服务端,复写了 createConnection()方法,该方法会构造 Client 对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @ frameworks/native/services/surfaceflinger/SurfaceFlinger.h class SurfaceFlinger : public BnSurfaceComposer, private IBinder::DeathRecipient, private HWComposer::EventHandler @ frameworks/native/include/gui/ISurfaceComposer.h class BnSurfaceComposer: public BnInterface<ISurfaceComposer> @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() { sp<ISurfaceComposerClient> bclient; sp<Client> client(new Client(this)); status_t err = client->initCheck(); if (err == NO_ERROR) { bclient = client; } return bclient; }
Client 继承 BnSurfaceComposerClient ,保存 SurfaceFlinger 对象的引用到成员变量 mFlinger。最终应用程序通过 ISurfaceComposer::createConnection() 方法获得 BpSurfaceComposerClient 代理对象,并将其保存在成员变量 mClient 。
1 2 3 4 5 6 @ frameworks/native/services/surfaceflinger/Client.cpp Client::Client(const sp<SurfaceFlinger>& flinger) : mFlinger(flinger) { }
总结:应用程序创建 SurfaceFlinger Client,需要首先获取 SurfaceFlinger 服务,通过 SurfaceFlinger::createConnection() 方法建立应用程序和 SurfaceFlinger 的连接。SurfaceFlinger 会创建一个 BnSurfaceComposerClient 本地对象,同时应用程序会得到一个 BpSurfaceComposerClient 代理类对象,后面会通过这个代理类对象创建 Surface。
SurfaceControl 根据 SurfaceComposerClient 对象创建 SurfafeControl 对象,其实就是通过 SurfaceFlinger 创建 Layer,下面分析代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @ frameworks/native/libs/gui/SurfaceComposerClient.cpp sp<SurfaceControl> SurfaceComposerClient::createSurface( const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) { sp<SurfaceControl> sur; if (mStatus == NO_ERROR) { sp<IBinder> handle; sp<IGraphicBufferProducer> gbp; status_t err = mClient->createSurface(name, w, h, format, flags, &handle, &gbp); ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err)); if (err == NO_ERROR) { sur = new SurfaceControl(this, handle, gbp); } } return sur; }
remote()->transact 根据 Binder 机制,调用 BnSurfaceComposerClient::onTransact() 本地方法,进而调用到 Client::createSurface() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 @ frameworks/native/libs/gui/ISurfaceComposerClient.cpp class BpSurfaceComposerClient : public BpInterface<ISurfaceComposerClient> { public: virtual status_t createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) { Parcel data, reply; data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor()); data.writeString8(name); data.writeInt32(w); data.writeInt32(h); data.writeInt32(format); data.writeInt32(flags); remote()->transact(CREATE_SURFACE, data, &reply); *handle = reply.readStrongBinder(); *gbp = interface_cast<IGraphicBufferProducer>(reply.readStrongBinder()); return reply.readInt32(); } } status_t BnSurfaceComposerClient::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case CREATE_SURFACE: { CHECK_INTERFACE(ISurfaceComposerClient, data, reply); String8 name = data.readString8(); uint32_t w = data.readInt32(); uint32_t h = data.readInt32(); PixelFormat format = data.readInt32(); uint32_t flags = data.readInt32(); sp<IBinder> handle; sp<IGraphicBufferProducer> gbp; status_t result = createSurface(name, w, h, format, flags, &handle, &gbp); reply->writeStrongBinder(handle); reply->writeStrongBinder(gbp->asBinder()); reply->writeInt32(result); return NO_ERROR; } break; } }
createSurface() 方法定义了一个消息类 MessageCreateLayer ,然后把它的对象通过 postMessageSync() 方法发送出去,这个消息是以同步的方式发送,因此函数结束后可以直接返回结果。所以会调用到消息处理方法 handler(),直接调用 SurfaceFlinger::createLayer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 @ frameworks/native/services/surfaceflinger/Client.cpp status_t Client::createSurface( const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) { class MessageCreateLayer : public MessageBase { SurfaceFlinger* flinger; Client* client; sp<IBinder>* handle; sp<IGraphicBufferProducer>* gbp; status_t result; const String8& name; uint32_t w, h; PixelFormat format; uint32_t flags; public: MessageCreateLayer(SurfaceFlinger* flinger, const String8& name, Client* client, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) : flinger(flinger), client(client), handle(handle), gbp(gbp), name(name), w(w), h(h), format(format), flags(flags) { } status_t getResult() const { return result; } virtual bool handler() { result = flinger->createLayer(name, client, w, h, format, flags, handle, gbp); return true; } }; sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), name, this, w, h, format, flags, handle, gbp); mFlinger->postMessageSync(msg); return static_cast<MessageCreateLayer*>( msg.get() )->getResult(); }
createLayer() 方法根据 flags 参数选择创建不同类型的 Layer ( NormalLayer 和 DimLayer ),这里以 createNormalLayer 为例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp status_t SurfaceFlinger::createLayer( const String8& name, const sp<Client>& client, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) { //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string()); if (int32_t(w|h) < 0) { ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)", int(w), int(h)); return BAD_VALUE; } status_t result = NO_ERROR; sp<Layer> layer; switch (flags & ISurfaceComposerClient::eFXSurfaceMask) { case ISurfaceComposerClient::eFXSurfaceNormal: case ISurfaceComposerClient::eFXSurfaceNoDisp: result = createNormalLayer(client, name, w, h, flags, format, handle, gbp, &layer); break; case ISurfaceComposerClient::eFXSurfaceDim: result = createDimLayer(client, name, w, h, flags, handle, gbp, &layer); break; default: result = BAD_VALUE; break; } if (result == NO_ERROR) { addClientLayer(client, *handle, *gbp, layer); if(!(flags & ISurfaceComposerClient::eFXSurfaceNoDisp)) { setTransactionFlags(eTransactionNeeded); } else { ALOGD("%s flags=%x", __FUNCTION__, flags); } } return result; }
createNormalLayer() 方法根据 format 参数不同创建 Layer 对象 outLayer ,调用 Layer::getHandle() 方法赋值给 handle,调用 Layer::getBufferQueue() 方法赋值给 gbp 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client, const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer) { // initialize the surfaces switch (format) { case PIXEL_FORMAT_TRANSPARENT: case PIXEL_FORMAT_TRANSLUCENT: format = PIXEL_FORMAT_RGBA_8888; break; case PIXEL_FORMAT_OPAQUE: #ifdef NO_RGBX_8888 format = PIXEL_FORMAT_RGB_565; #else format = PIXEL_FORMAT_RGBX_8888; #endif break; } #ifdef NO_RGBX_8888 if (format == PIXEL_FORMAT_RGBX_8888) format = PIXEL_FORMAT_RGBA_8888; #endif *outLayer = new Layer(this, client, name, w, h, flags); status_t err = (*outLayer)->setBuffers(w, h, format, flags); if (err == NO_ERROR) { *handle = (*outLayer)->getHandle(); *gbp = (*outLayer)->getBufferQueue(); } ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err)); return err; }
看下 Layer 类的构造方法和 onFirstRef() 方法的实现,这里会构造图像缓冲区队列 BufferQueue ,同时创建消费者对象 SurfaceFlingerConsumer 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w, uint32_t h, uint32_t flags) : contentDirty(false), sequence(uint32_t(android_atomic_inc(&sSequence))), mFlinger(flinger), mTextureName(-1U), mPremultipliedAlpha(true), mName("unnamed"), mDebug(false), mFormat(PIXEL_FORMAT_NONE), mOpaqueLayer(true), mTransactionFlags(0), mQueuedFrames(0), mCurrentTransform(0), mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), mCurrentOpacity(true), mRefreshPending(false), mFrameLatencyNeeded(false), mFiltering(false), mNeedsFiltering(false), mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), mSecure(false), mProtectedByApp(false), mHasSurface(false), mClientRef(client), mCaptureScreen(false) { mCurrentCrop.makeInvalid(); mFlinger->getRenderEngine().genTextures(1, &mTextureName); mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); uint32_t layerFlags = 0; if (flags & ISurfaceComposerClient::eHidden) layerFlags = layer_state_t::eLayerHidden; if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false; mName = name; mCurrentState.active.w = w; mCurrentState.active.h = h; mCurrentState.active.crop.makeInvalid(); mCurrentState.z = 0; mCurrentState.alpha = 0xFF; mCurrentState.layerStack = 0; mCurrentState.flags = layerFlags; mCurrentState.sequence = 0; mCurrentState.transform.set(0, 0); mCurrentState.requested = mCurrentState.active; // drawing state & current state are identical mDrawingState = mCurrentState; nsecs_t displayPeriod = flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); mFrameTracker.setDisplayRefreshPeriod(displayPeriod); } void Layer::onFirstRef() { // Creates a custom BufferQueue for SurfaceFlingerConsumer to use mBufferQueue = new SurfaceTextureLayer(mFlinger); mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mBufferQueue, mTextureName); mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); mSurfaceFlingerConsumer->setFrameAvailableListener(this); mSurfaceFlingerConsumer->setName(mName); #ifdef TARGET_DISABLE_TRIPLE_BUFFERING #warning "disabling triple buffering" mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2); #else mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3); #endif const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); updateTransformHint(hw); }
BufferQueue 是 IGraphicBufferProducer 服务端的实现,类的内部有个成员数组 BufferSlot mSlots[NUM_BUFFER_SLOTS],这与 Surface 类中 BufferSlot 的定义并不一样。这里 mGraphicBuffer 用以记录 BufferSlot 的缓冲区,mBufferState 用来追踪每个缓冲区的状态。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @ frameworks/native/include/gui/BufferQueue.h class BufferQueue : public BnGraphicBufferProducer, public BnGraphicBufferConsumer, private IBinder::DeathRecipient { public: virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async, uint32_t width, uint32_t height, uint32_t format, uint32_t usage); virtual status_t queueBuffer(int buf, const QueueBufferInput& input, QueueBufferOutput* output); virtual void cancelBuffer(int buf, const sp<Fence>& fence); virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen); virtual status_t releaseBuffer(int buf, uint64_t frameNumber, EGLDisplay display, EGLSyncKHR fence, const sp<Fence>& releaseFence); struct BufferSlot { sp<GraphicBuffer> mGraphicBuffer; enum BufferState { FREE = 0, DEQUEUED = 1, QUEUED = 2, ACQUIRED = 3 }; BufferState mBufferState; } BufferSlot mSlots[NUM_BUFFER_SLOTS]; sp<IGraphicBufferAlloc> mGraphicBufferAlloc; sp<IConsumerListener> mConsumerListener; }
紧接着调用 addClientLayer() 方法,把 Layer 对象和 SurfaceFlinger 以及应用程序关联起来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp void SurfaceFlinger::addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc) { // attach this layer to the client client->attachLayer(handle, lbc); // add this layer to the current state list Mutex::Autolock _l(mStateLock); mCurrentState.layersSortedByZ.add(lbc); mGraphicBufferProducerList.add(gbc->asBinder()); }
attachLayer() 将 handle 和 outLayer 添加到 Client::mLayers 成员变量中; SurfaceFlinger 分别使用成员变量 mCurrentState 和 mGraphicBufferProducerList 保存 outLayer 和 gbp 。
1 2 3 4 5 6 7 @ frameworks/native/services/surfaceflinger/Client.cpp void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer) { Mutex::Autolock _l(mLock); mLayers.add(handle, layer);//添加到client的mLayers变量中 }
根据前面获取的 handle 和 gbp ,构造 SurfaceControl 对象。
1 2 3 4 5 6 7 8 9 @ frameworks/native/libs/gui/SurfaceControl.cpp SurfaceControl::SurfaceControl( const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle, const sp<IGraphicBufferProducer>& gbp) : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp) { }
总结:通过 SurfaceFlinger 构造 Layer ,然后创建 surfaceControl 对象。该对象有三个参数,第一个参数是 SurfaceComposerClient 代理对象;第二个参数是 handle ,表示 SurfaceControl 的唯一性;第三个参数是 gbp ,表示 Layer 对象中的 GraphicBufferProducer 的生产者代理类对象。
Surface 通过 SurfaceControl 对象的 getSurface() 方法构造 Surface 对象。
1 2 3 4 5 6 7 8 9 10 11 12 @ frameworks/native/libs/gui/SurfaceControl.cpp sp<Surface> SurfaceControl::getSurface() const { Mutex::Autolock _l(mLock); if (mSurfaceData == 0) { // This surface is always consumed by SurfaceFlinger, so the // producerControlledByApp value doesn't matter; using false. mSurfaceData = new Surface(mGraphicBufferProducer, false); } return mSurfaceData; }
使用 mGraphicBufferProducer 代理类对象构造 Surface 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Surface::Surface( const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp) : mGraphicBufferProducer(bufferProducer) { // Initialize the ANativeWindow function pointers. ANativeWindow::setSwapInterval = hook_setSwapInterval; ANativeWindow::dequeueBuffer = hook_dequeueBuffer; ANativeWindow::cancelBuffer = hook_cancelBuffer; ANativeWindow::queueBuffer = hook_queueBuffer; ANativeWindow::query = hook_query; ANativeWindow::perform = hook_perform; ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; const_cast<int&>(ANativeWindow::minSwapInterval) = 0; const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; mReqWidth = 0; mReqHeight = 0; mReqFormat = 0; mReqUsage = 0; mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; mCrop.clear(); mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; mTransform = 0; mDefaultWidth = 0; mDefaultHeight = 0; mUserWidth = 0; mUserHeight = 0; mTransformHint = 0; mConsumerRunningBehind = false; mConnectedToCpu = false; mProducerControlledByApp = controlledByApp; mSwapIntervalZero = false; }
总结:当应用程序构建 Surface 的时候, SurfaceFlinger 会创建对应的图层 Layer, 并且把 Layer 的图像缓冲区的生产者对象赋值给 Surface 对象的成员变量 mGraphicBufferProducer ,这样应用程序就可以通过这个生产者接口更新数据。
那么缓冲区的消费者呢,Layer::onFirstRef() 方法中创建 SurfaceFlingerConsumer 对象,该类间接继承于 ConsumerBase 消费者基类,所以 mSurfaceFlingerConsumer->setFrameAvailableListener(this) 实际等于 ConsumerBase::setFrameAvailableListener(const Layer &) ,注册 Layer 为 Listener。
当缓冲区数据准备好,BufferQueue 就会调用 onFrameAvailable() 方法通知消费者,这里还涉及到很多 Binder 的流程,我们暂不关心,只看 Layer::onFrameAvailable() 方法的实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @ frameworks/native/libs/gui/ConsumerBase.cpp void ConsumerBase::setFrameAvailableListener( const wp<FrameAvailableListener>& listener) { CB_LOGV("setFrameAvailableListener"); Mutex::Autolock lock(mMutex); mFrameAvailableListener = listener; } void ConsumerBase::onFrameAvailable() { CB_LOGV("onFrameAvailable"); sp<FrameAvailableListener> listener; { // scope for the lock Mutex::Autolock lock(mMutex); listener = mFrameAvailableListener.promote(); } if (listener != NULL) { CB_LOGV("actually calling onFrameAvailable"); listener->onFrameAvailable(); } }
这里会去调用 SurfaceFlinger::signalLayerUpdate() 方法发送图层更新消息,通过 handleMessageInvalidate() 方法处理消息,调用 handlePageFlip() 把有效缓冲区换到前台,等待 SurfaceFlinger 合成显示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 @ frameworks/native/services/surfaceflinger/Layer.cpp void Layer::onFrameAvailable() { android_atomic_inc(&mQueuedFrames); mFlinger->signalLayerUpdate(); } @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp void SurfaceFlinger::signalLayerUpdate() { mEventQueue.invalidate(); } void SurfaceFlinger::onMessageReceived(int32_t what) { ATRACE_CALL(); switch (what) { case MessageQueue::TRANSACTION: handleMessageTransaction(); break; case MessageQueue::INVALIDATE: handleMessageTransaction(); handleMessageInvalidate(); signalRefresh(); break; case MessageQueue::REFRESH: handleMessageRefresh(); break; } } void SurfaceFlinger::handleMessageInvalidate() { ATRACE_CALL(); handlePageFlip(); } void SurfaceFlinger::handlePageFlip() { Region dirtyRegion; bool visibleRegions = false; const LayerVector& layers(mDrawingState.layersSortedByZ); const size_t count = layers.size(); for (size_t i=0 ; i<count ; i++) { const sp<Layer>& layer(layers[i]); const Region dirty(layer->latchBuffer(visibleRegions)); const Layer::State& s(layer->getDrawingState()); invalidateLayerStack(s.layerStack, dirty); } mVisibleRegionsDirty |= visibleRegions; }
Surface::lock() 这里通过前面代码构建的 Surface,获取图像缓冲区内容,首先调用 Surface::dequeueBuffer() 方法申请 Buffer 进行应用层图像绘制。
BufferQueue 的流程图如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 @ frameworks/native/libs/gui/Surface.cpp status_t Surface::lock( ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) { if (!mConnectedToCpu) { int err = Surface::connect(NATIVE_WINDOW_API_CPU); if (err) { return err; } // we're intending to do software rendering from this point setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); } ANativeWindowBuffer* out; int fenceFd = -1; status_t err = dequeueBuffer(&out, &fenceFd); if (err == NO_ERROR) { sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); sp<Fence> fence(new Fence(fenceFd)); err = fence->waitForever("Surface::lock"); void* vaddr; status_t res = backBuffer->lock( GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, newDirtyRegion.bounds(), &vaddr); if (res != 0) { err = INVALID_OPERATION; } else { mLockedBuffer = backBuffer; outBuffer->width = backBuffer->width; outBuffer->height = backBuffer->height; outBuffer->stride = backBuffer->stride; outBuffer->format = backBuffer->format; outBuffer->bits = vaddr; } } return err; } int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { //申请buffer status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, mSwapIntervalZero, reqW, reqH, mReqFormat, mReqUsage); //mSlots 在前面有提到,是 Layer 对象中定义的缓冲队列数组,这里获取队列中的第 buf 项 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { freeAllBuffers(); } if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { // 因为内存是在 SurfaceFlinger 进程中申请的,这里将内存映射到当前 Surface 所在进程 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); if (result != NO_ERROR) { ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); return result; } } //返回内存对象的指针 *buffer = gbuf.get(); return OK; }
mGraphicBufferProducer->dequeueBuffer() 进而会去调用 BpGraphicBufferProducer::dequeueBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer> { public: virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async, uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { Parcel data, reply; data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); data.writeInt32(async); data.writeInt32(w); data.writeInt32(h); data.writeInt32(format); data.writeInt32(usage); //这里通过 BpBinder 将相关参数进行序列化,并发送给 BBinder status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply); if (result != NO_ERROR) { ALOGE("BpGraphicBufferProducer::dequeueBuffer binder transact failed, result: %d", result); return result; } // 获取 BBinder 回复的int数据,这里指的是 mSlots 缓冲区数组的索引 *buf = reply.readInt32(); bool nonNull = reply.readInt32(); if (nonNull) { *fence = new Fence(); reply.read(**fence); } result = reply.readInt32(); return result; } }
回归到 BnGraphicBufferProducer 实现,这里会调用 BufferQueue::dequeueBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 status_t BnGraphicBufferProducer::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case DEQUEUE_BUFFER: { CHECK_INTERFACE(IGraphicBufferProducer, data, reply); bool async = data.readInt32(); uint32_t w = data.readInt32(); uint32_t h = data.readInt32(); uint32_t format = data.readInt32(); uint32_t usage = data.readInt32(); int buf; sp<Fence> fence; int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage); reply->writeInt32(buf); reply->writeInt32(fence != NULL); if (fence != NULL) { reply->write(*fence); } reply->writeInt32(result); return NO_ERROR; } break; } }
首先查找 mSlots 数组中 BufferSlot::FREE 状态的缓存区,如果没有找到继续等待消费者;设置找到的缓存区状态为 BufferSlot::DEQUEUED,并检查这个缓冲区的 mGraphicBuffer 内存是否满足使用条件,如果不满足则设置 BUFFER_NEEDS_REALLOCATION 标志,调用 mGraphicBufferAlloc->createGraphicBuffer() 重新申请内存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 @ frameworks/native/libs/gui/BufferQueue.cpp status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async, uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { bool tryAgain = true; while (tryAgain) { // look for a free buffer to give to the client found = INVALID_BUFFER_SLOT; int dequeuedCount = 0; int acquiredCount = 0; for (int i = 0; i < maxBufferCount; i++) { const int state = mSlots[i].mBufferState; switch (state) { case BufferSlot::DEQUEUED: dequeuedCount++; //统计dequeued buffer数量 break; case BufferSlot::ACQUIRED: acquiredCount++; break; case BufferSlot::FREE: /* We return the oldest of the free buffers to avoid * stalling the producer if possible. This is because * the consumer may still have pending reads of the * buffers in flight. */ if ((found < 0) || mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) { found = i; } break; } } // See whether a buffer has been queued since the last // setBufferCount so we know whether to perform the min undequeued // buffers check below. if (mBufferHasBeenQueued) { // make sure the client is not trying to dequeue more buffers // than allowed. const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1); const int minUndequeuedCount = getMinUndequeuedBufferCount(async); if (newUndequeuedCount < minUndequeuedCount) { ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) " "exceeded (dequeued=%d undequeudCount=%d)", minUndequeuedCount, dequeuedCount, newUndequeuedCount); return -EBUSY; } } // If no buffer is found, wait for a buffer to be released or for // the max buffer count to change. tryAgain = found == INVALID_BUFFER_SLOT; if (tryAgain) { // return an error if we're in "cannot block" mode (producer and consumer // are controlled by the application) -- however, the consumer is allowed // to acquire briefly an extra buffer (which could cause us to have to wait here) // and that's okay because we know the wait will be brief (it happens // if we dequeue a buffer while the consumer has acquired one but not released // the old one yet -- for e.g.: see GLConsumer::updateTexImage()). if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) { ST_LOGE("dequeueBuffer: would block! returning an error instead."); return WOULD_BLOCK; } mDequeueCondition.wait(mMutex); } } const int buf = found; *outBuf = found; mSlots[buf].mBufferState = BufferSlot::DEQUEUED; //对找到的Slot进行初始化操作 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer); if ((buffer == NULL) || (uint32_t(buffer->width) != w) || (uint32_t(buffer->height) != h) || (uint32_t(buffer->format) != format) || ((uint32_t(buffer->usage) & usage) != usage)) { mSlots[buf].mAcquireCalled = false; mSlots[buf].mGraphicBuffer = NULL; mSlots[buf].mRequestBufferCalled = false; mSlots[buf].mEglFence = EGL_NO_SYNC_KHR; mSlots[buf].mFence = Fence::NO_FENCE; mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION; } if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { status_t error; sp<GraphicBuffer> graphicBuffer( mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error)); if (graphicBuffer == 0) { ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed"); return error; } { // Scope for the lock Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!"); return NO_INIT; } mSlots[*outBuf].mFrameNumber = ~0; mSlots[*outBuf].mGraphicBuffer = graphicBuffer; } } }
BpGraphicBufferAlloc::createGraphicBuffer() 通过 Binder 机制调用 BnGraphicBufferAlloc::onTransact() ,进而调用 GraphicBufferAlloc::createGraphicBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 @ frameworks/native/libs/gui/IGraphicBufferAlloc.cpp class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc> { public: BpGraphicBufferAlloc(const sp<IBinder>& impl) : BpInterface<IGraphicBufferAlloc>(impl) { } virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage, status_t* error) { Parcel data, reply; data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor()); data.writeInt32(w); data.writeInt32(h); data.writeInt32(format); data.writeInt32(usage); remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply); sp<GraphicBuffer> graphicBuffer; status_t result = reply.readInt32(); if (result == NO_ERROR) { graphicBuffer = new GraphicBuffer(); result = reply.read(*graphicBuffer); // reply.readStrongBinder(); // here we don't even have to read the BufferReference from // the parcel, it'll die with the parcel. } *error = result; return graphicBuffer; } }; status_t BnGraphicBufferAlloc::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { // codes that don't require permission check /* BufferReference just keeps a strong reference to a * GraphicBuffer until it is destroyed (that is, until * no local or remote process have a reference to it). */ class BufferReference : public BBinder { sp<GraphicBuffer> buffer; public: BufferReference(const sp<GraphicBuffer>& buffer) : buffer(buffer) { } }; switch(code) { case CREATE_GRAPHIC_BUFFER: { CHECK_INTERFACE(IGraphicBufferAlloc, data, reply); uint32_t w = data.readInt32(); uint32_t h = data.readInt32(); PixelFormat format = data.readInt32(); uint32_t usage = data.readInt32(); status_t error; sp<GraphicBuffer> result = createGraphicBuffer(w, h, format, usage, &error); reply->writeInt32(error); if (result != 0) { reply->write(*result); // We add a BufferReference to this parcel to make sure the // buffer stays alive until the GraphicBuffer object on // the other side has been created. // This is needed so that the buffer handle can be // registered before the buffer is destroyed on implementations // that do not use file-descriptors to track their buffers. reply->writeStrongBinder( new BufferReference(result) ); } return NO_ERROR; } break; default: return BBinder::onTransact(code, data, reply, flags); } }
根据传递的宽度、高度、格式等信息来构造 GraphicBuffer 对象,GraphicBuffer 类的构造函数会调用 initSize() 分配图形缓冲区。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @ frameworks/native/libs/gui/GraphicBufferAlloc.cpp sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage, status_t* error) { sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); status_t err = graphicBuffer->initCheck(); *error = err; if (err != 0 || graphicBuffer->handle == 0) { if (err == NO_MEMORY) { GraphicBuffer::dumpAllocationsToSystemLog(); } ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) " "failed (%s), handle=%p", w, h, strerror(-err), graphicBuffer->handle); return 0; } return graphicBuffer; } @ frameworks/native/libs/ui/GraphicBuffer.cpp GraphicBuffer::GraphicBuffer(uint32_t w, uint32_t h, PixelFormat reqFormat, uint32_t reqUsage) : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), mInitCheck(NO_ERROR) { width = height = stride = format = usage = 0; handle = NULL; mInitCheck = initSize(w, h, reqFormat, reqUsage); } status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format, uint32_t reqUsage) { GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride); if (err == NO_ERROR) { this->width = w; this->height = h; this->format = format; this->usage = reqUsage; } return err; }
GraphicBufferAllocator 是作为分配图形缓冲区的工具类,这里使用单例模式获取 GraphicBufferAllocator 对象,加载 Gralloc HAL ,最终调用 gralloc_alloc 实现图形缓冲区的分配。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @ frameworks/native/include/ui/GraphicBufferAllocator.h class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator> { public: static inline GraphicBufferAllocator& get() { return getInstance(); } } @ frameworks/native/libs/ui/GraphicBufferAllocator.cpp GraphicBufferAllocator::GraphicBufferAllocator() : mAllocDev(0) { hw_module_t const* module; int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); if (err == 0) { gralloc_open(module, &mAllocDev); } }
到现在为止 BufferQueue 中已经申请到了内存,返回给 Surface 一个的数组下标,但这时候 Surface 还没有获取到任何图形缓冲区相关的东西。由于申请的内存是在 SurfaceFlinger 所在的进程,接下来调用 mGraphicBufferProducer->requestBuffer() 将 buffer 映射到 Surface 所在的进程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @ frameworks/native/libs/gui/BufferQueue.cpp status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) { ATRACE_CALL(); ST_LOGV("requestBuffer: slot=%d", slot); Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("requestBuffer: BufferQueue has been abandoned!"); return NO_INIT; } if (slot < 0 || slot >= NUM_BUFFER_SLOTS) { ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d", NUM_BUFFER_SLOTS, slot); return BAD_VALUE; } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) { ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)", slot, mSlots[slot].mBufferState); return BAD_VALUE; } mSlots[slot].mRequestBufferCalled = true; *buf = mSlots[slot].mGraphicBuffer; return NO_ERROR; }
在 requestBuffer() 方法返回后,调用 reply->write(*buffer) ,这里经过数据打包处理然后发送到应用程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 @ frameworks/native/libs/binder/Parcel.cpp status_t Parcel::write(const FlattenableHelperInterface& val) { status_t err; // size if needed const size_t len = val.getFlattenedSize(); const size_t fd_count = val.getFdCount(); err = this->writeInt32(len); if (err) return err; err = this->writeInt32(fd_count); if (err) return err; // payload void* const buf = this->writeInplace(PAD_SIZE(len)); if (buf == NULL) return BAD_VALUE; int* fds = NULL; if (fd_count) { fds = new int[fd_count]; } //数据的打包处理 err = val.flatten(buf, len, fds, fd_count); for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { err = this->writeDupFileDescriptor( fds[i] ); } if (fd_count) { delete [] fds; } return err; } @ frameworks/native/libs/ui/GraphicBuffer.cpp status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); if (size < sizeNeeded) return NO_MEMORY; size_t fdCountNeeded = GraphicBuffer::getFdCount(); if (count < fdCountNeeded) return NO_MEMORY; int* buf = static_cast<int*>(buffer); buf[0] = 'GBFR'; buf[1] = width; buf[2] = height; buf[3] = stride; buf[4] = format; buf[5] = usage; buf[6] = 0; buf[7] = 0; if (handle) { buf[6] = handle->numFds; buf[7] = handle->numInts; native_handle_t const* const h = handle; memcpy(fds, h->data, h->numFds*sizeof(int)); memcpy(&buf[8], h->data + h->numFds, h->numInts*sizeof(int)); } buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded); size -= sizeNeeded; fds += handle->numFds; count -= handle->numFds; return NO_ERROR; }
在应用程序端使用 reply.read(*graphicBuffer) 把数据读取出来,这里使用 Parcel::read() 方法,进而调用 GraphicBufer::unflatten() 方法,这里使用 fd 构造 native_handle 对象,然后调用 GraphicBufferMapper::registerBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 @ frameworks/native/libs/binder/Parcel.cpp status_t Parcel::read(FlattenableHelperInterface& val) const { // size const size_t len = this->readInt32(); const size_t fd_count = this->readInt32(); // payload void const* const buf = this->readInplace(PAD_SIZE(len)); if (buf == NULL) return BAD_VALUE; int* fds = NULL; if (fd_count) { fds = new int[fd_count]; } status_t err = NO_ERROR; for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { fds[i] = dup(this->readFileDescriptor()); if (fds[i] < 0) err = BAD_VALUE; } if (err == NO_ERROR) { err = val.unflatten(buf, len, fds, fd_count); } if (fd_count) { delete [] fds; } return err; } @ frameworks/native/libs/ui/GraphicBuffer.cpp status_t GraphicBuffer::unflatten( void const*& buffer, size_t& size, int const*& fds, size_t& count) { if (size < 8*sizeof(int)) return NO_MEMORY; int const* buf = static_cast<int const*>(buffer); if (buf[0] != 'GBFR') return BAD_TYPE; const size_t numFds = buf[6]; const size_t numInts = buf[7]; const size_t sizeNeeded = (8 + numInts) * sizeof(int); if (size < sizeNeeded) return NO_MEMORY; size_t fdCountNeeded = 0; if (count < fdCountNeeded) return NO_MEMORY; if (handle) { // free previous handle if any free_handle(); } if (numFds || numInts) { width = buf[1]; height = buf[2]; stride = buf[3]; format = buf[4]; usage = buf[5]; native_handle* h = native_handle_create(numFds, numInts); memcpy(h->data, fds, numFds*sizeof(int)); memcpy(h->data + numFds, &buf[8], numInts*sizeof(int)); handle = h; } else { width = height = stride = format = usage = 0; handle = NULL; } mOwner = ownHandle; if (handle != 0) { status_t err = mBufferMapper.registerBuffer(handle); if (err != NO_ERROR) { ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err); return err; } }
实际会调用 Gralloc 模块的 registerBuffer() 方法,这里会把申请的 buffer 进行 mmap 映射,然后将 vaddr 数据保存在 handle->base 中。
1 2 3 4 5 6 7 8 9 10 11 12 13 @ frameworks/native/libs/ui/GraphicBufferMapper.cpp status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle) { ATRACE_CALL(); status_t err; err = mAllocMod->registerBuffer(mAllocMod, handle); ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)", handle, err, strerror(-err)); return err; }
dequeueBuffer() 完成之后会调用 backBuffer->lock(),实际上调用的是 gralloc_lock(),直接返回 handle->base()。
总结:真正的申请图形缓冲区是在 SurfaceFlinger 进程中,因为 GraphicBufferAlloc 对象是在 SurfaceFlinger 中构造出来的,然后 BufferQueue 和 Surface 中的图形缓冲区都是通过 GraphicBuffer 的序列化和反序列化新映射出来的。
Surface::unlockAsndPost() 应用绘制完成后,将数据更新入 BufferQueue ,通知 Layer 更新,Layer 从 BufferQueue 中取出数据,通知SurfaceFlinger 进行合成处理。
Surface::unlockAndPost() 调用 Surface::queueBuffer() 方法,而 mGraphicBufferProducer->queueBuffer() 方法实际上会调用的是本地对象的 BnGraphicBufferProducer::queueBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 @ frameworks/native/libs/gui/Surface.cpp status_t Surface::unlockAndPost() { if (mLockedBuffer == 0) { ALOGE("Surface::unlockAndPost failed, no locked buffer"); return INVALID_OPERATION; } status_t err = mLockedBuffer->unlock(); ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); err = queueBuffer(mLockedBuffer.get(), -1); ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", mLockedBuffer->handle, strerror(-err)); mPostedBuffer = mLockedBuffer; mLockedBuffer = 0; return err; } int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { ATRACE_CALL(); ALOGV("Surface::queueBuffer"); Mutex::Autolock lock(mMutex); int64_t timestamp; bool isAutoTimestamp = false; if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { timestamp = systemTime(SYSTEM_TIME_MONOTONIC); isAutoTimestamp = true; ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", timestamp / 1000000.f); } else { timestamp = mTimestamp; } int i = getSlotFromBufferLocked(buffer); if (i < 0) { return i; } // Make sure the crop rectangle is entirely inside the buffer. Rect crop; mCrop.intersect(Rect(buffer->width, buffer->height), &crop); sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); IGraphicBufferProducer::QueueBufferOutput output; IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, crop, mScalingMode, mTransform, mSwapIntervalZero, fence); status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); if (err != OK) { ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); } uint32_t numPendingBuffers = 0; output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint, &numPendingBuffers); mConsumerRunningBehind = (numPendingBuffers >= 2); return err; }
mGraphicBufferProducer->queueBuffer() 会调用 remote()->transact(),进而调用 BufferQueue::queueBuffer() 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 @ frameworks/libs/gui/IGraphicBufferProducer.cpp class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer> { public: virtual status_t queueBuffer(int buf, const QueueBufferInput& input, QueueBufferOutput* output) { Parcel data, reply; data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); data.writeInt32(buf); data.write(input); status_t result = remote()->transact(QUEUE_BUFFER, data, &reply); if (result != NO_ERROR) { ALOGE("BpGraphicBufferProducer::queueBuffer binder transact failed, result: %d", result); return result; } memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output)); result = reply.readInt32(); return result; } } status_t BnGraphicBufferProducer::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case QUEUE_BUFFER: { CHECK_INTERFACE(IGraphicBufferProducer, data, reply); int buf = data.readInt32(); //读取Slot序号 QueueBufferInput input(data); QueueBufferOutput* const output = reinterpret_cast<QueueBufferOutput *>( reply->writeInplace(sizeof(QueueBufferOutput))); status_t result = queueBuffer(buf, input, output); reply->writeInt32(result); return NO_ERROR; } break; } }
queueBuffer 调用 listener->onFrameAvailable() ,父类的构造函数 ConsumerBase 会调用 Layer 的 onFrameAvailable() ,最后调用 signalLayerUpdate() 通知 SurfaceFlinger 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @ frameworks/native/libs/gui/BufferQueue.cpp status_t BufferQueue::queueBuffer(int buf, const QueueBufferInput& input, QueueBufferOutput* output) { sp<IConsumerListener> listener; const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer); mSlots[buf].mFence = fence; mSlots[buf].mBufferState = BufferSlot::QUEUED; mFrameCounter++; mSlots[buf].mFrameNumber = mFrameCounter; BufferItem item; item.mAcquireCalled = mSlots[buf].mAcquireCalled; item.mGraphicBuffer = mSlots[buf].mGraphicBuffer; item.mCrop = crop; item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); item.mScalingMode = scalingMode; item.mTimestamp = timestamp; item.mIsAutoTimestamp = isAutoTimestamp; item.mFrameNumber = mFrameCounter; item.mBuf = buf; item.mFence = fence; item.mIsDroppable = mDequeueBufferCannotBlock || async; mQueue.push_back(item); listener = mConsumerListener; mDequeueCondition.broadcast(); output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size()); listener->onFrameAvailable(); }
这里的 listener 保存了前面已经分配的 Layer 对象,而 Layer 的成员变量 mFlinger 保存了 SurfaceFlinger 对象,所以调用 signalLayerUpdate() 方法会唤醒其他线程,这部分后面再继续分析。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @ frameworks/native/libs/gui/BufferQueue.cpp void BufferQueue::ProxyConsumerListener::onFrameAvailable() { sp<ConsumerListener> listener(mConsumerListener.promote()); if (listener != NULL) { listener->onFrameAvailable(); } } @ frameworks/native/services/surfaceflinger/Layer.cpp void Layer::onFrameAvailable() { android_atomic_inc(&mQueuedFrames); mFlinger->signalLayerUpdate(); } @ frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp void SurfaceFlinger::signalLayerUpdate() { mEventQueue.invalidate(); }