1. 程式人生 > >Android-gui分析-buffer大小交換

Android-gui分析-buffer大小交換

這裡寫圖片描述

上圖為WMS, SurfaceFlinger, app如何同步buffer大小,總結下來有如下6 個步驟
1 Window寬度高度發生變化,如轉屏或者window動畫.
2 WMS更新SurfaceControl大小,這一步要通過事務來完成
3 SurfaceFlinger到下一個vsync週期處理事務翻轉,這時候會設定該Layer的mDefaultWidth和mDefaultHeight
4 為客戶端這邊的動作,客戶端會進行下一幀的繪製,繪製完成後進行queueBuffer, queueBuffer的過程會返回sf中關於
該layout的mDefaultWidth和mDefaultHeight,使用這個寬度高度更新request height和request width
5 客戶端使用新的request height ,request width去dequeueBuffer(注意這裡會儘量不改變dequeueBuffer的大小,如果發生轉屏的話是不用改變reques大小的,只需要queueBuffer進行旋轉buffer就可以)
6 下次queueBuffer前根據寬度高度去旋轉buffer的角度. 然後queueBuffer
7 sf收到queueBuffer和Layer的mDefaultWidth,mDefaultHeight相等後,開始使用新的寬度高度渲染.

所以這裡第四步還是渲染在舊的大小的buffer上. 這就算是一個過度過程.

const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
            (getBE().compositionInfo.mBuffer != nullptr) ;
    if (!isFixedSize()) {
        if (resizePending && getBE().compositionInfo.hwc.sidebandStream
== nullptr) { flags |= eDontUpdateGeometryState; } } // Here we apply various requested geometry states, depending on our // latching configuration. See Layer.h for a detailed discussion of // how geometry latching is controlled. if (!(flags & eDontUpdateGeometryState)) { Layer::State& editCurrentState(getCurrentState());
// If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize // mode, which causes attributes which normally latch regardless of scaling mode, // to be delayed. We copy the requested state to the active state making sure // to respect these rules (again see Layer.h for a detailed discussion). // // There is an awkward asymmetry in the handling of the crop states in the position // states, as can be seen below. Largely this arises from position and transform // being stored in the same data structure while having different latching rules. // b/38182305 // // Careful that "c" and editCurrentState may not begin as equivalent due to // applyPendingStates in the presence of deferred transactions. if (mFreezeGeometryUpdates) { float tx = c.active.transform.tx(); float ty = c.active.transform.ty(); c.active = c.requested; c.active.transform.set(tx, ty); editCurrentState.active = c.active; } else { editCurrentState.active = editCurrentState.requested; c.active = c.requested; } } if (s.active != c.active) { // invalidate and recompute the visible regions if needed flags |= Layer::eVisibleRegion; } if (c.sequence != s.sequence) { // invalidate and recompute the visible regions if needed flags |= eVisibleRegion; this->contentDirty = true; // we may use linear filtering, if the matrix scales us const uint8_t type = c.active.transform.getType(); mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE)); }

SurfaceFlinger在事務翻轉的時候並不基於去改變active的寬度高度,只是會鎖存起來.

關鍵的語句就是這個
const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
(getBE().compositionInfo.mBuffer != nullptr) ;
這個條件.

再來看看何時翻轉

bool LayerRejecter::reject(const sp<GraphicBuffer>& buf, const BufferItem& item) {
    if (buf == nullptr) {
        return false;
    }

    uint32_t bufWidth = buf->getWidth();
    uint32_t bufHeight = buf->getHeight();

    // check that we received a buffer of the right size
    // (Take the buffer's orientation into account)
    if (item.mTransform & Transform::ROT_90) {
        swap(bufWidth, bufHeight);
    }

    int actualScalingMode = mOverrideScalingMode >= 0 ? mOverrideScalingMode : item.mScalingMode;
    bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
    if (mFront.active != mFront.requested) {
        if (isFixedSize || (bufWidth == mFront.requested.w && bufHeight == mFront.requested.h)) {
            // Here we pretend the transaction happened by updating the
            // current and drawing states. Drawing state is only accessed
            // in this thread, no need to have it locked
            mFront.active = mFront.requested;

            // We also need to update the current state so that
            // we don't end-up overwriting the drawing state with
            // this stale current state during the next transaction
            //
            // NOTE: We don't need to hold the transaction lock here
            // because State::active is only accessed from this thread.
            mCurrent.active = mFront.active;
            mCurrent.modified = true;

            // recompute visible region
            mRecomputeVisibleRegions = true;

            mFreezeGeometryUpdates = false;

            if (mFront.crop != mFront.requestedCrop) {
                mFront.crop = mFront.requestedCrop;
                mCurrent.crop = mFront.requestedCrop;
                mRecomputeVisibleRegions = true;
            }
            if (mFront.finalCrop != mFront.requestedFinalCrop) {
                mFront.finalCrop = mFront.requestedFinalCrop;
                mCurrent.finalCrop = mFront.requestedFinalCrop;
                mRecomputeVisibleRegions = true;
            }
        }

        ALOGD_IF(DEBUG_RESIZE,
                 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
                 "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) "
                 "}\n"
                 "            requested={ wh={%4u,%4u} }}\n",
                 mName, bufWidth, bufHeight, item.mTransform, item.mScalingMode, mFront.active.w,
                 mFront.active.h, mFront.crop.left, mFront.crop.top, mFront.crop.right,
                 mFront.crop.bottom, mFront.crop.getWidth(), mFront.crop.getHeight(),
                 mFront.requested.w, mFront.requested.h);
    }

    if (!isFixedSize && !mStickyTransformSet) {
        if (mFront.active.w != bufWidth || mFront.active.h != bufHeight) {
            // reject this buffer
            ALOGE("[%s] rejecting buffer: "
                  "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
                  mName, bufWidth, bufHeight, mFront.active.w, mFront.active.h);
            return true;
        }
    }

    // if the transparent region has changed (this test is
    // conservative, but that's fine, worst case we're doing
    // a bit of extra work), we latch the new one and we
    // trigger a visible-region recompute.
    //
    // We latch the transparent region here, instead of above where we latch
    // the rest of the geometry because it is only content but not necessarily
    // resize dependent.
    if (!mFront.activeTransparentRegion.isTriviallyEqual(mFront.requestedTransparentRegion)) {
        mFront.activeTransparentRegion = mFront.requestedTransparentRegion;

        // We also need to update the current state so that
        // we don't end-up overwriting the drawing state with
        // this stale current state during the next transaction
        //
        // NOTE: We don't need to hold the transaction lock here
        // because State::active is only accessed from this thread.
        mCurrent.activeTransparentRegion = mFront.activeTransparentRegion;

        // recompute visible region
        mRecomputeVisibleRegions = true;
    }

    return false;
}

接下來要進行合成的buffer的寬度高度和request的一樣的時候,就可以使用新的高度渲染了.