Android6.0 SurfaceControl分析(一)SurfaceControl創建&使用 Surface創建&使用

一、SurfaceControl的創建

SurfaceControl的創建是在ViewRootImpl中調用requestLayout,最後到WMS的relayoutWindow函數創建SurfaceControl對象。是通過WindowState的WindowStateAnimator對象調用createSurfaceLocked對象創建的。最後再通過outSurface傳給ViewRootImpl。

  1. ……
  2. SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();//創建SurfaceControl
  3. if (surfaceControl != null) {
  4. outSurface.copyFrom(surfaceControl);//把Surface傳給ViewRootImpl
  5. if (SHOW_TRANSACTIONS) Slog.i(TAG,
  6. ” OUT SURFACE “ + outSurface + “: copied”);
  7. } else {
  8. // For some reason there isn’t a surface. Clear the
  9. // caller’s object so they see the same state.
  10. outSurface.release();
  11. }
  12. ……

然後在WindowStateAnimator的createSurfaceLocked中創建SurfaceControl,如下

  1. mSurfaceControl = new SurfaceControl(
  2. mSession.mSurfaceSession,
  3. attrs.getTitle().toString(),
  4. width, height, format, flags);

隨後會調用SurfaceControl的設置位置,設置layer等函數,這個我們後面再分析。

  1. SurfaceControl.openTransaction();
  2. try {
  3. mSurfaceX = left;
  4. mSurfaceY = top;
  5. try {
  6. mSurfaceControl.setPosition(left, top);
  7. mSurfaceLayer = mAnimLayer;
  8. final DisplayContent displayContent = w.getDisplayContent();
  9. if (displayContent != null) {
  10. mSurfaceControl.setLayerStack(displayContent.getDisplay().getLayerStack());
  11. }
  12. mSurfaceControl.setLayer(mAnimLayer);
  13. mSurfaceControl.setAlpha(0);
  14. mSurfaceShown = false;
  15. } catch (RuntimeException e) {
  16. Slog.w(TAG, "Error creating surface in " + w, e);
  17. mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
  18. }
  19. mLastHidden = true;
  20. } finally {
  21. SurfaceControl.closeTransaction();
  22. if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
  23. "<<< CLOSE TRANSACTION createSurfaceLocked");
  24. }

我們再來看看SurfaceControl的構造函數,這裏主要是調用了nativeCreate函數來獲取mNativeObject對象

  1. public SurfaceControl(SurfaceSession session,
  2. String name, int w, int h, int format, int flags)
  3. throws OutOfResourcesException {
  4. ......
  5. mName = name;
  6. mNativeObject = nativeCreate(session, name, w, h, format, flags);
  7. if (mNativeObject == 0) {
  8. throw new OutOfResourcesException(
  9. "Couldn't allocate SurfaceControl native object");
  10. }
  11. mCloseGuard.open("release");
  12. }

我們再看nativeCreate函數中,主要就是利用傳下來的SurfaceSession對象獲取client,然後調用createSurface。最後將創建的Surface對象返回,保存在SurfaceControl的mNativeObject對象中。這裏的session就是WindowState的Session對象的SurfaceSession對象,而這個對象就是保存着和SurfaceFlinger通信的client對象。

  1. static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
  2. jstring nameStr, jint w, jint h, jint format, jint flags) {
  3. ScopedUtfChars name(env, nameStr);
  4. sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
  5. sp<SurfaceControl> surface = client->createSurface(
  6. String8(name.c_str()), w, h, format, flags);
  7. if (surface == NULL) {
  8. jniThrowException(env, OutOfResourcesException, NULL);
  9. return 0;
  10. }
  11. surface->incStrong((void *)nativeCreate);
  12. return reinterpret_cast<jlong>(surface.get());
  13. }

這裏創建的SurfaceControl native層是在SurfaceComposerClient中createSurface函數中創建的,這裏通過mClient連接SurfaceFlinger獲取handle和gbp,然後創建SurfaceControl對象。這個SurfaceControl纔是最終在java層的SurfaceControl的nativeObject對象。

  1. sp<SurfaceControl> SurfaceComposerClient::createSurface(
  2. const String8& name,
  3. uint32_t w,
  4. uint32_t h,
  5. PixelFormat format,
  6. uint32_t flags)
  7. {
  8. sp<SurfaceControl> sur;
  9. if (mStatus == NO_ERROR) {
  10. sp<IBinder> handle;
  11. sp<IGraphicBufferProducer> gbp;
  12. status_t err = mClient->createSurface(name, w, h, format, flags,
  13. &handle, &gbp);
  14. ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
  15. if (err == NO_ERROR) {
  16. sur = new SurfaceControl(this, handle, gbp);
  17. }
  18. }
  19. return sur;
  20. }

