blob: b0722131029206ea3f1c2b5208b83f6f4a639441 [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>
Kalle Raitaa099a242017-01-11 11:17:29 -080042#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Mathias Agopian3e25fd82013-04-22 17:52:16 +020045#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070046#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070048#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070049#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080051#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware/HWComposer.h"
54
Mathias Agopian875d8e12013-06-07 15:35:48 -070055#include "RenderEngine/RenderEngine.h"
56
Dan Stozac5da2712016-07-20 15:38:12 -070057#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070058#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070059
David Sodman41fdfc92017-11-06 16:09:56 -080060#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062namespace android {
63
Mathias Agopian13127d82013-03-05 17:47:11 -080064int32_t Layer::sSequence = 1;
65
David Sodman41fdfc92017-11-06 16:09:56 -080066Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
67 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070068 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080069 sequence(uint32_t(android_atomic_inc(&sSequence))),
70 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070072 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070074 mPendingStateMutex(),
75 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080077 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080078 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070080 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080082 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080083 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080084 mFiltering(false),
85 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080086 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070087 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070088 mPotentialCursor(false),
89 mQueueItemLock(),
90 mQueueItemCondition(),
91 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070092 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070093 mAutoRefresh(false),
David Sodmanb8af7922017-12-21 15:17:55 -080094 mFreezeGeometryUpdates(false),
David Sodman2b727ac2017-12-21 14:28:08 -080095 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080096
Mathias Agopiana67932f2011-04-20 14:20:59 -070097 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098
99 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800100 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
101 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
102 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700105 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 mCurrentState.active.w = w;
108 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700109 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800110 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700111 mCurrentState.crop.makeInvalid();
112 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700113 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
114 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700116 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700120 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500121 mCurrentState.appId = 0;
122 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123
124 // drawing state & current state are identical
125 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700126
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127 const auto& hwc = flinger->getHwComposer();
128 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
129 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700130 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800131
132 CompositorTiming compositorTiming;
133 flinger->getCompositorTiming(&compositorTiming);
134 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700135
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() {
David Sodman577c8962017-12-08 14:50:53 -0800141 sp<Client> c(mClientRef.promote());
142 if (c != 0) {
143 c->detachLayer(this);
144 }
145
146 for (auto& point : mRemoteSyncPoints) {
147 point->setTransactionApplied();
148 }
149 for (auto& point : mLocalSyncPoints) {
150 point->setFrameAvailable();
151 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700152 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700153}
154
Mathias Agopian13127d82013-03-05 17:47:11 -0800155// ---------------------------------------------------------------------------
156// callbacks
157// ---------------------------------------------------------------------------
158
David Sodmaneb085e02017-10-05 18:49:04 -0700159/*
160 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
161 * Layer. So, the implementation is done in BufferLayer. When called on a
162 * ColorLayer object, it's essentially a NOP.
163 */
David Sodmaneb085e02017-10-05 18:49:04 -0700164void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
Chia-I Wuc6657022017-08-15 11:18:17 -0700166void Layer::onRemovedFromCurrentState() {
167 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
168
chaviw8b3871a2017-11-01 17:41:01 -0700169 mPendingRemoval = true;
170
Robert Carr5edb1ad2017-04-25 10:54:24 -0700171 if (mCurrentState.zOrderRelativeOf != nullptr) {
172 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
173 if (strongRelative != nullptr) {
174 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700175 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700176 }
177 mCurrentState.zOrderRelativeOf = nullptr;
178 }
179
Chia-I Wuc6657022017-08-15 11:18:17 -0700180 for (const auto& child : mCurrentChildren) {
181 child->onRemovedFromCurrentState();
182 }
183}
Chia-I Wu38512252017-05-17 14:36:16 -0700184
Chia-I Wuc6657022017-08-15 11:18:17 -0700185void Layer::onRemoved() {
186 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700187 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700188
Steven Thomasb02664d2017-07-26 18:48:28 -0700189 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700190
Robert Carr1f0a16a2016-10-24 16:27:39 -0700191 for (const auto& child : mCurrentChildren) {
192 child->onRemoved();
193 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700194}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700195
Mathias Agopian13127d82013-03-05 17:47:11 -0800196// ---------------------------------------------------------------------------
197// set-up
198// ---------------------------------------------------------------------------
199
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700200const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202}
203
chaviw13fdc492017-06-27 12:40:18 -0700204bool Layer::getPremultipledAlpha() const {
205 return mPremultipliedAlpha;
206}
207
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700208sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800209 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700210 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800211}
212
213// ---------------------------------------------------------------------------
214// h/w composer set-up
215// ---------------------------------------------------------------------------
216
Steven Thomasb02664d2017-07-26 18:48:28 -0700217bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700218 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700219 "Already have a layer for hwcId %d", hwcId);
David Sodman5d89c1d2017-12-14 15:54:51 -0800220
221 std::shared_ptr<LayerContainer> layer(new LayerContainer(hwc, hwcId));
Steven Thomasb02664d2017-07-26 18:48:28 -0700222 if (!layer) {
223 return false;
224 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700225 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 hwcInfo.hwc = hwc;
227 hwcInfo.layer = layer;
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 return true;
229}
230
Chia-I Wu83806892017-11-16 10:50:20 -0800231bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800233 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700235 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800236 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodman5d89c1d2017-12-14 15:54:51 -0800238 hwcInfo.layer = nullptr;
239
240 if (getBE().mHwcLayers.count(hwcId) == 1) {
241 getBE().mHwcLayers.erase(hwcId);
242 }
243
Chia-I Wu83806892017-11-16 10:50:20 -0800244 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700245}
246
247void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700250 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
251 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700252 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700253 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800254 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700255}
Steven Thomasb02664d2017-07-26 18:48:28 -0700256
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800257Rect Layer::getContentCrop() const {
258 // this is the crop rectangle that applies to the buffer
259 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700260 Rect crop;
261 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700263 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800264 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800265 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800266 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700269 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 return crop;
272}
273
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700274static Rect reduce(const Rect& win, const Region& exclude) {
275 if (CC_LIKELY(exclude.isEmpty())) {
276 return win;
277 }
278 if (exclude.isRect()) {
279 return win.reduce(exclude.getBounds());
280 }
281 return Region(win).subtract(exclude).getBounds();
282}
283
Dan Stoza80d61162017-12-20 15:57:52 -0800284static FloatRect reduce(const FloatRect& win, const Region& exclude) {
285 if (CC_LIKELY(exclude.isEmpty())) {
286 return win;
287 }
288 // Convert through Rect (by rounding) for lack of FloatRegion
289 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
290}
291
Robert Carr1f0a16a2016-10-24 16:27:39 -0700292Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
293 const Layer::State& s(getDrawingState());
294 Rect win(s.active.w, s.active.h);
295
296 if (!s.crop.isEmpty()) {
297 win.intersect(s.crop, &win);
298 }
299
300 Transform t = getTransform();
301 win = t.transform(win);
302
Robert Carr41b08b52017-06-01 16:11:34 -0700303 if (!s.finalCrop.isEmpty()) {
304 win.intersect(s.finalCrop, &win);
305 }
306
Chia-I Wue41dbe62017-06-13 14:10:56 -0700307 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700308 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
309 // When calculating the parent bounds for purposes of clipping,
310 // we don't need to constrain the parent to its transparent region.
311 // The transparent region is an optimization based on the
312 // buffer contents of the layer, but does not affect the space allocated to
313 // it by policy, and thus children should be allowed to extend into the
314 // parent's transparent region. In fact one of the main uses, is to reduce
315 // buffer allocation size in cases where a child window sits behind a main window
316 // (by marking the hole in the parent window as a transparent region)
317 if (p != nullptr) {
318 Rect bounds = p->computeScreenBounds(false);
319 bounds.intersect(win, &win);
320 }
321
322 if (reduceTransparentRegion) {
323 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
324 win = reduce(win, screenTransparentRegion);
325 }
326
327 return win;
328}
329
Dan Stoza80d61162017-12-20 15:57:52 -0800330FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700331 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700332 return computeBounds(s.activeTransparentRegion);
333}
334
Dan Stoza80d61162017-12-20 15:57:52 -0800335FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700336 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800337 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700338
339 if (!s.crop.isEmpty()) {
340 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342
343 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700344 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700345 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800346 // Look in computeScreenBounds recursive call for explanation of
347 // why we pass false here.
348 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 }
350
351 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800352
353 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700354 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800355 floatWin = t.transform(floatWin);
356 floatWin = floatWin.intersect(bounds.toFloatRect());
357 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700358 }
359
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700360 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800361 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800362}
363
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700365 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800366 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700367 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800368
369 // apply the projection's clipping to the window crop in
370 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700371 // if there are no window scaling involved, this operation will map to full
372 // pixels in the buffer.
373 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
374 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700375
376 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700377 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700378 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700379 }
380
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 Transform t = getTransform();
382 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000383 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
384 activeCrop.clear();
385 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700386 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800387 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000388 activeCrop.clear();
389 }
390 }
chaviwb1154d12017-10-31 14:15:36 -0700391
392 const auto& p = mDrawingParent.promote();
393 if (p != nullptr) {
394 auto parentCrop = p->computeInitialCrop(hw);
395 activeCrop.intersect(parentCrop, &activeCrop);
396 }
397
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398 return activeCrop;
399}
400
Dan Stoza5a423ea2017-02-16 14:10:39 -0800401FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 // the content crop is the area of the content that gets scaled to the
403 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800404 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405
406 // In addition there is a WM-specified crop we pull from our drawing state.
407 const State& s(getDrawingState());
408
409 // Screen space to make reduction to parent crop clearer.
410 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411 Transform t = getTransform();
412 // Back to layer space to work with the content crop.
413 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800414
Michael Lentine28ea2172014-11-19 18:32:37 -0800415 // This needs to be here as transform.transform(Rect) computes the
416 // transformed rect and then takes the bounding box of the result before
417 // returning. This means
418 // transform.inverse().transform(transform.transform(Rect)) != Rect
419 // in which case we need to make sure the final rect is clipped to the
420 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
422 activeCrop.clear();
423 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800424
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700425 // subtract the transparent region and snap to the bounds
426 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
427
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 // Transform the window crop to match the buffer coordinate system,
429 // which means using the inverse of the current transform set on the
430 // SurfaceFlingerConsumer.
431 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700432 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700434 * the code below applies the primary display's inverse transform to the
435 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 */
David Sodman41fdfc92017-11-06 16:09:56 -0800437 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // calculate the inverse transform
439 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800440 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800441 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800443 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800444 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445
446 int winWidth = s.active.w;
447 int winHeight = s.active.h;
448 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
449 // If the activeCrop has been rotate the ends are rotated but not
450 // the space itself so when transforming ends back we can't rely on
451 // a modification of the axes of rotation. To account for this we
452 // need to reorient the inverse rotation in terms of the current
453 // axes of rotation.
454 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
455 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
456 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800457 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 }
459 winWidth = s.active.h;
460 winHeight = s.active.w;
461 }
David Sodman41fdfc92017-11-06 16:09:56 -0800462 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463
464 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800465 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000466 float yScale = crop.getHeight() / float(winHeight);
467
David Sodman41fdfc92017-11-06 16:09:56 -0800468 float insetL = winCrop.left * xScale;
469 float insetT = winCrop.top * yScale;
470 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471 float insetB = (winHeight - winCrop.bottom) * yScale;
472
David Sodman41fdfc92017-11-06 16:09:56 -0800473 crop.left += insetL;
474 crop.top += insetT;
475 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 crop.bottom -= insetB;
477
Mathias Agopian13127d82013-03-05 17:47:11 -0800478 return crop;
479}
480
Robert Carrae060832016-11-28 10:51:00 -0800481void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700482{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800483 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700484 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700485
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700486 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 hwcInfo.forceClientComposition = false;
488
489 if (isSecure() && !displayDevice->isSecure()) {
490 hwcInfo.forceClientComposition = true;
491 }
492
Mathias Agopian13127d82013-03-05 17:47:11 -0800493 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700494 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500495 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700496 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800497 blendMode =
498 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 }
David Sodman4b7c4bc2017-11-17 12:13:59 -0800500 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800501
502 // apply the layer's transform, followed by the display's global transform
503 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700504 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700505 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700506 if (!s.crop.isEmpty()) {
507 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800509 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000510 activeCrop.clear();
511 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800513 // This needs to be here as transform.transform(Rect) computes the
514 // transformed rect and then takes the bounding box of the result before
515 // returning. This means
516 // transform.inverse().transform(transform.transform(Rect)) != Rect
517 // in which case we need to make sure the final rect is clipped to the
518 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800519 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000520 activeCrop.clear();
521 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700522 // mark regions outside the crop as transparent
523 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800524 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
525 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
526 activeTransparentRegion.orSelf(
527 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700528 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700529
Dan Stoza80d61162017-12-20 15:57:52 -0800530 // computeBounds returns a FloatRect to provide more accuracy during the
531 // transformation. We then round upon constructing 'frame'.
532 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700533 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800534 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000535 frame.clear();
536 }
537 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
539 frame.clear();
540 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541 const Transform& tr(displayDevice->getTransform());
542 Rect transformedFrame = tr.transform(frame);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800543 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544
Dan Stoza5a423ea2017-02-16 14:10:39 -0800545 FloatRect sourceCrop = computeCrop(displayDevice);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800546 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547
chaviw13fdc492017-06-27 12:40:18 -0700548 float alpha = static_cast<float>(getAlpha());
David Sodman4b7c4bc2017-11-17 12:13:59 -0800549 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550
David Sodman4b7c4bc2017-11-17 12:13:59 -0800551 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500552
Albert Chaulk2a589632017-05-04 16:59:44 -0400553 int type = s.type;
554 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700555 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400556 if (parent.get()) {
557 auto& parentState = parent->getDrawingState();
558 type = parentState.type;
559 appId = parentState.appId;
560 }
561
David Sodman4b7c4bc2017-11-17 12:13:59 -0800562 getBE().compositionInfo.hwc.type = type;
563 getBE().compositionInfo.hwc.appId = appId;
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800564
Mathias Agopian29a367b2011-07-12 14:51:45 -0700565 /*
566 * Transformations are applied in this order:
567 * 1) buffer orientation/flip/mirror
568 * 2) state transformation (window manager)
569 * 3) layer orientation (screen orientation)
570 * (NOTE: the matrices are multiplied in reverse order)
571 */
572
573 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700574 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700575
Robert Carrcae605c2017-03-29 12:10:31 -0700576 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700577 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700578 * the code below applies the primary display's inverse transform to the
579 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700580 */
David Sodman41fdfc92017-11-06 16:09:56 -0800581 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700582 // calculate the inverse transform
583 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800584 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700585 }
Robert Carrcae605c2017-03-29 12:10:31 -0700586
587 /*
588 * Here we cancel out the orientation component of the WM transform.
589 * The scaling and translate components are already included in our bounds
590 * computation so it's enough to just omit it in the composition.
591 * See comment in onDraw with ref to b/36727915 for why.
592 */
593 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700594 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700595
Jorim Jaggi5a963262018-01-17 15:57:44 +0100596 // STOPSHIP (b/72106793): If we have less than 25% scaling, HWC usually needs to use the rotator
597 // to handle it. However, there is one guaranteed frame of jank when we switch to using the
598 // rotator. In the meantime, we force GL composition instead until we have a better fix for the
599 // HWC issue.
600 bool extremeScaling = abs(t[0][0]) <= 0.25 || abs(t[1][1]) <= 0.25;
601
Mathias Agopian29a367b2011-07-12 14:51:45 -0700602 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800603 const uint32_t orientation = transform.getOrientation();
Jorim Jaggi5a963262018-01-17 15:57:44 +0100604 if (orientation & Transform::ROT_INVALID || extremeScaling) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 // we can only handle simple transformation
606 hwcInfo.forceClientComposition = true;
607 } else {
608 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800609 hwcInfo.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800610 getBE().compositionInfo.hwc.transform = transform;
611 }
612}
613
614void Layer::configureHwcLayer(const sp<const DisplayDevice>& displayDevice) {
615 ATRACE_CALL();
616
617 auto hwcId = displayDevice->getHwcDisplayId();
618 auto& hwcInfo = getBE().mHwcLayers[hwcId];
619 auto& hwcLayer = hwcInfo.layer;
620
621 auto error = (*hwcLayer)->setBlendMode(getBE().compositionInfo.hwc.blendMode);
622 ALOGE_IF(error != HWC2::Error::None,
623 "[%s] Failed to set blend mode %s:"
624 " %s (%d)",
625 mName.string(),
626 to_string(getBE().compositionInfo.hwc.blendMode).c_str(), to_string(error).c_str(),
627 static_cast<int32_t>(error));
628
629 error = (*hwcLayer)->setDisplayFrame(getBE().compositionInfo.hwc.displayFrame);
630 ALOGE_IF(error != HWC2::Error::None,
631 "[%s] Failed to set the display frame [%d, %d, %d, %d] %s (%d)",
632 mName.string(),
633 getBE().compositionInfo.hwc.displayFrame.left,
634 getBE().compositionInfo.hwc.displayFrame.right,
635 getBE().compositionInfo.hwc.displayFrame.top,
636 getBE().compositionInfo.hwc.displayFrame.bottom,
637 to_string(error).c_str(), static_cast<int32_t>(error));
638
639
640 error = (*hwcLayer)->setPlaneAlpha(getBE().compositionInfo.hwc.alpha);
641 ALOGE_IF(error != HWC2::Error::None,
642 "[%s] Failed to set plane alpha %.3f: "
643 "%s (%d)",
644 mName.string(), getBE().compositionInfo.hwc.alpha,
645 to_string(error).c_str(), static_cast<int32_t>(error));
646
647 error = (*hwcLayer)->setSourceCrop(getBE().compositionInfo.hwc.sourceCrop);
648 ALOGE_IF(error != HWC2::Error::None,
649 "[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: %s (%d)",
650 mName.string(),
651 getBE().compositionInfo.hwc.sourceCrop.left,
652 getBE().compositionInfo.hwc.sourceCrop.right,
653 getBE().compositionInfo.hwc.sourceCrop.top,
654 getBE().compositionInfo.hwc.sourceCrop.bottom,
655 to_string(error).c_str(), static_cast<int32_t>(error));
656
657 error = (*hwcLayer)->setTransform(getBE().compositionInfo.hwc.transform);
658 ALOGE_IF(error != HWC2::Error::None,
659 "[%s] Failed to set transform %s: "
660 "%s (%d)",
661 mName.string(),
662 to_string(getBE().compositionInfo.hwc.transform).c_str(), to_string(error).c_str(),
663 static_cast<int32_t>(error));
664
665 error = (*hwcLayer)->setZOrder(getBE().compositionInfo.hwc.z);
666 ALOGE_IF(error != HWC2::Error::None,
667 "[%s] Failed to set Z %u: %s (%d)",
668 mName.string(), getBE().compositionInfo.hwc.z,
669 to_string(error).c_str(), static_cast<int32_t>(error));
670
671 error = (*hwcLayer)->setInfo(getBE().compositionInfo.hwc.type, getBE().compositionInfo.hwc.appId);
672 ALOGE_IF(error != HWC2::Error::None,
673 "[%s] Failed to set info (%d)",
674 mName.string(), static_cast<int32_t>(error));
675
676 error = (*hwcLayer)->setVisibleRegion(getBE().compositionInfo.hwc.visibleRegion);
677 ALOGE_IF(error != HWC2::Error::None,
678 "[%s] Failed to set visible region: %s (%d)",
679 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
680
681 error = (*hwcLayer)->setSurfaceDamage(getBE().compositionInfo.hwc.surfaceDamage);
682 ALOGE_IF(error != HWC2::Error::None,
683 "[%s] Failed to set surface damage: %s (%d)",
684 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
685
686 error = (*hwcLayer)->setCompositionType(getBE().compositionInfo.compositionType);
687 ALOGE_IF(error != HWC2::Error::None,
688 "[%s] Failed to set composition type: %s (%d)",
689 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
690
691 error = (*hwcLayer)->setDataspace(getBE().compositionInfo.hwc.dataspace);
692 ALOGE_IF(error != HWC2::Error::None,
693 "[%s] Failed to set dataspace: %s (%d)",
694 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
695
696 error = (*hwcLayer)->setHdrMetadata(getBE().compositionInfo.hwc.hdrMetadata);
697 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
698 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
699 to_string(error).c_str(), static_cast<int32_t>(error));
700 }
701
702 error = (*hwcLayer)->setColor(getBE().compositionInfo.hwc.color);
703 ALOGE_IF(error != HWC2::Error::None,
704 "[%s] Failed to set color: %s (%d)",
705 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
706
707 if (getBE().compositionInfo.hwc.fence) {
708 error = (*hwcLayer)->setBuffer(getBE().compositionInfo.mBufferSlot,
709 getBE().compositionInfo.mBuffer, getBE().compositionInfo.hwc.fence);
David Sodman41fdfc92017-11-06 16:09:56 -0800710 ALOGE_IF(error != HWC2::Error::None,
David Sodman4b7c4bc2017-11-17 12:13:59 -0800711 "[%s] Failed to set buffer: %s (%d)",
712 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700714}
715
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700717 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
719 return;
720 }
721
David Sodman6f65f3e2017-11-03 14:28:09 -0700722 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723}
Dan Stozaee44edd2015-03-23 15:50:23 -0700724
chaviwc9232ed2017-11-14 15:31:15 -0800725bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700726 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800727 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
728 return false;
729 }
730
David Sodman6f65f3e2017-11-03 14:28:09 -0700731 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800732}
733
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
735 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700736 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700737 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738 return;
739 }
740
741 // This gives us only the "orientation" component of the transform
742 const State& s(getCurrentState());
743
744 // Apply the layer's transform, followed by the display's global transform
745 // Here we're guaranteed that the layer's transform preserves rects
746 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700747 if (!s.crop.isEmpty()) {
748 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 }
750 // Subtract the transparent region and snap to the bounds
751 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700752 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700754 if (!s.finalCrop.isEmpty()) {
755 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000756 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800757 auto& displayTransform(displayDevice->getTransform());
758 auto position = displayTransform.transform(frame);
759
David Sodman5d89c1d2017-12-14 15:54:51 -0800760 auto error = (*getBE().mHwcLayers[hwcId].layer)->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700761 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800762 ALOGE_IF(error != HWC2::Error::None,
763 "[%s] Failed to set cursor position "
764 "to (%d, %d): %s (%d)",
765 mName.string(), position.left, position.top, to_string(error).c_str(),
766 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767}
Riley Andrews03414a12014-07-01 14:22:59 -0700768
Mathias Agopian13127d82013-03-05 17:47:11 -0800769// ---------------------------------------------------------------------------
770// drawing...
771// ---------------------------------------------------------------------------
772
chaviwa76b2712017-09-20 12:02:26 -0700773void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
774 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800775}
776
chaviwa76b2712017-09-20 12:02:26 -0700777void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
778 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800779}
780
chaviwa76b2712017-09-20 12:02:26 -0700781void Layer::draw(const RenderArea& renderArea) const {
782 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800783}
784
David Sodman41fdfc92017-11-06 16:09:56 -0800785void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
786 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800787 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700788 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700789 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700790 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800791}
792
chaviwa76b2712017-09-20 12:02:26 -0700793void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800794 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800795}
796
David Sodman41fdfc92017-11-06 16:09:56 -0800797void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700798 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799 ALOGE("setCompositionType called without a valid HWC layer");
800 return;
801 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700802 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800803 auto& hwcLayer = hwcInfo.layer;
David Sodman5d89c1d2017-12-14 15:54:51 -0800804 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (*hwcLayer)->getId(), to_string(type).c_str(),
David Sodman41fdfc92017-11-06 16:09:56 -0800805 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806 if (hwcInfo.compositionType != type) {
807 ALOGV(" actually setting");
808 hwcInfo.compositionType = type;
809 if (callIntoHwc) {
David Sodman5d89c1d2017-12-14 15:54:51 -0800810 auto error = (*hwcLayer)->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800811 ALOGE_IF(error != HWC2::Error::None,
812 "[%s] Failed to set "
813 "composition type %s: %s (%d)",
814 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
815 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800816 }
817 }
818}
819
820HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700821 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
822 // If we're querying the composition type for a display that does not
823 // have a HWC counterpart, then it will always be Client
824 return HWC2::Composition::Client;
825 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700826 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700827 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800828 return HWC2::Composition::Invalid;
829 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700830 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800831}
832
833void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700834 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800835 ALOGE("setClearClientTarget called without a valid HWC layer");
836 return;
837 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700838 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800839}
840
841bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700842 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800843 ALOGE("getClearClientTarget called without a valid HWC layer");
844 return false;
845 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700846 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800847}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800848
Dan Stozacac35382016-01-27 12:21:06 -0800849bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
850 if (point->getFrameNumber() <= mCurrentFrameNumber) {
851 // Don't bother with a SyncPoint, since we've already latched the
852 // relevant frame
853 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700854 }
855
Dan Stozacac35382016-01-27 12:21:06 -0800856 Mutex::Autolock lock(mLocalSyncPointMutex);
857 mLocalSyncPoints.push_back(point);
858 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700859}
860
Mathias Agopian13127d82013-03-05 17:47:11 -0800861void Layer::setFiltering(bool filtering) {
862 mFiltering = filtering;
863}
864
865bool Layer::getFiltering() const {
866 return mFiltering;
867}
868
Mathias Agopian13127d82013-03-05 17:47:11 -0800869// ----------------------------------------------------------------------------
870// local state
871// ----------------------------------------------------------------------------
872
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000873static void boundPoint(vec2* point, const Rect& crop) {
874 if (point->x < crop.left) {
875 point->x = crop.left;
876 }
877 if (point->x > crop.right) {
878 point->x = crop.right;
879 }
880 if (point->y < crop.top) {
881 point->y = crop.top;
882 }
883 if (point->y > crop.bottom) {
884 point->y = crop.bottom;
885 }
886}
887
chaviwa76b2712017-09-20 12:02:26 -0700888void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
889 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700890 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700891 const Transform renderAreaTransform(renderArea.getTransform());
892 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800893 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700894
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000895 vec2 lt = vec2(win.left, win.top);
896 vec2 lb = vec2(win.left, win.bottom);
897 vec2 rb = vec2(win.right, win.bottom);
898 vec2 rt = vec2(win.right, win.top);
899
Robert Carr1f0a16a2016-10-24 16:27:39 -0700900 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000901 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700902 lt = layerTransform.transform(lt);
903 lb = layerTransform.transform(lb);
904 rb = layerTransform.transform(rb);
905 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000906 }
907
Robert Carrb5d3d262016-03-25 15:08:13 -0700908 if (!s.finalCrop.isEmpty()) {
909 boundPoint(&lt, s.finalCrop);
910 boundPoint(&lb, s.finalCrop);
911 boundPoint(&rb, s.finalCrop);
912 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000913 }
914
Mathias Agopianff2ed702013-09-01 21:36:12 -0700915 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700916 position[0] = renderAreaTransform.transform(lt);
917 position[1] = renderAreaTransform.transform(lb);
918 position[2] = renderAreaTransform.transform(rb);
919 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800920 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700921 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800922 }
923}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800924
David Sodman41fdfc92017-11-06 16:09:56 -0800925bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700926 const Layer::State& s(mDrawingState);
927 return (s.flags & layer_state_t::eLayerSecure);
928}
929
Mathias Agopian13127d82013-03-05 17:47:11 -0800930void Layer::setVisibleRegion(const Region& visibleRegion) {
931 // always called from main thread
932 this->visibleRegion = visibleRegion;
933}
934
935void Layer::setCoveredRegion(const Region& coveredRegion) {
936 // always called from main thread
937 this->coveredRegion = coveredRegion;
938}
939
David Sodman41fdfc92017-11-06 16:09:56 -0800940void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800941 // always called from main thread
942 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
943}
944
Robert Carre5f4f692018-01-12 13:12:28 -0800945void Layer::clearVisibilityRegions() {
946 visibleRegion.clear();
947 visibleNonTransparentRegion.clear();
948 coveredRegion.clear();
949}
950
Mathias Agopian13127d82013-03-05 17:47:11 -0800951// ----------------------------------------------------------------------------
952// transaction
953// ----------------------------------------------------------------------------
954
Dan Stoza7dde5992015-05-22 09:51:44 -0700955void Layer::pushPendingState() {
956 if (!mCurrentState.modified) {
957 return;
958 }
959
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 // If this transaction is waiting on the receipt of a frame, generate a sync
961 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800962 if (mCurrentState.barrierLayer != nullptr) {
963 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
964 if (barrierLayer == nullptr) {
965 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 // If we can't promote the layer we are intended to wait on,
967 // then it is expired or otherwise invalid. Allow this transaction
968 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800969 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800970 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800971 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800972 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800973 mRemoteSyncPoints.push_back(std::move(syncPoint));
974 } else {
975 // We already missed the frame we're supposed to synchronize
976 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800977 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800978 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700979 }
980
Dan Stoza7dde5992015-05-22 09:51:44 -0700981 // Wake us up to check if the frame has been received
982 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700983 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700984 }
985 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700986 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700987}
988
Pablo Ceballos05289c22016-04-14 15:49:55 -0700989void Layer::popPendingState(State* stateToCommit) {
990 auto oldFlags = stateToCommit->flags;
991 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800992 stateToCommit->flags =
993 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700994
995 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700996 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700997}
998
Pablo Ceballos05289c22016-04-14 15:49:55 -0700999bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 bool stateUpdateAvailable = false;
1001 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001002 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001003 if (mRemoteSyncPoints.empty()) {
1004 // If we don't have a sync point for this, apply it anyway. It
1005 // will be visually wrong, but it should keep us from getting
1006 // into too much trouble.
1007 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001008 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001009 stateUpdateAvailable = true;
1010 continue;
1011 }
1012
David Sodman41fdfc92017-11-06 16:09:56 -08001013 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
1014 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -08001015
1016 // Signal our end of the sync point and then dispose of it
1017 mRemoteSyncPoints.front()->setTransactionApplied();
1018 mRemoteSyncPoints.pop_front();
1019 continue;
1020 }
1021
Dan Stoza7dde5992015-05-22 09:51:44 -07001022 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1023 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001024 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001025 stateUpdateAvailable = true;
1026
1027 // Signal our end of the sync point and then dispose of it
1028 mRemoteSyncPoints.front()->setTransactionApplied();
1029 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001030 } else {
1031 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001032 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001033 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001034 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001035 stateUpdateAvailable = true;
1036 }
1037 }
1038
1039 // If we still have pending updates, wake SurfaceFlinger back up and point
1040 // it at this layer so we can process them
1041 if (!mPendingStates.empty()) {
1042 setTransactionFlags(eTransactionNeeded);
1043 mFlinger->setTransactionFlags(eTraversalNeeded);
1044 }
1045
1046 mCurrentState.modified = false;
1047 return stateUpdateAvailable;
1048}
1049
Mathias Agopian13127d82013-03-05 17:47:11 -08001050uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001051 ATRACE_CALL();
1052
Dan Stoza7dde5992015-05-22 09:51:44 -07001053 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001054 Layer::State c = getCurrentState();
1055 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001056 return 0;
1057 }
1058
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001059 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
David Sodman41fdfc92017-11-06 16:09:56 -08001061 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001062
David Sodmaneb085e02017-10-05 18:49:04 -07001063 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001064 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001065 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001066 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1067 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1068 " requested={ wh={%4u,%4u} }}\n"
1069 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1070 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001071 this, getName().string(), mCurrentTransform,
1072 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1073 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1074 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1075 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1076 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001078 // record the new size, form this point on, when the client request
1079 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001080 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001082
Robert Carre392b552017-09-19 12:16:05 -07001083 // Don't let Layer::doTransaction update the drawing state
1084 // if we have a pending resize, unless we are in fixed-size mode.
1085 // the drawing state will be updated only once we receive a buffer
1086 // with the correct size.
1087 //
1088 // In particular, we want to make sure the clip (which is part
1089 // of the geometry state) is latched together with the size but is
1090 // latched immediately when no resizing is involved.
1091 //
1092 // If a sideband stream is attached, however, we want to skip this
1093 // optimization so that transactions aren't missed when a buffer
1094 // never arrives
1095 //
1096 // In the case that we don't have a buffer we ignore other factors
1097 // and avoid entering the resizePending state. At a high level the
1098 // resizePending state is to avoid applying the state of the new buffer
1099 // to the old buffer. However in the state where we don't have an old buffer
1100 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001101 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001102 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001103 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001104 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001105 flags |= eDontUpdateGeometryState;
1106 }
1107 }
1108
Robert Carr7bf247e2017-05-18 14:02:49 -07001109 // Here we apply various requested geometry states, depending on our
1110 // latching configuration. See Layer.h for a detailed discussion of
1111 // how geometry latching is controlled.
1112 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001113 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001114
1115 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1116 // mode, which causes attributes which normally latch regardless of scaling mode,
1117 // to be delayed. We copy the requested state to the active state making sure
1118 // to respect these rules (again see Layer.h for a detailed discussion).
1119 //
1120 // There is an awkward asymmetry in the handling of the crop states in the position
1121 // states, as can be seen below. Largely this arises from position and transform
1122 // being stored in the same data structure while having different latching rules.
1123 // b/38182305
1124 //
1125 // Careful that "c" and editCurrentState may not begin as equivalent due to
1126 // applyPendingStates in the presence of deferred transactions.
1127 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001128 float tx = c.active.transform.tx();
1129 float ty = c.active.transform.ty();
1130 c.active = c.requested;
1131 c.active.transform.set(tx, ty);
1132 editCurrentState.active = c.active;
1133 } else {
1134 editCurrentState.active = editCurrentState.requested;
1135 c.active = c.requested;
1136 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 }
1138
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001139 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001140 // invalidate and recompute the visible regions if needed
1141 flags |= Layer::eVisibleRegion;
1142 }
1143
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001144 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001145 // invalidate and recompute the visible regions if needed
1146 flags |= eVisibleRegion;
1147 this->contentDirty = true;
1148
1149 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001150 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001151 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001152 }
1153
Dan Stozac8145172016-04-28 16:29:06 -07001154 // If the layer is hidden, signal and clear out all local sync points so
1155 // that transactions for layers depending on this layer's frames becoming
1156 // visible are not blocked
1157 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001158 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001159 }
1160
Mathias Agopian13127d82013-03-05 17:47:11 -08001161 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001162 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164}
1165
Pablo Ceballos05289c22016-04-14 15:49:55 -07001166void Layer::commitTransaction(const State& stateToCommit) {
1167 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001168}
1169
Mathias Agopian13127d82013-03-05 17:47:11 -08001170uint32_t Layer::getTransactionFlags(uint32_t flags) {
1171 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1172}
1173
1174uint32_t Layer::setTransactionFlags(uint32_t flags) {
1175 return android_atomic_or(flags, &mTransactionFlags);
1176}
1177
Robert Carr82364e32016-05-15 11:27:47 -07001178bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001179 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001180 return false;
1181 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001182
1183 // We update the requested and active position simultaneously because
1184 // we want to apply the position portion of the transform matrix immediately,
1185 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001186 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001187 if (immediate && !mFreezeGeometryUpdates) {
1188 // Here we directly update the active state
1189 // unlike other setters, because we store it within
1190 // the transform, but use different latching rules.
1191 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001192 mCurrentState.active.transform.set(x, y);
1193 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001194 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001195
Dan Stoza7dde5992015-05-22 09:51:44 -07001196 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 setTransactionFlags(eTransactionNeeded);
1198 return true;
1199}
Robert Carr82364e32016-05-15 11:27:47 -07001200
Robert Carr1f0a16a2016-10-24 16:27:39 -07001201bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1202 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1203 if (idx < 0) {
1204 return false;
1205 }
1206 if (childLayer->setLayer(z)) {
1207 mCurrentChildren.removeAt(idx);
1208 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001209 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001210 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001211 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001212}
1213
Robert Carr503c7042017-09-27 15:06:08 -07001214bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1215 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1216 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1217 if (idx < 0) {
1218 return false;
1219 }
1220 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1221 mCurrentChildren.removeAt(idx);
1222 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001223 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001224 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001225 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001226}
1227
Robert Carrae060832016-11-28 10:51:00 -08001228bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001229 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 mCurrentState.sequence++;
1231 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001232 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001233
1234 // Discard all relative layering.
1235 if (mCurrentState.zOrderRelativeOf != nullptr) {
1236 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1237 if (strongRelative != nullptr) {
1238 strongRelative->removeZOrderRelative(this);
1239 }
1240 mCurrentState.zOrderRelativeOf = nullptr;
1241 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 setTransactionFlags(eTransactionNeeded);
1243 return true;
1244}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001245
Robert Carrdb66e622017-04-10 16:55:57 -07001246void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1247 mCurrentState.zOrderRelatives.remove(relative);
1248 mCurrentState.sequence++;
1249 mCurrentState.modified = true;
1250 setTransactionFlags(eTransactionNeeded);
1251}
1252
1253void Layer::addZOrderRelative(const wp<Layer>& relative) {
1254 mCurrentState.zOrderRelatives.add(relative);
1255 mCurrentState.modified = true;
1256 mCurrentState.sequence++;
1257 setTransactionFlags(eTransactionNeeded);
1258}
1259
Robert Carr503d2bd2017-12-04 15:49:47 -08001260bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001261 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1262 if (handle == nullptr) {
1263 return false;
1264 }
1265 sp<Layer> relative = handle->owner.promote();
1266 if (relative == nullptr) {
1267 return false;
1268 }
1269
Robert Carr503d2bd2017-12-04 15:49:47 -08001270 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1271 mCurrentState.zOrderRelativeOf == relative) {
1272 return false;
1273 }
1274
Robert Carrdb66e622017-04-10 16:55:57 -07001275 mCurrentState.sequence++;
1276 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001277 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001278
chaviw9ab4bd12017-11-03 13:11:00 -07001279 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1280 if (oldZOrderRelativeOf != nullptr) {
1281 oldZOrderRelativeOf->removeZOrderRelative(this);
1282 }
Robert Carrdb66e622017-04-10 16:55:57 -07001283 mCurrentState.zOrderRelativeOf = relative;
1284 relative->addZOrderRelative(this);
1285
1286 setTransactionFlags(eTransactionNeeded);
1287
1288 return true;
1289}
1290
Mathias Agopian13127d82013-03-05 17:47:11 -08001291bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001292 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 mCurrentState.requested.w = w;
1294 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001295 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 setTransactionFlags(eTransactionNeeded);
1297 return true;
1298}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001299bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001300 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001301 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001302 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001303 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001304 setTransactionFlags(eTransactionNeeded);
1305 return true;
1306}
chaviw13fdc492017-06-27 12:40:18 -07001307
1308bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001309 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1310 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001311 return false;
1312
1313 mCurrentState.sequence++;
1314 mCurrentState.color.r = color.r;
1315 mCurrentState.color.g = color.g;
1316 mCurrentState.color.b = color.b;
1317 mCurrentState.modified = true;
1318 setTransactionFlags(eTransactionNeeded);
1319 return true;
1320}
1321
Mathias Agopian13127d82013-03-05 17:47:11 -08001322bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1323 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001324 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001325 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001326 setTransactionFlags(eTransactionNeeded);
1327 return true;
1328}
1329bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001330 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001331 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001332 setTransactionFlags(eTransactionNeeded);
1333 return true;
1334}
1335bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1336 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001337 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001338 mCurrentState.sequence++;
1339 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001340 mCurrentState.mask = mask;
1341 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001342 setTransactionFlags(eTransactionNeeded);
1343 return true;
1344}
Robert Carr99e27f02016-06-16 15:18:02 -07001345
1346bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001347 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001348 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001349 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001350 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001351 mCurrentState.crop = crop;
1352 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001353 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1354
Dan Stoza7dde5992015-05-22 09:51:44 -07001355 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001356 setTransactionFlags(eTransactionNeeded);
1357 return true;
1358}
Robert Carr8d5227b2017-03-16 15:41:03 -07001359
1360bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001361 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001362 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001363 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001364 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001365 mCurrentState.finalCrop = crop;
1366 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001367 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1368
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001369 mCurrentState.modified = true;
1370 setTransactionFlags(eTransactionNeeded);
1371 return true;
1372}
Mathias Agopian13127d82013-03-05 17:47:11 -08001373
Robert Carrc3574f72016-03-24 12:19:32 -07001374bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001375 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001376 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001377 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001378 return true;
1379}
1380
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001381void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001382 mCurrentState.appId = appId;
1383 mCurrentState.type = type;
1384 mCurrentState.modified = true;
1385 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001386}
1387
Mathias Agopian13127d82013-03-05 17:47:11 -08001388bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001389 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001390 mCurrentState.sequence++;
1391 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001392 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001393 setTransactionFlags(eTransactionNeeded);
1394 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001395}
1396
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001397bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001398 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001399 mCurrentState.sequence++;
1400 mCurrentState.dataSpace = dataSpace;
1401 mCurrentState.modified = true;
1402 setTransactionFlags(eTransactionNeeded);
1403 return true;
1404}
1405
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001406android_dataspace Layer::getDataSpace() const {
1407 return mCurrentState.dataSpace;
1408}
1409
Robert Carr1f0a16a2016-10-24 16:27:39 -07001410uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001411 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001412 if (p == nullptr) {
1413 return getDrawingState().layerStack;
1414 }
1415 return p->getLayerStack();
1416}
1417
David Sodman41fdfc92017-11-06 16:09:56 -08001418void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001419 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001420 mCurrentState.frameNumber = frameNumber;
1421 // We don't set eTransactionNeeded, because just receiving a deferral
1422 // request without any other state updates shouldn't actually induce a delay
1423 mCurrentState.modified = true;
1424 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001425 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001426 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001427 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001428}
1429
David Sodman41fdfc92017-11-06 16:09:56 -08001430void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001431 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1432 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001433}
1434
Dan Stozaee44edd2015-03-23 15:50:23 -07001435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436// ----------------------------------------------------------------------------
1437// pageflip handling...
1438// ----------------------------------------------------------------------------
1439
Robert Carr1f0a16a2016-10-24 16:27:39 -07001440bool Layer::isHiddenByPolicy() const {
1441 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001442 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001443 if (parent != nullptr && parent->isHiddenByPolicy()) {
1444 return true;
1445 }
1446 return s.flags & layer_state_t::eLayerHidden;
1447}
1448
David Sodman41fdfc92017-11-06 16:09:56 -08001449uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001450 // TODO: should we do something special if mSecure is set?
1451 if (mProtectedByApp) {
1452 // need a hardware-protected path to external video sink
1453 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001454 }
Riley Andrews03414a12014-07-01 14:22:59 -07001455 if (mPotentialCursor) {
1456 usage |= GraphicBuffer::USAGE_CURSOR;
1457 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001458 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001459 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001460}
1461
Mathias Agopian84300952012-11-21 16:02:13 -08001462void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001463 uint32_t orientation = 0;
1464 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001465 // The transform hint is used to improve performance, but we can
1466 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001467 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001468 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001469 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001470 if (orientation & Transform::ROT_INVALID) {
1471 orientation = 0;
1472 }
1473 }
David Sodmaneb085e02017-10-05 18:49:04 -07001474 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001475}
1476
Mathias Agopian13127d82013-03-05 17:47:11 -08001477// ----------------------------------------------------------------------------
1478// debugging
1479// ----------------------------------------------------------------------------
1480
Kalle Raitaa099a242017-01-11 11:17:29 -08001481LayerDebugInfo Layer::getLayerDebugInfo() const {
1482 LayerDebugInfo info;
1483 const Layer::State& ds = getDrawingState();
1484 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001485 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001486 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1487 info.mType = String8(getTypeId());
1488 info.mTransparentRegion = ds.activeTransparentRegion;
1489 info.mVisibleRegion = visibleRegion;
1490 info.mSurfaceDamageRegion = surfaceDamageRegion;
1491 info.mLayerStack = getLayerStack();
1492 info.mX = ds.active.transform.tx();
1493 info.mY = ds.active.transform.ty();
1494 info.mZ = ds.z;
1495 info.mWidth = ds.active.w;
1496 info.mHeight = ds.active.h;
1497 info.mCrop = ds.crop;
1498 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001499 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001500 info.mFlags = ds.flags;
1501 info.mPixelFormat = getPixelFormat();
1502 info.mDataSpace = getDataSpace();
1503 info.mMatrix[0][0] = ds.active.transform[0][0];
1504 info.mMatrix[0][1] = ds.active.transform[0][1];
1505 info.mMatrix[1][0] = ds.active.transform[1][0];
1506 info.mMatrix[1][1] = ds.active.transform[1][1];
1507 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001508 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001509 if (buffer != 0) {
1510 info.mActiveBufferWidth = buffer->getWidth();
1511 info.mActiveBufferHeight = buffer->getHeight();
1512 info.mActiveBufferStride = buffer->getStride();
1513 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001514 } else {
1515 info.mActiveBufferWidth = 0;
1516 info.mActiveBufferHeight = 0;
1517 info.mActiveBufferStride = 0;
1518 info.mActiveBufferFormat = 0;
1519 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001520 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001521 info.mNumQueuedFrames = getQueuedFrameCount();
1522 info.mRefreshPending = isBufferLatched();
1523 info.mIsOpaque = isOpaque(ds);
1524 info.mContentDirty = contentDirty;
1525 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001526}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001527
Dan Stozae22aec72016-08-01 13:20:59 -07001528void Layer::miniDumpHeader(String8& result) {
1529 result.append("----------------------------------------");
1530 result.append("---------------------------------------\n");
1531 result.append(" Layer name\n");
1532 result.append(" Z | ");
1533 result.append(" Comp Type | ");
1534 result.append(" Disp Frame (LTRB) | ");
1535 result.append(" Source Crop (LTRB)\n");
1536 result.append("----------------------------------------");
1537 result.append("---------------------------------------\n");
1538}
1539
1540void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001541 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001542 return;
1543 }
1544
1545 String8 name;
1546 if (mName.length() > 77) {
1547 std::string shortened;
1548 shortened.append(mName.string(), 36);
1549 shortened.append("[...]");
1550 shortened.append(mName.string() + (mName.length() - 36), 36);
1551 name = shortened.c_str();
1552 } else {
1553 name = mName;
1554 }
1555
1556 result.appendFormat(" %s\n", name.string());
1557
1558 const Layer::State& layerState(getDrawingState());
John Reck8c3b6ac2017-08-24 10:25:42 -07001559 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001560 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
David Sodman4b7c4bc2017-11-17 12:13:59 -08001561 const Rect& frame = getBE().compositionInfo.hwc.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001562 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
David Sodman4b7c4bc2017-11-17 12:13:59 -08001563 const FloatRect& crop = getBE().compositionInfo.hwc.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001564 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 -07001565
1566 result.append("- - - - - - - - - - - - - - - - - - - - ");
1567 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1568}
Dan Stozae22aec72016-08-01 13:20:59 -07001569
Svetoslavd85084b2014-03-20 10:28:31 -07001570void Layer::dumpFrameStats(String8& result) const {
1571 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001572}
1573
Svetoslavd85084b2014-03-20 10:28:31 -07001574void Layer::clearFrameStats() {
1575 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001576}
1577
Jamie Gennis6547ff42013-07-16 20:12:42 -07001578void Layer::logFrameStats() {
1579 mFrameTracker.logAndResetStats(mName);
1580}
1581
Svetoslavd85084b2014-03-20 10:28:31 -07001582void Layer::getFrameStats(FrameStats* outStats) const {
1583 mFrameTracker.getStats(outStats);
1584}
1585
Brian Andersond6927fb2016-07-23 23:37:30 -07001586void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001587 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001588 Mutex::Autolock lock(mFrameEventHistoryMutex);
1589 mFrameEventHistory.checkFencesForCompletion();
1590 mFrameEventHistory.dump(result);
1591}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001592
Brian Anderson5ea5e592016-12-01 16:54:33 -08001593void Layer::onDisconnect() {
1594 Mutex::Autolock lock(mFrameEventHistoryMutex);
1595 mFrameEventHistory.onDisconnect();
1596}
1597
Brian Anderson3890c392016-07-25 12:48:08 -07001598void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001599 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001600 Mutex::Autolock lock(mFrameEventHistoryMutex);
1601 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001602 // If there are any unsignaled fences in the aquire timeline at this
1603 // point, the previously queued frame hasn't been latched yet. Go ahead
1604 // and try to get the signal time here so the syscall is taken out of
1605 // the main thread's critical path.
1606 mAcquireTimeline.updateSignalTimes();
1607 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001608 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001609 mFrameEventHistory.addQueue(*newTimestamps);
1610 }
1611
Brian Anderson3890c392016-07-25 12:48:08 -07001612 if (outDelta) {
1613 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001614 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001615}
Dan Stozae77c7662016-05-13 11:37:28 -07001616
Chia-I Wu98f1c102017-05-30 14:54:08 -07001617size_t Layer::getChildrenCount() const {
1618 size_t count = 0;
1619 for (const sp<Layer>& child : mCurrentChildren) {
1620 count += 1 + child->getChildrenCount();
1621 }
1622 return count;
1623}
1624
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625void Layer::addChild(const sp<Layer>& layer) {
1626 mCurrentChildren.add(layer);
1627 layer->setParent(this);
1628}
1629
1630ssize_t Layer::removeChild(const sp<Layer>& layer) {
1631 layer->setParent(nullptr);
1632 return mCurrentChildren.remove(layer);
1633}
1634
Robert Carr1db73f62016-12-21 12:58:51 -08001635bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1636 sp<Handle> handle = nullptr;
1637 sp<Layer> newParent = nullptr;
1638 if (newParentHandle == nullptr) {
1639 return false;
1640 }
1641 handle = static_cast<Handle*>(newParentHandle.get());
1642 newParent = handle->owner.promote();
1643 if (newParent == nullptr) {
1644 ALOGE("Unable to promote Layer handle");
1645 return false;
1646 }
1647
1648 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001649 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001650
1651 sp<Client> client(child->mClientRef.promote());
1652 if (client != nullptr) {
1653 client->setParentLayer(newParent);
1654 }
1655 }
1656 mCurrentChildren.clear();
1657
1658 return true;
1659}
1660
Robert Carr578038f2018-03-09 12:25:24 -08001661void Layer::reparentChildrenForDrawing(const sp<Layer>& newParent) {
1662 for (const sp<Layer>& child : mDrawingChildren) {
1663 child->mDrawingParent = newParent;
1664 }
1665}
1666
chaviwf1961f72017-09-18 16:41:07 -07001667bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1668 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001669 return false;
1670 }
1671
1672 auto handle = static_cast<Handle*>(newParentHandle.get());
1673 sp<Layer> newParent = handle->owner.promote();
1674 if (newParent == nullptr) {
1675 ALOGE("Unable to promote Layer handle");
1676 return false;
1677 }
1678
chaviwf1961f72017-09-18 16:41:07 -07001679 sp<Layer> parent = getParent();
1680 if (parent != nullptr) {
1681 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001682 }
chaviwf1961f72017-09-18 16:41:07 -07001683 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001684
chaviwf1961f72017-09-18 16:41:07 -07001685 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001686 sp<Client> newParentClient(newParent->mClientRef.promote());
1687
chaviwf1961f72017-09-18 16:41:07 -07001688 if (client != newParentClient) {
1689 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001690 }
1691
chaviw06178942017-07-27 10:25:59 -07001692 return true;
1693}
1694
Robert Carr9524cb32017-02-13 11:32:32 -08001695bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001696 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001697 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001698 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001699 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001700 client->detachLayer(child.get());
1701 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001702 }
Robert Carr7f619b22017-11-06 12:56:35 -08001703 }
Robert Carr9524cb32017-02-13 11:32:32 -08001704
1705 return true;
1706}
1707
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001709 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710}
1711
1712void Layer::clearSyncPoints() {
1713 for (const auto& child : mCurrentChildren) {
1714 child->clearSyncPoints();
1715 }
1716
1717 Mutex::Autolock lock(mLocalSyncPointMutex);
1718 for (auto& point : mLocalSyncPoints) {
1719 point->setFrameAvailable();
1720 }
1721 mLocalSyncPoints.clear();
1722}
1723
1724int32_t Layer::getZ() const {
1725 return mDrawingState.z;
1726}
1727
Robert Carr29abff82017-12-04 13:51:20 -08001728bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1729 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1730 const State& state = useDrawing ? mDrawingState : mCurrentState;
1731 return state.zOrderRelativeOf != nullptr;
1732}
1733
David Sodman41fdfc92017-11-06 16:09:56 -08001734__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001735 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001736 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1737 "makeTraversalList received invalid stateSet");
1738 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1739 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1740 const State& state = useDrawing ? mDrawingState : mCurrentState;
1741
Robert Carr29abff82017-12-04 13:51:20 -08001742 if (state.zOrderRelatives.size() == 0) {
1743 *outSkipRelativeZUsers = true;
1744 return children;
1745 }
1746
Robert Carrdb66e622017-04-10 16:55:57 -07001747 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001748 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001749 sp<Layer> strongRelative = weakRelative.promote();
1750 if (strongRelative != nullptr) {
1751 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001752 }
1753 }
1754
Dan Stoza412903f2017-04-27 13:42:17 -07001755 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001756 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1757 if (childState.zOrderRelativeOf != nullptr) {
1758 continue;
1759 }
Robert Carrdb66e622017-04-10 16:55:57 -07001760 traverse.add(child);
1761 }
1762
1763 return traverse;
1764}
1765
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766/**
Robert Carrdb66e622017-04-10 16:55:57 -07001767 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768 */
Dan Stoza412903f2017-04-27 13:42:17 -07001769void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001770 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1771 // produce a new list for traversing, including our relatives, and not including our children
1772 // who are relatives of another surface. In the case that there are no relative Z,
1773 // makeTraversalList returns our children directly to avoid significant overhead.
1774 // However in this case we need to take the responsibility for filtering children which
1775 // are relatives of another surface here.
1776 bool skipRelativeZUsers = false;
1777 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001778
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001780 for (; i < list.size(); i++) {
1781 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001782 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1783 continue;
1784 }
1785
Robert Carrdb66e622017-04-10 16:55:57 -07001786 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001788 }
Dan Stoza412903f2017-04-27 13:42:17 -07001789 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001790 }
Robert Carr29abff82017-12-04 13:51:20 -08001791
Dan Stoza412903f2017-04-27 13:42:17 -07001792 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001793 for (; i < list.size(); i++) {
1794 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001795
1796 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1797 continue;
1798 }
Dan Stoza412903f2017-04-27 13:42:17 -07001799 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800 }
1801}
1802
1803/**
Robert Carrdb66e622017-04-10 16:55:57 -07001804 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001805 */
Dan Stoza412903f2017-04-27 13:42:17 -07001806void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1807 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001808 // See traverseInZOrder for documentation.
1809 bool skipRelativeZUsers = false;
1810 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001811
Robert Carr1f0a16a2016-10-24 16:27:39 -07001812 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001813 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001814 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001815
1816 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1817 continue;
1818 }
1819
Robert Carrdb66e622017-04-10 16:55:57 -07001820 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821 break;
1822 }
Dan Stoza412903f2017-04-27 13:42:17 -07001823 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824 }
Dan Stoza412903f2017-04-27 13:42:17 -07001825 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001826 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001827 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001828
1829 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1830 continue;
1831 }
1832
Dan Stoza412903f2017-04-27 13:42:17 -07001833 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001834 }
1835}
1836
chaviwa76b2712017-09-20 12:02:26 -07001837/**
1838 * Traverse only children in z order, ignoring relative layers.
1839 */
1840void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1841 const LayerVector::Visitor& visitor) {
1842 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1843 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1844
1845 size_t i = 0;
1846 for (; i < children.size(); i++) {
1847 const auto& relative = children[i];
1848 if (relative->getZ() >= 0) {
1849 break;
1850 }
1851 relative->traverseChildrenInZOrder(stateSet, visitor);
1852 }
1853 visitor(this);
1854 for (; i < children.size(); i++) {
1855 const auto& relative = children[i];
1856 relative->traverseChildrenInZOrder(stateSet, visitor);
1857 }
1858}
1859
Robert Carr1f0a16a2016-10-24 16:27:39 -07001860Transform Layer::getTransform() const {
1861 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001862 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001863 if (p != nullptr) {
1864 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001865
1866 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1867 // it isFixedSize) then there may be additional scaling not accounted
1868 // for in the transform. We need to mirror this scaling in child surfaces
1869 // or we will break the contract where WM can treat child surfaces as
1870 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001871 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001872 int bufferWidth;
1873 int bufferHeight;
1874 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001875 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1876 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001877 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001878 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1879 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001880 }
David Sodman41fdfc92017-11-06 16:09:56 -08001881 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1882 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001883 Transform extraParentScaling;
1884 extraParentScaling.set(sx, 0, 0, sy);
1885 t = t * extraParentScaling;
1886 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001887 }
1888 return t * getDrawingState().active.transform;
1889}
1890
chaviw13fdc492017-06-27 12:40:18 -07001891half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001892 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001893
chaviw13fdc492017-06-27 12:40:18 -07001894 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1895 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001896}
Robert Carr6452f122017-03-21 10:41:29 -07001897
chaviw13fdc492017-06-27 12:40:18 -07001898half4 Layer::getColor() const {
1899 const half4 color(getDrawingState().color);
1900 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001901}
Robert Carr6452f122017-03-21 10:41:29 -07001902
Robert Carr1f0a16a2016-10-24 16:27:39 -07001903void Layer::commitChildList() {
1904 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1905 const auto& child = mCurrentChildren[i];
1906 child->commitChildList();
1907 }
1908 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001909 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910}
1911
chaviw1d044282017-09-27 12:19:28 -07001912void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1913 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1914 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1915 const State& state = useDrawing ? mDrawingState : mCurrentState;
1916
1917 Transform requestedTransform = state.active.transform;
1918 Transform transform = getTransform();
1919
1920 layerInfo->set_id(sequence);
1921 layerInfo->set_name(getName().c_str());
1922 layerInfo->set_type(String8(getTypeId()));
1923
1924 for (const auto& child : children) {
1925 layerInfo->add_children(child->sequence);
1926 }
1927
1928 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1929 sp<Layer> strongRelative = weakRelative.promote();
1930 if (strongRelative != nullptr) {
1931 layerInfo->add_relatives(strongRelative->sequence);
1932 }
1933 }
1934
1935 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1936 layerInfo->mutable_transparent_region());
1937 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1938 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1939
1940 layerInfo->set_layer_stack(getLayerStack());
1941 layerInfo->set_z(state.z);
1942
1943 PositionProto* position = layerInfo->mutable_position();
1944 position->set_x(transform.tx());
1945 position->set_y(transform.ty());
1946
1947 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1948 requestedPosition->set_x(requestedTransform.tx());
1949 requestedPosition->set_y(requestedTransform.ty());
1950
1951 SizeProto* size = layerInfo->mutable_size();
1952 size->set_w(state.active.w);
1953 size->set_h(state.active.h);
1954
1955 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1956 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1957
1958 layerInfo->set_is_opaque(isOpaque(state));
1959 layerInfo->set_invalidate(contentDirty);
1960 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1961 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1962 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1963 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1964 layerInfo->set_flags(state.flags);
1965
1966 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1967 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1968
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001969 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001970 if (parent != nullptr) {
1971 layerInfo->set_parent(parent->sequence);
1972 }
1973
1974 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1975 if (zOrderRelativeOf != nullptr) {
1976 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1977 }
1978
David Sodman0cc69182017-11-17 12:12:07 -08001979 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001980 if (buffer != nullptr) {
1981 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001982 }
1983
1984 layerInfo->set_queued_frames(getQueuedFrameCount());
1985 layerInfo->set_refresh_pending(isBufferLatched());
1986}
1987
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001988void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1989 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1990
1991 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1992
1993 const Rect& frame = hwcInfo.displayFrame;
1994 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1995
1996 const FloatRect& crop = hwcInfo.sourceCrop;
1997 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1998
1999 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2000 layerInfo->set_hwc_transform(transform);
2001}
2002
Mathias Agopian13127d82013-03-05 17:47:11 -08002003// ---------------------------------------------------------------------------
2004
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002006
2007#if defined(__gl_h_)
2008#error "don't include gl/gl.h in this file"
2009#endif
2010
2011#if defined(__gl2_h_)
2012#error "don't include gl2/gl2.h in this file"
2013#endif