经过两年的时间

上篇博客我们说到SurfaceControl会调用openTransaction和closeTransaction来一起讲所有SurfaceControl的属性等传给SurfaceFlinger。我们来看下这个过程。

一、SurfaceControl

我们先来看下SurfaceControl.java中的相关函数,都是调用了JNI函数。

 
  1. public static void openTransaction() {

  2. nativeOpenTransaction();

  3. }

  4.  
  5. /** end a transaction */

  6. public static void closeTransaction() {

  7. nativeCloseTransaction();

  8. }

  9.  
  10. /** flag the transaction as an animation */

  11. public static void setAnimationTransaction() {

  12. nativeSetAnimationTransaction();

  13. }

  14.  
  15. public void setLayer(int zorder) {

  16. checkNotReleased();

  17. nativeSetLayer(mNativeObject, zorder);

  18. }

我们再来看下android_view_SurfaceControl.cpp下面的一些JNI函数,其中nativeOpenTransaction和nativeCloseTransaction都是调用了SurfaceComposerClient的静态函数,其他都是调用了c层的SurfaceControl的函数。

 
  1. static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {

  2. SurfaceComposerClient::openGlobalTransaction();

  3. }

  4.  
  5. static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {

  6. SurfaceComposerClient::closeGlobalTransaction();

  7. }

  8.  
  9. static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {

  10. SurfaceComposerClient::setAnimationTransaction();

  11. }

  12.  
  13. static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {

  14. SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);

  15. status_t err = ctrl->setLayer(zorder);

  16. if (err < 0 && err != NO_INIT) {

  17. doThrowIAE(env);

  18. }

  19. }

  20.  
  21. static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {

  22. SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);

  23. status_t err = ctrl->setPosition(x, y);

  24. if (err < 0 && err != NO_INIT) {

  25. doThrowIAE(env);

  26. }

  27. }

而SurfaceControl.cpp的函数都是调用了SurfaceComposerClient的相关函数。

 
  1. status_t SurfaceControl::setLayerStack(uint32_t layerStack) {

  2. status_t err = validate();

  3. if (err < 0) return err;

  4. return mClient->setLayerStack(mHandle, layerStack);

  5. }

  6. status_t SurfaceControl::setLayer(uint32_t layer) {

  7. status_t err = validate();

  8. if (err < 0) return err;

  9. return mClient->setLayer(mHandle, layer);

  10. }

  11. status_t SurfaceControl::setPosition(float x, float y) {

  12. status_t err = validate();

  13. if (err < 0) return err;

  14. return mClient->setPosition(mHandle, x, y);

  15. }

1.1 SurfaceComposerClient设置相关函数

而SurfaceComposerClient中的相关函数又分成两类一类是layer的状态,另一类是display的状态。

1.1.1 设置layer相关函数

下面我们先看layer的状态,有如下函数,都是通过getLayerStateLocked去查询相关layer的状态,没有的话就去添加到mComposerStates中。下面是ComposerState的结构体。

 
  1. struct ComposerState {

  2. sp<ISurfaceComposerClient> client;

  3. layer_state_t state;

  4. status_t write(Parcel& output) const;

  5. status_t read(const Parcel& input);

  6. };

