• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 namespace android {
26 
LayerRejecter(Layer::State & front,Layer::State & current,bool & recomputeVisibleRegions,bool stickySet,const char * name,int32_t overrideScalingMode,bool & freezePositionUpdates)27 LayerRejecter::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     mFreezeGeometryUpdates(freezePositionUpdates) {}
41 
reject(const sp<GraphicBuffer> & buf,const BufferItem & item)42 bool 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             mFreezeGeometryUpdates = false;
78 
79             if (mFront.crop != mFront.requestedCrop) {
80                 mFront.crop = mFront.requestedCrop;
81                 mCurrent.crop = mFront.requestedCrop;
82                 mRecomputeVisibleRegions = true;
83             }
84             if (mFront.finalCrop != mFront.requestedFinalCrop) {
85                 mFront.finalCrop = mFront.requestedFinalCrop;
86                 mCurrent.finalCrop = mFront.requestedFinalCrop;
87                 mRecomputeVisibleRegions = true;
88             }
89         }
90 
91         ALOGD_IF(DEBUG_RESIZE,
92                  "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
93                  "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) "
94                  "}\n"
95                  "            requested={ wh={%4u,%4u} }}\n",
96                  mName, bufWidth, bufHeight, item.mTransform, item.mScalingMode, mFront.active.w,
97                  mFront.active.h, mFront.crop.left, mFront.crop.top, mFront.crop.right,
98                  mFront.crop.bottom, mFront.crop.getWidth(), mFront.crop.getHeight(),
99                  mFront.requested.w, mFront.requested.h);
100     }
101 
102     if (!isFixedSize && !mStickyTransformSet) {
103         if (mFront.active.w != bufWidth || mFront.active.h != bufHeight) {
104             // reject this buffer
105             ALOGE("[%s] rejecting buffer: "
106                   "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
107                   mName, bufWidth, bufHeight, mFront.active.w, mFront.active.h);
108             return true;
109         }
110     }
111 
112     // if the transparent region has changed (this test is
113     // conservative, but that's fine, worst case we're doing
114     // a bit of extra work), we latch the new one and we
115     // trigger a visible-region recompute.
116     //
117     // We latch the transparent region here, instead of above where we latch
118     // the rest of the geometry because it is only content but not necessarily
119     // resize dependent.
120     if (!mFront.activeTransparentRegion.isTriviallyEqual(mFront.requestedTransparentRegion)) {
121         mFront.activeTransparentRegion = mFront.requestedTransparentRegion;
122 
123         // We also need to update the current state so that
124         // we don't end-up overwriting the drawing state with
125         // this stale current state during the next transaction
126         //
127         // NOTE: We don't need to hold the transaction lock here
128         // because State::active is only accessed from this thread.
129         mCurrent.activeTransparentRegion = mFront.activeTransparentRegion;
130 
131         // recompute visible region
132         mRecomputeVisibleRegions = true;
133     }
134 
135     return false;
136 }
137 
138 }  // namespace android
139