之前博客分析過創建WindowState的過程,會在addWindow中創建WindowState,然後調用其attach函數。WindowState的Session是保存和應用ViewRootImpl通信的Session對象。我們直接來看下WindowState的attach函數。

  1. void attach() {
  2. if (WindowManagerService.localLOGV) Slog.v(
  3. TAG, "Attaching " + this + " token=" + mToken
  4. + ", list=" + mToken.windows);
  5. mSession.windowAddedLocked();
  6. }

在Session的windowAddedLocked函數中創建了SurfaceSession對象。

  1. void windowAddedLocked() {
  2. if (mSurfaceSession == null) {
  3. if (WindowManagerService.localLOGV) Slog.v(
  4. WindowManagerService.TAG, "First window added to " + this + ", creating SurfaceSession");
  5. mSurfaceSession = new SurfaceSession();
  6. if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
  7. WindowManagerService.TAG, " NEW SURFACE SESSION " + mSurfaceSession);
  8. mService.mSessions.add(this);
  9. if (mLastReportedAnimatorScale != mService.getCurrentAnimatorScale()) {
  10. mService.dispatchNewAnimatorScaleLocked(this);
  11. }
  12. }
  13. mNumWindow++;
  14. }

SurfaceSession對象的構造函數就調用了nativeCreate函數,返回值保存在mNativeClient中了。

  1. public SurfaceSession() {
  2. mNativeClient = nativeCreate();
  3. }

這裏的nativeCreate是在android_view_SurfaceSession.cpp中的jni函數,這裏就是創建了SurfaceComposerClient對象,然後保存在了java層SurfaceSession的mNativeClient中了。

  1. static jlong nativeCreate(JNIEnv* env, jclass clazz) {
  2. SurfaceComposerClient* client = new SurfaceComposerClient();
  3. client->incStrong((void*)nativeCreate);
  4. return reinterpret_cast<jlong>(client);
  5. }

在SurfaceComposerClient::onFirstRef函數中(就是在對象新建之前會調用這個函數),裏面調用了ComposerService的createConnection 並且把返回值存入mClient對象。這個對象就是連接SurfaceFlinger的client對象。

  1. void SurfaceComposerClient::onFirstRef() {
  2. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  3. if (sm != 0) {
  4. sp<ISurfaceComposerClient> conn = sm->createConnection();
  5. if (conn != 0) {
  6. mClient = conn;
  7. mStatus = NO_ERROR;
  8. }
  9. }
  10. }
而最終ComposerService的connectLocked函數返回是連接SurfaceFlinger進程的client端的binder。
  1. void ComposerService::connectLocked() {
  2. const String16 name("SurfaceFlinger");
  3. while (getService(name, &mComposerService) != NO_ERROR) {
  4. usleep(250000);
  5. }
  6. assert(mComposerService != NULL);
  7. // Create the death listener.
  8. class DeathObserver : public IBinder::DeathRecipient {
  9. ComposerService& mComposerService;
  10. virtual void binderDied(const wp<IBinder>& who) {
  11. ALOGW("ComposerService remote (surfaceflinger) died [%p]",
  12. who.unsafe_get());
  13. mComposerService.composerServiceDied();
  14. }
  15. public:
  16. DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
  17. };
  18. mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
  19. IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
  20. }
  21. /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
  22. ComposerService& instance = ComposerService::getInstance();
  23. Mutex::Autolock _l(instance.mLock);
  24. if (instance.mComposerService == NULL) {
  25. ComposerService::getInstance().connectLocked();
  26. assert(instance.mComposerService != NULL);
  27. ALOGD("ComposerService reconnected");
  28. }
  29. return instance.mComposerService;
  30. }

我們再來回顧WMS中relayout 把Surface傳給應用ViewRootImpl,是調用瞭如下函數。是Surface對象的copyFrom函數

outSurface.copyFrom(surfaceControl);