getLayerStateLocked函数,就是去mComposerStates中获取相关layer的state。

 
  1. layer_state_t* Composer::getLayerStateLocked(

  2. const sp<SurfaceComposerClient>& client, const sp<IBinder>& 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. }

  17.  
  18. status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,

  19. const sp<IBinder>& id, float x, float y) {

  20. Mutex::Autolock _l(mLock);

  21. layer_state_t* s = getLayerStateLocked(client, id);

  22. if (!s)

  23. return BAD_INDEX;

  24. s->what |= layer_state_t::ePositionChanged;

  25. s->x = x;

  26. s->y = y;

  27. return NO_ERROR;

  28. }

  29.  
  30. status_t Composer::setSize(const sp<SurfaceComposerClient>& client,

  31. const sp<IBinder>& id, uint32_t w, uint32_t h) {

  32. Mutex::Autolock _l(mLock);

  33. layer_state_t* s = getLayerStateLocked(client, id);

  34. if (!s)

  35. return BAD_INDEX;

  36. s->what |= layer_state_t::eSizeChanged;

  37. s->w = w;

  38. s->h = h;

  39.  
  40. // Resizing a surface makes the transaction synchronous.

  41. mForceSynchronous = true;

  42.  
  43. return NO_ERROR;

  44. }

  45.  
  46. status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,

  47. const sp<IBinder>& id, uint32_t z) {

  48. Mutex::Autolock _l(mLock);

  49. layer_state_t* s = getLayerStateLocked(client, id);

  50. if (!s)

  51. return BAD_INDEX;

  52. s->what |= layer_state_t::eLayerChanged;

  53. s->z = z;

  54. return NO_ERROR;

  55. }

  56.  
  57. status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,

  58. const sp<IBinder>& id, uint32_t flags,

  59. uint32_t mask) {

  60. Mutex::Autolock _l(mLock);

  61. layer_state_t* s = getLayerStateLocked(client, id);

  62. if (!s)

  63. return BAD_INDEX;

  64. if (mask & layer_state_t::eLayerOpaque ||

  65. mask & layer_state_t::eLayerHidden ||

  66. mask & layer_state_t::eLayerSecure) {

  67. s->what |= layer_state_t::eFlagsChanged;

  68. }

  69. s->flags &= ~mask;

  70. s->flags |= (flags & mask);

  71. s->mask |= mask;

  72. return NO_ERROR;

  73. }

  74.  
  75. status_t Composer::setTransparentRegionHint(

  76. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,

  77. const Region& transparentRegion) {

  78. Mutex::Autolock _l(mLock);

  79. layer_state_t* s = getLayerStateLocked(client, id);

  80. if (!s)

  81. return BAD_INDEX;

  82. s->what |= layer_state_t::eTransparentRegionChanged;

  83. s->transparentRegion = transparentRegion;

  84. return NO_ERROR;

  85. }

  86.  
  87. status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,

  88. const sp<IBinder>& id, float alpha) {

  89. Mutex::Autolock _l(mLock);

  90. layer_state_t* s = getLayerStateLocked(client, id);

  91. if (!s)

  92. return BAD_INDEX;

  93. s->what |= layer_state_t::eAlphaChanged;

  94. s->alpha = alpha;

  95. return NO_ERROR;

  96. }

  97.  
  98. status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,

  99. const sp<IBinder>& id, uint32_t layerStack) {

  100. Mutex::Autolock _l(mLock);

  101. layer_state_t* s = getLayerStateLocked(client, id);

  102. if (!s)

  103. return BAD_INDEX;

  104. s->what |= layer_state_t::eLayerStackChanged;

  105. s->layerStack = layerStack;

  106. return NO_ERROR;

  107. }

  108.  
  109. status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,

  110. const sp<IBinder>& id, float dsdx, float dtdx,

  111. float dsdy, float dtdy) {

  112. Mutex::Autolock _l(mLock);

  113. layer_state_t* s = getLayerStateLocked(client, id);

  114. if (!s)

  115. return BAD_INDEX;

  116. s->what |= layer_state_t::eMatrixChanged;

  117. layer_state_t::matrix22_t matrix;

  118. matrix.dsdx = dsdx;

  119. matrix.dtdx = dtdx;

  120. matrix.dsdy = dsdy;

  121. matrix.dtdy = dtdy;

  122. s->matrix = matrix;

  123. return NO_ERROR;

  124. }

  125.  
  126. status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,

  127. const sp<IBinder>& id, const Rect& crop) {

  128. Mutex::Autolock _l(mLock);

  129. layer_state_t* s = getLayerStateLocked(client, id);

  130. if (!s)

  131. return BAD_INDEX;

  132. s->what |= layer_state_t::eCropChanged;

  133. s->crop = crop;

  134. return NO_ERROR;

  135. }

1.1.2 设置display相关函数

设置display相关函数,和前面类似。都是先调用getDisplayStateLocked函数获取相关display状态,如果没有加入到mDisplayStates中。

