blob: 50e2c6f55c6f583bd08ea80a40d0af52de613951 [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
chaviwc9232ed2017-11-14 15:31:15 -0800624bool Layer::getForceClientComposition(int32_t hwcId) {
625 if (mHwcLayers.count(hwcId) == 0) {
626 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
627 return false;
628 }
629
630 return mHwcLayers[hwcId].forceClientComposition;
631}
632
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
634 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman41fdfc92017-11-06 16:09:56 -0800635 if (mHwcLayers.count(hwcId) == 0 || getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 return;
637 }
638
639 // This gives us only the "orientation" component of the transform
640 const State& s(getCurrentState());
641
642 // Apply the layer's transform, followed by the display's global transform
643 // Here we're guaranteed that the layer's transform preserves rects
644 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700645 if (!s.crop.isEmpty()) {
646 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 }
648 // Subtract the transparent region and snap to the bounds
649 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700650 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700652 if (!s.finalCrop.isEmpty()) {
653 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000654 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 auto& displayTransform(displayDevice->getTransform());
656 auto position = displayTransform.transform(frame);
657
David Sodman41fdfc92017-11-06 16:09:56 -0800658 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, position.top);
659 ALOGE_IF(error != HWC2::Error::None,
660 "[%s] Failed to set cursor position "
661 "to (%d, %d): %s (%d)",
662 mName.string(), position.left, position.top, to_string(error).c_str(),
663 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664}
Riley Andrews03414a12014-07-01 14:22:59 -0700665
Mathias Agopian13127d82013-03-05 17:47:11 -0800666// ---------------------------------------------------------------------------
667// drawing...
668// ---------------------------------------------------------------------------
669
chaviwa76b2712017-09-20 12:02:26 -0700670void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
671 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800672}
673
chaviwa76b2712017-09-20 12:02:26 -0700674void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
675 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800676}
677
chaviwa76b2712017-09-20 12:02:26 -0700678void Layer::draw(const RenderArea& renderArea) const {
679 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800680}
681
David Sodman41fdfc92017-11-06 16:09:56 -0800682void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
683 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700684 RenderEngine& engine(mFlinger->getRenderEngine());
chaviwa76b2712017-09-20 12:02:26 -0700685 computeGeometry(renderArea, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700686 engine.setupFillWithColor(red, green, blue, alpha);
687 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800688}
689
chaviwa76b2712017-09-20 12:02:26 -0700690void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800691 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800692}
693
David Sodman41fdfc92017-11-06 16:09:56 -0800694void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695 if (mHwcLayers.count(hwcId) == 0) {
696 ALOGE("setCompositionType called without a valid HWC layer");
697 return;
698 }
699 auto& hwcInfo = mHwcLayers[hwcId];
700 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800701 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
702 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 if (hwcInfo.compositionType != type) {
704 ALOGV(" actually setting");
705 hwcInfo.compositionType = type;
706 if (callIntoHwc) {
707 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800708 ALOGE_IF(error != HWC2::Error::None,
709 "[%s] Failed to set "
710 "composition type %s: %s (%d)",
711 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
712 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713 }
714 }
715}
716
717HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700718 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
719 // If we're querying the composition type for a display that does not
720 // have a HWC counterpart, then it will always be Client
721 return HWC2::Composition::Client;
722 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700724 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 return HWC2::Composition::Invalid;
726 }
727 return mHwcLayers.at(hwcId).compositionType;
728}
729
730void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
731 if (mHwcLayers.count(hwcId) == 0) {
732 ALOGE("setClearClientTarget called without a valid HWC layer");
733 return;
734 }
735 mHwcLayers[hwcId].clearClientTarget = clear;
736}
737
738bool Layer::getClearClientTarget(int32_t hwcId) const {
739 if (mHwcLayers.count(hwcId) == 0) {
740 ALOGE("getClearClientTarget called without a valid HWC layer");
741 return false;
742 }
743 return mHwcLayers.at(hwcId).clearClientTarget;
744}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745
Dan Stozacac35382016-01-27 12:21:06 -0800746bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
747 if (point->getFrameNumber() <= mCurrentFrameNumber) {
748 // Don't bother with a SyncPoint, since we've already latched the
749 // relevant frame
750 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 }
752
Dan Stozacac35382016-01-27 12:21:06 -0800753 Mutex::Autolock lock(mLocalSyncPointMutex);
754 mLocalSyncPoints.push_back(point);
755 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700756}
757
Mathias Agopian13127d82013-03-05 17:47:11 -0800758void Layer::setFiltering(bool filtering) {
759 mFiltering = filtering;
760}
761
762bool Layer::getFiltering() const {
763 return mFiltering;
764}
765
Mathias Agopian13127d82013-03-05 17:47:11 -0800766// ----------------------------------------------------------------------------
767// local state
768// ----------------------------------------------------------------------------
769
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000770static void boundPoint(vec2* point, const Rect& crop) {
771 if (point->x < crop.left) {
772 point->x = crop.left;
773 }
774 if (point->x > crop.right) {
775 point->x = crop.right;
776 }
777 if (point->y < crop.top) {
778 point->y = crop.top;
779 }
780 if (point->y > crop.bottom) {
781 point->y = crop.bottom;
782 }
783}
784
chaviwa76b2712017-09-20 12:02:26 -0700785void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
786 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700787 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700788 const Transform renderAreaTransform(renderArea.getTransform());
789 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700790 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700791
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000792 vec2 lt = vec2(win.left, win.top);
793 vec2 lb = vec2(win.left, win.bottom);
794 vec2 rb = vec2(win.right, win.bottom);
795 vec2 rt = vec2(win.right, win.top);
796
Robert Carr1f0a16a2016-10-24 16:27:39 -0700797 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000798 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700799 lt = layerTransform.transform(lt);
800 lb = layerTransform.transform(lb);
801 rb = layerTransform.transform(rb);
802 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000803 }
804
Robert Carrb5d3d262016-03-25 15:08:13 -0700805 if (!s.finalCrop.isEmpty()) {
806 boundPoint(&lt, s.finalCrop);
807 boundPoint(&lb, s.finalCrop);
808 boundPoint(&rb, s.finalCrop);
809 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000810 }
811
Mathias Agopianff2ed702013-09-01 21:36:12 -0700812 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700813 position[0] = renderAreaTransform.transform(lt);
814 position[1] = renderAreaTransform.transform(lb);
815 position[2] = renderAreaTransform.transform(rb);
816 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800817 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700818 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800819 }
820}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800821
David Sodman41fdfc92017-11-06 16:09:56 -0800822bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700823 const Layer::State& s(mDrawingState);
824 return (s.flags & layer_state_t::eLayerSecure);
825}
826
Mathias Agopian13127d82013-03-05 17:47:11 -0800827void Layer::setVisibleRegion(const Region& visibleRegion) {
828 // always called from main thread
829 this->visibleRegion = visibleRegion;
830}
831
832void Layer::setCoveredRegion(const Region& coveredRegion) {
833 // always called from main thread
834 this->coveredRegion = coveredRegion;
835}
836
David Sodman41fdfc92017-11-06 16:09:56 -0800837void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800838 // always called from main thread
839 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
840}
841
842// ----------------------------------------------------------------------------
843// transaction
844// ----------------------------------------------------------------------------
845
Dan Stoza7dde5992015-05-22 09:51:44 -0700846void Layer::pushPendingState() {
847 if (!mCurrentState.modified) {
848 return;
849 }
850
Dan Stoza7dde5992015-05-22 09:51:44 -0700851 // If this transaction is waiting on the receipt of a frame, generate a sync
852 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800853 if (mCurrentState.barrierLayer != nullptr) {
854 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
855 if (barrierLayer == nullptr) {
856 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700857 // If we can't promote the layer we are intended to wait on,
858 // then it is expired or otherwise invalid. Allow this transaction
859 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800860 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800861 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800862 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800863 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800864 mRemoteSyncPoints.push_back(std::move(syncPoint));
865 } else {
866 // We already missed the frame we're supposed to synchronize
867 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800868 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800869 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700870 }
871
Dan Stoza7dde5992015-05-22 09:51:44 -0700872 // Wake us up to check if the frame has been received
873 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700874 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700875 }
876 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700877 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700878}
879
Pablo Ceballos05289c22016-04-14 15:49:55 -0700880void Layer::popPendingState(State* stateToCommit) {
881 auto oldFlags = stateToCommit->flags;
882 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800883 stateToCommit->flags =
884 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700885
886 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700887 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700888}
889
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700891 bool stateUpdateAvailable = false;
892 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800893 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 if (mRemoteSyncPoints.empty()) {
895 // If we don't have a sync point for this, apply it anyway. It
896 // will be visually wrong, but it should keep us from getting
897 // into too much trouble.
898 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 stateUpdateAvailable = true;
901 continue;
902 }
903
David Sodman41fdfc92017-11-06 16:09:56 -0800904 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
905 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800906
907 // Signal our end of the sync point and then dispose of it
908 mRemoteSyncPoints.front()->setTransactionApplied();
909 mRemoteSyncPoints.pop_front();
910 continue;
911 }
912
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
914 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700915 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 stateUpdateAvailable = true;
917
918 // Signal our end of the sync point and then dispose of it
919 mRemoteSyncPoints.front()->setTransactionApplied();
920 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800921 } else {
922 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700924 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 stateUpdateAvailable = true;
927 }
928 }
929
930 // If we still have pending updates, wake SurfaceFlinger back up and point
931 // it at this layer so we can process them
932 if (!mPendingStates.empty()) {
933 setTransactionFlags(eTransactionNeeded);
934 mFlinger->setTransactionFlags(eTraversalNeeded);
935 }
936
937 mCurrentState.modified = false;
938 return stateUpdateAvailable;
939}
940
Mathias Agopian13127d82013-03-05 17:47:11 -0800941uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800942 ATRACE_CALL();
943
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700945 Layer::State c = getCurrentState();
946 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700947 return 0;
948 }
949
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700950 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951
David Sodman41fdfc92017-11-06 16:09:56 -0800952 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700953
David Sodmaneb085e02017-10-05 18:49:04 -0700954 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700955 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000956 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800957 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
958 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
959 " requested={ wh={%4u,%4u} }}\n"
960 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
961 " requested={ wh={%4u,%4u} }}\n",
962 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(), c.active.w,
963 c.active.h, c.crop.left, c.crop.top, c.crop.right, c.crop.bottom,
964 c.crop.getWidth(), c.crop.getHeight(), c.requested.w, c.requested.h, s.active.w,
965 s.active.h, s.crop.left, s.crop.top, s.crop.right, s.crop.bottom,
966 s.crop.getWidth(), s.crop.getHeight(), s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700968 // record the new size, form this point on, when the client request
969 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700970 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700972
Robert Carre392b552017-09-19 12:16:05 -0700973 // Don't let Layer::doTransaction update the drawing state
974 // if we have a pending resize, unless we are in fixed-size mode.
975 // the drawing state will be updated only once we receive a buffer
976 // with the correct size.
977 //
978 // In particular, we want to make sure the clip (which is part
979 // of the geometry state) is latched together with the size but is
980 // latched immediately when no resizing is involved.
981 //
982 // If a sideband stream is attached, however, we want to skip this
983 // optimization so that transactions aren't missed when a buffer
984 // never arrives
985 //
986 // In the case that we don't have a buffer we ignore other factors
987 // and avoid entering the resizePending state. At a high level the
988 // resizePending state is to avoid applying the state of the new buffer
989 // to the old buffer. However in the state where we don't have an old buffer
990 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -0800991 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
992 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700993 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -0700994 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700995 flags |= eDontUpdateGeometryState;
996 }
997 }
998
Robert Carr7bf247e2017-05-18 14:02:49 -0700999 // Here we apply various requested geometry states, depending on our
1000 // latching configuration. See Layer.h for a detailed discussion of
1001 // how geometry latching is controlled.
1002 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001003 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001004
1005 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1006 // mode, which causes attributes which normally latch regardless of scaling mode,
1007 // to be delayed. We copy the requested state to the active state making sure
1008 // to respect these rules (again see Layer.h for a detailed discussion).
1009 //
1010 // There is an awkward asymmetry in the handling of the crop states in the position
1011 // states, as can be seen below. Largely this arises from position and transform
1012 // being stored in the same data structure while having different latching rules.
1013 // b/38182305
1014 //
1015 // Careful that "c" and editCurrentState may not begin as equivalent due to
1016 // applyPendingStates in the presence of deferred transactions.
1017 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001018 float tx = c.active.transform.tx();
1019 float ty = c.active.transform.ty();
1020 c.active = c.requested;
1021 c.active.transform.set(tx, ty);
1022 editCurrentState.active = c.active;
1023 } else {
1024 editCurrentState.active = editCurrentState.requested;
1025 c.active = c.requested;
1026 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 }
1028
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001029 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001030 // invalidate and recompute the visible regions if needed
1031 flags |= Layer::eVisibleRegion;
1032 }
1033
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001034 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 // invalidate and recompute the visible regions if needed
1036 flags |= eVisibleRegion;
1037 this->contentDirty = true;
1038
1039 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001040 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001041 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 }
1043
Dan Stozac8145172016-04-28 16:29:06 -07001044 // If the layer is hidden, signal and clear out all local sync points so
1045 // that transactions for layers depending on this layer's frames becoming
1046 // visible are not blocked
1047 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001048 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001049 }
1050
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001052 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054}
1055
Pablo Ceballos05289c22016-04-14 15:49:55 -07001056void Layer::commitTransaction(const State& stateToCommit) {
1057 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001058}
1059
Mathias Agopian13127d82013-03-05 17:47:11 -08001060uint32_t Layer::getTransactionFlags(uint32_t flags) {
1061 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1062}
1063
1064uint32_t Layer::setTransactionFlags(uint32_t flags) {
1065 return android_atomic_or(flags, &mTransactionFlags);
1066}
1067
Robert Carr82364e32016-05-15 11:27:47 -07001068bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001069 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 return false;
1071 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001072
1073 // We update the requested and active position simultaneously because
1074 // we want to apply the position portion of the transform matrix immediately,
1075 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001076 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001077 if (immediate && !mFreezeGeometryUpdates) {
1078 // Here we directly update the active state
1079 // unlike other setters, because we store it within
1080 // the transform, but use different latching rules.
1081 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001082 mCurrentState.active.transform.set(x, y);
1083 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001084 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001085
Dan Stoza7dde5992015-05-22 09:51:44 -07001086 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 setTransactionFlags(eTransactionNeeded);
1088 return true;
1089}
Robert Carr82364e32016-05-15 11:27:47 -07001090
Robert Carr1f0a16a2016-10-24 16:27:39 -07001091bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1092 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1093 if (idx < 0) {
1094 return false;
1095 }
1096 if (childLayer->setLayer(z)) {
1097 mCurrentChildren.removeAt(idx);
1098 mCurrentChildren.add(childLayer);
1099 }
1100 return true;
1101}
1102
Robert Carrae060832016-11-28 10:51:00 -08001103bool Layer::setLayer(int32_t z) {
David Sodman41fdfc92017-11-06 16:09:56 -08001104 if (mCurrentState.z == z) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 mCurrentState.sequence++;
1106 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001107 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001108
1109 // Discard all relative layering.
1110 if (mCurrentState.zOrderRelativeOf != nullptr) {
1111 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1112 if (strongRelative != nullptr) {
1113 strongRelative->removeZOrderRelative(this);
1114 }
1115 mCurrentState.zOrderRelativeOf = nullptr;
1116 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 setTransactionFlags(eTransactionNeeded);
1118 return true;
1119}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001120
Robert Carrdb66e622017-04-10 16:55:57 -07001121void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1122 mCurrentState.zOrderRelatives.remove(relative);
1123 mCurrentState.sequence++;
1124 mCurrentState.modified = true;
1125 setTransactionFlags(eTransactionNeeded);
1126}
1127
1128void Layer::addZOrderRelative(const wp<Layer>& relative) {
1129 mCurrentState.zOrderRelatives.add(relative);
1130 mCurrentState.modified = true;
1131 mCurrentState.sequence++;
1132 setTransactionFlags(eTransactionNeeded);
1133}
1134
1135bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1136 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1137 if (handle == nullptr) {
1138 return false;
1139 }
1140 sp<Layer> relative = handle->owner.promote();
1141 if (relative == nullptr) {
1142 return false;
1143 }
1144
1145 mCurrentState.sequence++;
1146 mCurrentState.modified = true;
1147 mCurrentState.z = z;
1148
chaviw9ab4bd12017-11-03 13:11:00 -07001149 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1150 if (oldZOrderRelativeOf != nullptr) {
1151 oldZOrderRelativeOf->removeZOrderRelative(this);
1152 }
Robert Carrdb66e622017-04-10 16:55:57 -07001153 mCurrentState.zOrderRelativeOf = relative;
1154 relative->addZOrderRelative(this);
1155
1156 setTransactionFlags(eTransactionNeeded);
1157
1158 return true;
1159}
1160
Mathias Agopian13127d82013-03-05 17:47:11 -08001161bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001162 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 mCurrentState.requested.w = w;
1164 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001165 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 setTransactionFlags(eTransactionNeeded);
1167 return true;
1168}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001169bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001170 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001171 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001172 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001173 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 setTransactionFlags(eTransactionNeeded);
1175 return true;
1176}
chaviw13fdc492017-06-27 12:40:18 -07001177
1178bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001179 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1180 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001181 return false;
1182
1183 mCurrentState.sequence++;
1184 mCurrentState.color.r = color.r;
1185 mCurrentState.color.g = color.g;
1186 mCurrentState.color.b = color.b;
1187 mCurrentState.modified = true;
1188 setTransactionFlags(eTransactionNeeded);
1189 return true;
1190}
1191
Mathias Agopian13127d82013-03-05 17:47:11 -08001192bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1193 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001194 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001195 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001196 setTransactionFlags(eTransactionNeeded);
1197 return true;
1198}
1199bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001200 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001201 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001202 setTransactionFlags(eTransactionNeeded);
1203 return true;
1204}
1205bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1206 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001207 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001208 mCurrentState.sequence++;
1209 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001210 mCurrentState.mask = mask;
1211 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001212 setTransactionFlags(eTransactionNeeded);
1213 return true;
1214}
Robert Carr99e27f02016-06-16 15:18:02 -07001215
1216bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001217 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001219 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001220 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001221 mCurrentState.crop = crop;
1222 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001223 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1224
Dan Stoza7dde5992015-05-22 09:51:44 -07001225 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
1227 return true;
1228}
Robert Carr8d5227b2017-03-16 15:41:03 -07001229
1230bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001231 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001232 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001233 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001234 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001235 mCurrentState.finalCrop = crop;
1236 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001237 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1238
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001239 mCurrentState.modified = true;
1240 setTransactionFlags(eTransactionNeeded);
1241 return true;
1242}
Mathias Agopian13127d82013-03-05 17:47:11 -08001243
Robert Carrc3574f72016-03-24 12:19:32 -07001244bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001245 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001246 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001247 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001248 return true;
1249}
1250
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001251void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001252 mCurrentState.appId = appId;
1253 mCurrentState.type = type;
1254 mCurrentState.modified = true;
1255 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001256}
1257
Mathias Agopian13127d82013-03-05 17:47:11 -08001258bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001259 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 mCurrentState.sequence++;
1261 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001262 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 setTransactionFlags(eTransactionNeeded);
1264 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001265}
1266
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001267bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001268 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001269 mCurrentState.sequence++;
1270 mCurrentState.dataSpace = dataSpace;
1271 mCurrentState.modified = true;
1272 setTransactionFlags(eTransactionNeeded);
1273 return true;
1274}
1275
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001276android_dataspace Layer::getDataSpace() const {
1277 return mCurrentState.dataSpace;
1278}
1279
Robert Carr1f0a16a2016-10-24 16:27:39 -07001280uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001281 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001282 if (p == nullptr) {
1283 return getDrawingState().layerStack;
1284 }
1285 return p->getLayerStack();
1286}
1287
David Sodman41fdfc92017-11-06 16:09:56 -08001288void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001289 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001290 mCurrentState.frameNumber = frameNumber;
1291 // We don't set eTransactionNeeded, because just receiving a deferral
1292 // request without any other state updates shouldn't actually induce a delay
1293 mCurrentState.modified = true;
1294 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001295 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001296 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001297 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001298}
1299
David Sodman41fdfc92017-11-06 16:09:56 -08001300void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001301 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1302 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001303}
1304
Dan Stozaee44edd2015-03-23 15:50:23 -07001305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306// ----------------------------------------------------------------------------
1307// pageflip handling...
1308// ----------------------------------------------------------------------------
1309
Robert Carr1f0a16a2016-10-24 16:27:39 -07001310bool Layer::isHiddenByPolicy() const {
1311 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001312 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001313 if (parent != nullptr && parent->isHiddenByPolicy()) {
1314 return true;
1315 }
1316 return s.flags & layer_state_t::eLayerHidden;
1317}
1318
David Sodman41fdfc92017-11-06 16:09:56 -08001319uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320 // TODO: should we do something special if mSecure is set?
1321 if (mProtectedByApp) {
1322 // need a hardware-protected path to external video sink
1323 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001324 }
Riley Andrews03414a12014-07-01 14:22:59 -07001325 if (mPotentialCursor) {
1326 usage |= GraphicBuffer::USAGE_CURSOR;
1327 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001328 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001329 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001330}
1331
Mathias Agopian84300952012-11-21 16:02:13 -08001332void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001333 uint32_t orientation = 0;
1334 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001335 // The transform hint is used to improve performance, but we can
1336 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001337 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001338 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001339 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001340 if (orientation & Transform::ROT_INVALID) {
1341 orientation = 0;
1342 }
1343 }
David Sodmaneb085e02017-10-05 18:49:04 -07001344 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001345}
1346
Mathias Agopian13127d82013-03-05 17:47:11 -08001347// ----------------------------------------------------------------------------
1348// debugging
1349// ----------------------------------------------------------------------------
1350
Kalle Raitaa099a242017-01-11 11:17:29 -08001351LayerDebugInfo Layer::getLayerDebugInfo() const {
1352 LayerDebugInfo info;
1353 const Layer::State& ds = getDrawingState();
1354 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001355 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001356 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1357 info.mType = String8(getTypeId());
1358 info.mTransparentRegion = ds.activeTransparentRegion;
1359 info.mVisibleRegion = visibleRegion;
1360 info.mSurfaceDamageRegion = surfaceDamageRegion;
1361 info.mLayerStack = getLayerStack();
1362 info.mX = ds.active.transform.tx();
1363 info.mY = ds.active.transform.ty();
1364 info.mZ = ds.z;
1365 info.mWidth = ds.active.w;
1366 info.mHeight = ds.active.h;
1367 info.mCrop = ds.crop;
1368 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001369 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001370 info.mFlags = ds.flags;
1371 info.mPixelFormat = getPixelFormat();
1372 info.mDataSpace = getDataSpace();
1373 info.mMatrix[0][0] = ds.active.transform[0][0];
1374 info.mMatrix[0][1] = ds.active.transform[0][1];
1375 info.mMatrix[1][0] = ds.active.transform[1][0];
1376 info.mMatrix[1][1] = ds.active.transform[1][1];
1377 {
1378 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1379 if (activeBuffer != 0) {
1380 info.mActiveBufferWidth = activeBuffer->getWidth();
1381 info.mActiveBufferHeight = activeBuffer->getHeight();
1382 info.mActiveBufferStride = activeBuffer->getStride();
1383 info.mActiveBufferFormat = activeBuffer->format;
1384 } else {
1385 info.mActiveBufferWidth = 0;
1386 info.mActiveBufferHeight = 0;
1387 info.mActiveBufferStride = 0;
1388 info.mActiveBufferFormat = 0;
1389 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001390 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001391 info.mNumQueuedFrames = getQueuedFrameCount();
1392 info.mRefreshPending = isBufferLatched();
1393 info.mIsOpaque = isOpaque(ds);
1394 info.mContentDirty = contentDirty;
1395 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001396}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001397
Dan Stozae22aec72016-08-01 13:20:59 -07001398void Layer::miniDumpHeader(String8& result) {
1399 result.append("----------------------------------------");
1400 result.append("---------------------------------------\n");
1401 result.append(" Layer name\n");
1402 result.append(" Z | ");
1403 result.append(" Comp Type | ");
1404 result.append(" Disp Frame (LTRB) | ");
1405 result.append(" Source Crop (LTRB)\n");
1406 result.append("----------------------------------------");
1407 result.append("---------------------------------------\n");
1408}
1409
1410void Layer::miniDump(String8& result, int32_t hwcId) const {
1411 if (mHwcLayers.count(hwcId) == 0) {
1412 return;
1413 }
1414
1415 String8 name;
1416 if (mName.length() > 77) {
1417 std::string shortened;
1418 shortened.append(mName.string(), 36);
1419 shortened.append("[...]");
1420 shortened.append(mName.string() + (mName.length() - 36), 36);
1421 name = shortened.c_str();
1422 } else {
1423 name = mName;
1424 }
1425
1426 result.appendFormat(" %s\n", name.string());
1427
1428 const Layer::State& layerState(getDrawingState());
1429 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001430 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001431 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001432 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001433 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001434 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001435 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 -07001436
1437 result.append("- - - - - - - - - - - - - - - - - - - - ");
1438 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1439}
Dan Stozae22aec72016-08-01 13:20:59 -07001440
Svetoslavd85084b2014-03-20 10:28:31 -07001441void Layer::dumpFrameStats(String8& result) const {
1442 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001443}
1444
Svetoslavd85084b2014-03-20 10:28:31 -07001445void Layer::clearFrameStats() {
1446 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001447}
1448
Jamie Gennis6547ff42013-07-16 20:12:42 -07001449void Layer::logFrameStats() {
1450 mFrameTracker.logAndResetStats(mName);
1451}
1452
Svetoslavd85084b2014-03-20 10:28:31 -07001453void Layer::getFrameStats(FrameStats* outStats) const {
1454 mFrameTracker.getStats(outStats);
1455}
1456
Brian Andersond6927fb2016-07-23 23:37:30 -07001457void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001458 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001459 Mutex::Autolock lock(mFrameEventHistoryMutex);
1460 mFrameEventHistory.checkFencesForCompletion();
1461 mFrameEventHistory.dump(result);
1462}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001463
Brian Anderson5ea5e592016-12-01 16:54:33 -08001464void Layer::onDisconnect() {
1465 Mutex::Autolock lock(mFrameEventHistoryMutex);
1466 mFrameEventHistory.onDisconnect();
1467}
1468
Brian Anderson3890c392016-07-25 12:48:08 -07001469void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001470 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001471 Mutex::Autolock lock(mFrameEventHistoryMutex);
1472 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001473 // If there are any unsignaled fences in the aquire timeline at this
1474 // point, the previously queued frame hasn't been latched yet. Go ahead
1475 // and try to get the signal time here so the syscall is taken out of
1476 // the main thread's critical path.
1477 mAcquireTimeline.updateSignalTimes();
1478 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001479 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001480 mFrameEventHistory.addQueue(*newTimestamps);
1481 }
1482
Brian Anderson3890c392016-07-25 12:48:08 -07001483 if (outDelta) {
1484 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001485 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001486}
Dan Stozae77c7662016-05-13 11:37:28 -07001487
Chia-I Wu98f1c102017-05-30 14:54:08 -07001488size_t Layer::getChildrenCount() const {
1489 size_t count = 0;
1490 for (const sp<Layer>& child : mCurrentChildren) {
1491 count += 1 + child->getChildrenCount();
1492 }
1493 return count;
1494}
1495
Robert Carr1f0a16a2016-10-24 16:27:39 -07001496void Layer::addChild(const sp<Layer>& layer) {
1497 mCurrentChildren.add(layer);
1498 layer->setParent(this);
1499}
1500
1501ssize_t Layer::removeChild(const sp<Layer>& layer) {
1502 layer->setParent(nullptr);
1503 return mCurrentChildren.remove(layer);
1504}
1505
Robert Carr1db73f62016-12-21 12:58:51 -08001506bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1507 sp<Handle> handle = nullptr;
1508 sp<Layer> newParent = nullptr;
1509 if (newParentHandle == nullptr) {
1510 return false;
1511 }
1512 handle = static_cast<Handle*>(newParentHandle.get());
1513 newParent = handle->owner.promote();
1514 if (newParent == nullptr) {
1515 ALOGE("Unable to promote Layer handle");
1516 return false;
1517 }
1518
1519 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001520 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001521
1522 sp<Client> client(child->mClientRef.promote());
1523 if (client != nullptr) {
1524 client->setParentLayer(newParent);
1525 }
1526 }
1527 mCurrentChildren.clear();
1528
1529 return true;
1530}
1531
chaviwf1961f72017-09-18 16:41:07 -07001532bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1533 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001534 return false;
1535 }
1536
1537 auto handle = static_cast<Handle*>(newParentHandle.get());
1538 sp<Layer> newParent = handle->owner.promote();
1539 if (newParent == nullptr) {
1540 ALOGE("Unable to promote Layer handle");
1541 return false;
1542 }
1543
chaviwf1961f72017-09-18 16:41:07 -07001544 sp<Layer> parent = getParent();
1545 if (parent != nullptr) {
1546 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001547 }
chaviwf1961f72017-09-18 16:41:07 -07001548 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001549
chaviwf1961f72017-09-18 16:41:07 -07001550 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001551 sp<Client> newParentClient(newParent->mClientRef.promote());
1552
chaviwf1961f72017-09-18 16:41:07 -07001553 if (client != newParentClient) {
1554 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001555 }
1556
chaviw06178942017-07-27 10:25:59 -07001557 return true;
1558}
1559
Robert Carr9524cb32017-02-13 11:32:32 -08001560bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07001561 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08001562 if (child == this) {
1563 return;
1564 }
1565
chaviw161410b02017-07-27 10:46:08 -07001566 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001567 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001568 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08001569 client->detachLayer(child);
1570 }
1571 });
1572
1573 return true;
1574}
1575
Robert Carr1f0a16a2016-10-24 16:27:39 -07001576void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001577 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001578}
1579
1580void Layer::clearSyncPoints() {
1581 for (const auto& child : mCurrentChildren) {
1582 child->clearSyncPoints();
1583 }
1584
1585 Mutex::Autolock lock(mLocalSyncPointMutex);
1586 for (auto& point : mLocalSyncPoints) {
1587 point->setFrameAvailable();
1588 }
1589 mLocalSyncPoints.clear();
1590}
1591
1592int32_t Layer::getZ() const {
1593 return mDrawingState.z;
1594}
1595
David Sodman41fdfc92017-11-06 16:09:56 -08001596__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
1597 LayerVector::StateSet stateSet) {
Dan Stoza412903f2017-04-27 13:42:17 -07001598 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1599 "makeTraversalList received invalid stateSet");
1600 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1601 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1602 const State& state = useDrawing ? mDrawingState : mCurrentState;
1603
1604 if (state.zOrderRelatives.size() == 0) {
1605 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07001606 }
1607 LayerVector traverse;
1608
Dan Stoza412903f2017-04-27 13:42:17 -07001609 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001610 sp<Layer> strongRelative = weakRelative.promote();
1611 if (strongRelative != nullptr) {
1612 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001613 }
1614 }
1615
Dan Stoza412903f2017-04-27 13:42:17 -07001616 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07001617 traverse.add(child);
1618 }
1619
1620 return traverse;
1621}
1622
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623/**
Robert Carrdb66e622017-04-10 16:55:57 -07001624 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625 */
Dan Stoza412903f2017-04-27 13:42:17 -07001626void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1627 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001628
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001630 for (; i < list.size(); i++) {
1631 const auto& relative = list[i];
1632 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001634 }
Dan Stoza412903f2017-04-27 13:42:17 -07001635 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001636 }
Dan Stoza412903f2017-04-27 13:42:17 -07001637 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001638 for (; i < list.size(); i++) {
1639 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001640 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 }
1642}
1643
1644/**
Robert Carrdb66e622017-04-10 16:55:57 -07001645 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 */
Dan Stoza412903f2017-04-27 13:42:17 -07001647void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1648 const LayerVector::Visitor& visitor) {
1649 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001650
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001652 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001653 const auto& relative = list[i];
1654 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 break;
1656 }
Dan Stoza412903f2017-04-27 13:42:17 -07001657 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001658 }
Dan Stoza412903f2017-04-27 13:42:17 -07001659 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001660 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001661 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001662 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663 }
1664}
1665
chaviwa76b2712017-09-20 12:02:26 -07001666/**
1667 * Traverse only children in z order, ignoring relative layers.
1668 */
1669void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1670 const LayerVector::Visitor& visitor) {
1671 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1672 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1673
1674 size_t i = 0;
1675 for (; i < children.size(); i++) {
1676 const auto& relative = children[i];
1677 if (relative->getZ() >= 0) {
1678 break;
1679 }
1680 relative->traverseChildrenInZOrder(stateSet, visitor);
1681 }
1682 visitor(this);
1683 for (; i < children.size(); i++) {
1684 const auto& relative = children[i];
1685 relative->traverseChildrenInZOrder(stateSet, visitor);
1686 }
1687}
1688
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689Transform Layer::getTransform() const {
1690 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001691 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001692 if (p != nullptr) {
1693 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001694
1695 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1696 // it isFixedSize) then there may be additional scaling not accounted
1697 // for in the transform. We need to mirror this scaling in child surfaces
1698 // or we will break the contract where WM can treat child surfaces as
1699 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07001700 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001701 int bufferWidth;
1702 int bufferHeight;
1703 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
1704 bufferWidth = p->mActiveBuffer->getWidth();
1705 bufferHeight = p->mActiveBuffer->getHeight();
1706 } else {
1707 bufferHeight = p->mActiveBuffer->getWidth();
1708 bufferWidth = p->mActiveBuffer->getHeight();
1709 }
David Sodman41fdfc92017-11-06 16:09:56 -08001710 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1711 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001712 Transform extraParentScaling;
1713 extraParentScaling.set(sx, 0, 0, sy);
1714 t = t * extraParentScaling;
1715 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 }
1717 return t * getDrawingState().active.transform;
1718}
1719
chaviw13fdc492017-06-27 12:40:18 -07001720half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001721 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001722
chaviw13fdc492017-06-27 12:40:18 -07001723 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1724 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001725}
Robert Carr6452f122017-03-21 10:41:29 -07001726
chaviw13fdc492017-06-27 12:40:18 -07001727half4 Layer::getColor() const {
1728 const half4 color(getDrawingState().color);
1729 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001730}
Robert Carr6452f122017-03-21 10:41:29 -07001731
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732void Layer::commitChildList() {
1733 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1734 const auto& child = mCurrentChildren[i];
1735 child->commitChildList();
1736 }
1737 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001738 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739}
1740
chaviw1d044282017-09-27 12:19:28 -07001741void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1742 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1743 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1744 const State& state = useDrawing ? mDrawingState : mCurrentState;
1745
1746 Transform requestedTransform = state.active.transform;
1747 Transform transform = getTransform();
1748
1749 layerInfo->set_id(sequence);
1750 layerInfo->set_name(getName().c_str());
1751 layerInfo->set_type(String8(getTypeId()));
1752
1753 for (const auto& child : children) {
1754 layerInfo->add_children(child->sequence);
1755 }
1756
1757 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1758 sp<Layer> strongRelative = weakRelative.promote();
1759 if (strongRelative != nullptr) {
1760 layerInfo->add_relatives(strongRelative->sequence);
1761 }
1762 }
1763
1764 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1765 layerInfo->mutable_transparent_region());
1766 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1767 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1768
1769 layerInfo->set_layer_stack(getLayerStack());
1770 layerInfo->set_z(state.z);
1771
1772 PositionProto* position = layerInfo->mutable_position();
1773 position->set_x(transform.tx());
1774 position->set_y(transform.ty());
1775
1776 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1777 requestedPosition->set_x(requestedTransform.tx());
1778 requestedPosition->set_y(requestedTransform.ty());
1779
1780 SizeProto* size = layerInfo->mutable_size();
1781 size->set_w(state.active.w);
1782 size->set_h(state.active.h);
1783
1784 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1785 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1786
1787 layerInfo->set_is_opaque(isOpaque(state));
1788 layerInfo->set_invalidate(contentDirty);
1789 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1790 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1791 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1792 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1793 layerInfo->set_flags(state.flags);
1794
1795 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1796 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1797
1798 auto parent = getParent();
1799 if (parent != nullptr) {
1800 layerInfo->set_parent(parent->sequence);
1801 }
1802
1803 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1804 if (zOrderRelativeOf != nullptr) {
1805 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1806 }
1807
1808 auto activeBuffer = getActiveBuffer();
1809 if (activeBuffer != nullptr) {
1810 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
1811 }
1812
1813 layerInfo->set_queued_frames(getQueuedFrameCount());
1814 layerInfo->set_refresh_pending(isBufferLatched());
1815}
1816
Mathias Agopian13127d82013-03-05 17:47:11 -08001817// ---------------------------------------------------------------------------
1818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001820
1821#if defined(__gl_h_)
1822#error "don't include gl/gl.h in this file"
1823#endif
1824
1825#if defined(__gl2_h_)
1826#error "don't include gl2/gl2.h in this file"
1827#endif