Android6.0 SurfaceControl分析(二)SurfaceControl和SurfaceFlinger通信

转载: https://blog.csdn.net/kc58236582/article/details/65445141

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

一、SurfaceControl

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


  
  
  1. public static void openTransaction() {
  2. nativeOpenTransaction();
  3. }
  4. /** end a transaction */
  5. public static void closeTransaction() {
  6. nativeCloseTransaction();
  7. }
  8. /** flag the transaction as an animation */
  9. public static void setAnimationTransaction() {
  10. nativeSetAnimationTransaction();
  11. }
  12. public void setLayer(int zorder) {
  13. checkNotReleased();
  14. nativeSetLayer(mNativeObject, zorder);
  15. }

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


  
  
  1. static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
  2. SurfaceComposerClient::openGlobalTransaction();
  3. }
  4. static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
  5. SurfaceComposerClient::closeGlobalTransaction();
  6. }
  7. static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
  8. SurfaceComposerClient::setAnimationTransaction();
  9. }
  10. static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
  11. SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
  12. status_t err = ctrl->setLayer(zorder);
  13. if (err < 0 && err != NO_INIT) {
  14. doThrowIAE(env);
  15. }
  16. }
  17. static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
  18. SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
  19. status_t err = ctrl->setPosition(x, y);
  20. if (err < 0 && err != NO_INIT) {
  21. doThrowIAE(env);
  22. }
  23. }
而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. ComposerState s;
  4. s.client = client->mClient;
  5. s.state.surface = id;
  6. ssize_t index = mComposerStates.indexOf(s);
  7. if (index < 0) {
  8. // we don't have it, add an initialized layer_state to our list
  9. index = mComposerStates.add(s);
  10. }
  11. ComposerState* const out = mComposerStates.editArray();
  12. return &(out[index].state);
  13. }
  14. status_t Composer::setPosition( const sp<SurfaceComposerClient>& client,
  15. const sp<IBinder>& id, float x, float y) {
  16. Mutex::Autolock _l(mLock);
  17. layer_state_t* s = getLayerStateLocked(client, id);
  18. if (!s)
  19. return BAD_INDEX;
  20. s->what |= layer_state_t::ePositionChanged;
  21. s->x = x;
  22. s->y = y;
  23. return NO_ERROR;
  24. }
  25. status_t Composer::setSize( const sp<SurfaceComposerClient>& client,
  26. const sp<IBinder>& id, uint32_t w, uint32_t h) {
  27. Mutex::Autolock _l(mLock);
  28. layer_state_t* s = getLayerStateLocked(client, id);
  29. if (!s)
  30. return BAD_INDEX;
  31. s->what |= layer_state_t::eSizeChanged;
  32. s->w = w;
  33. s->h = h;
  34. // Resizing a surface makes the transaction synchronous.
  35. mForceSynchronous = true;
  36. return NO_ERROR;
  37. }
  38. status_t Composer::setLayer( const sp<SurfaceComposerClient>& client,
  39. const sp<IBinder>& id, uint32_t z) {
  40. Mutex::Autolock _l(mLock);
  41. layer_state_t* s = getLayerStateLocked(client, id);
  42. if (!s)
  43. return BAD_INDEX;
  44. s->what |= layer_state_t::eLayerChanged;
  45. s->z = z;
  46. return NO_ERROR;
  47. }
  48. status_t Composer::setFlags( const sp<SurfaceComposerClient>& client,
  49. const sp<IBinder>& id, uint32_t flags,
  50. uint32_t mask) {
  51. Mutex::Autolock _l(mLock);
  52. layer_state_t* s = getLayerStateLocked(client, id);
  53. if (!s)
  54. return BAD_INDEX;
  55. if (mask & layer_state_t::eLayerOpaque ||
  56. mask & layer_state_t::eLayerHidden ||
  57. mask & layer_state_t::eLayerSecure) {
  58. s->what |= layer_state_t::eFlagsChanged;
  59. }
  60. s->flags &= ~mask;
  61. s->flags |= (flags & mask);
  62. s->mask |= mask;
  63. return NO_ERROR;
  64. }
  65. status_t Composer::setTransparentRegionHint(
  66. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  67. const Region& transparentRegion) {
  68. Mutex::Autolock _l(mLock);
  69. layer_state_t* s = getLayerStateLocked(client, id);
  70. if (!s)
  71. return BAD_INDEX;
  72. s->what |= layer_state_t::eTransparentRegionChanged;
  73. s->transparentRegion = transparentRegion;
  74. return NO_ERROR;
  75. }
  76. status_t Composer::setAlpha( const sp<SurfaceComposerClient>& client,
  77. const sp<IBinder>& id, float alpha) {
  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::eAlphaChanged;
  83. s->alpha = alpha;
  84. return NO_ERROR;
  85. }
  86. status_t Composer::setLayerStack( const sp<SurfaceComposerClient>& client,
  87. const sp<IBinder>& id, uint32_t layerStack) {
  88. Mutex::Autolock _l(mLock);
  89. layer_state_t* s = getLayerStateLocked(client, id);
  90. if (!s)
  91. return BAD_INDEX;
  92. s->what |= layer_state_t::eLayerStackChanged;
  93. s->layerStack = layerStack;
  94. return NO_ERROR;
  95. }
  96. status_t Composer::setMatrix( const sp<SurfaceComposerClient>& client,
  97. const sp<IBinder>& id, float dsdx, float dtdx,
  98. float dsdy, float dtdy) {
  99. Mutex::Autolock _l(mLock);
  100. layer_state_t* s = getLayerStateLocked(client, id);
  101. if (!s)
  102. return BAD_INDEX;
  103. s->what |= layer_state_t::eMatrixChanged;
  104. layer_state_t:: matrix22_t matrix;
  105. matrix.dsdx = dsdx;
  106. matrix.dtdx = dtdx;
  107. matrix.dsdy = dsdy;
  108. matrix.dtdy = dtdy;
  109. s->matrix = matrix;
  110. return NO_ERROR;
  111. }
  112. status_t Composer::setCrop( const sp<SurfaceComposerClient>& client,
  113. const sp<IBinder>& id, const Rect& crop) {
  114. Mutex::Autolock _l(mLock);
  115. layer_state_t* s = getLayerStateLocked(client, id);
  116. if (!s)
  117. return BAD_INDEX;
  118. s->what |= layer_state_t::eCropChanged;
  119. s->crop = crop;
  120. return NO_ERROR;
  121. }