DisplayState的结构体如下:

 
  1. struct DisplayState {

  2.  
  3. enum {

  4. eOrientationDefault = 0,

  5. eOrientation90 = 1,

  6. eOrientation180 = 2,

  7. eOrientation270 = 3,

  8. eOrientationUnchanged = 4,

  9. eOrientationSwapMask = 0x01

  10. };

  11.  
  12. enum {

  13. eSurfaceChanged = 0x01,

  14. eLayerStackChanged = 0x02,

  15. eDisplayProjectionChanged = 0x04,

  16. eDisplaySizeChanged = 0x08

  17. };

  18.  
  19. uint32_t what;

  20. sp<IBinder> token;

  21. sp<IGraphicBufferProducer> surface;

  22. uint32_t layerStack;

  23. uint32_t orientation;

  24. Rect viewport;

  25. Rect frame;

  26. uint32_t width, height;

  27. status_t write(Parcel& output) const;

  28. status_t read(const Parcel& input);

  29. };

每个相关设置display的函数都是先调用getDisplayStateLocked函数来获取相关display状态,如果没有加入到mDisplayStates中。

 
  1. DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {

  2. DisplayState s;

  3. s.token = token;

  4. ssize_t index = mDisplayStates.indexOf(s);

  5. if (index < 0) {

  6. // we don't have it, add an initialized layer_state to our list

  7. s.what = 0;

  8. index = mDisplayStates.add(s);

  9. }

  10. return mDisplayStates.editItemAt(static_cast<size_t>(index));

  11. }

  12.  
  13. void Composer::setDisplaySurface(const sp<IBinder>& token,

  14. const sp<IGraphicBufferProducer>& bufferProducer) {

  15. Mutex::Autolock _l(mLock);

  16. DisplayState& s(getDisplayStateLocked(token));

  17. s.surface = bufferProducer;

  18. s.what |= DisplayState::eSurfaceChanged;

  19. }

  20.  
  21. void Composer::setDisplayLayerStack(const sp<IBinder>& token,

  22. uint32_t layerStack) {

  23. Mutex::Autolock _l(mLock);

  24. DisplayState& s(getDisplayStateLocked(token));

  25. s.layerStack = layerStack;

  26. s.what |= DisplayState::eLayerStackChanged;

  27. }

  28.  
  29. void Composer::setDisplayProjection(const sp<IBinder>& token,

  30. uint32_t orientation,

  31. const Rect& layerStackRect,

  32. const Rect& displayRect) {

  33. Mutex::Autolock _l(mLock);

  34. DisplayState& s(getDisplayStateLocked(token));

  35. s.orientation = orientation;

  36. s.viewport = layerStackRect;

  37. s.frame = displayRect;

  38. s.what |= DisplayState::eDisplayProjectionChanged;

  39. mForceSynchronous = true; // TODO: do we actually still need this?

  40. }

  41.  
  42. void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {

  43. Mutex::Autolock _l(mLock);

  44. DisplayState& s(getDisplayStateLocked(token));

  45. s.width = width;

  46. s.height = height;

  47. s.what |= DisplayState::eDisplaySizeChanged;

  48. }

1.3和SurfaceFlinger通信

看下面openGlobalTransactionImpl函数,上锁直接将mTransactionNestCount加1,然后在closeGlobalTransactionImpl函数中检查mTransactionNestCount的值是否为1.如果大于1,将直接退出。这是多个地方调用openGlobalTransaction和closeGlobalTransaction时,只有最后一次才会和SurfaceFlinger通信。最后把mComposerStates和mDisplayStates通过调用SurfaceFlinger的setTransactionState方法传入SurfaceFlinger中。

 
  1. void Composer::openGlobalTransactionImpl() {

  2. { // scope for the lock

  3. Mutex::Autolock _l(mLock);

  4. mTransactionNestCount += 1;

  5. }

  6. }

  7.  
  8. void Composer::closeGlobalTransactionImpl(bool synchronous) {

  9. sp<ISurfaceComposer> sm(ComposerService::getComposerService());

  10.  
  11. Vector<ComposerState> transaction;

  12. Vector<DisplayState> displayTransaction;

  13. uint32_t flags = 0;

  14.  
  15. { // scope for the lock

  16. Mutex::Autolock _l(mLock);

  17. mForceSynchronous |= synchronous;

  18. if (!mTransactionNestCount) {

  19. ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "

  20. "call to openGlobalTransaction().");

  21. } else if (--mTransactionNestCount) {//确保mTransactionNestCount在1的时候才会和SurfaceFlinger通信

  22. return;

  23. }

  24.  
  25. transaction = mComposerStates;

  26. mComposerStates.clear();

  27.  
  28. displayTransaction = mDisplayStates;

  29. mDisplayStates.clear();

  30.  
  31. if (mForceSynchronous) {

  32. flags |= ISurfaceComposer::eSynchronous;

  33. }

  34. if (mAnimation) {

  35. flags |= ISurfaceComposer::eAnimation;

  36. }

  37.  
  38. mForceSynchronous = false;

  39. mAnimation = false;

  40. }

  41.  
  42. sm->setTransactionState(transaction, displayTransaction, flags);//调用SurfaceFlinger的setTransactionState函数

  43. }