這裏我們調用了native函數nativeCreateFromSurfaceControl重新獲取一個一個native對象,最後再調用Surface的setNativeObjectLocked函數保存在Surface對象的mNativeObject中。

  1. public void copyFrom(SurfaceControl other) {
  2. if (other == null) {
  3. throw new IllegalArgumentException("other must not be null");
  4. }
  5. long surfaceControlPtr = other.mNativeObject;
  6. if (surfaceControlPtr == 0) {
  7. throw new NullPointerException(
  8. "SurfaceControl native object is null. Are you using a released SurfaceControl?");
  9. }
  10. long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr);//傳入的參數是native層的SurfaceControl
  11. synchronized (mLock) {
  12. if (mNativeObject != 0) {
  13. nativeRelease(mNativeObject);
  14. }
  15. setNativeObjectLocked(newNativeObject);
  16. }
  17. }

nativeCreateFromSurfaceControl函數就是從native層的SurfaceControl對象調用getSurface對象,然後返回保存在Surface java對象的mNativeObject中。

  1. static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
  2. jlong surfaceControlNativeObj) {
  3. /*
  4. * This is used by the WindowManagerService just after constructing
  5. * a Surface and is necessary for returning the Surface reference to
  6. * the caller. At this point, we should only have a SurfaceControl.
  7. */
  8. sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
  9. sp<Surface> surface(ctrl->getSurface());
  10. if (surface != NULL) {
  11. surface->incStrong(&sRefBaseOwner);
  12. }
  13. return reinterpret_cast<jlong>(surface.get());
  14. }

最終調用了SurfaceControl的getSurface,之前在SurfaceComposerClient中連接SurfaceFlinger時把gbp,傳入了SurfaceControl。這裏就是創建Surface使用。

  1. sp<Surface> SurfaceControl::getSurface() const
  2. {
  3. Mutex::Autolock _l(mLock);
  4. if (mSurfaceData == 0) {
  5. // This surface is always consumed by SurfaceFlinger, so the
  6. // producerControlledByApp value doesn't matter; using false.
  7. mSurfaceData = new Surface(mGraphicBufferProducer, false);
  8. }
  9. return mSurfaceData;
  10. }

因此最後就很明顯,WMS中有些窗口需要設置屬性、大小、Z軸等最後調用的是SurfaceControl.cpp,而應用申請buffer等是調用Surface.cpp。這樣就把WMS和ViewRootImpl分開了。


二、Surface和SurfaceControl的native層

上面說到WMS的窗口設置屬性和應用的ViewRootImpl最後是通過SurfaceControl和Surface的native層和SurfaceFlinger通信的。

2.1 SurfaceControl

我們先來看SurfaceControl的,以下面例子爲例:

  1. public void setLayer(int zorder) {
  2. checkNotReleased();
  3. nativeSetLayer(mNativeObject, zorder);
  4. }
然後到android_view_SurfaceControl.cpp的nativeSetLayer,最後就是調用保存在SurfaceControl java層的mNativeObjcet(這個就是native層的SurfaceControl)。因此最後是調用了SurfaceControl native的setLayer
  1. static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
  2. SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
  3. status_t err = ctrl->setLayer(zorder);
  4. if (err < 0 && err != NO_INIT) {
  5. doThrowIAE(env);
  6. }
  7. }

SurfaceControl中setLayer是調用了SurfaceComposerClient的setLayer函數,在SurfaceComposerClient::setLayer又是調用了Composer的setLayer函數

  1. status_t SurfaceControl::setLayer(uint32_t layer) {
  2. status_t err = validate();
  3. if (err < 0) return err;
  4. return mClient->setLayer(mHandle, layer);
  5. }
  1. status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, uint32_t z) {
  2. return getComposer().setLayer(this, id, z);
  3. }

這裏最後調用Composer的setLayer函數,這裏通過getLayerStateLocked獲取到對應layer的state。像position,size等都是通過這個函數獲取其layer的state,然後再修改其相關值。

  1. status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
  2. const sp<IBinder>& id, uint32_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. }
我們來看getLayerStateLocked就是獲取相關的ComposerState,Composer把所有的Layer的state都放在mComposerStates中。
  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);//沒有這個layer,把它添加到mComposerStates中
  10. }
  11. ComposerState* const out = mComposerStates.editArray();
  12. return &(out[index].state);
  13. }