1.1.2 设置display相关函数

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

DisplayState的结构体如下:


  
  
  1. struct DisplayState {
  2. enum {
  3. eOrientationDefault = 0,
  4. eOrientation90 = 1,
  5. eOrientation180 = 2,
  6. eOrientation270 = 3,
  7. eOrientationUnchanged = 4,
  8. eOrientationSwapMask = 0x01
  9. };
  10. enum {
  11. eSurfaceChanged = 0x01,
  12. eLayerStackChanged = 0x02,
  13. eDisplayProjectionChanged = 0x04,
  14. eDisplaySizeChanged = 0x08
  15. };
  16. uint32_t what;
  17. sp<IBinder> token;
  18. sp<IGraphicBufferProducer> surface;
  19. uint32_t layerStack;
  20. uint32_t orientation;
  21. Rect viewport;
  22. Rect frame;
  23. uint32_t width, height;
  24. status_t write(Parcel& output) const;
  25. status_t read( const Parcel& input);
  26. };

每个相关设置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. void Composer::setDisplaySurface( const sp<IBinder>& token,
  13. const sp<IGraphicBufferProducer>& bufferProducer) {
  14. Mutex::Autolock _l(mLock);
  15. DisplayState& s(getDisplayStateLocked(token));
  16. s.surface = bufferProducer;
  17. s.what |= DisplayState::eSurfaceChanged;
  18. }
  19. void Composer::setDisplayLayerStack( const sp<IBinder>& token,
  20. uint32_t layerStack) {
  21. Mutex::Autolock _l(mLock);
  22. DisplayState& s(getDisplayStateLocked(token));
  23. s.layerStack = layerStack;
  24. s.what |= DisplayState::eLayerStackChanged;
  25. }
  26. void Composer::setDisplayProjection( const sp<IBinder>& token,
  27. uint32_t orientation,
  28. const Rect& layerStackRect,
  29. const Rect& displayRect) {
  30. Mutex::Autolock _l(mLock);
  31. DisplayState& s(getDisplayStateLocked(token));
  32. s.orientation = orientation;
  33. s.viewport = layerStackRect;
  34. s.frame = displayRect;
  35. s.what |= DisplayState::eDisplayProjectionChanged;
  36. mForceSynchronous = true; // TODO: do we actually still need this?
  37. }
  38. void Composer::setDisplaySize( const sp<IBinder>& token, uint32_t width, uint32_t height) {
  39. Mutex::Autolock _l(mLock);
  40. DisplayState& s(getDisplayStateLocked(token));
  41. s.width = width;
  42. s.height = height;
  43. s.what |= DisplayState::eDisplaySizeChanged;
  44. }

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. void Composer::closeGlobalTransactionImpl( bool synchronous) {
  8. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  9. Vector<ComposerState> transaction;
  10. Vector<DisplayState> displayTransaction;
  11. uint32_t flags = 0;
  12. { // scope for the lock
  13. Mutex::Autolock _l(mLock);
  14. mForceSynchronous |= synchronous;
  15. if (!mTransactionNestCount) {
  16. ALOGW( "At least one call to closeGlobalTransaction() was not matched by a prior "
  17. "call to openGlobalTransaction().");
  18. } else if (--mTransactionNestCount) { //确保mTransactionNestCount在1的时候才会和SurfaceFlinger通信
  19. return;
  20. }
  21. transaction = mComposerStates;
  22. mComposerStates.clear();
  23. displayTransaction = mDisplayStates;
  24. mDisplayStates.clear();
  25. if (mForceSynchronous) {
  26. flags |= ISurfaceComposer::eSynchronous;
  27. }
  28. if (mAnimation) {
  29. flags |= ISurfaceComposer::eAnimation;
  30. }
  31. mForceSynchronous = false;
  32. mAnimation = false;
  33. }
  34. sm->setTransactionState(transaction, displayTransaction, flags); //调用SurfaceFlinger的setTransactionState函数
  35. }