二、SurfaceFlinger

在SurfaceFlinger中是在setTransactionState函数中处理,然后遍历各个DisplayState调用setDisplayStateLocked设置display状态,接着遍历ComposerState然后调用setClientStateLocked设置状态。最后会调用setTransactionFlags函数,最后会发送INVALIDATE消息,这个消息的处理在之前的博客分析过。

 
  1. void SurfaceFlinger::setTransactionState(

  2. const Vector<ComposerState>& state,

  3. const Vector<DisplayState>& displays,

  4. uint32_t flags)

  5. {

  6. ATRACE_CALL();

  7. Mutex::Autolock _l(mStateLock);

  8. uint32_t transactionFlags = 0;

  9.  
  10. if (flags & eAnimation) {

  11. // For window updates that are part of an animation we must wait for

  12. // previous animation "frames" to be handled.

  13. while (mAnimTransactionPending) {

  14. status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));

  15. if (CC_UNLIKELY(err != NO_ERROR)) {

  16. // just in case something goes wrong in SF, return to the

  17. // caller after a few seconds.

  18. ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "

  19. "waiting for previous animation frame");

  20. mAnimTransactionPending = false;

  21. break;

  22. }

  23. }

  24. }

  25.  
  26. size_t count = displays.size();

  27. for (size_t i=0 ; i<count ; i++) {

  28. const DisplayState& s(displays[i]);

  29. transactionFlags |= setDisplayStateLocked(s);

  30. }

  31.  
  32. count = state.size();

  33. for (size_t i=0 ; i<count ; i++) {

  34. const ComposerState& s(state[i]);

  35. // Here we need to check that the interface we're given is indeed

  36. // one of our own. A malicious client could give us a NULL

  37. // IInterface, or one of its own or even one of our own but a

  38. // different type. All these situations would cause us to crash.

  39. //

  40. // NOTE: it would be better to use RTTI as we could directly check

  41. // that we have a Client*. however, RTTI is disabled in Android.

  42. if (s.client != NULL) {

  43. sp<IBinder> binder = IInterface::asBinder(s.client);

  44. if (binder != NULL) {

  45. String16 desc(binder->getInterfaceDescriptor());

  46. if (desc == ISurfaceComposerClient::descriptor) {

  47. sp<Client> client( static_cast<Client *>(s.client.get()) );

  48. transactionFlags |= setClientStateLocked(client, s.state);

  49. }

  50. }

  51. }

  52. }

  53.  
  54. if (transactionFlags) {

  55. // this triggers the transaction

  56. setTransactionFlags(transactionFlags);

  57.  
  58. // if this is a synchronous transaction, wait for it to take effect

  59. // before returning.

  60. if (flags & eSynchronous) {

  61. mTransactionPending = true;

  62. }

  63. if (flags & eAnimation) {

  64. mAnimTransactionPending = true;

  65. }

  66. while (mTransactionPending) {

  67. status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));

  68. if (CC_UNLIKELY(err != NO_ERROR)) {

  69. // just in case something goes wrong in SF, return to the

  70. // called after a few seconds.

  71. ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");

  72. mTransactionPending = false;

  73. break;

  74. }

  75. }

  76. }

  77. }


