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的一样的时候,就可以使用新的高度渲染了.

猜你喜欢

转载自blog.csdn.net/woai110120130/article/details/80291498