blob: f5fb109ae656aa47ce3f4f06c274d502dd20612c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <gui/BufferQueue.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080052#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070073 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080078 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080079 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070081 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080083 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080084 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080085 mFiltering(false),
86 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070087 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070089 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070090 mPotentialCursor(false),
91 mQueueItemLock(),
92 mQueueItemCondition(),
93 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070094 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070095 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -080096 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -080097
Mathias Agopiana67932f2011-04-20 14:20:59 -070098 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099
100 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800101 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
102 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
103 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700106 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 mCurrentState.active.w = w;
109 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700110 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800111 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700112 mCurrentState.crop.makeInvalid();
113 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700114 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
115 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700117 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700121 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500122 mCurrentState.appId = 0;
123 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 // drawing state & current state are identical
126 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127
Dan Stoza9e56aa02015-11-02 13:00:03 -0800128 const auto& hwc = flinger->getHwComposer();
129 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
130 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132
133 CompositorTiming compositorTiming;
134 flinger->getCompositorTiming(&compositorTiming);
135 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800136}
137
David Sodman41fdfc92017-11-06 16:09:56 -0800138void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700140Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700141 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700142}
143
Mathias Agopian13127d82013-03-05 17:47:11 -0800144// ---------------------------------------------------------------------------
145// callbacks
146// ---------------------------------------------------------------------------
147
David Sodmaneb085e02017-10-05 18:49:04 -0700148/*
149 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
150 * Layer. So, the implementation is done in BufferLayer. When called on a
151 * ColorLayer object, it's essentially a NOP.
152 */
David Sodmaneb085e02017-10-05 18:49:04 -0700153void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800154
Chia-I Wuc6657022017-08-15 11:18:17 -0700155void Layer::onRemovedFromCurrentState() {
156 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
157
chaviw8b3871a2017-11-01 17:41:01 -0700158 mPendingRemoval = true;
159
Robert Carr5edb1ad2017-04-25 10:54:24 -0700160 if (mCurrentState.zOrderRelativeOf != nullptr) {
161 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
162 if (strongRelative != nullptr) {
163 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700164 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700165 }
166 mCurrentState.zOrderRelativeOf = nullptr;
167 }
168
Chia-I Wuc6657022017-08-15 11:18:17 -0700169 for (const auto& child : mCurrentChildren) {
170 child->onRemovedFromCurrentState();
171 }
172}
Chia-I Wu38512252017-05-17 14:36:16 -0700173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174void Layer::onRemoved() {
175 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700176 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700177
Steven Thomasb02664d2017-07-26 18:48:28 -0700178 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700179
Robert Carr1f0a16a2016-10-24 16:27:39 -0700180 for (const auto& child : mCurrentChildren) {
181 child->onRemoved();
182 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700183}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700184
Mathias Agopian13127d82013-03-05 17:47:11 -0800185// ---------------------------------------------------------------------------
186// set-up
187// ---------------------------------------------------------------------------
188
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700189const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800190 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
chaviw13fdc492017-06-27 12:40:18 -0700193bool Layer::getPremultipledAlpha() const {
194 return mPremultipliedAlpha;
195}
196
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700197sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700199 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800200}
201
202// ---------------------------------------------------------------------------
203// h/w composer set-up
204// ---------------------------------------------------------------------------
205
Steven Thomasb02664d2017-07-26 18:48:28 -0700206bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman41fdfc92017-11-06 16:09:56 -0800207 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700208 HWC2::Layer* layer = hwc->createLayer(hwcId);
209 if (!layer) {
210 return false;
211 }
212 HWCInfo& hwcInfo = mHwcLayers[hwcId];
213 hwcInfo.hwc = hwc;
214 hwcInfo.layer = layer;
215 layer->setLayerDestroyedListener(
David Sodman41fdfc92017-11-06 16:09:56 -0800216 [this, hwcId](HWC2::Layer* /*layer*/) { mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700217 return true;
218}
219
220void Layer::destroyHwcLayer(int32_t hwcId) {
221 if (mHwcLayers.count(hwcId) == 0) {
222 return;
223 }
224 auto& hwcInfo = mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800225 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
227 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
228 // The layer destroyed listener should have cleared the entry from
229 // mHwcLayers. Verify that.
David Sodman41fdfc92017-11-06 16:09:56 -0800230 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Stale layer entry in mHwcLayers");
Steven Thomasb02664d2017-07-26 18:48:28 -0700231}
232
233void Layer::destroyAllHwcLayers() {
234 size_t numLayers = mHwcLayers.size();
235 for (size_t i = 0; i < numLayers; ++i) {
236 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
237 destroyHwcLayer(mHwcLayers.begin()->first);
238 }
239 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800240 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700241}
Steven Thomasb02664d2017-07-26 18:48:28 -0700242
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800243Rect Layer::getContentCrop() const {
244 // this is the crop rectangle that applies to the buffer
245 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 Rect crop;
247 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800248 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700249 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800250 } else if (mActiveBuffer != NULL) {
251 // otherwise we use the whole buffer
252 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700253 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800254 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700255 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700256 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 return crop;
258}
259
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700260static Rect reduce(const Rect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 if (exclude.isRect()) {
265 return win.reduce(exclude.getBounds());
266 }
267 return Region(win).subtract(exclude).getBounds();
268}
269
Robert Carr1f0a16a2016-10-24 16:27:39 -0700270Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
271 const Layer::State& s(getDrawingState());
272 Rect win(s.active.w, s.active.h);
273
274 if (!s.crop.isEmpty()) {
275 win.intersect(s.crop, &win);
276 }
277
278 Transform t = getTransform();
279 win = t.transform(win);
280
Robert Carr41b08b52017-06-01 16:11:34 -0700281 if (!s.finalCrop.isEmpty()) {
282 win.intersect(s.finalCrop, &win);
283 }
284
Chia-I Wue41dbe62017-06-13 14:10:56 -0700285 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700286 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
287 // When calculating the parent bounds for purposes of clipping,
288 // we don't need to constrain the parent to its transparent region.
289 // The transparent region is an optimization based on the
290 // buffer contents of the layer, but does not affect the space allocated to
291 // it by policy, and thus children should be allowed to extend into the
292 // parent's transparent region. In fact one of the main uses, is to reduce
293 // buffer allocation size in cases where a child window sits behind a main window
294 // (by marking the hole in the parent window as a transparent region)
295 if (p != nullptr) {
296 Rect bounds = p->computeScreenBounds(false);
297 bounds.intersect(win, &win);
298 }
299
300 if (reduceTransparentRegion) {
301 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
302 win = reduce(win, screenTransparentRegion);
303 }
304
305 return win;
306}
307
Mathias Agopian13127d82013-03-05 17:47:11 -0800308Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700309 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700310 return computeBounds(s.activeTransparentRegion);
311}
312
313Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
314 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800315 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700316
317 if (!s.crop.isEmpty()) {
318 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800319 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700320
321 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700322 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800324 // Look in computeScreenBounds recursive call for explanation of
325 // why we pass false here.
326 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700327 }
328
329 Transform t = getTransform();
330 if (p != nullptr) {
331 win = t.transform(win);
332 win.intersect(bounds, &win);
333 win = t.inverse().transform(win);
334 }
335
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700336 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700337 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800338}
339
Robert Carr1f0a16a2016-10-24 16:27:39 -0700340Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700341 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700343 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800344
345 // apply the projection's clipping to the window crop in
346 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700347 // if there are no window scaling involved, this operation will map to full
348 // pixels in the buffer.
349 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
350 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700351
352 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700353 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700354 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700355 }
356
Robert Carr1f0a16a2016-10-24 16:27:39 -0700357 Transform t = getTransform();
358 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000359 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
360 activeCrop.clear();
361 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700362 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800363 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000364 activeCrop.clear();
365 }
366 }
chaviwb1154d12017-10-31 14:15:36 -0700367
368 const auto& p = mDrawingParent.promote();
369 if (p != nullptr) {
370 auto parentCrop = p->computeInitialCrop(hw);
371 activeCrop.intersect(parentCrop, &activeCrop);
372 }
373
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374 return activeCrop;
375}
376
Dan Stoza5a423ea2017-02-16 14:10:39 -0800377FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 // the content crop is the area of the content that gets scaled to the
379 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800380 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381
382 // In addition there is a WM-specified crop we pull from our drawing state.
383 const State& s(getDrawingState());
384
385 // Screen space to make reduction to parent crop clearer.
386 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 Transform t = getTransform();
388 // Back to layer space to work with the content crop.
389 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800390
Michael Lentine28ea2172014-11-19 18:32:37 -0800391 // This needs to be here as transform.transform(Rect) computes the
392 // transformed rect and then takes the bounding box of the result before
393 // returning. This means
394 // transform.inverse().transform(transform.transform(Rect)) != Rect
395 // in which case we need to make sure the final rect is clipped to the
396 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000397 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
398 activeCrop.clear();
399 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800400
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700401 // subtract the transparent region and snap to the bounds
402 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
403
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 // Transform the window crop to match the buffer coordinate system,
405 // which means using the inverse of the current transform set on the
406 // SurfaceFlingerConsumer.
407 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700408 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000409 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700410 * the code below applies the primary display's inverse transform to the
411 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 */
David Sodman41fdfc92017-11-06 16:09:56 -0800413 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 // calculate the inverse transform
415 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800416 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800417 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800419 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800420 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421
422 int winWidth = s.active.w;
423 int winHeight = s.active.h;
424 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
425 // If the activeCrop has been rotate the ends are rotated but not
426 // the space itself so when transforming ends back we can't rely on
427 // a modification of the axes of rotation. To account for this we
428 // need to reorient the inverse rotation in terms of the current
429 // axes of rotation.
430 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
431 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
432 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800433 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 }
435 winWidth = s.active.h;
436 winHeight = s.active.w;
437 }
David Sodman41fdfc92017-11-06 16:09:56 -0800438 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439
440 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800441 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 float yScale = crop.getHeight() / float(winHeight);
443
David Sodman41fdfc92017-11-06 16:09:56 -0800444 float insetL = winCrop.left * xScale;
445 float insetT = winCrop.top * yScale;
446 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 float insetB = (winHeight - winCrop.bottom) * yScale;
448
David Sodman41fdfc92017-11-06 16:09:56 -0800449 crop.left += insetL;
450 crop.top += insetT;
451 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 crop.bottom -= insetB;
453
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 return crop;
455}
456
Robert Carrae060832016-11-28 10:51:00 -0800457void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700458{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459 const auto hwcId = displayDevice->getHwcDisplayId();
460 auto& hwcInfo = mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700461
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700462 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463 hwcInfo.forceClientComposition = false;
464
465 if (isSecure() && !displayDevice->isSecure()) {
466 hwcInfo.forceClientComposition = true;
467 }
468
469 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700470
Mathias Agopian13127d82013-03-05 17:47:11 -0800471 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700472 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500473 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700474 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800475 blendMode =
476 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477 }
David Revemanecf0fa52017-03-03 11:32:44 -0500478 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800479 ALOGE_IF(error != HWC2::Error::None,
480 "[%s] Failed to set blend mode %s:"
481 " %s (%d)",
482 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
483 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800484
485 // apply the layer's transform, followed by the display's global transform
486 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700487 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700488 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700489 if (!s.crop.isEmpty()) {
490 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700491 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800492 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000493 activeCrop.clear();
494 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700495 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800496 // This needs to be here as transform.transform(Rect) computes the
497 // transformed rect and then takes the bounding box of the result before
498 // returning. This means
499 // transform.inverse().transform(transform.transform(Rect)) != Rect
500 // in which case we need to make sure the final rect is clipped to the
501 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800502 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700505 // mark regions outside the crop as transparent
506 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800507 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
508 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
509 activeTransparentRegion.orSelf(
510 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700511 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512
513 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700514 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800515 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000516 frame.clear();
517 }
518 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000519 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
520 frame.clear();
521 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800522 const Transform& tr(displayDevice->getTransform());
523 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500524 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700525 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800526 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
527 transformedFrame.left, transformedFrame.top, transformedFrame.right,
528 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700529 } else {
530 hwcInfo.displayFrame = transformedFrame;
531 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800532
Dan Stoza5a423ea2017-02-16 14:10:39 -0800533 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700535 if (error != HWC2::Error::None) {
536 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800537 "%s (%d)",
538 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
539 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700540 } else {
541 hwcInfo.sourceCrop = sourceCrop;
542 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543
chaviw13fdc492017-06-27 12:40:18 -0700544 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700545 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800546 ALOGE_IF(error != HWC2::Error::None,
547 "[%s] Failed to set plane alpha %.3f: "
548 "%s (%d)",
549 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550
Robert Carrae060832016-11-28 10:51:00 -0800551 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800552 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
553 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500554
Albert Chaulk2a589632017-05-04 16:59:44 -0400555 int type = s.type;
556 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700557 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400558 if (parent.get()) {
559 auto& parentState = parent->getDrawingState();
560 type = parentState.type;
561 appId = parentState.appId;
562 }
563
564 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800565 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
566 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800567
Mathias Agopian29a367b2011-07-12 14:51:45 -0700568 /*
569 * Transformations are applied in this order:
570 * 1) buffer orientation/flip/mirror
571 * 2) state transformation (window manager)
572 * 3) layer orientation (screen orientation)
573 * (NOTE: the matrices are multiplied in reverse order)
574 */
575
576 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700577 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700578
Robert Carrcae605c2017-03-29 12:10:31 -0700579 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700580 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700581 * the code below applies the primary display's inverse transform to the
582 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700583 */
David Sodman41fdfc92017-11-06 16:09:56 -0800584 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700585 // calculate the inverse transform
586 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800587 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700588 }
Robert Carrcae605c2017-03-29 12:10:31 -0700589
590 /*
591 * Here we cancel out the orientation component of the WM transform.
592 * The scaling and translate components are already included in our bounds
593 * computation so it's enough to just omit it in the composition.
594 * See comment in onDraw with ref to b/36727915 for why.
595 */
596 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700597 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700598
599 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800600 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 if (orientation & Transform::ROT_INVALID) {
602 // we can only handle simple transformation
603 hwcInfo.forceClientComposition = true;
604 } else {
605 auto transform = static_cast<HWC2::Transform>(orientation);
606 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800607 ALOGE_IF(error != HWC2::Error::None,
608 "[%s] Failed to set transform %s: "
609 "%s (%d)",
610 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
611 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700613}
614
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615void Layer::forceClientComposition(int32_t hwcId) {
616 if (mHwcLayers.count(hwcId) == 0) {
617 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
618 return;
619 }
620
621 mHwcLayers[hwcId].forceClientComposition = true;
622}
Dan Stozaee44edd2015-03-23 15:50:23 -0700623
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
625 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman41fdfc92017-11-06 16:09:56 -0800626 if (mHwcLayers.count(hwcId) == 0 || getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 return;
628 }
629
630 // This gives us only the "orientation" component of the transform
631 const State& s(getCurrentState());
632
633 // Apply the layer's transform, followed by the display's global transform
634 // Here we're guaranteed that the layer's transform preserves rects
635 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700636 if (!s.crop.isEmpty()) {
637 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 }
639 // Subtract the transparent region and snap to the bounds
640 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700641 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700643 if (!s.finalCrop.isEmpty()) {
644 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000645 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 auto& displayTransform(displayDevice->getTransform());
647 auto position = displayTransform.transform(frame);
648
David Sodman41fdfc92017-11-06 16:09:56 -0800649 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, position.top);
650 ALOGE_IF(error != HWC2::Error::None,
651 "[%s] Failed to set cursor position "
652 "to (%d, %d): %s (%d)",
653 mName.string(), position.left, position.top, to_string(error).c_str(),
654 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655}
Riley Andrews03414a12014-07-01 14:22:59 -0700656
Mathias Agopian13127d82013-03-05 17:47:11 -0800657// ---------------------------------------------------------------------------
658// drawing...
659// ---------------------------------------------------------------------------
660
chaviwa76b2712017-09-20 12:02:26 -0700661void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
662 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800663}
664
chaviwa76b2712017-09-20 12:02:26 -0700665void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
666 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800667}
668
chaviwa76b2712017-09-20 12:02:26 -0700669void Layer::draw(const RenderArea& renderArea) const {
670 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800671}
672
David Sodman41fdfc92017-11-06 16:09:56 -0800673void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
674 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700675 RenderEngine& engine(mFlinger->getRenderEngine());
chaviwa76b2712017-09-20 12:02:26 -0700676 computeGeometry(renderArea, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700677 engine.setupFillWithColor(red, green, blue, alpha);
678 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800679}
680
chaviwa76b2712017-09-20 12:02:26 -0700681void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800682 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800683}
684
David Sodman41fdfc92017-11-06 16:09:56 -0800685void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 if (mHwcLayers.count(hwcId) == 0) {
687 ALOGE("setCompositionType called without a valid HWC layer");
688 return;
689 }
690 auto& hwcInfo = mHwcLayers[hwcId];
691 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800692 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
693 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 if (hwcInfo.compositionType != type) {
695 ALOGV(" actually setting");
696 hwcInfo.compositionType = type;
697 if (callIntoHwc) {
698 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800699 ALOGE_IF(error != HWC2::Error::None,
700 "[%s] Failed to set "
701 "composition type %s: %s (%d)",
702 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
703 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 }
705 }
706}
707
708HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700709 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
710 // If we're querying the composition type for a display that does not
711 // have a HWC counterpart, then it will always be Client
712 return HWC2::Composition::Client;
713 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700715 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 return HWC2::Composition::Invalid;
717 }
718 return mHwcLayers.at(hwcId).compositionType;
719}
720
721void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
722 if (mHwcLayers.count(hwcId) == 0) {
723 ALOGE("setClearClientTarget called without a valid HWC layer");
724 return;
725 }
726 mHwcLayers[hwcId].clearClientTarget = clear;
727}
728
729bool Layer::getClearClientTarget(int32_t hwcId) const {
730 if (mHwcLayers.count(hwcId) == 0) {
731 ALOGE("getClearClientTarget called without a valid HWC layer");
732 return false;
733 }
734 return mHwcLayers.at(hwcId).clearClientTarget;
735}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736
Dan Stozacac35382016-01-27 12:21:06 -0800737bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
738 if (point->getFrameNumber() <= mCurrentFrameNumber) {
739 // Don't bother with a SyncPoint, since we've already latched the
740 // relevant frame
741 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700742 }
743
Dan Stozacac35382016-01-27 12:21:06 -0800744 Mutex::Autolock lock(mLocalSyncPointMutex);
745 mLocalSyncPoints.push_back(point);
746 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700747}
748
Mathias Agopian13127d82013-03-05 17:47:11 -0800749void Layer::setFiltering(bool filtering) {
750 mFiltering = filtering;
751}
752
753bool Layer::getFiltering() const {
754 return mFiltering;
755}
756
Mathias Agopian13127d82013-03-05 17:47:11 -0800757// ----------------------------------------------------------------------------
758// local state
759// ----------------------------------------------------------------------------
760
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000761static void boundPoint(vec2* point, const Rect& crop) {
762 if (point->x < crop.left) {
763 point->x = crop.left;
764 }
765 if (point->x > crop.right) {
766 point->x = crop.right;
767 }
768 if (point->y < crop.top) {
769 point->y = crop.top;
770 }
771 if (point->y > crop.bottom) {
772 point->y = crop.bottom;
773 }
774}
775
chaviwa76b2712017-09-20 12:02:26 -0700776void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
777 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700778 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700779 const Transform renderAreaTransform(renderArea.getTransform());
780 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700781 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700782
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000783 vec2 lt = vec2(win.left, win.top);
784 vec2 lb = vec2(win.left, win.bottom);
785 vec2 rb = vec2(win.right, win.bottom);
786 vec2 rt = vec2(win.right, win.top);
787
Robert Carr1f0a16a2016-10-24 16:27:39 -0700788 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000789 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700790 lt = layerTransform.transform(lt);
791 lb = layerTransform.transform(lb);
792 rb = layerTransform.transform(rb);
793 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000794 }
795
Robert Carrb5d3d262016-03-25 15:08:13 -0700796 if (!s.finalCrop.isEmpty()) {
797 boundPoint(&lt, s.finalCrop);
798 boundPoint(&lb, s.finalCrop);
799 boundPoint(&rb, s.finalCrop);
800 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000801 }
802
Mathias Agopianff2ed702013-09-01 21:36:12 -0700803 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700804 position[0] = renderAreaTransform.transform(lt);
805 position[1] = renderAreaTransform.transform(lb);
806 position[2] = renderAreaTransform.transform(rb);
807 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800808 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700809 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800810 }
811}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800812
David Sodman41fdfc92017-11-06 16:09:56 -0800813bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700814 const Layer::State& s(mDrawingState);
815 return (s.flags & layer_state_t::eLayerSecure);
816}
817
Mathias Agopian13127d82013-03-05 17:47:11 -0800818void Layer::setVisibleRegion(const Region& visibleRegion) {
819 // always called from main thread
820 this->visibleRegion = visibleRegion;
821}
822
823void Layer::setCoveredRegion(const Region& coveredRegion) {
824 // always called from main thread
825 this->coveredRegion = coveredRegion;
826}
827
David Sodman41fdfc92017-11-06 16:09:56 -0800828void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800829 // always called from main thread
830 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
831}
832
833// ----------------------------------------------------------------------------
834// transaction
835// ----------------------------------------------------------------------------
836
Dan Stoza7dde5992015-05-22 09:51:44 -0700837void Layer::pushPendingState() {
838 if (!mCurrentState.modified) {
839 return;
840 }
841
Dan Stoza7dde5992015-05-22 09:51:44 -0700842 // If this transaction is waiting on the receipt of a frame, generate a sync
843 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800844 if (mCurrentState.barrierLayer != nullptr) {
845 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
846 if (barrierLayer == nullptr) {
847 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700848 // If we can't promote the layer we are intended to wait on,
849 // then it is expired or otherwise invalid. Allow this transaction
850 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800851 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800852 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800853 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800854 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800855 mRemoteSyncPoints.push_back(std::move(syncPoint));
856 } else {
857 // We already missed the frame we're supposed to synchronize
858 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800859 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800860 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 }
862
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 // Wake us up to check if the frame has been received
864 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700865 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 }
867 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700868 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700869}
870
Pablo Ceballos05289c22016-04-14 15:49:55 -0700871void Layer::popPendingState(State* stateToCommit) {
872 auto oldFlags = stateToCommit->flags;
873 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800874 stateToCommit->flags =
875 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700876
877 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700878 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700879}
880
Pablo Ceballos05289c22016-04-14 15:49:55 -0700881bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700882 bool stateUpdateAvailable = false;
883 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800884 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 if (mRemoteSyncPoints.empty()) {
886 // If we don't have a sync point for this, apply it anyway. It
887 // will be visually wrong, but it should keep us from getting
888 // into too much trouble.
889 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700891 stateUpdateAvailable = true;
892 continue;
893 }
894
David Sodman41fdfc92017-11-06 16:09:56 -0800895 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
896 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800897
898 // Signal our end of the sync point and then dispose of it
899 mRemoteSyncPoints.front()->setTransactionApplied();
900 mRemoteSyncPoints.pop_front();
901 continue;
902 }
903
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
905 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700906 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 stateUpdateAvailable = true;
908
909 // Signal our end of the sync point and then dispose of it
910 mRemoteSyncPoints.front()->setTransactionApplied();
911 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800912 } else {
913 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700916 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 stateUpdateAvailable = true;
918 }
919 }
920
921 // If we still have pending updates, wake SurfaceFlinger back up and point
922 // it at this layer so we can process them
923 if (!mPendingStates.empty()) {
924 setTransactionFlags(eTransactionNeeded);
925 mFlinger->setTransactionFlags(eTraversalNeeded);
926 }
927
928 mCurrentState.modified = false;
929 return stateUpdateAvailable;
930}
931
Mathias Agopian13127d82013-03-05 17:47:11 -0800932uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800933 ATRACE_CALL();
934
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700936 Layer::State c = getCurrentState();
937 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 return 0;
939 }
940
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700941 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
David Sodman41fdfc92017-11-06 16:09:56 -0800943 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700944
David Sodmaneb085e02017-10-05 18:49:04 -0700945 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700946 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000947 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800948 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
949 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
950 " requested={ wh={%4u,%4u} }}\n"
951 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
952 " requested={ wh={%4u,%4u} }}\n",
953 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(), c.active.w,
954 c.active.h, c.crop.left, c.crop.top, c.crop.right, c.crop.bottom,
955 c.crop.getWidth(), c.crop.getHeight(), c.requested.w, c.requested.h, s.active.w,
956 s.active.h, s.crop.left, s.crop.top, s.crop.right, s.crop.bottom,
957 s.crop.getWidth(), s.crop.getHeight(), s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700959 // record the new size, form this point on, when the client request
960 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700961 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700963
Robert Carre392b552017-09-19 12:16:05 -0700964 // Don't let Layer::doTransaction update the drawing state
965 // if we have a pending resize, unless we are in fixed-size mode.
966 // the drawing state will be updated only once we receive a buffer
967 // with the correct size.
968 //
969 // In particular, we want to make sure the clip (which is part
970 // of the geometry state) is latched together with the size but is
971 // latched immediately when no resizing is involved.
972 //
973 // If a sideband stream is attached, however, we want to skip this
974 // optimization so that transactions aren't missed when a buffer
975 // never arrives
976 //
977 // In the case that we don't have a buffer we ignore other factors
978 // and avoid entering the resizePending state. At a high level the
979 // resizePending state is to avoid applying the state of the new buffer
980 // to the old buffer. However in the state where we don't have an old buffer
981 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -0800982 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
983 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700984 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -0700985 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700986 flags |= eDontUpdateGeometryState;
987 }
988 }
989
Robert Carr7bf247e2017-05-18 14:02:49 -0700990 // Here we apply various requested geometry states, depending on our
991 // latching configuration. See Layer.h for a detailed discussion of
992 // how geometry latching is controlled.
993 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -0700994 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700995
996 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
997 // mode, which causes attributes which normally latch regardless of scaling mode,
998 // to be delayed. We copy the requested state to the active state making sure
999 // to respect these rules (again see Layer.h for a detailed discussion).
1000 //
1001 // There is an awkward asymmetry in the handling of the crop states in the position
1002 // states, as can be seen below. Largely this arises from position and transform
1003 // being stored in the same data structure while having different latching rules.
1004 // b/38182305
1005 //
1006 // Careful that "c" and editCurrentState may not begin as equivalent due to
1007 // applyPendingStates in the presence of deferred transactions.
1008 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001009 float tx = c.active.transform.tx();
1010 float ty = c.active.transform.ty();
1011 c.active = c.requested;
1012 c.active.transform.set(tx, ty);
1013 editCurrentState.active = c.active;
1014 } else {
1015 editCurrentState.active = editCurrentState.requested;
1016 c.active = c.requested;
1017 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001018 }
1019
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001020 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 // invalidate and recompute the visible regions if needed
1022 flags |= Layer::eVisibleRegion;
1023 }
1024
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001025 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001026 // invalidate and recompute the visible regions if needed
1027 flags |= eVisibleRegion;
1028 this->contentDirty = true;
1029
1030 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001031 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001032 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001033 }
1034
Dan Stozac8145172016-04-28 16:29:06 -07001035 // If the layer is hidden, signal and clear out all local sync points so
1036 // that transactions for layers depending on this layer's frames becoming
1037 // visible are not blocked
1038 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001039 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001040 }
1041
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001043 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045}
1046
Pablo Ceballos05289c22016-04-14 15:49:55 -07001047void Layer::commitTransaction(const State& stateToCommit) {
1048 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001049}
1050
Mathias Agopian13127d82013-03-05 17:47:11 -08001051uint32_t Layer::getTransactionFlags(uint32_t flags) {
1052 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1053}
1054
1055uint32_t Layer::setTransactionFlags(uint32_t flags) {
1056 return android_atomic_or(flags, &mTransactionFlags);
1057}
1058
Robert Carr82364e32016-05-15 11:27:47 -07001059bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001060 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 return false;
1062 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001063
1064 // We update the requested and active position simultaneously because
1065 // we want to apply the position portion of the transform matrix immediately,
1066 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001067 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001068 if (immediate && !mFreezeGeometryUpdates) {
1069 // Here we directly update the active state
1070 // unlike other setters, because we store it within
1071 // the transform, but use different latching rules.
1072 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001073 mCurrentState.active.transform.set(x, y);
1074 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001075 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001076
Dan Stoza7dde5992015-05-22 09:51:44 -07001077 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 setTransactionFlags(eTransactionNeeded);
1079 return true;
1080}
Robert Carr82364e32016-05-15 11:27:47 -07001081
Robert Carr1f0a16a2016-10-24 16:27:39 -07001082bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1083 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1084 if (idx < 0) {
1085 return false;
1086 }
1087 if (childLayer->setLayer(z)) {
1088 mCurrentChildren.removeAt(idx);
1089 mCurrentChildren.add(childLayer);
1090 }
1091 return true;
1092}
1093
Robert Carrae060832016-11-28 10:51:00 -08001094bool Layer::setLayer(int32_t z) {
David Sodman41fdfc92017-11-06 16:09:56 -08001095 if (mCurrentState.z == z) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 mCurrentState.sequence++;
1097 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001098 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001099
1100 // Discard all relative layering.
1101 if (mCurrentState.zOrderRelativeOf != nullptr) {
1102 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1103 if (strongRelative != nullptr) {
1104 strongRelative->removeZOrderRelative(this);
1105 }
1106 mCurrentState.zOrderRelativeOf = nullptr;
1107 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 setTransactionFlags(eTransactionNeeded);
1109 return true;
1110}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001111
Robert Carrdb66e622017-04-10 16:55:57 -07001112void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1113 mCurrentState.zOrderRelatives.remove(relative);
1114 mCurrentState.sequence++;
1115 mCurrentState.modified = true;
1116 setTransactionFlags(eTransactionNeeded);
1117}
1118
1119void Layer::addZOrderRelative(const wp<Layer>& relative) {
1120 mCurrentState.zOrderRelatives.add(relative);
1121 mCurrentState.modified = true;
1122 mCurrentState.sequence++;
1123 setTransactionFlags(eTransactionNeeded);
1124}
1125
1126bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1127 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1128 if (handle == nullptr) {
1129 return false;
1130 }
1131 sp<Layer> relative = handle->owner.promote();
1132 if (relative == nullptr) {
1133 return false;
1134 }
1135
1136 mCurrentState.sequence++;
1137 mCurrentState.modified = true;
1138 mCurrentState.z = z;
1139
chaviw9ab4bd12017-11-03 13:11:00 -07001140 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1141 if (oldZOrderRelativeOf != nullptr) {
1142 oldZOrderRelativeOf->removeZOrderRelative(this);
1143 }
Robert Carrdb66e622017-04-10 16:55:57 -07001144 mCurrentState.zOrderRelativeOf = relative;
1145 relative->addZOrderRelative(this);
1146
1147 setTransactionFlags(eTransactionNeeded);
1148
1149 return true;
1150}
1151
Mathias Agopian13127d82013-03-05 17:47:11 -08001152bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001153 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 mCurrentState.requested.w = w;
1155 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001156 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001157 setTransactionFlags(eTransactionNeeded);
1158 return true;
1159}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001160bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001161 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001163 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001164 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 setTransactionFlags(eTransactionNeeded);
1166 return true;
1167}
chaviw13fdc492017-06-27 12:40:18 -07001168
1169bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001170 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1171 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001172 return false;
1173
1174 mCurrentState.sequence++;
1175 mCurrentState.color.r = color.r;
1176 mCurrentState.color.g = color.g;
1177 mCurrentState.color.b = color.b;
1178 mCurrentState.modified = true;
1179 setTransactionFlags(eTransactionNeeded);
1180 return true;
1181}
1182
Mathias Agopian13127d82013-03-05 17:47:11 -08001183bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1184 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001185 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001186 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 setTransactionFlags(eTransactionNeeded);
1188 return true;
1189}
1190bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001191 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001192 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 setTransactionFlags(eTransactionNeeded);
1194 return true;
1195}
1196bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1197 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001198 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001199 mCurrentState.sequence++;
1200 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001201 mCurrentState.mask = mask;
1202 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001203 setTransactionFlags(eTransactionNeeded);
1204 return true;
1205}
Robert Carr99e27f02016-06-16 15:18:02 -07001206
1207bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001208 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001209 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001210 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001211 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001212 mCurrentState.crop = crop;
1213 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001214 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1215
Dan Stoza7dde5992015-05-22 09:51:44 -07001216 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 setTransactionFlags(eTransactionNeeded);
1218 return true;
1219}
Robert Carr8d5227b2017-03-16 15:41:03 -07001220
1221bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001222 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001223 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001224 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001225 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001226 mCurrentState.finalCrop = crop;
1227 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001228 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1229
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001230 mCurrentState.modified = true;
1231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Mathias Agopian13127d82013-03-05 17:47:11 -08001234
Robert Carrc3574f72016-03-24 12:19:32 -07001235bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001236 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001237 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001238 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001239 return true;
1240}
1241
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001242void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001243 mCurrentState.appId = appId;
1244 mCurrentState.type = type;
1245 mCurrentState.modified = true;
1246 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001247}
1248
Mathias Agopian13127d82013-03-05 17:47:11 -08001249bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001250 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001251 mCurrentState.sequence++;
1252 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001253 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 setTransactionFlags(eTransactionNeeded);
1255 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001256}
1257
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001258bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001259 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001260 mCurrentState.sequence++;
1261 mCurrentState.dataSpace = dataSpace;
1262 mCurrentState.modified = true;
1263 setTransactionFlags(eTransactionNeeded);
1264 return true;
1265}
1266
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001267android_dataspace Layer::getDataSpace() const {
1268 return mCurrentState.dataSpace;
1269}
1270
Robert Carr1f0a16a2016-10-24 16:27:39 -07001271uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001272 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001273 if (p == nullptr) {
1274 return getDrawingState().layerStack;
1275 }
1276 return p->getLayerStack();
1277}
1278
David Sodman41fdfc92017-11-06 16:09:56 -08001279void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001280 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001281 mCurrentState.frameNumber = frameNumber;
1282 // We don't set eTransactionNeeded, because just receiving a deferral
1283 // request without any other state updates shouldn't actually induce a delay
1284 mCurrentState.modified = true;
1285 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001286 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001287 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001288 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001289}
1290
David Sodman41fdfc92017-11-06 16:09:56 -08001291void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001292 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1293 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001294}
1295
Dan Stozaee44edd2015-03-23 15:50:23 -07001296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297// ----------------------------------------------------------------------------
1298// pageflip handling...
1299// ----------------------------------------------------------------------------
1300
Robert Carr1f0a16a2016-10-24 16:27:39 -07001301bool Layer::isHiddenByPolicy() const {
1302 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001303 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001304 if (parent != nullptr && parent->isHiddenByPolicy()) {
1305 return true;
1306 }
1307 return s.flags & layer_state_t::eLayerHidden;
1308}
1309
David Sodman41fdfc92017-11-06 16:09:56 -08001310uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001311 // TODO: should we do something special if mSecure is set?
1312 if (mProtectedByApp) {
1313 // need a hardware-protected path to external video sink
1314 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001315 }
Riley Andrews03414a12014-07-01 14:22:59 -07001316 if (mPotentialCursor) {
1317 usage |= GraphicBuffer::USAGE_CURSOR;
1318 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001319 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001321}
1322
Mathias Agopian84300952012-11-21 16:02:13 -08001323void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001324 uint32_t orientation = 0;
1325 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001326 // The transform hint is used to improve performance, but we can
1327 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001328 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001329 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001330 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001331 if (orientation & Transform::ROT_INVALID) {
1332 orientation = 0;
1333 }
1334 }
David Sodmaneb085e02017-10-05 18:49:04 -07001335 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001336}
1337
Mathias Agopian13127d82013-03-05 17:47:11 -08001338// ----------------------------------------------------------------------------
1339// debugging
1340// ----------------------------------------------------------------------------
1341
Kalle Raitaa099a242017-01-11 11:17:29 -08001342LayerDebugInfo Layer::getLayerDebugInfo() const {
1343 LayerDebugInfo info;
1344 const Layer::State& ds = getDrawingState();
1345 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001346 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001347 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1348 info.mType = String8(getTypeId());
1349 info.mTransparentRegion = ds.activeTransparentRegion;
1350 info.mVisibleRegion = visibleRegion;
1351 info.mSurfaceDamageRegion = surfaceDamageRegion;
1352 info.mLayerStack = getLayerStack();
1353 info.mX = ds.active.transform.tx();
1354 info.mY = ds.active.transform.ty();
1355 info.mZ = ds.z;
1356 info.mWidth = ds.active.w;
1357 info.mHeight = ds.active.h;
1358 info.mCrop = ds.crop;
1359 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001360 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001361 info.mFlags = ds.flags;
1362 info.mPixelFormat = getPixelFormat();
1363 info.mDataSpace = getDataSpace();
1364 info.mMatrix[0][0] = ds.active.transform[0][0];
1365 info.mMatrix[0][1] = ds.active.transform[0][1];
1366 info.mMatrix[1][0] = ds.active.transform[1][0];
1367 info.mMatrix[1][1] = ds.active.transform[1][1];
1368 {
1369 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1370 if (activeBuffer != 0) {
1371 info.mActiveBufferWidth = activeBuffer->getWidth();
1372 info.mActiveBufferHeight = activeBuffer->getHeight();
1373 info.mActiveBufferStride = activeBuffer->getStride();
1374 info.mActiveBufferFormat = activeBuffer->format;
1375 } else {
1376 info.mActiveBufferWidth = 0;
1377 info.mActiveBufferHeight = 0;
1378 info.mActiveBufferStride = 0;
1379 info.mActiveBufferFormat = 0;
1380 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001381 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001382 info.mNumQueuedFrames = getQueuedFrameCount();
1383 info.mRefreshPending = isBufferLatched();
1384 info.mIsOpaque = isOpaque(ds);
1385 info.mContentDirty = contentDirty;
1386 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001387}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001388
Dan Stozae22aec72016-08-01 13:20:59 -07001389void Layer::miniDumpHeader(String8& result) {
1390 result.append("----------------------------------------");
1391 result.append("---------------------------------------\n");
1392 result.append(" Layer name\n");
1393 result.append(" Z | ");
1394 result.append(" Comp Type | ");
1395 result.append(" Disp Frame (LTRB) | ");
1396 result.append(" Source Crop (LTRB)\n");
1397 result.append("----------------------------------------");
1398 result.append("---------------------------------------\n");
1399}
1400
1401void Layer::miniDump(String8& result, int32_t hwcId) const {
1402 if (mHwcLayers.count(hwcId) == 0) {
1403 return;
1404 }
1405
1406 String8 name;
1407 if (mName.length() > 77) {
1408 std::string shortened;
1409 shortened.append(mName.string(), 36);
1410 shortened.append("[...]");
1411 shortened.append(mName.string() + (mName.length() - 36), 36);
1412 name = shortened.c_str();
1413 } else {
1414 name = mName;
1415 }
1416
1417 result.appendFormat(" %s\n", name.string());
1418
1419 const Layer::State& layerState(getDrawingState());
1420 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001421 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001422 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001423 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001424 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001425 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001426 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right, crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001427
1428 result.append("- - - - - - - - - - - - - - - - - - - - ");
1429 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1430}
Dan Stozae22aec72016-08-01 13:20:59 -07001431
Svetoslavd85084b2014-03-20 10:28:31 -07001432void Layer::dumpFrameStats(String8& result) const {
1433 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001434}
1435
Svetoslavd85084b2014-03-20 10:28:31 -07001436void Layer::clearFrameStats() {
1437 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001438}
1439
Jamie Gennis6547ff42013-07-16 20:12:42 -07001440void Layer::logFrameStats() {
1441 mFrameTracker.logAndResetStats(mName);
1442}
1443
Svetoslavd85084b2014-03-20 10:28:31 -07001444void Layer::getFrameStats(FrameStats* outStats) const {
1445 mFrameTracker.getStats(outStats);
1446}
1447
Brian Andersond6927fb2016-07-23 23:37:30 -07001448void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001449 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001450 Mutex::Autolock lock(mFrameEventHistoryMutex);
1451 mFrameEventHistory.checkFencesForCompletion();
1452 mFrameEventHistory.dump(result);
1453}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001454
Brian Anderson5ea5e592016-12-01 16:54:33 -08001455void Layer::onDisconnect() {
1456 Mutex::Autolock lock(mFrameEventHistoryMutex);
1457 mFrameEventHistory.onDisconnect();
1458}
1459
Brian Anderson3890c392016-07-25 12:48:08 -07001460void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001461 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001462 Mutex::Autolock lock(mFrameEventHistoryMutex);
1463 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001464 // If there are any unsignaled fences in the aquire timeline at this
1465 // point, the previously queued frame hasn't been latched yet. Go ahead
1466 // and try to get the signal time here so the syscall is taken out of
1467 // the main thread's critical path.
1468 mAcquireTimeline.updateSignalTimes();
1469 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001470 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001471 mFrameEventHistory.addQueue(*newTimestamps);
1472 }
1473
Brian Anderson3890c392016-07-25 12:48:08 -07001474 if (outDelta) {
1475 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001476 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001477}
Dan Stozae77c7662016-05-13 11:37:28 -07001478
Chia-I Wu98f1c102017-05-30 14:54:08 -07001479size_t Layer::getChildrenCount() const {
1480 size_t count = 0;
1481 for (const sp<Layer>& child : mCurrentChildren) {
1482 count += 1 + child->getChildrenCount();
1483 }
1484 return count;
1485}
1486
Robert Carr1f0a16a2016-10-24 16:27:39 -07001487void Layer::addChild(const sp<Layer>& layer) {
1488 mCurrentChildren.add(layer);
1489 layer->setParent(this);
1490}
1491
1492ssize_t Layer::removeChild(const sp<Layer>& layer) {
1493 layer->setParent(nullptr);
1494 return mCurrentChildren.remove(layer);
1495}
1496
Robert Carr1db73f62016-12-21 12:58:51 -08001497bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1498 sp<Handle> handle = nullptr;
1499 sp<Layer> newParent = nullptr;
1500 if (newParentHandle == nullptr) {
1501 return false;
1502 }
1503 handle = static_cast<Handle*>(newParentHandle.get());
1504 newParent = handle->owner.promote();
1505 if (newParent == nullptr) {
1506 ALOGE("Unable to promote Layer handle");
1507 return false;
1508 }
1509
1510 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001511 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001512
1513 sp<Client> client(child->mClientRef.promote());
1514 if (client != nullptr) {
1515 client->setParentLayer(newParent);
1516 }
1517 }
1518 mCurrentChildren.clear();
1519
1520 return true;
1521}
1522
chaviwf1961f72017-09-18 16:41:07 -07001523bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1524 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001525 return false;
1526 }
1527
1528 auto handle = static_cast<Handle*>(newParentHandle.get());
1529 sp<Layer> newParent = handle->owner.promote();
1530 if (newParent == nullptr) {
1531 ALOGE("Unable to promote Layer handle");
1532 return false;
1533 }
1534
chaviwf1961f72017-09-18 16:41:07 -07001535 sp<Layer> parent = getParent();
1536 if (parent != nullptr) {
1537 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001538 }
chaviwf1961f72017-09-18 16:41:07 -07001539 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001540
chaviwf1961f72017-09-18 16:41:07 -07001541 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001542 sp<Client> newParentClient(newParent->mClientRef.promote());
1543
chaviwf1961f72017-09-18 16:41:07 -07001544 if (client != newParentClient) {
1545 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001546 }
1547
chaviw06178942017-07-27 10:25:59 -07001548 return true;
1549}
1550
Robert Carr9524cb32017-02-13 11:32:32 -08001551bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07001552 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08001553 if (child == this) {
1554 return;
1555 }
1556
chaviw161410b02017-07-27 10:46:08 -07001557 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001558 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001559 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08001560 client->detachLayer(child);
1561 }
1562 });
1563
1564 return true;
1565}
1566
Robert Carr1f0a16a2016-10-24 16:27:39 -07001567void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001568 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001569}
1570
1571void Layer::clearSyncPoints() {
1572 for (const auto& child : mCurrentChildren) {
1573 child->clearSyncPoints();
1574 }
1575
1576 Mutex::Autolock lock(mLocalSyncPointMutex);
1577 for (auto& point : mLocalSyncPoints) {
1578 point->setFrameAvailable();
1579 }
1580 mLocalSyncPoints.clear();
1581}
1582
1583int32_t Layer::getZ() const {
1584 return mDrawingState.z;
1585}
1586
David Sodman41fdfc92017-11-06 16:09:56 -08001587__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
1588 LayerVector::StateSet stateSet) {
Dan Stoza412903f2017-04-27 13:42:17 -07001589 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1590 "makeTraversalList received invalid stateSet");
1591 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1592 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1593 const State& state = useDrawing ? mDrawingState : mCurrentState;
1594
1595 if (state.zOrderRelatives.size() == 0) {
1596 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07001597 }
1598 LayerVector traverse;
1599
Dan Stoza412903f2017-04-27 13:42:17 -07001600 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001601 sp<Layer> strongRelative = weakRelative.promote();
1602 if (strongRelative != nullptr) {
1603 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001604 }
1605 }
1606
Dan Stoza412903f2017-04-27 13:42:17 -07001607 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07001608 traverse.add(child);
1609 }
1610
1611 return traverse;
1612}
1613
Robert Carr1f0a16a2016-10-24 16:27:39 -07001614/**
Robert Carrdb66e622017-04-10 16:55:57 -07001615 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001616 */
Dan Stoza412903f2017-04-27 13:42:17 -07001617void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1618 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001619
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001621 for (; i < list.size(); i++) {
1622 const auto& relative = list[i];
1623 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001624 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001625 }
Dan Stoza412903f2017-04-27 13:42:17 -07001626 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001627 }
Dan Stoza412903f2017-04-27 13:42:17 -07001628 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001629 for (; i < list.size(); i++) {
1630 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001631 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 }
1633}
1634
1635/**
Robert Carrdb66e622017-04-10 16:55:57 -07001636 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 */
Dan Stoza412903f2017-04-27 13:42:17 -07001638void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1639 const LayerVector::Visitor& visitor) {
1640 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001641
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001643 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001644 const auto& relative = list[i];
1645 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 break;
1647 }
Dan Stoza412903f2017-04-27 13:42:17 -07001648 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001649 }
Dan Stoza412903f2017-04-27 13:42:17 -07001650 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001651 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001652 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001653 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 }
1655}
1656
chaviwa76b2712017-09-20 12:02:26 -07001657/**
1658 * Traverse only children in z order, ignoring relative layers.
1659 */
1660void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1661 const LayerVector::Visitor& visitor) {
1662 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1663 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1664
1665 size_t i = 0;
1666 for (; i < children.size(); i++) {
1667 const auto& relative = children[i];
1668 if (relative->getZ() >= 0) {
1669 break;
1670 }
1671 relative->traverseChildrenInZOrder(stateSet, visitor);
1672 }
1673 visitor(this);
1674 for (; i < children.size(); i++) {
1675 const auto& relative = children[i];
1676 relative->traverseChildrenInZOrder(stateSet, visitor);
1677 }
1678}
1679
Robert Carr1f0a16a2016-10-24 16:27:39 -07001680Transform Layer::getTransform() const {
1681 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001682 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001683 if (p != nullptr) {
1684 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001685
1686 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1687 // it isFixedSize) then there may be additional scaling not accounted
1688 // for in the transform. We need to mirror this scaling in child surfaces
1689 // or we will break the contract where WM can treat child surfaces as
1690 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07001691 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001692 int bufferWidth;
1693 int bufferHeight;
1694 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
1695 bufferWidth = p->mActiveBuffer->getWidth();
1696 bufferHeight = p->mActiveBuffer->getHeight();
1697 } else {
1698 bufferHeight = p->mActiveBuffer->getWidth();
1699 bufferWidth = p->mActiveBuffer->getHeight();
1700 }
David Sodman41fdfc92017-11-06 16:09:56 -08001701 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1702 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001703 Transform extraParentScaling;
1704 extraParentScaling.set(sx, 0, 0, sy);
1705 t = t * extraParentScaling;
1706 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001707 }
1708 return t * getDrawingState().active.transform;
1709}
1710
chaviw13fdc492017-06-27 12:40:18 -07001711half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001712 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001713
chaviw13fdc492017-06-27 12:40:18 -07001714 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1715 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001716}
Robert Carr6452f122017-03-21 10:41:29 -07001717
chaviw13fdc492017-06-27 12:40:18 -07001718half4 Layer::getColor() const {
1719 const half4 color(getDrawingState().color);
1720 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001721}
Robert Carr6452f122017-03-21 10:41:29 -07001722
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723void Layer::commitChildList() {
1724 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1725 const auto& child = mCurrentChildren[i];
1726 child->commitChildList();
1727 }
1728 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001729 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730}
1731
chaviw1d044282017-09-27 12:19:28 -07001732void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1733 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1734 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1735 const State& state = useDrawing ? mDrawingState : mCurrentState;
1736
1737 Transform requestedTransform = state.active.transform;
1738 Transform transform = getTransform();
1739
1740 layerInfo->set_id(sequence);
1741 layerInfo->set_name(getName().c_str());
1742 layerInfo->set_type(String8(getTypeId()));
1743
1744 for (const auto& child : children) {
1745 layerInfo->add_children(child->sequence);
1746 }
1747
1748 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1749 sp<Layer> strongRelative = weakRelative.promote();
1750 if (strongRelative != nullptr) {
1751 layerInfo->add_relatives(strongRelative->sequence);
1752 }
1753 }
1754
1755 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1756 layerInfo->mutable_transparent_region());
1757 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1758 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1759
1760 layerInfo->set_layer_stack(getLayerStack());
1761 layerInfo->set_z(state.z);
1762
1763 PositionProto* position = layerInfo->mutable_position();
1764 position->set_x(transform.tx());
1765 position->set_y(transform.ty());
1766
1767 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1768 requestedPosition->set_x(requestedTransform.tx());
1769 requestedPosition->set_y(requestedTransform.ty());
1770
1771 SizeProto* size = layerInfo->mutable_size();
1772 size->set_w(state.active.w);
1773 size->set_h(state.active.h);
1774
1775 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1776 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1777
1778 layerInfo->set_is_opaque(isOpaque(state));
1779 layerInfo->set_invalidate(contentDirty);
1780 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1781 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1782 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1783 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1784 layerInfo->set_flags(state.flags);
1785
1786 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1787 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1788
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001789 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001790 if (parent != nullptr) {
1791 layerInfo->set_parent(parent->sequence);
1792 }
1793
1794 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1795 if (zOrderRelativeOf != nullptr) {
1796 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1797 }
1798
1799 auto activeBuffer = getActiveBuffer();
1800 if (activeBuffer != nullptr) {
1801 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
1802 }
1803
1804 layerInfo->set_queued_frames(getQueuedFrameCount());
1805 layerInfo->set_refresh_pending(isBufferLatched());
1806}
1807
Mathias Agopian13127d82013-03-05 17:47:11 -08001808// ---------------------------------------------------------------------------
1809
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001811
1812#if defined(__gl_h_)
1813#error "don't include gl/gl.h in this file"
1814#endif
1815
1816#if defined(__gl2_h_)
1817#error "don't include gl2/gl2.h in this file"
1818#endif