Sort by each Layer in SurfaceFlinger

Original http://blog.csdn.net/panzhenjie/article/details/10916619

surfaceflinger The main job is to compose the different layers passed down from the upper layer.

Here, let’s discuss the up-down sorting relationship of each layer in surfaceflinger and related code implementation. The code is based on android4.3

First introduce two classes, SurfaceFlinger and Client .

Simply put, the relationship between these two classes can be understood as follows: SurfaceFlinger implements a specific composition service, and every program with UI needs to be rendered through SurfaceFlinger.

These programs can call SurfaceFlinger through some interfaces of Client to achieve this purpose.

There is a createSurface member function in the Client class

[ cpp] view plain copy

  1. status_t Client::createSurface(
  2. const String8& name,
  3. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  4. < span> sp* handle,
  5. sp* gbp)
  6. {
  7. /*
  8. * createSurface must be called from the GL thread so that it can
  9. * have access to the GL context.
  10. */
  11. classMessageCreateLayer: publicMessageBase{
  12. SurfaceFlinger*flinger;
  13. Client* client;
  14. sp* handle;
  15. sp* gbp;
  16. status_t result;
  17. const String8& name;
  18. uint32_t w, h;
  19. PixelFormat format;
  20. uint32_t flags;
  21. public:
  22. MessageCreateLayer(SurfaceFlinger* flinger,
  23. constString8&name, Client*client,< /span>
  24. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, uint32_t flags,
  25. hand IB ,
  26. sp* gbp)
  27. : flinger(flinger), client(client(flinger) ),
  28. handle(handle), gbp(gbp),
  29. name(name), w(w), h(h), format(format), flags(flags){
  30. status_t getResult() const{returnresult;}} span>
  31. virtual< /span>boolhandler(){
  32. result=flinger->createLayer(name, client, w, h, format, flags,
  33. Handle, gbp);
  34. returntrue;
  35. li>
  36. sp msg = new MessageCreateLayer(mFlinger.get(),
  37. name, this, w, h, format, flags, handle, gbp);
  38. mFlinger->postMessageSync(msg);
  39. returnstatic_cast( msg.get())->getResult();
  40. }

The createLayer function is Private function of SurfaceFlinger class, but because Client is his friend, it can be called directly to create a layer.

[cpp] view plain copy

  1. private:
  2. friendclassClient;
  3. friend classDisplayEventConnection;
  4. friend< /span>classLayer;
  5. friendclass SurfaceTextureLayer;

Look at the code of createLayer

[cpp] view plain copy

  1. status_t SurfaceFlinger::createLayer(
  2. const String8& name,
  3. const< span> sp& client,
  4. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  5. sp* handle, sp*gbp)
  6. {
  7. //ALOGD(“createLayer for (%d x %d), name=%s”, w, h, name.string());
  8. if(int32_t(w|h)<0){ span>
  9. ALOGE(“createLay er() failed, w or h is negative (w=%d, h=%d)”,
  10. int(w),int(h));
  11. return BAD_VALUE;
  12. < span>
  13. status_t result= NO_ERROR;
  14. < li class="alt">

  15. sp layer;
  16. < /span>

  17. switch(flags& ISurfaceComposerClient::eFXSurfaceMask){
  18. case ISurfaceComposerClient::eFXSurfaceNormal:
  19. result = createNormalLayer(client,
  20. name, w, h, flags, format,
  21. Handle, gbp, &layer);
  22. break;
  23. case ISurfaceComposerClient::eFXSurfaceDim: span>
  24. result = createDimLayer(client,
  25. name, w, h, flags, < /span>
  26. Handle, gbp, &layer;
  27. break
  28. break span>;
  29. default:
  30. result = BAD_VALUE;
  31. break ;
  32. if(result==NO_ERROR){
  33. addClientLayer(client, *handle, *gbp, layer) ;
  34. setTransactionFlags(eTransactionNeeded);
  35. returnresult;
  36. }

This function is very clear, mainly calling createNormalLayer and createDimLayer to create different layers.

Let’s ignore createDimLayer first and just look at the implementation of createNormalLayer

[cpp] view plain copy

  1. status_t SurfaceFlinger::createNormalLayer(constsp& client,< /span>
  2. const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, span>
  3. sp* handle, sp* gbp, sp* outLayer)
  4. < span>{
  5. // initialize the surfaces
  6. switch(format){
  7. casePIXEL_FORMAT_TRANSPARENT:
  8. casePIXEL_FORMAT_TRANSLUCENT:
  9. format= PIXEL_FORMAT_RGBA_8888;
  10. break ;
  11. case PIXEL_FORMAT_OPAQUE:< /span>
  12. #ifdef NO_RGBX_8888
  13. format = PIXEL_FORMAT_RGB_565;
  14. #else
  15. format= PIXEL_FORMAT_RGBX_8888;
  16. #endif
  17. break;
  18. < li>

  19. #ifdef NO_RGBX_8888
  20. if(format==PIXEL_FORMAT_RGBX_8888)
  21. Format = PIXEL_FORMAT_RGBA_8888;
  22. #endif< /li>
  23. *outLayer=newLayer(< /span>this, client, name, w, h, flags);
  24. status_t err = (*outLayer)->setBuffers(w, h, format, flags);
  25. if(err==NO_ERROR){
  26. *handle=(*outLayer)->getHandle();
  27. *gbp=(*outLayer)->getBufferQueue();
  28. li>
  29. ALOGE_IF(err, “createNormalLayer() failed(%s)”< /span>, strerror(-err));
  30. return err;
  31. }

Here we mainly create a Layer object.

[cpp] view plain copy

  1. Layer::Layer(SurfaceFlinger*flinger, constsp& client,
  2. const String8& name, uint32_t w, uint32_t h, uint32_t flags)
  3. : contentDirty(< span class="keyword">false),
  4. sequence(uint32_t(android_atomic_inc(&sSequence))),
  5. mFlinger(flinger),
  6. mTextureName(-1U),
  7. mPremultipliedAlpha(true),
  8. mName(< span class="string">“unname d”),
  9. mDebug(false< span>),
  10. mFormat(PIXEL_FORMAT_NONE),
  11. mGLExtensions(GLExtensions: :getInstance()),
  12. mOpaqueLayer(true), li>
  13. mTransactionFlags(0),
  14. mQueuedFrames(0),
  15. mCurrentTransform(0),
  16. mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
  17. (true),
  18. mRefreshPending(false span>),
  19. mFrameLatencyNeeded(false),
  20. mFiltering(false),
  21. mNeedsFiltering(false),
  22. mSecure(false),
  23. mProtectedByApp(false< span>),
  24. mHasSurface(false),
  25. mClientRef(client)
  26. {
  27. mCurrentCrop.makeInvalid();
  28. glGenTextures(1,&mTextureName);
  29. /span>
  30. uint32_t layerFlags = 0;
  31. if (flags& ISurfaceComposerClient::eHidden)
  32. layer Flags = layer_state_t::eLayerHidden;
  33. if< /span>(flags&ISurfaceComposerClient::eNonPremultiplied)
  34. mPremultipliedAlpha=false
  35. span>;

  36. mName= name;
  37. mCurrentState.active.w=w;
  38. < span> mCurrentState.active.h = h;
  39. mCurrentState.active.crop.makeInvalid();
  40. mCurrentState.z = 0;
  41. mCurrentState.alpha = 0xFF;
  42. mCurrentState. layerStack = 0;
  43. mCurrentState.flags = layerFlags;
  44. mCurrentState.sequence = 0; /span>
  45. mCurrentState.transform.set(0, 0);
  46. mCurrentState.requested= mCurrentState.active;
  47. // drawing state& current state areidentical
  48. mDrawingState= mCurrentState;
  49. }

Here we mainly focus on information related to layer order

[cpp] view plain copy

  1. < span> sequence(uint32_t(android_atomic_inc(&sSequence))),
  2. mCurrentState.z = 0;
  3. mCurrentState.layerStack = 0;

These three variables are determined With the order between the layers, let me explain the specific meaning.

The first is layerStack, you can understand it as the meaning of a group. That is to say, the layers belonging to different groups do not interfere with each other.

There is a DisplayDevice class in SurfaceFlinger, which represents the device used for display, such as LCD or HDMI.

There is also a member variable mLayerStack in DisplayDevice. During composition, only the same layer as the device’s layerstack can be displayed on this device.

The second one is z. In fact, it means z-order, which means the order of the x, y, and z axes on the z axis. The larger the number, the higher it is, and the smaller the number, the lower it is.

The third is sequence. Because sSequence is a static variable, the effect of incremental addition is to set a unique and increasing sequence number for each layer.

The introduction of the concept is over, let’s continue to look at the code to see if this is the case.

After creating the layer, createLayer will call addClientLayer to add the information of this layer to the current state information.

[cpp] view plain copy

  1. void SurfaceFlinger::addClientLayer(constsp& client, li>
  2. constsp&handle,
  3. constsp&gbc,
  4. constsp&lbc)
  5. {
  6. // attach this layer to the client
  7. < span> client->attachLayer(handle, lbc);
  8. // add this layer to the current state list
  9. < span> Mutex::Autolock_l(mStateLock);
  10. mCurrentState.layersSortedByZ.add(lbc);
  11. mGraphicBufferProducerList.add(gbc->asBinder());
  12. }
The layersSortedByZ variable is very important. When surfaceflinger is really rendering, it is used to know which layer is on top and which is on bottom.

The add function here is responsible for putting the layer in

[cpp] view plain copy

  1. ssize_t SortedVectorImpl::add(constvoid* item)
  2. {
  3. size_torder;
  4. ssize_tindex=_indexOrderOf(item,&order);
  5. < li class="alt">if(index<0){

  6. index= VectorImpl::insertAt(item, order, 1);
  7. } else {
  8. index = VectorImpl::replaceAt(item, index);
  9. returnindex;
  10. }

[cpp] view plain copy

  1. ssize_t SortedVectorImpl::_indexOrderOf(const< span>void*item,size_t*order) span>const
  2. {
  3. // binary search
  4. ssize_terr=NAME_NOT_FOUND; span>
  5. ssize_t = 0;
  6. ssize_t h = size()-1;
  7. ssize_t mid;
  8. const< /span>void*a=arrayImpl();
  9. span class=”keyword”>constsize_ts=itemSize();< /li>
  10. while(l<=h){
  11. mid = l + (h- l)/2;
  12. constvoid*constcurr=reinterpret_cast<constchar*>(a)+(mid*s);
  13. const< span>int c = do_compare(curr, item);
  14. if(c== 0 ){
  15. err = l = mid;
  16. break;
  17. else< span>if(c<0){
  18. l=mid+ 1;
  19. else{
  20. h = mid- 1;
  21. span>
  22. if(order)*order=l;< /li>
  23. return err;
  24. }

[cpp] view plain copy

  1. int SurfaceFlinger::LayerVector::do_compare(constvoid* lhs,
  2. constvoid< /span>* rhs)const
  3. < span>{
  4. // sort layers per layer-stack, then by z-order and finally by sequence
  5. const sp& l(*reinterpret_cast<const sp *>(lhs));
  6. const sp& r(*< /span>reinterpret_cast<const sp*>(rhs)) ;
  7. uint32_tls = l->currentState().layerStack;
  8. uint32_t rs = r->currentState().layerStack;
  9. if(ls!=rs)
  10. keyword”>return< /span>ls-rs;
  11. uint32_tlz= l->currentState().z;
  12. uint32_t rz = r->currentState().z;
  13. if(lz!=rz)
  14. returnlz-rz;
  15. returnl-> sequence r->sequence;
  16. }

Attach the post There are three functions, the main function of which is to determine where the layer should be inserted in layersSortedByZ.

From do_compare, we can see that it is the same as what I just analyzed.

The first step is to compare layerstacks and separate different layerstacks.

Then compare z, and finally, assuming that these are the same, compare the unique layer serial numbers.

But so far, layerStack and z are only 0 when they are initialized, so when creating a layer, it just puts him into layersSortedByZ according to the serial number, in fact, his order is still not set.

Next, we are going to find out where these are set up.

Everyone should know bootanimation, which is the program that is responsible for drawing the flashing Android characters when booting up.

I found such code in it

[ cpp] view plain copy

  1. // create the native surface
  2. sp control= session()->createSurface(String8(“BootAnimation”),
  3. dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
  4. SurfaceComposerClient: :openGlobalTransaction();
  5. control->setLayer(0x40000000);
  6. SurfaceComposerClient::closeGlobalTransaction ();

The previous createSurface has been analyzed.

The following is setLayer, what exactly is this 0x40000000 set?

我们一步步往下看

[cpp] view plain copy

  1. status_t SurfaceControl::setLayer(int32_t layer) {  
  2.     status_t err = validate();  
  3.     if (err < 0) return err;  
  4.     const sp& client(mClient);  
  5.     return client->setLayer(mHandle, layer);  
  6. }  

[cpp] view plain copy

  1. status_t SurfaceComposerClient::setLayer(const sp& id, int32_t z) {  
  2.     return getComposer().setLayer(this, id, z);  
  3. }  

[cpp] view plain copy

  1. status_t Composer::setLayer(const sp& client,  
  2.         const sp& id, int32_t z) {  
  3.     Mutex::Autolock _l(mLock);  
  4.     layer_state_t* s = getLayerStateLocked(client, id);  
  5.     if (!s)  
  6.         return BAD_INDEX;  
  7.     s->what |= layer_state_t::eLayerChanged;  
  8.     s->z = z;  
  9.     return NO_ERROR;  
  10. }  

可以看到,这个layer变量最终变成了z,存进了layer_state_t结构体内。

这个结构体是哪来的?在看看getLayerStateLocked

[cpp] view plain copy

  1. layer_state_t* Composer::getLayerStateLocked(  
  2.         const sp& client, const sp& id) {  
  3.   
  4.     ComposerState s;  
  5.     s.client = client->mClient;  
  6.     s.state.surface = id;  
  7.   
  8.     ssize_t index = mComposerStates.indexOf(s);  
  9.     if (index < 0) {  
  10.         / / we don’t have it, add an initialized layer_state to our list  
  11.         index = mComposerStates.add(s);  
  12.     }  
  13.   
  14.     ComposerState* const out = mComposerStates.editArray();  
  15.     return &(out[index].state);  
  16. }  

原来是从mComposerStates里找来的啊。

这些代码看上去是在做相关的操作,但是设置还没有具体生效。

下面我们看看SurfaceComposerClient::closeGlobalTransaction()的作用

[cpp] view plain copy

  1. void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {  
  2.     Composer::closeGlobalTransaction(synchronous);  
  3. }  

[cpp] view plain copy

  1. void Composer::closeGlobalTransactionImpl(bool synchron ous) {  
  2.     sp sm(ComposerService::getComposerService());  
  3.   
  4.     Vector transaction;  
  5.     Vector displayTransaction;  
  6.     uint32_t flags = 0;  
  7.   
  8.     { // scope for the lock  
  9.         Mutex::Autolock _l(mLock);  
  10.         mForceSynchronous |= synchronous;  
  11.         if (!mTransactionNestCount) {  
  12.             ALOGW(“At least one call to closeGlobalTransaction() was not matched by a prior “  
  13.                     “call to openGlobalTransaction().”);  
  14.         } else if (–mTransactionNestCount) {  
  15.             return;  
  16.         }  
  17.   
  18.         transaction = mComposerStates;  
  19.         mComposerStates.clear();  
  20.   
  21.         displayTransaction = mDisplayStates;  
  22.         mDisplayStates.clear();  
  23.   
  24.         if (mForceSynchronous) {  
  25.             flags |= ISurfaceComposer::eSynchronous;  
  26.         }  
  27.         if (mAnimation) {  
  28.             flags |= ISurfaceComposer::eAnimation;  
  29.         }  
  30.         if (mTransition) {  
  31.             flags |= ISurfaceComposer::eTransition;  
  32.         }  
  33.         if (mOrientationEnd) {  
  34.             flags |= ISurfaceComposer::eOrientationEnd;  
  35.         }  
  36.         mForceSynchronous = false;  
  37.         mAnimation = false;  
  38.     }  
  39.   
  40.    sm->setTransactionS tate(transaction, displayTransaction, flags);  
  41. }  

mComposerStates被赋值给transaction,然后通过sm->setTransactionState传递下去。

[cpp] view plain copy

  1. void SurfaceFlinger::setTransactionState(  
  2.         const Vector& state,  
  3.         const Vector& displays,  
  4.         uint32_t flags)  
  5. {  
  6.     ……  
  7.     count = state.size();  
  8.     for (size_t i=0 ; i
  9.         const ComposerState& s(state[i]);  
  10.         // Here we need to check that the interface we’re given is indeed  
  11.         // one of our own. A malicious client could give us a NULL  
  12.         // IInterface, or one of its own or even one of our own but a  
  13.         // different type. All these situations would cause us to crash.  
  14.         //  
  15.         // NOTE: it would be better to use RTTI as we could directly check  
  16.         // that we have a Client*. however, RTTI is disabled in Android.  
  17.         if (s.client != NULL) {  
  18.             sp binder = s.client->asBinder();  
  19.             if (binder != NULL) {  
  20.                 String16 desc(binder->getInterfaceDescriptor());  
  21.                 if (desc == ISurfaceComposerClient::descriptor) {  
  22.                     sp client( static_cast(s.client.get()) );  
  23.                     transactionFlags |= setClientStateLocked(client, s.state);  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28.     ……  
  29. }  

[cpp] view plain copy

  1. uint32_t SurfaceFlinger::setClientStateLocked(  
  2.         const sp& client,  
  3.         const layer_state_t& s)  
  4. {  
  5.     uint32_t flags = 0;  
  6.     sp layer(client->getLayerUser(s.surface));  
  7.     if (layer != 0) {  
  8.         const uint32_t what = s.what ;  
  9.         if (what & layer_state_t::ePositionChanged) {  
  10.             if (layer->setPosition(s.x, s.y))  
  11.                 flags |= eTraversalNeeded;  
  12.         }  
  13.         if (what & layer_state_t::eLayerChanged) {  
  14.             // NOTE: index needs to be calculated before we update the state  
  15.             ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);  
  16.             if (layer->setLayer(s.z)) {  
  17.                 mCurrentState.layersSortedByZ.removeAt(idx);  
  18.                 mCurrentState.layersSortedByZ.add(layer);  
  19.                 // we need traversal (state changed)  
  20.                 // AND transaction (list changed)  
  21.                 flags |= eTransactionNeeded|eTraversalNeeded;  
  22.             }  
  23.         }  
  24.     ……  
  25.     }  
  26. }  

[cpp] view plain copy

  1. bool Layer::setLayer(uint32_t z) {  
  2.     if (mCurrentState.z == z)  
  3.         return false;  
  4.     mCurrentState.sequence++;  
  5.     mCurrentState.z = z;  
  6.     setTransactionFlags(eTransactionNeeded);  
  7.     return true;  
  8. }  

可以看到,只要设置的z值和之前的不同,setLayer就会返回true。

然后mCurrentState.layersSortedByZ.removeAt和mCurrentState.layersSortedByZ.add就会被执行。

至此,layer的真正z-order就确定好了。

原文  http://blog.csdn.net/panzhenjie/article/details/10916619

surfaceflinger的主要工作就是负责把上层传递下来的各个不同的layer进行composition。

这里,我们来讨论一下各个layer在surfaceflinger中的上下排序关系和相关的代码实现,代码基于android4.3

首先介绍一下两个类,SurfaceFlinger和Client。

简单的说,这两个类的关系可以这么理解:SurfaceFlinger实现了具体的composition的服务,而每一个有UI的程序都需要通过SurfaceFlinger去实现渲染。

这些程序可以通过Client的一些接口来调用SurfaceFlinger以实现这个目的。

Client类中有一个createSurface成员函数

[cpp] view plain copy

  1. status_t Client::createSurface(  
  2.         const String8& name,  
  3.         uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  4.         sp* handle,  
  5.         sp* gbp)  
  6. {  
  7.     /* 
  8.      * createSurface must be called from the GL thread so that it can 
  9.      * have access to the GL context. 
  10. < li>     */  

  11.   
  12.     class MessageCreateLayer : public MessageBase {  
  13.         SurfaceFlinger* flinger;  
  14.         Client* client;  
  15.         sp* handle;  
  16.         sp* gbp;  
  17.         status_t result;  
  18.         const String8& name;  
  19.         uint32_t w, h;  
  20.         PixelFormat format;  
  21.         uint32_t flags;  
  22.     public:  
  23.         MessageCreateLayer(Sur faceFlinger* flinger,  
  24.                 const String8& name, Client* client,  
  25.                 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  26.                 sp* handle,  
  27.                 sp* gbp)  
  28.             : flinger(flinger), client(client),  
  29.               handle(handle), gbp(gbp),  
  30.               name(name), w(w), h(h), format(format), flags(flags) {  
  31.         }  
  32.         status_t getResult() const { return result; }  
  33.         virtual bool handler() {  
  34.             result = flinger->createLayer(name, client, w, h, format, flags,  
  35.                     handle, gbp);  
  36.             return true;  
  37.         }  
  38.     };  
  39.   
  40.     sp msg = new MessageCreateLayer(mFlinger.get(),  
  41.             name, this, w, h, format, flags, handle, gbp);  
  42.     mFlinger->postMessageSync(msg);  
  43.     return static_cast( msg.get() )->getResult();  
  44. }  

createLayer函数是SurfaceFlinger类的私有函数,但是因为Client是他的友元,所以可以直接调用来创建一个layer。

[cpp] view plain copy

  1. private:  
  2.     friend class Client;  
  3.     friend class DisplayEventConnection;  
  4.     friend class Layer;  
  5.     friend class SurfaceTextureLayer;  

看下createLayer的代码

< strong>[cpp] view plain copy

  1. status_t SurfaceFlinger::createLayer(  
  2.         const String8& name,  
  3.         const sp& client,  
  4.         uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  5.         sp* handle, sp* gbp)  
  6. {  
  7.     //ALOGD(“createLayer for (%d x %d), name=%s”, w, h, name.string());  
  8.     if (int32_t(w|h) < 0) {  
  9.         ALOGE(“createLayer() failed, w or h is negative (w= %d, h=%d)”,  
  10.                 int(w), int(h));  
  11.         return BAD_VALUE;  
  12.     }  
  13.   
  14.     status_t result = NO_ERROR;  
  15.   
  16.     sp layer;  
  17.   
  18.     switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {  
  19.         case ISurfaceComposerClient::eFXSurfaceNormal:  
  20.             result = createNormalLayer(client,  
  21.                     name, w, h, flags, format,  
  22.                     handle, gbp, &layer);  
  23.             break;  
  24.         case ISurfaceComposerClient::eFXSurfaceDim:  
  25.             result = createDimLayer(client,  
  26.                     name, w, h, flags,  
  27.                     handle, gbp, &layer);  
  28.             break;  
  29.         default:  
  30.             result = BAD_VALUE;  
  31.             break;  
  32.     }  
  33.   
  34.     if (result == NO_ERROR) {  
  35.         addClientLayer(client, *handle, *gbp, layer);  
  36.         setTransactionFlags(eTransactionNeeded);  
  37.     }  
  38.     return result;  
  39. }  

这个函数很清晰,主要是调用createNormalLayer和createDimLayer去创建不同的layer。

我们先忽略createDimLayer,只看createNormalLayer的实现

[cpp] view plain copy

  1. status_t SurfaceFlinger::createNormalLayer(const sp& client,  
  2.         const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,  
  3.         sp* handle, sp* gbp, sp* outLayer)  
  4. {  
  5.     // initialize the surfaces  
  6.     switch (format) {  
  7.     case PIXEL_FORMAT_TRANSPARENT:  
  8.     case PIXEL_FORMAT_TRANSLUCENT:  
  9.         format = PIXEL_FORMAT_RGBA_8888;  
  10.         break;  
  11.     case PIXEL_FORMAT_OPAQUE:  
  12. #ifdef NO_RGBX_8888  
  13.         format = PIXEL_FORMAT_RGB_565;  
  14. #else  
  15.         format = PIXEL_FORMAT_RGBX_8888;  
  16. #endif  
  17.         break;  
  18.     }  
  19.   
  20. #ifdef NO_RGBX_8888  

  21.     if (format == PIXEL_FORMAT_RGBX_8888)  
  22.         format = PIXEL_FORMAT_RGBA_8888;  
  23. #endif  
  24.   
  25.     *outLayer = new Layer(this, client, name, w, h, flags);  
  26.     status_t err = (*outLayer)->setBuffers(w, h, format, flags);  
  27.     if (err == NO_ERROR) {  
  28.         *handle = (*outLayer)->getHandle();  
  29.         *gbp = (*outLayer)->getBufferQueue();  
  30.     }  
  31.   
  32.     ALOGE_IF(err, “createNormalLayer() failed (%s)”, strerror(-err));  
  33.     return err;  
  34. }  

这里主要是创建了一个Layer对象。

[cpp] view plain copy

  1. Layer::Layer(SurfaceFlinger* flinger, const sp& client,  
  2.         const String8& name, uint32_t w, uint32_t h, uint32_t flags)  
  3.     :   contentDirty(false),  
  4.         sequence(uint32_t(android_atomic_inc(&sSequence))),  
  5.         mFlinger(flinger),  
  6.         mTextureName(-1U),  
  7.         mPremultipliedAlpha(true),  
  8.         mName(“unnamed”),  
  9.         mDebug(false),  
  10.         mFormat(PIXEL_FORMAT_NONE),  
  11.         mGLExtensions(GLExtensions::getInstance()),  
  12.         mOpaqueLayer(true),  
  13.         mTransactionFlags(0),  
  14.         mQueuedFrames(0),  
  15.         mCurrentTransform(0),  
  16.         mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),  
  17.         mCurrentOpacity(true),  
  18.         mRefreshPending(false),  
  19.         mFrameLatencyNeeded(false),  
  20.         mF iltering(false),  
  21.         mNeedsFiltering(false),  
  22.         mSecure(false),  
  23.         mProtectedByApp(false),  
  24.         mHasSurface(false),  
  25.         mClientRef(client)  
  26. {  
  27.     mCurrentCrop.makeInvalid();  
  28.     glGenTextures(1, &mTextureName);  
  29.   
  30.     uint32_t layerFlags = 0;  
  31.     if (flags & ISurfaceComposerClient::eHidden)  
  32.         layerFlags = layer_state_t::eLayerHidden;  
  33.   
  34.     if (flags & ISurfaceComposerClient::eNonPremultiplied)  
  35.         mPremultipliedAlpha = false;  
  36.   
  37.     mName = name;  
  38.   
  39.     mCurrentState.active.w = w;  
  40.     mCurrentState.active.h = h;  
  41.     mCurrentState.active.crop.makeInvalid();  
  42.     mCurrentState.z = 0;  
  43.     mCurrentState.alpha = 0xFF;  
  44.     mCurrentState.layerStack = 0;  
  45.     mCurrentState.flags = layerFlags;  
  46.     mCurrentState.sequence = 0;  
  47.     mCurrentState.transform.set(0, 0);  
  48.     mCurrentState.requested = mCurrentState.active;  
  49.   
  50.     // drawing state & current state are identical  
  51.     mDrawingState = mCurrentState;  
  52. }  

这里我们主要关注和layer顺序相关的信息

[cpp] view plain copy

  1.     sequence(uint32_t(android_atomic_inc(&sSequence))),  
  2.   
  3.     mCurrentState.z = 0;  
  4.     mCurrentState.layerStack = 0;  

这三个变量决定了layer之间的顺序,我来说明一下具体的含义。

首先是layerStack,大家可以把它理解为组的含义。也就是说属于不同组的layer之间互不干扰。

SurfaceFlinger中有一个DisplayDevice类,他表示用来显示的设备,譬如LCD或者是HDMI。

DisplayDevice里也有一个成员变量mLayerStack,在进行composition的时候,只有和这个device的layerstack相同的layer才可能被显示在这个设备上。

第二个是z,其实他就是z-order的意思,表示x,y,z轴的z轴上的顺序。数字越大,表示越在上面,数字越小,表示越在下面。

第三个是sequence,因为sSequence是一个static的变量,所以递加的效果就是为每一个layer设置一个唯一且递增的序列号。

概念介绍完了,我们继续看代码,看看到底是不是这样。

创建完layer之后,createLayer会调用addClientLayer把这个layer的信息添加到当前的状态信息里去。

[cpp] view plain copy

  1. void SurfaceFlinger::addClientLayer(const sp& client,  
  2.         const sp& handle,  
  3.         const sp& gbc,  
  4.         const sp& lbc)  
  5. {  
  6.     // attach this layer to the client  
  7.     client->attachLayer(handle, lbc);  
  8.   
  9.     // add this layer to the current state list  
  10.     Mutex::Autolock _l(mStateLock);  
  11.     mCurrentState.layersSortedByZ.add(lbc);  
  12.     mGraphicBufferProducerList.add(gbc->asBinder());  
  13. }  

layersSortedByZ变量很重要,surfaceflinger真正渲染的时候就是靠它来知道哪个layer在上哪个在下的。

这里的add函数就负责把layer放进去

[cpp] view plain copy

  1. ssize_t SortedVectorImpl::add(const void* item)  
  2. {  
  3.     size_t order;  
  4.     ssize_t index = _indexOrderOf(item, &order);  
  5.     if (index < 0) {  
  6.         index = VectorImpl::insertAt(item, order, 1);  
  7.     } else {  
  8.         index = VectorImpl::replaceAt(item, index);  
  9.     }  
  10.     return index;  
  11. }  

[cpp] view plain copy

  1. ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const  
  2. {  
  3.     // binary search  
  4.     ssize_t err = NAME_NOT_FOUND;  
  5.     ssize_t l = 0;  
  6.     ssize_t h = size()-1;  
  7.     ssize _t mid;  
  8.     const void* a = arrayImpl();  
  9.     const size_t s = itemSize();  
  10.     while (l <= h) {  
  11.         mid = l + (h – l)/2;  
  12.         const voidconst curr = reinterpret_cast<const char *>(a) + (mid*s);  
  13.         const int c = do_compare(curr, item);  
  14.         if (c == 0) {  
  15.             err = l = mid;  
  16.             break;  
  17.         } else if (c < 0) {  
  18.             l = mid + 1;  
  19.         } else {  
  20.             h = mid – 1;  
  21.         }  
  22.     }  
  23.     if (order) *order = l;  
  24.     return err;  
  25. }  

[cpp] view plain copy

  1. int SurfaceFlinger::LayerVector::do_compare(const void* lhs,  
  2.     const void* rhs) const  
  3. {  
  4.     // sort layers per layer-stack, then by z-order and finally by sequence  
  5.     const sp& l(*reinterpret_cast<const sp*>(lhs));  
  6.     const sp& r(*reinterpret_cast<const sp*>(rhs));  
  7.   
  8.     uint32_t ls = l->currentState().layerStack;  
  9.     uint32_t rs = r->currentState().layerStack;  
  10.     if (ls != rs)  
  11.         return ls – rs;  
  12.   
  13.     uint32_t lz = l->currentState().z;  
  14.     uint32_t rz = r->currentState().z;  
  15.     if (lz != rz)  
  16.         return lz – rz;  
  17.   
  18.     ret urn l->sequence – r->sequence;  
  19. }  

连着贴了3个函数,其主要作用就是判断这个layer要插在layersSortedByZ的什么位置。

从do_compare我们可以看出,和我刚才分析的是一样的。

第一步是比较layerstack,不同的layerstack分开。

然后再比较z,最后假设这些都一样,就比较唯一的layer序列号。

但是至今为止,layerStack和z都还只是初始化时的0,所以在创建layer的时候,只是把他根据序列号放进layersSortedByZ而已,其实他的顺序还是没有设置的。

下面我们就要去找找看到底在哪里设置了这些。

大家应该都知道bootanimation吧,就是开机负责绘制闪啊闪的Android字样的那个程序。

在里面我找到了这样的代码

[cpp] view plain copy

  1. // create the native surface  
  2. sp control = session()->createSurface(String8(“BootAnimation”),  
  3.         dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);  
  4.   
  5. SurfaceComposerClient::openGlobalTransaction();  
  6. control->setLayer(0x40000000);  
  7. SurfaceComposerClient::closeGlobalTransaction();  

前面的createSurface我们在前面已经分析完成了。

下面就是setLayer了,这个0x40000000到底是设置了什么那?

我们一步步往下看

[cpp] view plain copy

  1. status_t SurfaceControl::setLayer(int32_t layer) {  
  2.     status_t err = validate();  
  3.     if (err < 0) return err;  
  4.     const sp& client(mClient);  
  5.     return client->setLayer(mHandle, layer);  
  6. }  

[cpp] view plain copy

  1. status_t SurfaceComposerClient::setLayer(const sp& id, int32_t z) {  
  2.     return getComposer().setLayer(this, id, z);  
  3. }  

[cpp] view plain copy

  1. status_t Composer::setLayer(const sp& client,  
  2.         const sp& id, int32_t z) {  
  3.     Mutex::Autolock _l(mLock);  
  4.     layer_st ate_t* s = getLayerStateLocked(client, id);  
  5.     if (!s)  
  6.         return BAD_INDEX;  
  7.     s->what |= layer_state_t::eLayerChanged;  
  8.     s->z = z;  
  9.     return NO_ERROR;  
  10. }  

可以看到,这个layer变量最终变成了z,存进了layer_state_t结构体内。

这个结构体是哪来的?在看看getLayerStateLocked

[cpp] view plain copy

  1. layer_state_t* Composer::getLayerStateLocked(  
  2.         const sp& client, const sp& id) {  
  3.   
  4.     ComposerState s;  
  5.     s.client = client->mClient;  
  6.     s.state.surface = id;  
  7.   
  8.     ssize_t index = mComposerStates.indexOf(s);  
  9.     if (index < 0) {  
  10.         // we don’t have it, add an initialized layer_state to our list  
  11.         index = mComposerStates.add(s);  
  12.     }  
  13.   
  14.     ComposerState* const out = mComposerStates.editArray();  
  15.     return &(out[index].state);  
  16. }  

原来是从mComposerStates里找来的啊。

这些代码看上去是在做相关的操作,但是设置还没有具体生效。

下面我们看看SurfaceComposerClient::closeGlobalTransaction()的作用

[cpp] view plain copy

  1. void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {  
  2.     Composer::closeGlobalTransaction(synchronous);  
  3. }  

[cpp] view plain copy

  1. void Composer::closeGlobalTransactionImpl(bool synchronous) {  
  2.     sp sm(ComposerService::getComposerService());  
  3.   
  4.     Vector transaction;  
  5.     Vector displayTransaction;  
  6.     uint32_t flags = 0;  
  7.   
  8.     { // scope for the lock  
  9.         Mutex::Autolock _l(mLock);  
  10.         mForceSynchronous |= synchronous;  
  11.         if (!mTransactionNestCount) {  
  12.             ALOGW(“At least one call to closeGlobalTransaction() was not matched by a prior “  
  13.                     “call to openGlobalTransaction().”);  
  14.         } else if (–mTransactionNestCount) {  
  15.             return;  
  16.         }  
  17.   
  18.         transaction = mComposerStates;  
  19.         mComposerStates.clear();  
  20.   
  21.         displayTransaction = mDisplayStates;  
  22.         mDisplayStates.clear();  
  23.   
  24.         if (mForceSynchronous) {  
  25.             flags |= ISurfaceComposer::eSynchronous;  
  26.         }  
  27.         if (mAnim ation) {  
  28.             flags |= ISurfaceComposer::eAnimation;  
  29.         }  
  30.         if (mTransition) {  
  31.             flags |= ISurfaceComposer::eTransition;  
  32.         }  
  33.         if (mOrientationEnd) {  
  34.             flags |= ISurfaceComposer::eOrientationEnd;  
  35.         }  
  36.         mForceSynchronous = false;  
  37.         mAnimation = false;  
  38.     }  
  39.   
  40.    sm->setTransactionState(transaction, displayTransaction, flags);  
  41. }  

mComposerStates被赋值给transaction,然后通过sm->setTransactionState传递下去。

[cpp] view plain copy

  1. void SurfaceFlinger::setTransactionState(  
  2.         const Vector& state,  
  3.         const Vector& displays,  
  4.         uint32_t flags)  
  5. {  
  6.     ……  
  7.     count = state.size();  
  8.     for (size_t i=0 ; i
  9.         const ComposerState& s(s tate[i]);  
  10.         // Here we need to check that the interface we’re given is indeed  
  11.         // one of our own. A malicious client could give us a NULL  
  12.         // IInterface, or one of its own or even one of our own but a  
  13.         // different type. All these situations would cause us to crash.  
  14.         //  
  15.         // NOTE: it would be better to use RTTI as we could directly check  
  16.         // that we have a Client*. however, RTTI is disabled in Android.  
  17.         if (s.client != NULL) {  
  18.             sp binder = s.client->asBinder();  
  19.             if (binder != NULL) {  
  20.                 String16 desc(binder->getInterfaceDescriptor());  
  21.                 if (desc == ISurfaceComposerClient::descriptor) {  
  22.                     sp client( static_cast(s.client.get()) );  
  23.                     transactionFlags |= setClientStateLocked(client, s.state);  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28.     ……  
  29. }  

[cpp] view plain copy

  1. uint32_t SurfaceFlinger::setClientStateLocked(  
  2.         const sp& client,  
  3.         const layer_state_t& s)  
  4. {  
  5.     uint32_t flags = 0;  
  6.     sp layer(client->getLayerUser(s.surface));  
  7.     if (layer != 0) {  
  8.         const uint32_t what = s.what;  
  9.         if (what & layer_state_t::ePositionChanged) {  
  10.             if (layer->setPosition(s.x, s.y))  
  11.                 flags |= eTraversalNeeded;  
  12.         }  
  13.         if (what & layer_state_t::eLayerChanged) {  
  14.             // NOTE: index needs to be calculated before we update the state  
  15.             ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);  
  16.             if (layer->setLayer(s.z)) {  
  17.                 mCurrentState.layersSortedByZ.removeAt(idx);  
  18.                 mCurrentState.layersSortedByZ.add(layer);  
  19.                 // we need traversal (state changed)  
  20.                 // AND transaction (list changed)  
  21.                 flags |= eTransactionNeeded|eTraversalNeeded;  
  22.             }  
  23.         }  
  24.     ……  
  25.     }  
  26. }  

[cpp] view plain copy

  1. bool Layer::setLayer(uint32_t z) {  
  2.     if (mCurrentState.z == z)  
  3.         return false;  
  4.     mCurrentState.sequence++;  
  5.     mCurrentState.z = z;  
  6.     setTransactionFlags(eTransactionNeeded);  
  7.     return true;  
  8. }  

可以看到,只要设置的z值和之前的不同,setLayer就会返回true。

然后mCurrentState.layersSortedByZ.removeAt和mCurrentState.layersSortedByZ.add就会被执行。

至此,layer的真正z-order就确定好了。

[cpp] view plain copy

  1. status_t Client::createSurface(  
  2.         const String8& name,  
  3.         uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  4.         sp* handle,  
  5.         sp* gbp)  
  6. {  
  7.     /* 
  8.      * createSurface must be called from the GL thread so that it can 
  9.      * have access to the GL context. 
  10.      */  
  11.   
  12.     class MessageCreateLayer : public MessageBase {  
  13.         SurfaceFlinger* flinger;  
  14.         Client* client;  
  15.         sp* handle;  
  16.         sp* gbp;  
  17.         status_t result;  
  18.         const String8& name;  
  19.         uint32_t w, h;  
  20.         PixelFormat format;  
  21.         uint32_t flags;  
  22.     public:  
  23.         MessageCreateLayer(SurfaceFlinger* flinger,  
  24.                 const String8& name, Client* client,  
  25.                 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  26.                 sp* handle,  
  27.                 sp* gbp)  
  28.             : flinger(flinger), client(client),  
  29.               handle(handle), gbp(gbp),  
  30.               name(name), w(w), h(h), format(format), flags(flags) {  
  31.         }  
  32.         status_t getResult() const { return result; }  
  33.         virtual bool handler() {  
  34.             result = flinger->createLayer(name, client, w, h, fo rmat, flags,  
  35.                     handle, gbp);  
  36.             return true;  
  37.         }  
  38.     };  
  39.   
  40.     sp msg = new MessageCreateLayer(mFlinger.get(),  
  41.             name, this, w, h, format, flags, handle, gbp);  
  42.     mFlinger->postMessageSync(msg);  
  43.     return static_cast( msg.get() )->getResult();  
  44. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. private:  
  2.     friend class Client;  
  3.     friend class DisplayEventConnection;  
  4.     friend class Layer;  
  5.     friend class SurfaceTextureLayer;  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. status_t SurfaceFlinger::createLayer(  
  2.         const String8& name,  
  3.         const sp& client,  
  4.         uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,  
  5.         sp* handle, sp* gbp)  
  6. {  
  7.     //ALOGD(“createLayer for (%d x %d), name=%s”, w, h, name.string());  
  8.     if (int32_t(w|h) < 0) {  
  9.         ALOGE(“createLayer() failed, w or h is negative (w=%d, h=%d)”,  
  10.                 int(w), int(h));  
  11.         return BAD_VALUE;  
  12.     }  
  13.   
  14.     status_t result = NO_ERROR;  
  15.   
  16.     sp layer;  
  17.   
  18.     switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {  
  19.         case ISurfaceComposerClient::eFXSurfaceNormal:  
  20.             result = create NormalLayer(client,  
  21.                     name, w, h, flags, format,  
  22.                     handle, gbp, &layer);  
  23.             break;  
  24.         case ISurfaceComposerClient::eFXSurfaceDim:  
  25.             result = createDimLayer(client,  
  26.                     name, w, h, flags,  
  27.                     handle, gbp, &layer);  
  28.             break;  
  29.         default:  
  30.             result = BAD_VALUE;  
  31.             break;  
  32.     }  
  33.   
  34.     if (result == NO_ERROR) {  
  35.         addClientLayer(client, *handle, *gbp, layer);  
  36.         setTransactionFlags(eTransactionNeeded);  
  37.     }  
  38.     return result;  
  39. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. status_t SurfaceFlinger::createNormalLayer(const sp& client,  
  2.         < span class="keyword">const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,  
  3.         sp* handle, sp* gbp, sp* outLayer)  
  4. {  
  5.     // initialize the surfaces  
  6.     switch (format) {  
  7.     case PIXEL_FORMAT_TRANSPARENT:  
  8.     case PIXEL_FORMAT_TRANSLUCENT:  
  9.         format = PIXEL_FORMAT_RGBA_8888;  
  10.         break;  
  11.     case PIXEL_FORMAT_OPAQUE:  
  12. #ifdef NO_RGBX_8888  
  13.         format = PIXEL_FORMAT_RGB_565;  
  14. #else  
  15.         format = PIXEL_FORMAT_RGBX_8888;  
  16. #endif  
  17.         break;  
  18.     }  
  19.   
  20. #ifdef NO_RGBX_8888  
  21.     if (format == PIXEL_FORMAT_RGBX_8888)  
  22.         format = PIXEL_FORMAT_RGBA_8888;  
  23. #endif  
  24.   
  25.     *outLayer = new Layer(this, client, name, w, h, flags);  
  26.     status_t err = (*outLayer)->setBuffers(w, h, format, flags);  
  27.     if (err == NO_ERROR) {  
  28.         *handle = (*outLayer)->getHandle();  
  29.         *gbp = (*outLayer)->getBufferQueue();  
  30.     }  
  31.   
  32.     ALOGE_IF(err, “createNormalLayer() failed (%s)”, strerror(-err));  
  33.     return err;  
  34. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. Layer::Layer(SurfaceFlinger* flinger, const sp& client,  
  2.         const String8& name, uint32_t w, uint32_t h, uint32_t flags)  
  3.     :   contentDirty(false),  
  4.         sequence(uint32_t(android_atomic_inc(&sSequence))),  
  5.         mFlinger(flinger),  
  6.         mTextureName(-1U),  
  7.         mPremultipliedAlpha(true),  
  8.         mName(“unnamed”),  
  9.         mDebug(false),  
  10.         mFormat(PIXEL_FORMAT_NONE),  
  11.         mGLExtensions(GLExtensions::getInstance()),  
  12.         mOpaqueLayer(true),  
  13.         mTransactionFlags(0),  
  14.         mQueuedFrames(0),  
  15.         mCurrentTransform(0),  
  16.         mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),  
  17.         mCurrentOpacity(true),  
  18.         mRefreshPending(false),  
  19.         mFrameLatencyNeeded(false),  
  20.         mFiltering(false),  
  21.         mNeedsFiltering(false),  
  22.         mSecure(false),  
  23.         mProtectedByApp(false),  
  24.         mHasSurface(false),  
  25.         mClientRef(client)  
  26. {  
  27.     mCurrentCrop.makeInvalid();  
  28.     glGenTextures(1, &mTextureName);  
  29.   
  30.     uint32_t layerFlags = 0;  
  31.     if (flags & ISurfaceComposerClient::eHidden)  
  32.         layerFlags = layer_state_t::eLayerHidden;  
  33.   
  34.     if (flags & ISurfaceComposerClient::eNonPremultiplied)  
  35.         mPremultipliedAlpha = false;  
  36.   
  37.     mName = name;  
  38.   
  39.     mCurrentState.active.w = w;  
  40.     mCurrentState.active.h = h;  
  41.     mCurrentState.active.crop.makeInvalid();  
  42.     mCurrentState.z = 0;  
  43.     mCurrentState.alpha = 0xFF;  
  44.     mCurrentState.layerStack = 0;  
  45.     mCurrentState.flags = layerFlags;  
  46.     mCurrentState.sequence = 0;  
  47.     mCurrentState.transform.set(0, 0);  
  48.     mCurrentState.requested = mCurrentState.active;  
  49.   
  50.     // drawing state & current state are identical  
  51.     mDrawingState = mCurrentState;  
  52. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1.     sequence(uint32_t(android_atomic_inc(&sSequence))),  
  2.   
  3.     mCurrentState.z = 0;  
  4.     mCurrentState.layerStack = 0;  

[cpp] view plain copy

[cpp] view plain c opy

[cpp] view plain copy

  1. void SurfaceFlinger::addClientLayer(const sp& client,  
  2.         const sp& handle,  
  3.         const sp& gbc,  
  4.         const sp& lbc)  
  5. {  
  6.     // attach this layer to the client  
  7.     client->attachLayer(handle, lbc);  
  8.   
  9.     // add this layer to the current state list  
  10.     Mutex::Autolock _l(mStateLock);  
  11.     mCurrentState.layersSortedByZ.add(lbc);  
  12.     mGraphicBufferProducerList.add(gbc->asBinder());  
  13. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. ssize_t SortedVectorImpl::add(const void* item)  
  2. {  
  3.     size_t order;  
  4.     ssize_t index = _indexOrderOf(item, &order);  
  5.     if (index < 0) {  
  6.         index = VectorImpl::insertAt(item, order, 1);  
  7.     } else {  
  8.         index = VectorImpl::replaceAt(item, index);  
  9.     }  
  10.     return index;  
  11. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. ssize_t SortedVectorImpl:: _indexOrderOf(const void* item, size_t* order) const  
  2. {  
  3.     // binary search  
  4.     ssize_t err = NAME_NOT_FOUND;  
  5.     ssize_t l = 0;  
  6.     ssize_t h = size()-1;  
  7.     ssize_t mid;  
  8.     const void* a = arrayImpl();  
  9.     const size_t s = itemSize();  
  10.     while (l <= h) {  
  11.         mid = l + (h – l)/2;  
  12.         const voidconst curr = reinterpret_cast<const char *>(a) + (mid*s);  
  13.         const int c = do_compare(curr, item);  
  14.         if (c == 0) {  
  15.             err = l = mid;  
  16.             break;  
  17.         } else if (c < 0) {  
  18.             l = mid + 1;  
  19.         } else {  
  20.             h = mid – 1;  
  21.         }  
  22.     }  
  23.     if (order) *order = l;  
  24.     return err;  
  25. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. int SurfaceFlinger::LayerVector::do_compare(const voi d* lhs,  
  2.     const void* rhs) const  
  3. {  
  4.     // sort layers per layer-stack, then by z-order and finally by sequence  
  5.     const sp& l(*reinterpret_cast<const sp*>(lhs));  
  6.     const sp& r(*reinterpret_cast<const sp*>(rhs));  
  7.   
  8.     uint32_t ls = l->currentState().layerStack;  
  9.     uint32_t rs = r->currentState().layerStack;  
  10.     if (ls != rs)  
  11.         return ls – rs;  
  12.   
  13.     uint32_t lz = l->currentState().z;  
  14.     uint32_t rz = r->currentState().z;  
  15.     if (lz != rz)  
  16.         return lz – rz;  
  17.   
  18.     return l->sequence – r->sequence;  
  19. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. // create the native surface  
  2. sp control = session()->createSurface(String8(“BootAnimation”),  
  3.         dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);  
  4.   
  5. SurfaceComposerClient::openGlobalTransaction();  
  6. control->setLayer(0x40000000);  
  7. SurfaceComposerClient::closeGlobalTransaction();  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. status_t SurfaceControl::setLayer(int32_t layer) {  
  2.     status_t err = validate();  
  3.     if (err < 0) return err;  
  4.     const sp& client(mClient);  
  5.     return client->setLayer(mHandle, layer);  
  6. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. status_t SurfaceComposerClient::setLayer(const sp& id, int32_t z) {  
  2.     return getComposer().setLayer(this, id, z);  
  3. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. status_t Composer::setLayer(const sp& client,  
  2.         const sp& id, int32_t z) {  < /li>
  3.     Mutex::Autolock _l(mLock);  
  4.     layer_state_t* s = getLayerStateLocked(client, id);  
  5.     if (!s)  
  6.         return BAD_INDEX;  
  7.     s->what |= layer_state_t::eLayerChanged;  
  8.     s->z = z;  
  9.     return NO_ERROR;  
  10. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. layer_state_t* Composer::get LayerStateLocked(  
  2.         const sp& client, const sp& id) {  
  3.   
  4.     ComposerState s;  
  5.     s.client = client->mClient;  
  6.     s.state.surface = id;  
  7.   
  8.     ssize_t index = mComposerStates.indexOf(s);  
  9.     if (index < 0) {  
  10.         // we don’t have it, add an initialized layer_state to our list  
  11.         index = mComposerStates.add(s);  
  12.     }  
  13.   
  14.     ComposerState* const out = m ComposerStates.editArray();  
  15.     return &(out[index].state);  
  16. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {  
  2.     Composer::closeGlobalTransaction(synchronous);  
  3. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. void Composer::closeGlobalTransactionImpl(bool synchronous) {  
  2.     sp sm(ComposerService::getComposerService());  
  3.   
  4.     Vector transaction;  
  5.     Vector displayTransaction;  
  6.     uint32_t flags = 0;  
  7.   
  8.     { // scope for the lock  
  9.         Mutex::Autolock _l(mLock);  
  10.         mForceSynchronous |= synchronous;  
  11.         if (!mTransactionNestCount) {  
  12.             ALOGW(“At least one call to closeGlobalTransaction() was not matched by a prior “  
  13.                     “call to openGlobalTransaction().”);  
  14.         } else if (–mTransactionNestCount) {  
  15.             return;  
  16.         }  
  17.   
  18.         transaction = mComposerStates;  
  19.         mComposerStates.clear();  
  20.   
  21.         displayTransaction = mDisplayStates;  
  22.         mDispla yStates.clear();  
  23.   
  24.         if (mForceSynchronous) {  
  25.             flags |= ISurfaceComposer::eSynchronous;  
  26.         }  
  27.         if (mAnimation) {  
  28.             flags |= ISurfaceComposer::eAnimation;  
  29.         }  
  30.         if (mTransition) {  
  31.             flags |= ISurfaceComposer::eTransition;  
  32.         }  
  33.         if (mOrientationEnd) {  
  34.             flags |= ISurfaceComposer::eOrientationEnd;  
  35.         }  
  36.         mFo rceSynchronous = false;  
  37.         mAnimation = false;  
  38.     }  
  39.   
  40.    sm->setTransactionState(transaction, displayTransaction, flags);  
  41. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. void SurfaceFlinger::setTransactionState(  
  2.         const Vector& state,  
  3.         const Vector& displays,  
  4.         uint32_t flags)  
  5. {  
  6.     ……  
  7.     count = state.size();  
  8.     for (size_t i=0 ; i
  9.         const ComposerState& s(state[i]);  
  10.         // Here we need to check that the interface we’re given is indeed  
  11.         // one of our own. A malicious client could give us a NULL  
  12.         // IInterface, or one of its own or even one of our own but a  
  13.         // different type. All these situations would cause us to crash.  
  14.         //  
  15.         // NOTE: it would be better to use RTTI as we could directly check  
  16.         // that we have a Client*. however, RTTI is disabled in Android.  
  17.         if (s.client != NULL) {  
  18.             sp binder = s.client->asBinder();  
  19.             if (binder != NULL) {  
  20.                 String16 desc(binder->getInterfaceDescriptor());  
  21.                 if (desc == ISurfaceComp oserClient::descriptor) {  
  22.                     sp client( static_cast(s.client.get()) );  
  23.                     transactionFlags |= setClientStateLocked(client, s.state);  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28.     ……  
  29. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. uint32_t SurfaceFlinger::setClientStateLocked(  
  2.         const sp& client,  
  3.         const layer_state_t& s)  
  4. {  
  5.     uint32_t flags = 0;  
  6.     sp layer(client->getLayerUser(s.surface));  
  7.     if (layer != 0) {  
  8.         const uint32_t what = s.what;  
  9.         if (what & layer_state_t::ePositionChanged) {  
  10.             if (layer->setPosition(s.x, s.y))  
  11.                 flags |= eTraversalNeeded;  
  12.         }  
  13.         if (what & layer_state_t::eLayerChanged) {  
  14.             // NOTE: index needs to be calculated before we update the state  
  15.             ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);  
  16.             if (layer->setLayer(s.z)) {  
  17.                 mCurrentState.layersSortedByZ.removeAt(idx);  
  18.                 mCurrentState.layersSortedByZ.add(layer);  
  19.                 // we need traversal (state changed)  
  20.                 // AND transaction (list changed)  
  21.                 flags |= eTransactionNeeded|eTraversalNeeded;  
  22.             }  
  23.         }  
  24.     ……  
  25.     }  
  26. }  

[cpp] view plain copy

[cpp] view plain copy

[cpp] view plain copy

  1. bool Layer::setLayer(uint32_t z) {  
  2.     if (mCurrentState.z == z)  
  3.         return false;  
  4.     mCurrentState.sequence++;  
  5.     mCurrentState.z = z;  
  6. < li>    setTransactionFlags(eTransactionNeeded);  

  7.     return true;  
  8. }  

[cpp] view plain copy

[cpp] view plain copy

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1491 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.