setDisplayStateLocked函数是从mCurrentState中获取其DisplayState,然后设置其一些变量。

 
  1. uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)

  2. {

  3. ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);

  4. if (dpyIdx < 0)

  5. return 0;

  6.  
  7. uint32_t flags = 0;

  8. DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));

  9. if (disp.isValid()) {

  10. const uint32_t what = s.what;

  11. if (what & DisplayState::eSurfaceChanged) {

  12. if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {

  13. disp.surface = s.surface;

  14. flags |= eDisplayTransactionNeeded;

  15. }

  16. }

  17. if (what & DisplayState::eLayerStackChanged) {

  18. if (disp.layerStack != s.layerStack) {

  19. disp.layerStack = s.layerStack;

  20. flags |= eDisplayTransactionNeeded;

  21. }

  22. }

  23. if (what & DisplayState::eDisplayProjectionChanged) {

  24. if (disp.orientation != s.orientation) {

  25. disp.orientation = s.orientation;

  26. flags |= eDisplayTransactionNeeded;

  27. }

  28. if (disp.frame != s.frame) {

  29. disp.frame = s.frame;

  30. flags |= eDisplayTransactionNeeded;

  31. }

  32. if (disp.viewport != s.viewport) {

  33. disp.viewport = s.viewport;

  34. flags |= eDisplayTransactionNeeded;

  35. }

  36. }

  37. if (what & DisplayState::eDisplaySizeChanged) {

  38. if (disp.width != s.width) {

  39. disp.width = s.width;

  40. flags |= eDisplayTransactionNeeded;

  41. }

  42. if (disp.height != s.height) {

  43. disp.height = s.height;

  44. flags |= eDisplayTransactionNeeded;

  45. }

  46. }

  47. }

  48. return flags;

  49. }

setClientStateLocked是获取layer,然后设置一些layer的属性,最后重新放入mCurrentState.layersSortedByZ中。

 
  1. uint32_t SurfaceFlinger::setClientStateLocked(

  2. const sp<Client>& client,

  3. const layer_state_t& s)

  4. {

  5. uint32_t flags = 0;

  6. sp<Layer> layer(client->getLayerUser(s.surface));//获取layer

  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. if (what & layer_state_t::eSizeChanged) {

  25. if (layer->setSize(s.w, s.h)) {

  26. flags |= eTraversalNeeded;

  27. }

  28. }

  29. if (what & layer_state_t::eAlphaChanged) {

  30. if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))

  31. flags |= eTraversalNeeded;

  32. }

  33. if (what & layer_state_t::eMatrixChanged) {

  34. if (layer->setMatrix(s.matrix))

  35. flags |= eTraversalNeeded;

  36. }

  37. if (what & layer_state_t::eTransparentRegionChanged) {

  38. if (layer->setTransparentRegionHint(s.transparentRegion))

  39. flags |= eTraversalNeeded;

  40. }

  41. if (what & layer_state_t::eFlagsChanged) {

  42. if (layer->setFlags(s.flags, s.mask))

  43. flags |= eTraversalNeeded;

  44. }

  45. if (what & layer_state_t::eCropChanged) {

  46. if (layer->setCrop(s.crop))

  47. flags |= eTraversalNeeded;

  48. }

  49. if (what & layer_state_t::eLayerStackChanged) {

  50. // NOTE: index needs to be calculated before we update the state

  51. ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);

  52. if (layer->setLayerStack(s.layerStack)) {

  53. mCurrentState.layersSortedByZ.removeAt(idx);

  54. mCurrentState.layersSortedByZ.add(layer);

  55. // we need traversal (state changed)

  56. // AND transaction (list changed)

  57. flags |= eTransactionNeeded|eTraversalNeeded;

  58. }

  59. }

  60. }

  61. return flags;

  62. }

这些函数我们都在博客http://blog.csdn.net/kc58236582/article/details/52778333中,每个layer的onFrameAvailable中最后也会调用signalLayerUpdate函数,最后一样会发送INVALIDATE消息。至于这个消息的处理在这篇博客中分析过了,会对各个layer属性、display更新。

三、Surface

至于Surface,之前我们分析过,在Surface中会先调用mGraphicBufferProducer的dequeueBuffer函数,然后再调用mGraphicBufferProducer的queueBuffer函数。这样在SurfaceFlinger的 每个Layer的onFrameAvailable函数就会执行,这样也会统计到每个layer的数据有变化了。

猜你喜欢

转载自blog.csdn.net/hanguohui3/article/details/82191758
今日推荐