LayerRejecter.cpp revision a9347647eca3101c014be902b713772de3977d87
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "LayerRejecter.h"
18
19#include <gui/BufferItem.h>
20
21#include "clz.h"
22
23#define DEBUG_RESIZE 0
24
25namespace android {
26
27LayerRejecter::LayerRejecter(Layer::State& front,
28                             Layer::State& current,
29                             bool& recomputeVisibleRegions,
30                             bool stickySet,
31                             const char* name,
32                             int32_t overrideScalingMode,
33                             bool& freezePositionUpdates)
34  : mFront(front),
35    mCurrent(current),
36    mRecomputeVisibleRegions(recomputeVisibleRegions),
37    mStickyTransformSet(stickySet),
38    mName(name),
39    mOverrideScalingMode(overrideScalingMode),
40    mFreezePositionUpdates(freezePositionUpdates) {}
41
42bool LayerRejecter::reject(const sp<GraphicBuffer>& buf, const BufferItem& item) {
43    if (buf == NULL) {
44        return false;
45    }
46
47    uint32_t bufWidth = buf->getWidth();
48    uint32_t bufHeight = buf->getHeight();
49
50    // check that we received a buffer of the right size
51    // (Take the buffer's orientation into account)
52    if (item.mTransform & Transform::ROT_90) {
53        swap(bufWidth, bufHeight);
54    }
55
56    int actualScalingMode = mOverrideScalingMode >= 0 ? mOverrideScalingMode : item.mScalingMode;
57    bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
58    if (mFront.active != mFront.requested) {
59        if (isFixedSize || (bufWidth == mFront.requested.w && bufHeight == mFront.requested.h)) {
60            // Here we pretend the transaction happened by updating the
61            // current and drawing states. Drawing state is only accessed
62            // in this thread, no need to have it locked
63            mFront.active = mFront.requested;
64
65            // We also need to update the current state so that
66            // we don't end-up overwriting the drawing state with
67            // this stale current state during the next transaction
68            //
69            // NOTE: We don't need to hold the transaction lock here
70            // because State::active is only accessed from this thread.
71            mCurrent.active = mFront.active;
72            mCurrent.modified = true;
73
74            // recompute visible region
75            mRecomputeVisibleRegions = true;
76        }
77
78        ALOGD_IF(DEBUG_RESIZE,
79                 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
80                 "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) "
81                 "}\n"
82                 "            requested={ wh={%4u,%4u} }}\n",
83                 mName, bufWidth, bufHeight, item.mTransform, item.mScalingMode, mFront.active.w,
84                 mFront.active.h, mFront.crop.left, mFront.crop.top, mFront.crop.right,
85                 mFront.crop.bottom, mFront.crop.getWidth(), mFront.crop.getHeight(),
86                 mFront.requested.w, mFront.requested.h);
87    }
88
89    if (!isFixedSize && !mStickyTransformSet) {
90        if (mFront.active.w != bufWidth || mFront.active.h != bufHeight) {
91            // reject this buffer
92            ALOGE("[%s] rejecting buffer: "
93                  "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
94                  mName, bufWidth, bufHeight, mFront.active.w, mFront.active.h);
95            return true;
96        }
97    }
98
99    // if the transparent region has changed (this test is
100    // conservative, but that's fine, worst case we're doing
101    // a bit of extra work), we latch the new one and we
102    // trigger a visible-region recompute.
103    if (!mFront.activeTransparentRegion.isTriviallyEqual(mFront.requestedTransparentRegion)) {
104        mFront.activeTransparentRegion = mFront.requestedTransparentRegion;
105
106        // We also need to update the current state so that
107        // we don't end-up overwriting the drawing state with
108        // this stale current state during the next transaction
109        //
110        // NOTE: We don't need to hold the transaction lock here
111        // because State::active is only accessed from this thread.
112        mCurrent.activeTransparentRegion = mFront.activeTransparentRegion;
113
114        // recompute visible region
115        mRecomputeVisibleRegions = true;
116    }
117
118    if (mFront.crop != mFront.requestedCrop) {
119        mFront.crop = mFront.requestedCrop;
120        mCurrent.crop = mFront.requestedCrop;
121        mRecomputeVisibleRegions = true;
122    }
123    mFreezePositionUpdates = false;
124
125    return false;
126}
127
128}  // namespace android
129