這裏我們不得不提下,整個SurfaceFlinger和SystemServer通信就一個ComposerService,因爲它是單例。但是SurfaceControl有很多,每個窗口都有一個。但是最後所有的SurfaceControl都要在這裏ComposerService和SurfaceFlinger通信,因此在mComposerStates中保存了所有layer的state。是通過SurfaceControl的openTransaction開啓closeTransaction結束。然後一把和SurfaceFlinger通信,避免頻繁通信造成效率下降。(具體我們下篇博客分析),但是比如我們createSurface是每個SurfaceComposerClient自己通過mClient和SurfaceFlinger通信,創建一個layer的。


我們再來看SurfaceComposerClient的onFirstRef函數,是調用了SurfaceFlinger的createConnection,然後保存在了自己的mClient中。這個mClient就是每個SurfaceControl用來和SurfaceFlinger通信的。

  1. void SurfaceComposerClient::onFirstRef() {
  2. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  3. if (sm != 0) {
  4. sp<ISurfaceComposerClient> conn = sm->createConnection();
  5. if (conn != 0) {
  6. mClient = conn;
  7. mStatus = NO_ERROR;
  8. }
  9. }
  10. }

我們再來看看SurfaceFlinger的createConnection函數,就是新建了一個Client對象。這個對象是一個Binder的server端。

  1. sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
  2. {
  3. sp<ISurfaceComposerClient> bclient;
  4. sp<Client> client(new Client(this));
  5. status_t err = client->initCheck();
  6. if (err == NO_ERROR) {
  7. bclient = client;
  8. }
  9. return bclient;
  10. }

這個Client的createSurface就是調用了SurfaceFlinger的createLayer,然後創建一個layer。這個layer主要是一個handle和一個gbp對象。

  1. status_t Client::createSurface(
  2. const String8& name,
  3. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  4. sp<IBinder>* handle,
  5. sp<IGraphicBufferProducer>* 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. class MessageCreateLayer : public MessageBase {
  12. SurfaceFlinger* flinger;
  13. Client* client;
  14. sp<IBinder>* handle;
  15. sp<IGraphicBufferProducer>* 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. const String8& name, Client* client,
  24. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  25. sp<IBinder>* handle,
  26. sp<IGraphicBufferProducer>* gbp)
  27. : flinger(flinger), client(client),
  28. handle(handle), gbp(gbp),
  29. name(name), w(w), h(h), format(format), flags(flags) {
  30. }
  31. status_t getResult() const { return result; }
  32. virtual bool handler() {
  33. result = flinger->createLayer(name, client, w, h, format, flags,
  34. handle, gbp);
  35. return true;
  36. }
  37. };
  38. sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
  39. name, this, w, h, format, flags, handle, gbp);
  40. mFlinger->postMessageSync(msg);
  41. return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
  42. }

這個client對象就是用來創建Surface,銷燬Surface,獲取和清除Surface信息。

2.2 Surface

下面我們再來看java層的Surface,看lockCanvas函數。最後是調用了nativeLockCanvas函數

  1. public Canvas lockCanvas(Rect inOutDirty)
  2. throws Surface.OutOfResourcesException, IllegalArgumentException {
  3. synchronized (mLock) {
  4. checkNotReleasedLocked();
  5. if (mLockedObject != 0) {
  6. // Ideally, nativeLockCanvas() would throw in this situation and prevent the
  7. // double-lock, but that won't happen if mNativeObject was updated. We can't
  8. // abandon the old mLockedObject because it might still be in use, so instead
  9. // we just refuse to re-lock the Surface.
  10. throw new IllegalArgumentException("Surface was already locked");
  11. }
  12. mLockedObject = nativeLockCanvas(mNativeObject, mCanvas, inOutDirty);
  13. return mCanvas;
  14. }
  15. }

