blob: c520f54c36c746d076b8086a65766b6c863662c1 [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();
rongliucfb187b2018-03-14 12:26:23 -0700558 if (parentState.type >= 0 || parentState.appId >= 0) {
559 type = parentState.type;
560 appId = parentState.appId;
561 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400562 }
563
David Sodman4b7c4bc2017-11-17 12:13:59 -0800564 getBE().compositionInfo.hwc.type = type;
565 getBE().compositionInfo.hwc.appId = appId;
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800566
Mathias Agopian29a367b2011-07-12 14:51:45 -0700567 /*
568 * Transformations are applied in this order:
569 * 1) buffer orientation/flip/mirror
570 * 2) state transformation (window manager)
571 * 3) layer orientation (screen orientation)
572 * (NOTE: the matrices are multiplied in reverse order)
573 */
574
575 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700576 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700577
Robert Carrcae605c2017-03-29 12:10:31 -0700578 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700579 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700580 * the code below applies the primary display's inverse transform to the
581 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700582 */
David Sodman41fdfc92017-11-06 16:09:56 -0800583 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700584 // calculate the inverse transform
585 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800586 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700587 }
Robert Carrcae605c2017-03-29 12:10:31 -0700588
589 /*
590 * Here we cancel out the orientation component of the WM transform.
591 * The scaling and translate components are already included in our bounds
592 * computation so it's enough to just omit it in the composition.
593 * See comment in onDraw with ref to b/36727915 for why.
594 */
595 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700596 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700597
Jorim Jaggi5a963262018-01-17 15:57:44 +0100598 // STOPSHIP (b/72106793): If we have less than 25% scaling, HWC usually needs to use the rotator
599 // to handle it. However, there is one guaranteed frame of jank when we switch to using the
600 // rotator. In the meantime, we force GL composition instead until we have a better fix for the
601 // HWC issue.
602 bool extremeScaling = abs(t[0][0]) <= 0.25 || abs(t[1][1]) <= 0.25;
603
Mathias Agopian29a367b2011-07-12 14:51:45 -0700604 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800605 const uint32_t orientation = transform.getOrientation();
Jorim Jaggi5a963262018-01-17 15:57:44 +0100606 if (orientation & Transform::ROT_INVALID || extremeScaling) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800607 // we can only handle simple transformation
608 hwcInfo.forceClientComposition = true;
609 } else {
610 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800611 hwcInfo.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800612 getBE().compositionInfo.hwc.transform = transform;
613 }
614}
615
616void Layer::configureHwcLayer(const sp<const DisplayDevice>& displayDevice) {
617 ATRACE_CALL();
618
619 auto hwcId = displayDevice->getHwcDisplayId();
620 auto& hwcInfo = getBE().mHwcLayers[hwcId];
621 auto& hwcLayer = hwcInfo.layer;
622
623 auto error = (*hwcLayer)->setBlendMode(getBE().compositionInfo.hwc.blendMode);
624 ALOGE_IF(error != HWC2::Error::None,
625 "[%s] Failed to set blend mode %s:"
626 " %s (%d)",
627 mName.string(),
628 to_string(getBE().compositionInfo.hwc.blendMode).c_str(), to_string(error).c_str(),
629 static_cast<int32_t>(error));
630
631 error = (*hwcLayer)->setDisplayFrame(getBE().compositionInfo.hwc.displayFrame);
632 ALOGE_IF(error != HWC2::Error::None,
633 "[%s] Failed to set the display frame [%d, %d, %d, %d] %s (%d)",
634 mName.string(),
635 getBE().compositionInfo.hwc.displayFrame.left,
636 getBE().compositionInfo.hwc.displayFrame.right,
637 getBE().compositionInfo.hwc.displayFrame.top,
638 getBE().compositionInfo.hwc.displayFrame.bottom,
639 to_string(error).c_str(), static_cast<int32_t>(error));
640
641
642 error = (*hwcLayer)->setPlaneAlpha(getBE().compositionInfo.hwc.alpha);
643 ALOGE_IF(error != HWC2::Error::None,
644 "[%s] Failed to set plane alpha %.3f: "
645 "%s (%d)",
646 mName.string(), getBE().compositionInfo.hwc.alpha,
647 to_string(error).c_str(), static_cast<int32_t>(error));
648
649 error = (*hwcLayer)->setSourceCrop(getBE().compositionInfo.hwc.sourceCrop);
650 ALOGE_IF(error != HWC2::Error::None,
651 "[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: %s (%d)",
652 mName.string(),
653 getBE().compositionInfo.hwc.sourceCrop.left,
654 getBE().compositionInfo.hwc.sourceCrop.right,
655 getBE().compositionInfo.hwc.sourceCrop.top,
656 getBE().compositionInfo.hwc.sourceCrop.bottom,
657 to_string(error).c_str(), static_cast<int32_t>(error));
658
659 error = (*hwcLayer)->setTransform(getBE().compositionInfo.hwc.transform);
660 ALOGE_IF(error != HWC2::Error::None,
661 "[%s] Failed to set transform %s: "
662 "%s (%d)",
663 mName.string(),
664 to_string(getBE().compositionInfo.hwc.transform).c_str(), to_string(error).c_str(),
665 static_cast<int32_t>(error));
666
667 error = (*hwcLayer)->setZOrder(getBE().compositionInfo.hwc.z);
668 ALOGE_IF(error != HWC2::Error::None,
669 "[%s] Failed to set Z %u: %s (%d)",
670 mName.string(), getBE().compositionInfo.hwc.z,
671 to_string(error).c_str(), static_cast<int32_t>(error));
672
673 error = (*hwcLayer)->setInfo(getBE().compositionInfo.hwc.type, getBE().compositionInfo.hwc.appId);
674 ALOGE_IF(error != HWC2::Error::None,
675 "[%s] Failed to set info (%d)",
676 mName.string(), static_cast<int32_t>(error));
677
678 error = (*hwcLayer)->setVisibleRegion(getBE().compositionInfo.hwc.visibleRegion);
679 ALOGE_IF(error != HWC2::Error::None,
680 "[%s] Failed to set visible region: %s (%d)",
681 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
682
683 error = (*hwcLayer)->setSurfaceDamage(getBE().compositionInfo.hwc.surfaceDamage);
684 ALOGE_IF(error != HWC2::Error::None,
685 "[%s] Failed to set surface damage: %s (%d)",
686 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
687
688 error = (*hwcLayer)->setCompositionType(getBE().compositionInfo.compositionType);
689 ALOGE_IF(error != HWC2::Error::None,
690 "[%s] Failed to set composition type: %s (%d)",
691 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
692
693 error = (*hwcLayer)->setDataspace(getBE().compositionInfo.hwc.dataspace);
694 ALOGE_IF(error != HWC2::Error::None,
695 "[%s] Failed to set dataspace: %s (%d)",
696 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
697
698 error = (*hwcLayer)->setHdrMetadata(getBE().compositionInfo.hwc.hdrMetadata);
699 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
700 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
701 to_string(error).c_str(), static_cast<int32_t>(error));
702 }
703
704 error = (*hwcLayer)->setColor(getBE().compositionInfo.hwc.color);
705 ALOGE_IF(error != HWC2::Error::None,
706 "[%s] Failed to set color: %s (%d)",
707 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
708
709 if (getBE().compositionInfo.hwc.fence) {
710 error = (*hwcLayer)->setBuffer(getBE().compositionInfo.mBufferSlot,
711 getBE().compositionInfo.mBuffer, getBE().compositionInfo.hwc.fence);
David Sodman41fdfc92017-11-06 16:09:56 -0800712 ALOGE_IF(error != HWC2::Error::None,
David Sodman4b7c4bc2017-11-17 12:13:59 -0800713 "[%s] Failed to set buffer: %s (%d)",
714 mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700716}
717
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700719 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
721 return;
722 }
723
David Sodman6f65f3e2017-11-03 14:28:09 -0700724 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725}
Dan Stozaee44edd2015-03-23 15:50:23 -0700726
chaviwc9232ed2017-11-14 15:31:15 -0800727bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700728 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800729 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
730 return false;
731 }
732
David Sodman6f65f3e2017-11-03 14:28:09 -0700733 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800734}
735
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
737 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700738 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700739 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 return;
741 }
742
743 // This gives us only the "orientation" component of the transform
744 const State& s(getCurrentState());
745
746 // Apply the layer's transform, followed by the display's global transform
747 // Here we're guaranteed that the layer's transform preserves rects
748 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700749 if (!s.crop.isEmpty()) {
750 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800751 }
752 // Subtract the transparent region and snap to the bounds
753 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700754 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700756 if (!s.finalCrop.isEmpty()) {
757 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000758 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759 auto& displayTransform(displayDevice->getTransform());
760 auto position = displayTransform.transform(frame);
761
David Sodman5d89c1d2017-12-14 15:54:51 -0800762 auto error = (*getBE().mHwcLayers[hwcId].layer)->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700763 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800764 ALOGE_IF(error != HWC2::Error::None,
765 "[%s] Failed to set cursor position "
766 "to (%d, %d): %s (%d)",
767 mName.string(), position.left, position.top, to_string(error).c_str(),
768 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769}
Riley Andrews03414a12014-07-01 14:22:59 -0700770
Mathias Agopian13127d82013-03-05 17:47:11 -0800771// ---------------------------------------------------------------------------
772// drawing...
773// ---------------------------------------------------------------------------
774
chaviwa76b2712017-09-20 12:02:26 -0700775void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
776 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800777}
778
chaviwa76b2712017-09-20 12:02:26 -0700779void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
780 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800781}
782
chaviwa76b2712017-09-20 12:02:26 -0700783void Layer::draw(const RenderArea& renderArea) const {
784 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800785}
786
David Sodman41fdfc92017-11-06 16:09:56 -0800787void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
788 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800789 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700790 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700791 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700792 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800793}
794
chaviwa76b2712017-09-20 12:02:26 -0700795void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800796 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800797}
798
David Sodman41fdfc92017-11-06 16:09:56 -0800799void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700800 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800801 ALOGE("setCompositionType called without a valid HWC layer");
802 return;
803 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700804 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800805 auto& hwcLayer = hwcInfo.layer;
David Sodman5d89c1d2017-12-14 15:54:51 -0800806 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (*hwcLayer)->getId(), to_string(type).c_str(),
David Sodman41fdfc92017-11-06 16:09:56 -0800807 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800808 if (hwcInfo.compositionType != type) {
809 ALOGV(" actually setting");
810 hwcInfo.compositionType = type;
811 if (callIntoHwc) {
David Sodman5d89c1d2017-12-14 15:54:51 -0800812 auto error = (*hwcLayer)->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800813 ALOGE_IF(error != HWC2::Error::None,
814 "[%s] Failed to set "
815 "composition type %s: %s (%d)",
816 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
817 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800818 }
819 }
820}
821
822HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700823 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
824 // If we're querying the composition type for a display that does not
825 // have a HWC counterpart, then it will always be Client
826 return HWC2::Composition::Client;
827 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700828 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700829 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800830 return HWC2::Composition::Invalid;
831 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700832 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800833}
834
835void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700836 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800837 ALOGE("setClearClientTarget called without a valid HWC layer");
838 return;
839 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700840 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800841}
842
843bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700844 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800845 ALOGE("getClearClientTarget called without a valid HWC layer");
846 return false;
847 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700848 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800849}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800850
Dan Stozacac35382016-01-27 12:21:06 -0800851bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
852 if (point->getFrameNumber() <= mCurrentFrameNumber) {
853 // Don't bother with a SyncPoint, since we've already latched the
854 // relevant frame
855 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700856 }
857
Dan Stozacac35382016-01-27 12:21:06 -0800858 Mutex::Autolock lock(mLocalSyncPointMutex);
859 mLocalSyncPoints.push_back(point);
860 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700861}
862
Mathias Agopian13127d82013-03-05 17:47:11 -0800863void Layer::setFiltering(bool filtering) {
864 mFiltering = filtering;
865}
866
867bool Layer::getFiltering() const {
868 return mFiltering;
869}
870
Mathias Agopian13127d82013-03-05 17:47:11 -0800871// ----------------------------------------------------------------------------
872// local state
873// ----------------------------------------------------------------------------
874
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000875static void boundPoint(vec2* point, const Rect& crop) {
876 if (point->x < crop.left) {
877 point->x = crop.left;
878 }
879 if (point->x > crop.right) {
880 point->x = crop.right;
881 }
882 if (point->y < crop.top) {
883 point->y = crop.top;
884 }
885 if (point->y > crop.bottom) {
886 point->y = crop.bottom;
887 }
888}
889
chaviwa76b2712017-09-20 12:02:26 -0700890void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
891 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700892 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700893 const Transform renderAreaTransform(renderArea.getTransform());
894 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800895 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700896
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000897 vec2 lt = vec2(win.left, win.top);
898 vec2 lb = vec2(win.left, win.bottom);
899 vec2 rb = vec2(win.right, win.bottom);
900 vec2 rt = vec2(win.right, win.top);
901
Robert Carr1f0a16a2016-10-24 16:27:39 -0700902 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000903 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700904 lt = layerTransform.transform(lt);
905 lb = layerTransform.transform(lb);
906 rb = layerTransform.transform(rb);
907 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000908 }
909
Robert Carrb5d3d262016-03-25 15:08:13 -0700910 if (!s.finalCrop.isEmpty()) {
911 boundPoint(&lt, s.finalCrop);
912 boundPoint(&lb, s.finalCrop);
913 boundPoint(&rb, s.finalCrop);
914 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000915 }
916
Mathias Agopianff2ed702013-09-01 21:36:12 -0700917 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700918 position[0] = renderAreaTransform.transform(lt);
919 position[1] = renderAreaTransform.transform(lb);
920 position[2] = renderAreaTransform.transform(rb);
921 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800922 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700923 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800924 }
925}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800926
David Sodman41fdfc92017-11-06 16:09:56 -0800927bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700928 const Layer::State& s(mDrawingState);
929 return (s.flags & layer_state_t::eLayerSecure);
930}
931
Mathias Agopian13127d82013-03-05 17:47:11 -0800932void Layer::setVisibleRegion(const Region& visibleRegion) {
933 // always called from main thread
934 this->visibleRegion = visibleRegion;
935}
936
937void Layer::setCoveredRegion(const Region& coveredRegion) {
938 // always called from main thread
939 this->coveredRegion = coveredRegion;
940}
941
David Sodman41fdfc92017-11-06 16:09:56 -0800942void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 // always called from main thread
944 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
945}
946
Robert Carre5f4f692018-01-12 13:12:28 -0800947void Layer::clearVisibilityRegions() {
948 visibleRegion.clear();
949 visibleNonTransparentRegion.clear();
950 coveredRegion.clear();
951}
952
Mathias Agopian13127d82013-03-05 17:47:11 -0800953// ----------------------------------------------------------------------------
954// transaction
955// ----------------------------------------------------------------------------
956
Dan Stoza7dde5992015-05-22 09:51:44 -0700957void Layer::pushPendingState() {
958 if (!mCurrentState.modified) {
959 return;
960 }
961
Dan Stoza7dde5992015-05-22 09:51:44 -0700962 // If this transaction is waiting on the receipt of a frame, generate a sync
963 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800964 if (mCurrentState.barrierLayer != nullptr) {
965 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
966 if (barrierLayer == nullptr) {
967 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 // If we can't promote the layer we are intended to wait on,
969 // then it is expired or otherwise invalid. Allow this transaction
970 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800971 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800972 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800973 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800974 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800975 mRemoteSyncPoints.push_back(std::move(syncPoint));
976 } else {
977 // We already missed the frame we're supposed to synchronize
978 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800979 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800980 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700981 }
982
Dan Stoza7dde5992015-05-22 09:51:44 -0700983 // Wake us up to check if the frame has been received
984 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700985 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700986 }
987 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700988 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700989}
990
Pablo Ceballos05289c22016-04-14 15:49:55 -0700991void Layer::popPendingState(State* stateToCommit) {
992 auto oldFlags = stateToCommit->flags;
993 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800994 stateToCommit->flags =
995 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700996
997 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700998 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700999}
1000
Pablo Ceballos05289c22016-04-14 15:49:55 -07001001bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001002 bool stateUpdateAvailable = false;
1003 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001004 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001005 if (mRemoteSyncPoints.empty()) {
1006 // If we don't have a sync point for this, apply it anyway. It
1007 // will be visually wrong, but it should keep us from getting
1008 // into too much trouble.
1009 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001010 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001011 stateUpdateAvailable = true;
1012 continue;
1013 }
1014
David Sodman41fdfc92017-11-06 16:09:56 -08001015 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
1016 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -08001017
1018 // Signal our end of the sync point and then dispose of it
1019 mRemoteSyncPoints.front()->setTransactionApplied();
1020 mRemoteSyncPoints.pop_front();
1021 continue;
1022 }
1023
Dan Stoza7dde5992015-05-22 09:51:44 -07001024 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1025 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001026 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001027 stateUpdateAvailable = true;
1028
1029 // Signal our end of the sync point and then dispose of it
1030 mRemoteSyncPoints.front()->setTransactionApplied();
1031 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001032 } else {
1033 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001034 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001035 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001036 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001037 stateUpdateAvailable = true;
1038 }
1039 }
1040
1041 // If we still have pending updates, wake SurfaceFlinger back up and point
1042 // it at this layer so we can process them
1043 if (!mPendingStates.empty()) {
1044 setTransactionFlags(eTransactionNeeded);
1045 mFlinger->setTransactionFlags(eTraversalNeeded);
1046 }
1047
1048 mCurrentState.modified = false;
1049 return stateUpdateAvailable;
1050}
1051
Mathias Agopian13127d82013-03-05 17:47:11 -08001052uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001053 ATRACE_CALL();
1054
Dan Stoza7dde5992015-05-22 09:51:44 -07001055 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001056 Layer::State c = getCurrentState();
1057 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001058 return 0;
1059 }
1060
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001061 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062
David Sodman41fdfc92017-11-06 16:09:56 -08001063 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001064
David Sodmaneb085e02017-10-05 18:49:04 -07001065 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001066 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001067 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001068 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1069 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1070 " requested={ wh={%4u,%4u} }}\n"
1071 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1072 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001073 this, getName().string(), mCurrentTransform,
1074 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1075 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1076 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1077 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1078 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001080 // record the new size, form this point on, when the client request
1081 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001082 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001084
Robert Carre392b552017-09-19 12:16:05 -07001085 // Don't let Layer::doTransaction update the drawing state
1086 // if we have a pending resize, unless we are in fixed-size mode.
1087 // the drawing state will be updated only once we receive a buffer
1088 // with the correct size.
1089 //
1090 // In particular, we want to make sure the clip (which is part
1091 // of the geometry state) is latched together with the size but is
1092 // latched immediately when no resizing is involved.
1093 //
1094 // If a sideband stream is attached, however, we want to skip this
1095 // optimization so that transactions aren't missed when a buffer
1096 // never arrives
1097 //
1098 // In the case that we don't have a buffer we ignore other factors
1099 // and avoid entering the resizePending state. At a high level the
1100 // resizePending state is to avoid applying the state of the new buffer
1101 // to the old buffer. However in the state where we don't have an old buffer
1102 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001103 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001104 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001105 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001106 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001107 flags |= eDontUpdateGeometryState;
1108 }
1109 }
1110
Robert Carr7bf247e2017-05-18 14:02:49 -07001111 // Here we apply various requested geometry states, depending on our
1112 // latching configuration. See Layer.h for a detailed discussion of
1113 // how geometry latching is controlled.
1114 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001115 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001116
1117 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1118 // mode, which causes attributes which normally latch regardless of scaling mode,
1119 // to be delayed. We copy the requested state to the active state making sure
1120 // to respect these rules (again see Layer.h for a detailed discussion).
1121 //
1122 // There is an awkward asymmetry in the handling of the crop states in the position
1123 // states, as can be seen below. Largely this arises from position and transform
1124 // being stored in the same data structure while having different latching rules.
1125 // b/38182305
1126 //
1127 // Careful that "c" and editCurrentState may not begin as equivalent due to
1128 // applyPendingStates in the presence of deferred transactions.
1129 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001130 float tx = c.active.transform.tx();
1131 float ty = c.active.transform.ty();
1132 c.active = c.requested;
1133 c.active.transform.set(tx, ty);
1134 editCurrentState.active = c.active;
1135 } else {
1136 editCurrentState.active = editCurrentState.requested;
1137 c.active = c.requested;
1138 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001139 }
1140
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001141 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001142 // invalidate and recompute the visible regions if needed
1143 flags |= Layer::eVisibleRegion;
1144 }
1145
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001146 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001147 // invalidate and recompute the visible regions if needed
1148 flags |= eVisibleRegion;
1149 this->contentDirty = true;
1150
1151 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001152 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001153 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 }
1155
Dan Stozac8145172016-04-28 16:29:06 -07001156 // If the layer is hidden, signal and clear out all local sync points so
1157 // that transactions for layers depending on this layer's frames becoming
1158 // visible are not blocked
1159 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001160 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001161 }
1162
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001164 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166}
1167
Pablo Ceballos05289c22016-04-14 15:49:55 -07001168void Layer::commitTransaction(const State& stateToCommit) {
1169 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001170}
1171
Mathias Agopian13127d82013-03-05 17:47:11 -08001172uint32_t Layer::getTransactionFlags(uint32_t flags) {
1173 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1174}
1175
1176uint32_t Layer::setTransactionFlags(uint32_t flags) {
1177 return android_atomic_or(flags, &mTransactionFlags);
1178}
1179
Robert Carr82364e32016-05-15 11:27:47 -07001180bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001181 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001182 return false;
1183 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001184
1185 // We update the requested and active position simultaneously because
1186 // we want to apply the position portion of the transform matrix immediately,
1187 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001188 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001189 if (immediate && !mFreezeGeometryUpdates) {
1190 // Here we directly update the active state
1191 // unlike other setters, because we store it within
1192 // the transform, but use different latching rules.
1193 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001194 mCurrentState.active.transform.set(x, y);
1195 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001196 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001197
Dan Stoza7dde5992015-05-22 09:51:44 -07001198 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001199 setTransactionFlags(eTransactionNeeded);
1200 return true;
1201}
Robert Carr82364e32016-05-15 11:27:47 -07001202
Robert Carr1f0a16a2016-10-24 16:27:39 -07001203bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1204 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1205 if (idx < 0) {
1206 return false;
1207 }
1208 if (childLayer->setLayer(z)) {
1209 mCurrentChildren.removeAt(idx);
1210 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001211 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001212 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001213 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001214}
1215
Robert Carr503c7042017-09-27 15:06:08 -07001216bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1217 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1218 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1219 if (idx < 0) {
1220 return false;
1221 }
1222 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1223 mCurrentChildren.removeAt(idx);
1224 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001225 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001226 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001227 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001228}
1229
Robert Carrae060832016-11-28 10:51:00 -08001230bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001231 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 mCurrentState.sequence++;
1233 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001234 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001235
1236 // Discard all relative layering.
1237 if (mCurrentState.zOrderRelativeOf != nullptr) {
1238 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1239 if (strongRelative != nullptr) {
1240 strongRelative->removeZOrderRelative(this);
1241 }
1242 mCurrentState.zOrderRelativeOf = nullptr;
1243 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 setTransactionFlags(eTransactionNeeded);
1245 return true;
1246}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001247
Robert Carrdb66e622017-04-10 16:55:57 -07001248void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1249 mCurrentState.zOrderRelatives.remove(relative);
1250 mCurrentState.sequence++;
1251 mCurrentState.modified = true;
1252 setTransactionFlags(eTransactionNeeded);
1253}
1254
1255void Layer::addZOrderRelative(const wp<Layer>& relative) {
1256 mCurrentState.zOrderRelatives.add(relative);
1257 mCurrentState.modified = true;
1258 mCurrentState.sequence++;
1259 setTransactionFlags(eTransactionNeeded);
1260}
1261
Robert Carr503d2bd2017-12-04 15:49:47 -08001262bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001263 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1264 if (handle == nullptr) {
1265 return false;
1266 }
1267 sp<Layer> relative = handle->owner.promote();
1268 if (relative == nullptr) {
1269 return false;
1270 }
1271
Robert Carr503d2bd2017-12-04 15:49:47 -08001272 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1273 mCurrentState.zOrderRelativeOf == relative) {
1274 return false;
1275 }
1276
Robert Carrdb66e622017-04-10 16:55:57 -07001277 mCurrentState.sequence++;
1278 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001279 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001280
chaviw9ab4bd12017-11-03 13:11:00 -07001281 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1282 if (oldZOrderRelativeOf != nullptr) {
1283 oldZOrderRelativeOf->removeZOrderRelative(this);
1284 }
Robert Carrdb66e622017-04-10 16:55:57 -07001285 mCurrentState.zOrderRelativeOf = relative;
1286 relative->addZOrderRelative(this);
1287
1288 setTransactionFlags(eTransactionNeeded);
1289
1290 return true;
1291}
1292
Mathias Agopian13127d82013-03-05 17:47:11 -08001293bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001294 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001295 mCurrentState.requested.w = w;
1296 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001297 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001298 setTransactionFlags(eTransactionNeeded);
1299 return true;
1300}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001301bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001302 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001303 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001304 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001305 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001306 setTransactionFlags(eTransactionNeeded);
1307 return true;
1308}
chaviw13fdc492017-06-27 12:40:18 -07001309
1310bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001311 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1312 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001313 return false;
1314
1315 mCurrentState.sequence++;
1316 mCurrentState.color.r = color.r;
1317 mCurrentState.color.g = color.g;
1318 mCurrentState.color.b = color.b;
1319 mCurrentState.modified = true;
1320 setTransactionFlags(eTransactionNeeded);
1321 return true;
1322}
1323
Mathias Agopian13127d82013-03-05 17:47:11 -08001324bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1325 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001326 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001327 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001328 setTransactionFlags(eTransactionNeeded);
1329 return true;
1330}
1331bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001332 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001333 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001334 setTransactionFlags(eTransactionNeeded);
1335 return true;
1336}
1337bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1338 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001339 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001340 mCurrentState.sequence++;
1341 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001342 mCurrentState.mask = mask;
1343 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 setTransactionFlags(eTransactionNeeded);
1345 return true;
1346}
Robert Carr99e27f02016-06-16 15:18:02 -07001347
1348bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001349 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001350 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001351 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001352 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001353 mCurrentState.crop = crop;
1354 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001355 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1356
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001358 setTransactionFlags(eTransactionNeeded);
1359 return true;
1360}
Robert Carr8d5227b2017-03-16 15:41:03 -07001361
1362bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001363 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001364 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001365 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001366 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001367 mCurrentState.finalCrop = crop;
1368 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001369 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1370
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001371 mCurrentState.modified = true;
1372 setTransactionFlags(eTransactionNeeded);
1373 return true;
1374}
Mathias Agopian13127d82013-03-05 17:47:11 -08001375
Robert Carrc3574f72016-03-24 12:19:32 -07001376bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001377 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001378 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001379 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001380 return true;
1381}
1382
rongliucfb187b2018-03-14 12:26:23 -07001383void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001384 mCurrentState.appId = appId;
1385 mCurrentState.type = type;
1386 mCurrentState.modified = true;
1387 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001388}
1389
Mathias Agopian13127d82013-03-05 17:47:11 -08001390bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001391 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001392 mCurrentState.sequence++;
1393 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001394 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001395 setTransactionFlags(eTransactionNeeded);
1396 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001397}
1398
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001399bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001400 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001401 mCurrentState.sequence++;
1402 mCurrentState.dataSpace = dataSpace;
1403 mCurrentState.modified = true;
1404 setTransactionFlags(eTransactionNeeded);
1405 return true;
1406}
1407
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001408android_dataspace Layer::getDataSpace() const {
1409 return mCurrentState.dataSpace;
1410}
1411
Robert Carr1f0a16a2016-10-24 16:27:39 -07001412uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001413 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001414 if (p == nullptr) {
1415 return getDrawingState().layerStack;
1416 }
1417 return p->getLayerStack();
1418}
1419
David Sodman41fdfc92017-11-06 16:09:56 -08001420void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001421 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001422 mCurrentState.frameNumber = frameNumber;
1423 // We don't set eTransactionNeeded, because just receiving a deferral
1424 // request without any other state updates shouldn't actually induce a delay
1425 mCurrentState.modified = true;
1426 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001427 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001428 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001429 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001430}
1431
David Sodman41fdfc92017-11-06 16:09:56 -08001432void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001433 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1434 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001435}
1436
Dan Stozaee44edd2015-03-23 15:50:23 -07001437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438// ----------------------------------------------------------------------------
1439// pageflip handling...
1440// ----------------------------------------------------------------------------
1441
Robert Carr1f0a16a2016-10-24 16:27:39 -07001442bool Layer::isHiddenByPolicy() const {
1443 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001444 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001445 if (parent != nullptr && parent->isHiddenByPolicy()) {
1446 return true;
1447 }
1448 return s.flags & layer_state_t::eLayerHidden;
1449}
1450
David Sodman41fdfc92017-11-06 16:09:56 -08001451uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001452 // TODO: should we do something special if mSecure is set?
1453 if (mProtectedByApp) {
1454 // need a hardware-protected path to external video sink
1455 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001456 }
Riley Andrews03414a12014-07-01 14:22:59 -07001457 if (mPotentialCursor) {
1458 usage |= GraphicBuffer::USAGE_CURSOR;
1459 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001460 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001461 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001462}
1463
Mathias Agopian84300952012-11-21 16:02:13 -08001464void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001465 uint32_t orientation = 0;
1466 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001467 // The transform hint is used to improve performance, but we can
1468 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001469 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001470 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001471 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001472 if (orientation & Transform::ROT_INVALID) {
1473 orientation = 0;
1474 }
1475 }
David Sodmaneb085e02017-10-05 18:49:04 -07001476 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001477}
1478
Mathias Agopian13127d82013-03-05 17:47:11 -08001479// ----------------------------------------------------------------------------
1480// debugging
1481// ----------------------------------------------------------------------------
1482
Kalle Raitaa099a242017-01-11 11:17:29 -08001483LayerDebugInfo Layer::getLayerDebugInfo() const {
1484 LayerDebugInfo info;
1485 const Layer::State& ds = getDrawingState();
1486 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001487 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001488 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1489 info.mType = String8(getTypeId());
1490 info.mTransparentRegion = ds.activeTransparentRegion;
1491 info.mVisibleRegion = visibleRegion;
1492 info.mSurfaceDamageRegion = surfaceDamageRegion;
1493 info.mLayerStack = getLayerStack();
1494 info.mX = ds.active.transform.tx();
1495 info.mY = ds.active.transform.ty();
1496 info.mZ = ds.z;
1497 info.mWidth = ds.active.w;
1498 info.mHeight = ds.active.h;
1499 info.mCrop = ds.crop;
1500 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001501 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001502 info.mFlags = ds.flags;
1503 info.mPixelFormat = getPixelFormat();
1504 info.mDataSpace = getDataSpace();
1505 info.mMatrix[0][0] = ds.active.transform[0][0];
1506 info.mMatrix[0][1] = ds.active.transform[0][1];
1507 info.mMatrix[1][0] = ds.active.transform[1][0];
1508 info.mMatrix[1][1] = ds.active.transform[1][1];
1509 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001510 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001511 if (buffer != 0) {
1512 info.mActiveBufferWidth = buffer->getWidth();
1513 info.mActiveBufferHeight = buffer->getHeight();
1514 info.mActiveBufferStride = buffer->getStride();
1515 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001516 } else {
1517 info.mActiveBufferWidth = 0;
1518 info.mActiveBufferHeight = 0;
1519 info.mActiveBufferStride = 0;
1520 info.mActiveBufferFormat = 0;
1521 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001522 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001523 info.mNumQueuedFrames = getQueuedFrameCount();
1524 info.mRefreshPending = isBufferLatched();
1525 info.mIsOpaque = isOpaque(ds);
1526 info.mContentDirty = contentDirty;
1527 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001528}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001529
Dan Stozae22aec72016-08-01 13:20:59 -07001530void Layer::miniDumpHeader(String8& result) {
1531 result.append("----------------------------------------");
1532 result.append("---------------------------------------\n");
1533 result.append(" Layer name\n");
1534 result.append(" Z | ");
1535 result.append(" Comp Type | ");
1536 result.append(" Disp Frame (LTRB) | ");
1537 result.append(" Source Crop (LTRB)\n");
1538 result.append("----------------------------------------");
1539 result.append("---------------------------------------\n");
1540}
1541
1542void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001543 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001544 return;
1545 }
1546
1547 String8 name;
1548 if (mName.length() > 77) {
1549 std::string shortened;
1550 shortened.append(mName.string(), 36);
1551 shortened.append("[...]");
1552 shortened.append(mName.string() + (mName.length() - 36), 36);
1553 name = shortened.c_str();
1554 } else {
1555 name = mName;
1556 }
1557
1558 result.appendFormat(" %s\n", name.string());
1559
1560 const Layer::State& layerState(getDrawingState());
John Reck8c3b6ac2017-08-24 10:25:42 -07001561 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001562 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
David Sodman4b7c4bc2017-11-17 12:13:59 -08001563 const Rect& frame = getBE().compositionInfo.hwc.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001564 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
David Sodman4b7c4bc2017-11-17 12:13:59 -08001565 const FloatRect& crop = getBE().compositionInfo.hwc.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001566 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 -07001567
1568 result.append("- - - - - - - - - - - - - - - - - - - - ");
1569 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1570}
Dan Stozae22aec72016-08-01 13:20:59 -07001571
Svetoslavd85084b2014-03-20 10:28:31 -07001572void Layer::dumpFrameStats(String8& result) const {
1573 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001574}
1575
Svetoslavd85084b2014-03-20 10:28:31 -07001576void Layer::clearFrameStats() {
1577 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001578}
1579
Jamie Gennis6547ff42013-07-16 20:12:42 -07001580void Layer::logFrameStats() {
1581 mFrameTracker.logAndResetStats(mName);
1582}
1583
Svetoslavd85084b2014-03-20 10:28:31 -07001584void Layer::getFrameStats(FrameStats* outStats) const {
1585 mFrameTracker.getStats(outStats);
1586}
1587
Brian Andersond6927fb2016-07-23 23:37:30 -07001588void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001589 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001590 Mutex::Autolock lock(mFrameEventHistoryMutex);
1591 mFrameEventHistory.checkFencesForCompletion();
1592 mFrameEventHistory.dump(result);
1593}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001594
Brian Anderson5ea5e592016-12-01 16:54:33 -08001595void Layer::onDisconnect() {
1596 Mutex::Autolock lock(mFrameEventHistoryMutex);
1597 mFrameEventHistory.onDisconnect();
1598}
1599
Brian Anderson3890c392016-07-25 12:48:08 -07001600void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001601 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001602 Mutex::Autolock lock(mFrameEventHistoryMutex);
1603 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001604 // If there are any unsignaled fences in the aquire timeline at this
1605 // point, the previously queued frame hasn't been latched yet. Go ahead
1606 // and try to get the signal time here so the syscall is taken out of
1607 // the main thread's critical path.
1608 mAcquireTimeline.updateSignalTimes();
1609 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001610 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001611 mFrameEventHistory.addQueue(*newTimestamps);
1612 }
1613
Brian Anderson3890c392016-07-25 12:48:08 -07001614 if (outDelta) {
1615 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001616 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001617}
Dan Stozae77c7662016-05-13 11:37:28 -07001618
Chia-I Wu98f1c102017-05-30 14:54:08 -07001619size_t Layer::getChildrenCount() const {
1620 size_t count = 0;
1621 for (const sp<Layer>& child : mCurrentChildren) {
1622 count += 1 + child->getChildrenCount();
1623 }
1624 return count;
1625}
1626
Robert Carr1f0a16a2016-10-24 16:27:39 -07001627void Layer::addChild(const sp<Layer>& layer) {
1628 mCurrentChildren.add(layer);
1629 layer->setParent(this);
1630}
1631
1632ssize_t Layer::removeChild(const sp<Layer>& layer) {
1633 layer->setParent(nullptr);
1634 return mCurrentChildren.remove(layer);
1635}
1636
Robert Carr1db73f62016-12-21 12:58:51 -08001637bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1638 sp<Handle> handle = nullptr;
1639 sp<Layer> newParent = nullptr;
1640 if (newParentHandle == nullptr) {
1641 return false;
1642 }
1643 handle = static_cast<Handle*>(newParentHandle.get());
1644 newParent = handle->owner.promote();
1645 if (newParent == nullptr) {
1646 ALOGE("Unable to promote Layer handle");
1647 return false;
1648 }
1649
1650 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001651 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001652
1653 sp<Client> client(child->mClientRef.promote());
1654 if (client != nullptr) {
1655 client->setParentLayer(newParent);
1656 }
1657 }
1658 mCurrentChildren.clear();
1659
1660 return true;
1661}
1662
Robert Carr578038f2018-03-09 12:25:24 -08001663void Layer::reparentChildrenForDrawing(const sp<Layer>& newParent) {
1664 for (const sp<Layer>& child : mDrawingChildren) {
1665 child->mDrawingParent = newParent;
1666 }
1667}
1668
chaviwf1961f72017-09-18 16:41:07 -07001669bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1670 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001671 return false;
1672 }
1673
1674 auto handle = static_cast<Handle*>(newParentHandle.get());
1675 sp<Layer> newParent = handle->owner.promote();
1676 if (newParent == nullptr) {
1677 ALOGE("Unable to promote Layer handle");
1678 return false;
1679 }
1680
chaviwf1961f72017-09-18 16:41:07 -07001681 sp<Layer> parent = getParent();
1682 if (parent != nullptr) {
1683 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001684 }
chaviwf1961f72017-09-18 16:41:07 -07001685 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001686
chaviwf1961f72017-09-18 16:41:07 -07001687 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001688 sp<Client> newParentClient(newParent->mClientRef.promote());
1689
chaviwf1961f72017-09-18 16:41:07 -07001690 if (client != newParentClient) {
1691 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001692 }
1693
chaviw06178942017-07-27 10:25:59 -07001694 return true;
1695}
1696
Robert Carr9524cb32017-02-13 11:32:32 -08001697bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001698 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001699 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001700 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001701 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001702 client->detachLayer(child.get());
1703 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001704 }
Robert Carr7f619b22017-11-06 12:56:35 -08001705 }
Robert Carr9524cb32017-02-13 11:32:32 -08001706
1707 return true;
1708}
1709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001711 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712}
1713
1714void Layer::clearSyncPoints() {
1715 for (const auto& child : mCurrentChildren) {
1716 child->clearSyncPoints();
1717 }
1718
1719 Mutex::Autolock lock(mLocalSyncPointMutex);
1720 for (auto& point : mLocalSyncPoints) {
1721 point->setFrameAvailable();
1722 }
1723 mLocalSyncPoints.clear();
1724}
1725
1726int32_t Layer::getZ() const {
1727 return mDrawingState.z;
1728}
1729
Robert Carr29abff82017-12-04 13:51:20 -08001730bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1731 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1732 const State& state = useDrawing ? mDrawingState : mCurrentState;
1733 return state.zOrderRelativeOf != nullptr;
1734}
1735
David Sodman41fdfc92017-11-06 16:09:56 -08001736__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001737 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001738 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1739 "makeTraversalList received invalid stateSet");
1740 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1741 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1742 const State& state = useDrawing ? mDrawingState : mCurrentState;
1743
Robert Carr29abff82017-12-04 13:51:20 -08001744 if (state.zOrderRelatives.size() == 0) {
1745 *outSkipRelativeZUsers = true;
1746 return children;
1747 }
1748
Robert Carrdb66e622017-04-10 16:55:57 -07001749 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001750 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001751 sp<Layer> strongRelative = weakRelative.promote();
1752 if (strongRelative != nullptr) {
1753 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001754 }
1755 }
1756
Dan Stoza412903f2017-04-27 13:42:17 -07001757 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001758 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1759 if (childState.zOrderRelativeOf != nullptr) {
1760 continue;
1761 }
Robert Carrdb66e622017-04-10 16:55:57 -07001762 traverse.add(child);
1763 }
1764
1765 return traverse;
1766}
1767
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768/**
Robert Carrdb66e622017-04-10 16:55:57 -07001769 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 */
Dan Stoza412903f2017-04-27 13:42:17 -07001771void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001772 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1773 // produce a new list for traversing, including our relatives, and not including our children
1774 // who are relatives of another surface. In the case that there are no relative Z,
1775 // makeTraversalList returns our children directly to avoid significant overhead.
1776 // However in this case we need to take the responsibility for filtering children which
1777 // are relatives of another surface here.
1778 bool skipRelativeZUsers = false;
1779 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001780
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001782 for (; i < list.size(); i++) {
1783 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001784 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1785 continue;
1786 }
1787
Robert Carrdb66e622017-04-10 16:55:57 -07001788 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001790 }
Dan Stoza412903f2017-04-27 13:42:17 -07001791 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 }
Robert Carr29abff82017-12-04 13:51:20 -08001793
Dan Stoza412903f2017-04-27 13:42:17 -07001794 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001795 for (; i < list.size(); i++) {
1796 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001797
1798 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1799 continue;
1800 }
Dan Stoza412903f2017-04-27 13:42:17 -07001801 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001802 }
1803}
1804
1805/**
Robert Carrdb66e622017-04-10 16:55:57 -07001806 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807 */
Dan Stoza412903f2017-04-27 13:42:17 -07001808void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1809 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001810 // See traverseInZOrder for documentation.
1811 bool skipRelativeZUsers = false;
1812 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001813
Robert Carr1f0a16a2016-10-24 16:27:39 -07001814 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001815 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001816 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001817
1818 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1819 continue;
1820 }
1821
Robert Carrdb66e622017-04-10 16:55:57 -07001822 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 break;
1824 }
Dan Stoza412903f2017-04-27 13:42:17 -07001825 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 }
Dan Stoza412903f2017-04-27 13:42:17 -07001827 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001828 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001829 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001830
1831 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1832 continue;
1833 }
1834
Dan Stoza412903f2017-04-27 13:42:17 -07001835 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001836 }
1837}
1838
chaviwa76b2712017-09-20 12:02:26 -07001839/**
1840 * Traverse only children in z order, ignoring relative layers.
1841 */
1842void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1843 const LayerVector::Visitor& visitor) {
1844 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1845 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1846
1847 size_t i = 0;
1848 for (; i < children.size(); i++) {
1849 const auto& relative = children[i];
1850 if (relative->getZ() >= 0) {
1851 break;
1852 }
1853 relative->traverseChildrenInZOrder(stateSet, visitor);
1854 }
1855 visitor(this);
1856 for (; i < children.size(); i++) {
1857 const auto& relative = children[i];
1858 relative->traverseChildrenInZOrder(stateSet, visitor);
1859 }
1860}
1861
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862Transform Layer::getTransform() const {
1863 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001864 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001865 if (p != nullptr) {
1866 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001867
1868 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1869 // it isFixedSize) then there may be additional scaling not accounted
1870 // for in the transform. We need to mirror this scaling in child surfaces
1871 // or we will break the contract where WM can treat child surfaces as
1872 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001873 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001874 int bufferWidth;
1875 int bufferHeight;
1876 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001877 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1878 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001879 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001880 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1881 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001882 }
David Sodman41fdfc92017-11-06 16:09:56 -08001883 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1884 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001885 Transform extraParentScaling;
1886 extraParentScaling.set(sx, 0, 0, sy);
1887 t = t * extraParentScaling;
1888 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889 }
1890 return t * getDrawingState().active.transform;
1891}
1892
chaviw13fdc492017-06-27 12:40:18 -07001893half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001894 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001895
chaviw13fdc492017-06-27 12:40:18 -07001896 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1897 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001898}
Robert Carr6452f122017-03-21 10:41:29 -07001899
chaviw13fdc492017-06-27 12:40:18 -07001900half4 Layer::getColor() const {
1901 const half4 color(getDrawingState().color);
1902 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001903}
Robert Carr6452f122017-03-21 10:41:29 -07001904
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905void Layer::commitChildList() {
1906 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1907 const auto& child = mCurrentChildren[i];
1908 child->commitChildList();
1909 }
1910 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001911 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001912}
1913
chaviw1d044282017-09-27 12:19:28 -07001914void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1915 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1916 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1917 const State& state = useDrawing ? mDrawingState : mCurrentState;
1918
1919 Transform requestedTransform = state.active.transform;
1920 Transform transform = getTransform();
1921
1922 layerInfo->set_id(sequence);
1923 layerInfo->set_name(getName().c_str());
1924 layerInfo->set_type(String8(getTypeId()));
1925
1926 for (const auto& child : children) {
1927 layerInfo->add_children(child->sequence);
1928 }
1929
1930 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1931 sp<Layer> strongRelative = weakRelative.promote();
1932 if (strongRelative != nullptr) {
1933 layerInfo->add_relatives(strongRelative->sequence);
1934 }
1935 }
1936
1937 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1938 layerInfo->mutable_transparent_region());
1939 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1940 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1941
1942 layerInfo->set_layer_stack(getLayerStack());
1943 layerInfo->set_z(state.z);
1944
1945 PositionProto* position = layerInfo->mutable_position();
1946 position->set_x(transform.tx());
1947 position->set_y(transform.ty());
1948
1949 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1950 requestedPosition->set_x(requestedTransform.tx());
1951 requestedPosition->set_y(requestedTransform.ty());
1952
1953 SizeProto* size = layerInfo->mutable_size();
1954 size->set_w(state.active.w);
1955 size->set_h(state.active.h);
1956
1957 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1958 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1959
1960 layerInfo->set_is_opaque(isOpaque(state));
1961 layerInfo->set_invalidate(contentDirty);
1962 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1963 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1964 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1965 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1966 layerInfo->set_flags(state.flags);
1967
1968 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1969 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1970
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001971 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001972 if (parent != nullptr) {
1973 layerInfo->set_parent(parent->sequence);
1974 }
1975
1976 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1977 if (zOrderRelativeOf != nullptr) {
1978 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1979 }
1980
David Sodman0cc69182017-11-17 12:12:07 -08001981 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001982 if (buffer != nullptr) {
1983 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001984 }
1985
1986 layerInfo->set_queued_frames(getQueuedFrameCount());
1987 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001988 layerInfo->set_window_type(state.type);
1989 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001990}
1991
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001992void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1993 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1994
1995 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1996
1997 const Rect& frame = hwcInfo.displayFrame;
1998 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1999
2000 const FloatRect& crop = hwcInfo.sourceCrop;
2001 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2002
2003 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2004 layerInfo->set_hwc_transform(transform);
2005}
2006
Mathias Agopian13127d82013-03-05 17:47:11 -08002007// ---------------------------------------------------------------------------
2008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002009}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002010
2011#if defined(__gl_h_)
2012#error "don't include gl/gl.h in this file"
2013#endif
2014
2015#if defined(__gl2_h_)
2016#error "don't include gl2/gl2.h in this file"
2017#endif