二、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. if (flags & eAnimation) {
  10. // For window updates that are part of an animation we must wait for
  11. // previous animation "frames" to be handled.
  12. while (mAnimTransactionPending) {
  13. status_t err = mTransactionCV.waitRelative(mStateLock, s2ns( 5));
  14. if (CC_UNLIKELY(err != NO_ERROR)) {
  15. // just in case something goes wrong in SF, return to the
  16. // caller after a few seconds.
  17. ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
  18. "waiting for previous animation frame");
  19. mAnimTransactionPending = false;
  20. break;
  21. }
  22. }
  23. }
  24. size_t count = displays.size();
  25. for ( size_t i= 0 ; i<count ; i++) {
  26. const DisplayState& s(displays[i]);
  27. transactionFlags |= setDisplayStateLocked(s);
  28. }
  29. count = state.size();
  30. for ( size_t i= 0 ; i<count ; i++) {
  31. const ComposerState& s(state[i]);
  32. // Here we need to check that the interface we're given is indeed
  33. // one of our own. A malicious client could give us a NULL
  34. // IInterface, or one of its own or even one of our own but a
  35. // different type. All these situations would cause us to crash.
  36. //
  37. // NOTE: it would be better to use RTTI as we could directly check
  38. // that we have a Client*. however, RTTI is disabled in Android.
  39. if (s.client != NULL) {
  40. sp<IBinder> binder = IInterface::asBinder(s.client);
  41. if (binder != NULL) {
  42. String16 desc(binder->getInterfaceDescriptor());
  43. if (desc == ISurfaceComposerClient::descriptor) {
  44. sp<Client> client( static_cast<Client *>(s.client.get()) );
  45. transactionFlags |= setClientStateLocked(client, s.state);
  46. }
  47. }
  48. }
  49. }
  50. if (transactionFlags) {
  51. // this triggers the transaction
  52. setTransactionFlags(transactionFlags);
  53. // if this is a synchronous transaction, wait for it to take effect
  54. // before returning.
  55. if (flags & eSynchronous) {
  56. mTransactionPending = true;
  57. }
  58. if (flags & eAnimation) {
  59. mAnimTransactionPending = true;
  60. }
  61. while (mTransactionPending) {
  62. status_t err = mTransactionCV.waitRelative(mStateLock, s2ns( 5));
  63. if (CC_UNLIKELY(err != NO_ERROR)) {
  64. // just in case something goes wrong in SF, return to the
  65. // called after a few seconds.
  66. ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
  67. mTransactionPending = false;
  68. break;
  69. }
  70. }
  71. }
  72. }


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. uint32_t flags = 0;
  7. DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
  8. if (disp.isValid()) {
  9. const uint32_t what = s.what;
  10. if (what & DisplayState::eSurfaceChanged) {
  11. if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
  12. disp.surface = s.surface;
  13. flags |= eDisplayTransactionNeeded;
  14. }
  15. }
  16. if (what & DisplayState::eLayerStackChanged) {
  17. if (disp.layerStack != s.layerStack) {
  18. disp.layerStack = s.layerStack;
  19. flags |= eDisplayTransactionNeeded;
  20. }
  21. }
  22. if (what & DisplayState::eDisplayProjectionChanged) {
  23. if (disp.orientation != s.orientation) {
  24. disp.orientation = s.orientation;
  25. flags |= eDisplayTransactionNeeded;
  26. }
  27. if (disp.frame != s.frame) {
  28. disp.frame = s.frame;
  29. flags |= eDisplayTransactionNeeded;
  30. }
  31. if (disp.viewport != s.viewport) {
  32. disp.viewport = s.viewport;
  33. flags |= eDisplayTransactionNeeded;
  34. }
  35. }
  36. if (what & DisplayState::eDisplaySizeChanged) {
  37. if (disp.width != s.width) {
  38. disp.width = s.width;
  39. flags |= eDisplayTransactionNeeded;
  40. }
  41. if (disp.height != s.height) {
  42. disp.height = s.height;
  43. flags |= eDisplayTransactionNeeded;
  44. }
  45. }
  46. }
  47. return flags;
  48. }

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的数据有变化了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值