Android5.0 显示系统(三)————Surface的创建

来源:互联网 发布:此何遽不为福乎的为 编辑:程序博客网 时间:2024/06/05 19:44
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),        160, 240, PIXEL_FORMAT_RGB_565, 0);sp<Surface> surface = surfaceControl->getSurface();
frameworks\native\libs\gui\SurfaceComposerClient.cppsp<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;/*Step1: 创建服务端Layer*/        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) {            /*Step2: 创建SurfaceControl对象*/            sur = new SurfaceControl(this, handle, gbp);        }    }    return sur;}
      handle和gbp是Binder对象,他们的值都是在SurfaceFlinger中设置,其中gdp就是图像缓冲区对象。这个handle对象的作用和前面介绍Activity中的token作用类似,也是利用Binder的唯一性为Surface生成一个唯一标识。
/*Step1*/frameworks\native\libs\gui\ISurfaceComposerClient.cppvirtual 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();    }}
frameworks\native\libs\gui\ISurfaceComposerClient.cppstatus_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;      ......}frameworks\native\services\surfaceflinger\Client.cppstatus_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(); //返回结果}
      这个函数中定义了一个消息类MessageCreateLayer,然后把它的对象通过postMessageSync()方法发送出去,这个消息是以同步的方式发送,因此函数结束后可以直接返回结果。因此就直接到了handler函数,在这个函数中调用了SurfaceFlinger的createLayer()函数。
frameworks\native\services\surfaceflinger\SurfaceFlinger.cppstatus_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){    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;/*根据传进来的flags来创建普通的的Layer还是模糊的Layer。*/    switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {        case ISurfaceComposerClient::eFXSurfaceNormal:   //普通的Surface                    result = createNormalLayer(client,                    name, w, h, flags, format,                    handle, gbp, &layer);            break;        case ISurfaceComposerClient::eFXSurfaceDim:   //模糊的Surface            result = createDimLayer(client,                    name, w, h, flags,                    handle, gbp, &layer);            break;        default:            result = BAD_VALUE;            break;    }    if (result == NO_ERROR) {    /*Step1.2 绑定Layer对象*/        addClientLayer(client, *handle, *gbp, layer);        setTransactionFlags(eTransactionNeeded);    }    return result;}
/*Step1.1 创建Layer对象*/
      我们先来看看创建普通的Layer,根据传入的format参数,选择不同格式。然后创建一个Layer对象,具体Layer我们后续介绍。然后获取handle和gbp。
frameworks\native\services\surfaceflinger\SurfaceFlinger.cppstatus_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:        format = PIXEL_FORMAT_RGBX_8888;        break;    }/*创建一个Layer*/    *outLayer = new Layer(this, client, name, w, h, flags);/*Surface中的handle和gbp从这里得到*/    status_t err = (*outLayer)->setBuffers(w, h, format, flags);    if (err == NO_ERROR) {/*返回Layer的文件句柄*//*Handle标识Surface全局变量的唯一性,其他没有什么实际意思*/        *handle = (*outLayer)->getHandle();/*返回生产者*/        *gbp = (*outLayer)->getProducer();    }    ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));    return err;}
获取handle

      下面我们再来看handle的获取,只是新建一个Handle,而这个Handle只是一个Binder的实现,就是标识Surface的全局唯一性,还有就是当Layer被销毁时,会调用LayerCleaner的析构函数进行一些处理。

createNormalLayer()->getHandle()sp<IBinder> Layer::getHandle() {      Mutex::Autolock _l(mLock);        LOG_ALWAYS_FATAL_IF(mHasSurface,              "Layer::getHandle() has already been called");        mHasSurface = true;        class Handle : public BBinder, public LayerCleaner {          wp<const Layer> mOwner;      public:          Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)              : LayerCleaner(flinger, layer), mOwner(layer) {          }      };        return new Handle(mFlinger, this);  } 
gbp图像缓冲区
      那么我们再来看看gbp的获取,就是Layer中mProducer成员变量。
createNormalLayer()->getProducer()sp<IGraphicBufferProducer> Layer::getProducer() const {      return mProducer;  }  
/*Step1.2 */
先假设我们已经创建了Layer对象,并且layer对象对应的handle和gbp。这样我们只是创建了Layer对象,但是我们还没有把创建的Layer对象告诉其他对象,这部分工作是addClientLayer()方法来完成的。addClientLayer(client, *handle, *gbp, layer);为应用程序申请Layer,一方面需要告诉SurfaceFlinger,另一方面要记录到各Client内部中。这两个步骤是由addClientLayer()分别调用Client::attachLayer()和SurfaceFlinger::addLayer_1()完成。
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);//让此Layer与Client相关联    // add this layer to the current state list    Mutex::Autolock _l(mStateLock);    mCurrentState.layersSortedByZ.add(lbc);//将Layer按顺序添加到全局变量中    mGraphicBufferProducerList.add(gbc->asBinder());//将gdb也添加到全局变量中}void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer){    Mutex::Autolock _l(mLock);    mLayers.add(handle, layer);//添加到client的mLayers变量中}ssize_t SortedVectorImpl::add(const void* item){    size_t order;    ssize_t index = _indexOrderOf(item, &order);    if (index < 0) {        index = VectorImpl::insertAt(item, order, 1);    } else {        index = VectorImpl::replaceAt(item, index);    }    return index;}


/*Step2*/
//sur = new SurfaceControl(this, handle, gbp);frameworks\native\libs\gui\SurfaceControl.cppSurfaceControl::SurfaceControl(        const sp<SurfaceComposerClient>& client,        const sp<IBinder>& handle,        const sp<IGraphicBufferProducer>& gbp)    : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp){}sp<Surface> surface = surfaceControl->getSurface();frameworks\native\libs\gui\SurfaceControl.cppsp<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;}frameworks\native\libs\gui\Surface.cppSurface::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;    mStickyTransform = 0;    mDefaultWidth = 0;    mDefaultHeight = 0;    mUserWidth = 0;    mUserHeight = 0;    mTransformHint = 0;    mConsumerRunningBehind = false;    mConnectedToCpu = false;    mProducerControlledByApp = controlledByApp;    mSwapIntervalZero = false;}
      而mProducer是在Layer的onFirstRef()中赋值的,我们注意MonitoredProducer构造函数的一个参数producer,事实上MonitoredProducer只是一个代理类,真正的实现在这个producer参数。

      总结:surfaceControl->getSurface()创建了一个surfaceControl对象,该对象有三个参数,第一个参数是SurfaceComposerClient对象;第二个参数是handle,表示SurfaceControl的唯一性;第三个参数是gbp,表示Layer对象的GraphicBufferProducer的binder对象。


阅读全文
0 0
原创粉丝点击