我們再來看android_view_Surface.cpp的nativeLockCanvas函數,先是調用Surface的lock函數,這個函數會從SurfaceFlinger中申請buffer,然後

  1. static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
  2. jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
  3. sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
  4. if (!isSurfaceValid(surface)) {
  5. doThrowIAE(env);
  6. return 0;
  7. }
  8. Rect dirtyRect;
  9. Rect* dirtyRectPtr = NULL;
  10. if (dirtyRectObj) {
  11. dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
  12. dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
  13. dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
  14. dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
  15. dirtyRectPtr = &dirtyRect;
  16. }
  17. ANativeWindow_Buffer outBuffer;
  18. status_t err = surface->lock(&outBuffer, dirtyRectPtr);//會從SurfaceFlinger中申請buffer
  19. if (err < 0) {
  20. const char* const exception = (err == NO_MEMORY) ?
  21. OutOfResourcesException :
  22. "java/lang/IllegalArgumentException";
  23. jniThrowException(env, exception, NULL);
  24. return 0;
  25. }
  26. SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
  27. convertPixelFormat(outBuffer.format),
  28. kPremul_SkAlphaType);
  29. if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
  30. info.fAlphaType = kOpaque_SkAlphaType;
  31. }
  32. SkBitmap bitmap;
  33. ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
  34. bitmap.setInfo(info, bpr);
  35. if (outBuffer.width > 0 && outBuffer.height > 0) {
  36. bitmap.setPixels(outBuffer.bits);
  37. } else {
  38. // be safe with an empty bitmap.
  39. bitmap.setPixels(NULL);
  40. }
  41. Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
  42. nativeCanvas->setBitmap(bitmap);
  43. if (dirtyRectPtr) {
  44. nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top,
  45. dirtyRect.right, dirtyRect.bottom);
  46. }
  47. if (dirtyRectObj) {
  48. env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left);
  49. env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top);
  50. env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right);
  51. env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
  52. }
  53. // Create another reference to the surface and return it. This reference
  54. // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
  55. // because the latter could be replaced while the surface is locked.
  56. sp<Surface> lockedSurface(surface);
  57. lockedSurface->incStrong(&sRefBaseOwner);
  58. return (jlong) lockedSurface.get();
  59. }

在Surface的lock函數中會調用dequeueBuffer函數,這個函數會通過mGraphicBufferProducer的dequeueBuffer函數來申請buffer

  1. int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
  2. ATRACE_CALL();
  3. ALOGV("Surface::dequeueBuffer");
  4. uint32_t reqWidth;
  5. uint32_t reqHeight;
  6. bool swapIntervalZero;
  7. PixelFormat reqFormat;
  8. uint32_t reqUsage;
  9. {
  10. Mutex::Autolock lock(mMutex);
  11. reqWidth = mReqWidth ? mReqWidth : mUserWidth;
  12. reqHeight = mReqHeight ? mReqHeight : mUserHeight;
  13. swapIntervalZero = mSwapIntervalZero;
  14. reqFormat = mReqFormat;
  15. reqUsage = mReqUsage;
  16. } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
  17. int buf = -1;
  18. sp<Fence> fence;
  19. status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
  20. reqWidth, reqHeight, reqFormat, reqUsage);
  21. if (result < 0) {
  22. ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
  23. "failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat,
  24. reqUsage, result);
  25. return result;
  26. }
  27. Mutex::Autolock lock(mMutex);
  28. sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
  29. // this should never happen
  30. ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
  31. if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
  32. freeAllBuffers();
  33. }
  34. if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
  35. result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
  36. if (result != NO_ERROR) {
  37. ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
  38. mGraphicBufferProducer->cancelBuffer(buf, fence);
  39. return result;
  40. }
  41. }
  42. if (fence->isValid()) {
  43. *fenceFd = fence->dup();
  44. if (*fenceFd == -1) {
  45. ALOGE("dequeueBuffer: error duping fence: %d", errno);
  46. // dup() should never fail; something is badly wrong. Soldier on
  47. // and hope for the best; the worst that should happen is some
  48. // visible corruption that lasts until the next frame.
  49. }
  50. } else {
  51. *fenceFd = -1;
  52. }
  53. *buffer = gbuf.get();
  54. return OK;
  55. }


三、總結

這樣我們很明顯,應用申請buffer和窗口設置屬性等完全分開來了。應用ViewRootImpl申請buffer通過Surface、窗口在WMS中通過SurfaceControl設置屬性、Z軸、大小(通過openTransaction和closeTransaction開啓關閉,因爲設置窗口屬性是所有窗口一起設置,然後通過ComposerService和SurfaceFlinger連接一起設置過去)。而在創建SurfaceComposerClient時,會和SurfaceFlinger通信調用一個createConnection函數,然後SurfaceFlinger那邊會創建一個Client對象(Binder的server端),這樣每個SurfaceComposerClient都有一個client和SurfaceFlinger通信了(也就是在SurfaceControl中能通過這個client,申請SurfaceFlinger創建一個layer,這種行爲是每個SurfaceControl自己和SurfaceFlinger通信,而不是一起通過ComposerService一起喝SurfaceFlinger通信了)。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章