blob: 615270fc5095d650a1516f5ec864e0315f777fc6 [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
Chia-I Wu30505fb2018-03-26 16:20:31 -0700493 auto& hwcLayer = hwcInfo.layer;
494
Mathias Agopian13127d82013-03-05 17:47:11 -0800495 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700496 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500497 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700498 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800499 blendMode =
500 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700502 auto error = (*hwcLayer)->setBlendMode(blendMode);
503 ALOGE_IF(error != HWC2::Error::None,
504 "[%s] Failed to set blend mode %s:"
505 " %s (%d)",
506 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
507 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800508
509 // apply the layer's transform, followed by the display's global transform
510 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700511 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700513 if (!s.crop.isEmpty()) {
514 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700515 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800516 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000517 activeCrop.clear();
518 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700519 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800520 // This needs to be here as transform.transform(Rect) computes the
521 // transformed rect and then takes the bounding box of the result before
522 // returning. This means
523 // transform.inverse().transform(transform.transform(Rect)) != Rect
524 // in which case we need to make sure the final rect is clipped to the
525 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800526 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000527 activeCrop.clear();
528 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700529 // mark regions outside the crop as transparent
530 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800531 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
532 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
533 activeTransparentRegion.orSelf(
534 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700536
Dan Stoza80d61162017-12-20 15:57:52 -0800537 // computeBounds returns a FloatRect to provide more accuracy during the
538 // transformation. We then round upon constructing 'frame'.
539 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700540 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800541 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 frame.clear();
543 }
544 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000545 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
546 frame.clear();
547 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800548 const Transform& tr(displayDevice->getTransform());
549 Rect transformedFrame = tr.transform(frame);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700550 error = (*hwcLayer)->setDisplayFrame(transformedFrame);
551 if (error != HWC2::Error::None) {
552 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
553 transformedFrame.left, transformedFrame.top, transformedFrame.right,
554 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
555 } else {
556 hwcInfo.displayFrame = transformedFrame;
557 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558
Dan Stoza5a423ea2017-02-16 14:10:39 -0800559 FloatRect sourceCrop = computeCrop(displayDevice);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700560 error = (*hwcLayer)->setSourceCrop(sourceCrop);
561 if (error != HWC2::Error::None) {
562 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
563 "%s (%d)",
564 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
565 to_string(error).c_str(), static_cast<int32_t>(error));
566 } else {
567 hwcInfo.sourceCrop = sourceCrop;
568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
chaviw13fdc492017-06-27 12:40:18 -0700570 float alpha = static_cast<float>(getAlpha());
Chia-I Wu30505fb2018-03-26 16:20:31 -0700571 error = (*hwcLayer)->setPlaneAlpha(alpha);
572 ALOGE_IF(error != HWC2::Error::None,
573 "[%s] Failed to set plane alpha %.3f: "
574 "%s (%d)",
575 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
Chia-I Wu30505fb2018-03-26 16:20:31 -0700577 error = (*hwcLayer)->setZOrder(z);
578 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
579 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500580
Albert Chaulk2a589632017-05-04 16:59:44 -0400581 int type = s.type;
582 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700583 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400584 if (parent.get()) {
585 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700586 if (parentState.type >= 0 || parentState.appId >= 0) {
587 type = parentState.type;
588 appId = parentState.appId;
589 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400590 }
591
Chia-I Wu30505fb2018-03-26 16:20:31 -0700592 error = (*hwcLayer)->setInfo(type, appId);
593 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
594 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800595
Mathias Agopian29a367b2011-07-12 14:51:45 -0700596 /*
597 * Transformations are applied in this order:
598 * 1) buffer orientation/flip/mirror
599 * 2) state transformation (window manager)
600 * 3) layer orientation (screen orientation)
601 * (NOTE: the matrices are multiplied in reverse order)
602 */
603
604 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700605 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700606
Robert Carrcae605c2017-03-29 12:10:31 -0700607 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700609 * the code below applies the primary display's inverse transform to the
610 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700611 */
David Sodman41fdfc92017-11-06 16:09:56 -0800612 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 // calculate the inverse transform
614 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800615 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 }
Robert Carrcae605c2017-03-29 12:10:31 -0700617
618 /*
619 * Here we cancel out the orientation component of the WM transform.
620 * The scaling and translate components are already included in our bounds
621 * computation so it's enough to just omit it in the composition.
622 * See comment in onDraw with ref to b/36727915 for why.
623 */
624 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700625 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700626
Jorim Jaggi5a963262018-01-17 15:57:44 +0100627 // STOPSHIP (b/72106793): If we have less than 25% scaling, HWC usually needs to use the rotator
628 // to handle it. However, there is one guaranteed frame of jank when we switch to using the
629 // rotator. In the meantime, we force GL composition instead until we have a better fix for the
630 // HWC issue.
631 bool extremeScaling = abs(t[0][0]) <= 0.25 || abs(t[1][1]) <= 0.25;
632
Mathias Agopian29a367b2011-07-12 14:51:45 -0700633 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800634 const uint32_t orientation = transform.getOrientation();
Jorim Jaggi5a963262018-01-17 15:57:44 +0100635 if (orientation & Transform::ROT_INVALID || extremeScaling) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 // we can only handle simple transformation
Chia-I Wu30505fb2018-03-26 16:20:31 -0700637 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 } else {
639 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800640 hwcInfo.transform = transform;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700641 auto error = (*hwcLayer)->setTransform(transform);
642 ALOGE_IF(error != HWC2::Error::None,
643 "[%s] Failed to set transform %s: "
644 "%s (%d)",
645 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
646 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800647 }
648}
649
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700651 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
653 return;
654 }
655
David Sodman6f65f3e2017-11-03 14:28:09 -0700656 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657}
Dan Stozaee44edd2015-03-23 15:50:23 -0700658
chaviwc9232ed2017-11-14 15:31:15 -0800659bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700660 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800661 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
662 return false;
663 }
664
David Sodman6f65f3e2017-11-03 14:28:09 -0700665 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800666}
667
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
669 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700670 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700671 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672 return;
673 }
674
675 // This gives us only the "orientation" component of the transform
676 const State& s(getCurrentState());
677
678 // Apply the layer's transform, followed by the display's global transform
679 // Here we're guaranteed that the layer's transform preserves rects
680 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700681 if (!s.crop.isEmpty()) {
682 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 }
684 // Subtract the transparent region and snap to the bounds
685 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700686 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700688 if (!s.finalCrop.isEmpty()) {
689 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000690 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 auto& displayTransform(displayDevice->getTransform());
692 auto position = displayTransform.transform(frame);
693
David Sodman5d89c1d2017-12-14 15:54:51 -0800694 auto error = (*getBE().mHwcLayers[hwcId].layer)->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700695 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800696 ALOGE_IF(error != HWC2::Error::None,
697 "[%s] Failed to set cursor position "
698 "to (%d, %d): %s (%d)",
699 mName.string(), position.left, position.top, to_string(error).c_str(),
700 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701}
Riley Andrews03414a12014-07-01 14:22:59 -0700702
Mathias Agopian13127d82013-03-05 17:47:11 -0800703// ---------------------------------------------------------------------------
704// drawing...
705// ---------------------------------------------------------------------------
706
chaviwa76b2712017-09-20 12:02:26 -0700707void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
708 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800709}
710
chaviwa76b2712017-09-20 12:02:26 -0700711void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
712 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800713}
714
chaviwa76b2712017-09-20 12:02:26 -0700715void Layer::draw(const RenderArea& renderArea) const {
716 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800717}
718
David Sodman41fdfc92017-11-06 16:09:56 -0800719void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
720 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800721 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700722 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700723 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700724 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
chaviwa76b2712017-09-20 12:02:26 -0700727void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800728 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
Chia-I Wu30505fb2018-03-26 16:20:31 -0700731void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
732 if (getBE().mHwcLayers.count(hwcId) == 0) {
733 ALOGE("setCompositionType called without a valid HWC layer");
734 return;
735 }
736 auto& hwcInfo = getBE().mHwcLayers[hwcId];
737 auto& hwcLayer = hwcInfo.layer;
738 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (*hwcLayer)->getId(), to_string(type).c_str(),
739 static_cast<int>(callIntoHwc));
740 if (hwcInfo.compositionType != type) {
741 ALOGV(" actually setting");
742 hwcInfo.compositionType = type;
743 if (callIntoHwc) {
744 auto error = (*hwcLayer)->setCompositionType(type);
745 ALOGE_IF(error != HWC2::Error::None,
746 "[%s] Failed to set "
747 "composition type %s: %s (%d)",
748 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
749 static_cast<int32_t>(error));
750 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800751 }
752}
753
754HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700755 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
756 // If we're querying the composition type for a display that does not
757 // have a HWC counterpart, then it will always be Client
758 return HWC2::Composition::Client;
759 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700760 if (getBE().mHwcLayers.count(hwcId) == 0) {
761 ALOGE("getCompositionType called with an invalid HWC layer");
762 return HWC2::Composition::Invalid;
763 }
764 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765}
766
767void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700768 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769 ALOGE("setClearClientTarget called without a valid HWC layer");
770 return;
771 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700772 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773}
774
775bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777 ALOGE("getClearClientTarget called without a valid HWC layer");
778 return false;
779 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700780 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782
Dan Stozacac35382016-01-27 12:21:06 -0800783bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
784 if (point->getFrameNumber() <= mCurrentFrameNumber) {
785 // Don't bother with a SyncPoint, since we've already latched the
786 // relevant frame
787 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700788 }
789
Dan Stozacac35382016-01-27 12:21:06 -0800790 Mutex::Autolock lock(mLocalSyncPointMutex);
791 mLocalSyncPoints.push_back(point);
792 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700793}
794
Mathias Agopian13127d82013-03-05 17:47:11 -0800795void Layer::setFiltering(bool filtering) {
796 mFiltering = filtering;
797}
798
799bool Layer::getFiltering() const {
800 return mFiltering;
801}
802
Mathias Agopian13127d82013-03-05 17:47:11 -0800803// ----------------------------------------------------------------------------
804// local state
805// ----------------------------------------------------------------------------
806
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000807static void boundPoint(vec2* point, const Rect& crop) {
808 if (point->x < crop.left) {
809 point->x = crop.left;
810 }
811 if (point->x > crop.right) {
812 point->x = crop.right;
813 }
814 if (point->y < crop.top) {
815 point->y = crop.top;
816 }
817 if (point->y > crop.bottom) {
818 point->y = crop.bottom;
819 }
820}
821
chaviwa76b2712017-09-20 12:02:26 -0700822void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
823 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700824 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700825 const Transform renderAreaTransform(renderArea.getTransform());
826 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800827 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700828
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000829 vec2 lt = vec2(win.left, win.top);
830 vec2 lb = vec2(win.left, win.bottom);
831 vec2 rb = vec2(win.right, win.bottom);
832 vec2 rt = vec2(win.right, win.top);
833
Robert Carr1f0a16a2016-10-24 16:27:39 -0700834 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000835 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700836 lt = layerTransform.transform(lt);
837 lb = layerTransform.transform(lb);
838 rb = layerTransform.transform(rb);
839 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000840 }
841
Robert Carrb5d3d262016-03-25 15:08:13 -0700842 if (!s.finalCrop.isEmpty()) {
843 boundPoint(&lt, s.finalCrop);
844 boundPoint(&lb, s.finalCrop);
845 boundPoint(&rb, s.finalCrop);
846 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000847 }
848
Mathias Agopianff2ed702013-09-01 21:36:12 -0700849 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700850 position[0] = renderAreaTransform.transform(lt);
851 position[1] = renderAreaTransform.transform(lb);
852 position[2] = renderAreaTransform.transform(rb);
853 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800854 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700855 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800856 }
857}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800858
David Sodman41fdfc92017-11-06 16:09:56 -0800859bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700860 const Layer::State& s(mDrawingState);
861 return (s.flags & layer_state_t::eLayerSecure);
862}
863
Mathias Agopian13127d82013-03-05 17:47:11 -0800864void Layer::setVisibleRegion(const Region& visibleRegion) {
865 // always called from main thread
866 this->visibleRegion = visibleRegion;
867}
868
869void Layer::setCoveredRegion(const Region& coveredRegion) {
870 // always called from main thread
871 this->coveredRegion = coveredRegion;
872}
873
David Sodman41fdfc92017-11-06 16:09:56 -0800874void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 // always called from main thread
876 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
877}
878
Robert Carre5f4f692018-01-12 13:12:28 -0800879void Layer::clearVisibilityRegions() {
880 visibleRegion.clear();
881 visibleNonTransparentRegion.clear();
882 coveredRegion.clear();
883}
884
Mathias Agopian13127d82013-03-05 17:47:11 -0800885// ----------------------------------------------------------------------------
886// transaction
887// ----------------------------------------------------------------------------
888
Dan Stoza7dde5992015-05-22 09:51:44 -0700889void Layer::pushPendingState() {
890 if (!mCurrentState.modified) {
891 return;
892 }
893
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 // If this transaction is waiting on the receipt of a frame, generate a sync
895 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800896 if (mCurrentState.barrierLayer != nullptr) {
897 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
898 if (barrierLayer == nullptr) {
899 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 // If we can't promote the layer we are intended to wait on,
901 // then it is expired or otherwise invalid. Allow this transaction
902 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800903 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800904 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800905 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800906 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800907 mRemoteSyncPoints.push_back(std::move(syncPoint));
908 } else {
909 // We already missed the frame we're supposed to synchronize
910 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800911 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800912 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 }
914
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 // Wake us up to check if the frame has been received
916 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700917 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 }
919 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700920 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700921}
922
Pablo Ceballos05289c22016-04-14 15:49:55 -0700923void Layer::popPendingState(State* stateToCommit) {
924 auto oldFlags = stateToCommit->flags;
925 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800926 stateToCommit->flags =
927 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928
929 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700930 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700931}
932
Pablo Ceballos05289c22016-04-14 15:49:55 -0700933bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 bool stateUpdateAvailable = false;
935 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800936 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 if (mRemoteSyncPoints.empty()) {
938 // If we don't have a sync point for this, apply it anyway. It
939 // will be visually wrong, but it should keep us from getting
940 // into too much trouble.
941 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700942 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 stateUpdateAvailable = true;
944 continue;
945 }
946
David Sodman41fdfc92017-11-06 16:09:56 -0800947 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
948 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800949
950 // Signal our end of the sync point and then dispose of it
951 mRemoteSyncPoints.front()->setTransactionApplied();
952 mRemoteSyncPoints.pop_front();
953 continue;
954 }
955
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
957 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700958 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 stateUpdateAvailable = true;
960
961 // Signal our end of the sync point and then dispose of it
962 mRemoteSyncPoints.front()->setTransactionApplied();
963 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800964 } else {
965 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700968 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 stateUpdateAvailable = true;
970 }
971 }
972
973 // If we still have pending updates, wake SurfaceFlinger back up and point
974 // it at this layer so we can process them
975 if (!mPendingStates.empty()) {
976 setTransactionFlags(eTransactionNeeded);
977 mFlinger->setTransactionFlags(eTraversalNeeded);
978 }
979
980 mCurrentState.modified = false;
981 return stateUpdateAvailable;
982}
983
Mathias Agopian13127d82013-03-05 17:47:11 -0800984uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800985 ATRACE_CALL();
986
Dan Stoza7dde5992015-05-22 09:51:44 -0700987 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700988 Layer::State c = getCurrentState();
989 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 return 0;
991 }
992
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700993 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994
David Sodman41fdfc92017-11-06 16:09:56 -0800995 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700996
David Sodmaneb085e02017-10-05 18:49:04 -0700997 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700998 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000999 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001000 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1001 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1002 " requested={ wh={%4u,%4u} }}\n"
1003 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1004 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001005 this, getName().string(), mCurrentTransform,
1006 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1007 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1008 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1009 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1010 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001012 // record the new size, form this point on, when the client request
1013 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001014 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001016
Robert Carre392b552017-09-19 12:16:05 -07001017 // Don't let Layer::doTransaction update the drawing state
1018 // if we have a pending resize, unless we are in fixed-size mode.
1019 // the drawing state will be updated only once we receive a buffer
1020 // with the correct size.
1021 //
1022 // In particular, we want to make sure the clip (which is part
1023 // of the geometry state) is latched together with the size but is
1024 // latched immediately when no resizing is involved.
1025 //
1026 // If a sideband stream is attached, however, we want to skip this
1027 // optimization so that transactions aren't missed when a buffer
1028 // never arrives
1029 //
1030 // In the case that we don't have a buffer we ignore other factors
1031 // and avoid entering the resizePending state. At a high level the
1032 // resizePending state is to avoid applying the state of the new buffer
1033 // to the old buffer. However in the state where we don't have an old buffer
1034 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001035 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001036 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001037 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001038 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001039 flags |= eDontUpdateGeometryState;
1040 }
1041 }
1042
Robert Carr7bf247e2017-05-18 14:02:49 -07001043 // Here we apply various requested geometry states, depending on our
1044 // latching configuration. See Layer.h for a detailed discussion of
1045 // how geometry latching is controlled.
1046 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001047 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001048
1049 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1050 // mode, which causes attributes which normally latch regardless of scaling mode,
1051 // to be delayed. We copy the requested state to the active state making sure
1052 // to respect these rules (again see Layer.h for a detailed discussion).
1053 //
1054 // There is an awkward asymmetry in the handling of the crop states in the position
1055 // states, as can be seen below. Largely this arises from position and transform
1056 // being stored in the same data structure while having different latching rules.
1057 // b/38182305
1058 //
1059 // Careful that "c" and editCurrentState may not begin as equivalent due to
1060 // applyPendingStates in the presence of deferred transactions.
1061 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001062 float tx = c.active.transform.tx();
1063 float ty = c.active.transform.ty();
1064 c.active = c.requested;
1065 c.active.transform.set(tx, ty);
1066 editCurrentState.active = c.active;
1067 } else {
1068 editCurrentState.active = editCurrentState.requested;
1069 c.active = c.requested;
1070 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 }
1072
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001073 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 // invalidate and recompute the visible regions if needed
1075 flags |= Layer::eVisibleRegion;
1076 }
1077
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001078 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001079 // invalidate and recompute the visible regions if needed
1080 flags |= eVisibleRegion;
1081 this->contentDirty = true;
1082
1083 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001084 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001085 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 }
1087
Dan Stozac8145172016-04-28 16:29:06 -07001088 // If the layer is hidden, signal and clear out all local sync points so
1089 // that transactions for layers depending on this layer's frames becoming
1090 // visible are not blocked
1091 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001092 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001093 }
1094
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001096 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098}
1099
Pablo Ceballos05289c22016-04-14 15:49:55 -07001100void Layer::commitTransaction(const State& stateToCommit) {
1101 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001102}
1103
Mathias Agopian13127d82013-03-05 17:47:11 -08001104uint32_t Layer::getTransactionFlags(uint32_t flags) {
1105 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1106}
1107
1108uint32_t Layer::setTransactionFlags(uint32_t flags) {
1109 return android_atomic_or(flags, &mTransactionFlags);
1110}
1111
Robert Carr82364e32016-05-15 11:27:47 -07001112bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001113 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 return false;
1115 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001116
1117 // We update the requested and active position simultaneously because
1118 // we want to apply the position portion of the transform matrix immediately,
1119 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001120 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001121 if (immediate && !mFreezeGeometryUpdates) {
1122 // Here we directly update the active state
1123 // unlike other setters, because we store it within
1124 // the transform, but use different latching rules.
1125 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001126 mCurrentState.active.transform.set(x, y);
1127 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001128 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001129
Dan Stoza7dde5992015-05-22 09:51:44 -07001130 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 setTransactionFlags(eTransactionNeeded);
1132 return true;
1133}
Robert Carr82364e32016-05-15 11:27:47 -07001134
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1136 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1137 if (idx < 0) {
1138 return false;
1139 }
1140 if (childLayer->setLayer(z)) {
1141 mCurrentChildren.removeAt(idx);
1142 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001143 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001145 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146}
1147
Robert Carr503c7042017-09-27 15:06:08 -07001148bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1149 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1150 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1151 if (idx < 0) {
1152 return false;
1153 }
1154 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1155 mCurrentChildren.removeAt(idx);
1156 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001157 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001158 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001159 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001160}
1161
Robert Carrae060832016-11-28 10:51:00 -08001162bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001163 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 mCurrentState.sequence++;
1165 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001166 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001167
1168 // Discard all relative layering.
1169 if (mCurrentState.zOrderRelativeOf != nullptr) {
1170 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1171 if (strongRelative != nullptr) {
1172 strongRelative->removeZOrderRelative(this);
1173 }
1174 mCurrentState.zOrderRelativeOf = nullptr;
1175 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001176 setTransactionFlags(eTransactionNeeded);
1177 return true;
1178}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001179
Robert Carrdb66e622017-04-10 16:55:57 -07001180void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1181 mCurrentState.zOrderRelatives.remove(relative);
1182 mCurrentState.sequence++;
1183 mCurrentState.modified = true;
1184 setTransactionFlags(eTransactionNeeded);
1185}
1186
1187void Layer::addZOrderRelative(const wp<Layer>& relative) {
1188 mCurrentState.zOrderRelatives.add(relative);
1189 mCurrentState.modified = true;
1190 mCurrentState.sequence++;
1191 setTransactionFlags(eTransactionNeeded);
1192}
1193
Robert Carr503d2bd2017-12-04 15:49:47 -08001194bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001195 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1196 if (handle == nullptr) {
1197 return false;
1198 }
1199 sp<Layer> relative = handle->owner.promote();
1200 if (relative == nullptr) {
1201 return false;
1202 }
1203
Robert Carr503d2bd2017-12-04 15:49:47 -08001204 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1205 mCurrentState.zOrderRelativeOf == relative) {
1206 return false;
1207 }
1208
Robert Carrdb66e622017-04-10 16:55:57 -07001209 mCurrentState.sequence++;
1210 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001211 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001212
chaviw9ab4bd12017-11-03 13:11:00 -07001213 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1214 if (oldZOrderRelativeOf != nullptr) {
1215 oldZOrderRelativeOf->removeZOrderRelative(this);
1216 }
Robert Carrdb66e622017-04-10 16:55:57 -07001217 mCurrentState.zOrderRelativeOf = relative;
1218 relative->addZOrderRelative(this);
1219
1220 setTransactionFlags(eTransactionNeeded);
1221
1222 return true;
1223}
1224
Mathias Agopian13127d82013-03-05 17:47:11 -08001225bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001226 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 mCurrentState.requested.w = w;
1228 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001229 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 setTransactionFlags(eTransactionNeeded);
1231 return true;
1232}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001233bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001234 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001236 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001237 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
chaviw13fdc492017-06-27 12:40:18 -07001241
1242bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001243 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1244 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001245 return false;
1246
1247 mCurrentState.sequence++;
1248 mCurrentState.color.r = color.r;
1249 mCurrentState.color.g = color.g;
1250 mCurrentState.color.b = color.b;
1251 mCurrentState.modified = true;
1252 setTransactionFlags(eTransactionNeeded);
1253 return true;
1254}
1255
Mathias Agopian13127d82013-03-05 17:47:11 -08001256bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1257 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001258 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001259 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 setTransactionFlags(eTransactionNeeded);
1261 return true;
1262}
1263bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001264 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001265 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 setTransactionFlags(eTransactionNeeded);
1267 return true;
1268}
1269bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1270 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001271 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 mCurrentState.sequence++;
1273 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001274 mCurrentState.mask = mask;
1275 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001276 setTransactionFlags(eTransactionNeeded);
1277 return true;
1278}
Robert Carr99e27f02016-06-16 15:18:02 -07001279
1280bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001281 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001282 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001283 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001284 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001285 mCurrentState.crop = crop;
1286 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001287 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1288
Dan Stoza7dde5992015-05-22 09:51:44 -07001289 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 setTransactionFlags(eTransactionNeeded);
1291 return true;
1292}
Robert Carr8d5227b2017-03-16 15:41:03 -07001293
1294bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001295 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001296 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001297 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001298 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001299 mCurrentState.finalCrop = crop;
1300 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001301 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1302
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001303 mCurrentState.modified = true;
1304 setTransactionFlags(eTransactionNeeded);
1305 return true;
1306}
Mathias Agopian13127d82013-03-05 17:47:11 -08001307
Robert Carrc3574f72016-03-24 12:19:32 -07001308bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001309 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001310 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001311 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001312 return true;
1313}
1314
rongliucfb187b2018-03-14 12:26:23 -07001315void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001316 mCurrentState.appId = appId;
1317 mCurrentState.type = type;
1318 mCurrentState.modified = true;
1319 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001320}
1321
Mathias Agopian13127d82013-03-05 17:47:11 -08001322bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001323 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001324 mCurrentState.sequence++;
1325 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001326 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001327 setTransactionFlags(eTransactionNeeded);
1328 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001329}
1330
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001331bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001332 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001333 mCurrentState.sequence++;
1334 mCurrentState.dataSpace = dataSpace;
1335 mCurrentState.modified = true;
1336 setTransactionFlags(eTransactionNeeded);
1337 return true;
1338}
1339
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001340android_dataspace Layer::getDataSpace() const {
1341 return mCurrentState.dataSpace;
1342}
1343
Robert Carr1f0a16a2016-10-24 16:27:39 -07001344uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001345 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001346 if (p == nullptr) {
1347 return getDrawingState().layerStack;
1348 }
1349 return p->getLayerStack();
1350}
1351
David Sodman41fdfc92017-11-06 16:09:56 -08001352void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001353 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001354 mCurrentState.frameNumber = frameNumber;
1355 // We don't set eTransactionNeeded, because just receiving a deferral
1356 // request without any other state updates shouldn't actually induce a delay
1357 mCurrentState.modified = true;
1358 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001359 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001360 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001361 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001362}
1363
David Sodman41fdfc92017-11-06 16:09:56 -08001364void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001365 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1366 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001367}
1368
Dan Stozaee44edd2015-03-23 15:50:23 -07001369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370// ----------------------------------------------------------------------------
1371// pageflip handling...
1372// ----------------------------------------------------------------------------
1373
Robert Carr1f0a16a2016-10-24 16:27:39 -07001374bool Layer::isHiddenByPolicy() const {
1375 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001376 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001377 if (parent != nullptr && parent->isHiddenByPolicy()) {
1378 return true;
1379 }
1380 return s.flags & layer_state_t::eLayerHidden;
1381}
1382
David Sodman41fdfc92017-11-06 16:09:56 -08001383uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001384 // TODO: should we do something special if mSecure is set?
1385 if (mProtectedByApp) {
1386 // need a hardware-protected path to external video sink
1387 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001388 }
Riley Andrews03414a12014-07-01 14:22:59 -07001389 if (mPotentialCursor) {
1390 usage |= GraphicBuffer::USAGE_CURSOR;
1391 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001392 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001393 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001394}
1395
Mathias Agopian84300952012-11-21 16:02:13 -08001396void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001397 uint32_t orientation = 0;
1398 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001399 // The transform hint is used to improve performance, but we can
1400 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001402 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001404 if (orientation & Transform::ROT_INVALID) {
1405 orientation = 0;
1406 }
1407 }
David Sodmaneb085e02017-10-05 18:49:04 -07001408 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001409}
1410
Mathias Agopian13127d82013-03-05 17:47:11 -08001411// ----------------------------------------------------------------------------
1412// debugging
1413// ----------------------------------------------------------------------------
1414
Kalle Raitaa099a242017-01-11 11:17:29 -08001415LayerDebugInfo Layer::getLayerDebugInfo() const {
1416 LayerDebugInfo info;
1417 const Layer::State& ds = getDrawingState();
1418 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001419 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001420 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1421 info.mType = String8(getTypeId());
1422 info.mTransparentRegion = ds.activeTransparentRegion;
1423 info.mVisibleRegion = visibleRegion;
1424 info.mSurfaceDamageRegion = surfaceDamageRegion;
1425 info.mLayerStack = getLayerStack();
1426 info.mX = ds.active.transform.tx();
1427 info.mY = ds.active.transform.ty();
1428 info.mZ = ds.z;
1429 info.mWidth = ds.active.w;
1430 info.mHeight = ds.active.h;
1431 info.mCrop = ds.crop;
1432 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001433 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001434 info.mFlags = ds.flags;
1435 info.mPixelFormat = getPixelFormat();
1436 info.mDataSpace = getDataSpace();
1437 info.mMatrix[0][0] = ds.active.transform[0][0];
1438 info.mMatrix[0][1] = ds.active.transform[0][1];
1439 info.mMatrix[1][0] = ds.active.transform[1][0];
1440 info.mMatrix[1][1] = ds.active.transform[1][1];
1441 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001442 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001443 if (buffer != 0) {
1444 info.mActiveBufferWidth = buffer->getWidth();
1445 info.mActiveBufferHeight = buffer->getHeight();
1446 info.mActiveBufferStride = buffer->getStride();
1447 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 } else {
1449 info.mActiveBufferWidth = 0;
1450 info.mActiveBufferHeight = 0;
1451 info.mActiveBufferStride = 0;
1452 info.mActiveBufferFormat = 0;
1453 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001454 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 info.mNumQueuedFrames = getQueuedFrameCount();
1456 info.mRefreshPending = isBufferLatched();
1457 info.mIsOpaque = isOpaque(ds);
1458 info.mContentDirty = contentDirty;
1459 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001460}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001461
Dan Stozae22aec72016-08-01 13:20:59 -07001462void Layer::miniDumpHeader(String8& result) {
1463 result.append("----------------------------------------");
1464 result.append("---------------------------------------\n");
1465 result.append(" Layer name\n");
1466 result.append(" Z | ");
1467 result.append(" Comp Type | ");
1468 result.append(" Disp Frame (LTRB) | ");
1469 result.append(" Source Crop (LTRB)\n");
1470 result.append("----------------------------------------");
1471 result.append("---------------------------------------\n");
1472}
1473
1474void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001475 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001476 return;
1477 }
1478
1479 String8 name;
1480 if (mName.length() > 77) {
1481 std::string shortened;
1482 shortened.append(mName.string(), 36);
1483 shortened.append("[...]");
1484 shortened.append(mName.string() + (mName.length() - 36), 36);
1485 name = shortened.c_str();
1486 } else {
1487 name = mName;
1488 }
1489
1490 result.appendFormat(" %s\n", name.string());
1491
1492 const Layer::State& layerState(getDrawingState());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001493 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001494 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001495 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001496 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001497 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Chia-I Wu30505fb2018-03-26 16:20:31 -07001498 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001499 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 -07001500
1501 result.append("- - - - - - - - - - - - - - - - - - - - ");
1502 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1503}
Dan Stozae22aec72016-08-01 13:20:59 -07001504
Svetoslavd85084b2014-03-20 10:28:31 -07001505void Layer::dumpFrameStats(String8& result) const {
1506 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001507}
1508
Svetoslavd85084b2014-03-20 10:28:31 -07001509void Layer::clearFrameStats() {
1510 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001511}
1512
Jamie Gennis6547ff42013-07-16 20:12:42 -07001513void Layer::logFrameStats() {
1514 mFrameTracker.logAndResetStats(mName);
1515}
1516
Svetoslavd85084b2014-03-20 10:28:31 -07001517void Layer::getFrameStats(FrameStats* outStats) const {
1518 mFrameTracker.getStats(outStats);
1519}
1520
Brian Andersond6927fb2016-07-23 23:37:30 -07001521void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001522 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001523 Mutex::Autolock lock(mFrameEventHistoryMutex);
1524 mFrameEventHistory.checkFencesForCompletion();
1525 mFrameEventHistory.dump(result);
1526}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001527
Brian Anderson5ea5e592016-12-01 16:54:33 -08001528void Layer::onDisconnect() {
1529 Mutex::Autolock lock(mFrameEventHistoryMutex);
1530 mFrameEventHistory.onDisconnect();
1531}
1532
Brian Anderson3890c392016-07-25 12:48:08 -07001533void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001534 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001535 Mutex::Autolock lock(mFrameEventHistoryMutex);
1536 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001537 // If there are any unsignaled fences in the aquire timeline at this
1538 // point, the previously queued frame hasn't been latched yet. Go ahead
1539 // and try to get the signal time here so the syscall is taken out of
1540 // the main thread's critical path.
1541 mAcquireTimeline.updateSignalTimes();
1542 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001543 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001544 mFrameEventHistory.addQueue(*newTimestamps);
1545 }
1546
Brian Anderson3890c392016-07-25 12:48:08 -07001547 if (outDelta) {
1548 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001550}
Dan Stozae77c7662016-05-13 11:37:28 -07001551
Chia-I Wu98f1c102017-05-30 14:54:08 -07001552size_t Layer::getChildrenCount() const {
1553 size_t count = 0;
1554 for (const sp<Layer>& child : mCurrentChildren) {
1555 count += 1 + child->getChildrenCount();
1556 }
1557 return count;
1558}
1559
Robert Carr1f0a16a2016-10-24 16:27:39 -07001560void Layer::addChild(const sp<Layer>& layer) {
1561 mCurrentChildren.add(layer);
1562 layer->setParent(this);
1563}
1564
1565ssize_t Layer::removeChild(const sp<Layer>& layer) {
1566 layer->setParent(nullptr);
1567 return mCurrentChildren.remove(layer);
1568}
1569
Robert Carr1db73f62016-12-21 12:58:51 -08001570bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1571 sp<Handle> handle = nullptr;
1572 sp<Layer> newParent = nullptr;
1573 if (newParentHandle == nullptr) {
1574 return false;
1575 }
1576 handle = static_cast<Handle*>(newParentHandle.get());
1577 newParent = handle->owner.promote();
1578 if (newParent == nullptr) {
1579 ALOGE("Unable to promote Layer handle");
1580 return false;
1581 }
1582
1583 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001584 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001585
1586 sp<Client> client(child->mClientRef.promote());
1587 if (client != nullptr) {
1588 client->setParentLayer(newParent);
1589 }
1590 }
1591 mCurrentChildren.clear();
1592
1593 return true;
1594}
1595
Robert Carr578038f2018-03-09 12:25:24 -08001596void Layer::reparentChildrenForDrawing(const sp<Layer>& newParent) {
1597 for (const sp<Layer>& child : mDrawingChildren) {
1598 child->mDrawingParent = newParent;
1599 }
1600}
1601
chaviwf1961f72017-09-18 16:41:07 -07001602bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1603 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001604 return false;
1605 }
1606
1607 auto handle = static_cast<Handle*>(newParentHandle.get());
1608 sp<Layer> newParent = handle->owner.promote();
1609 if (newParent == nullptr) {
1610 ALOGE("Unable to promote Layer handle");
1611 return false;
1612 }
1613
chaviwf1961f72017-09-18 16:41:07 -07001614 sp<Layer> parent = getParent();
1615 if (parent != nullptr) {
1616 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001617 }
chaviwf1961f72017-09-18 16:41:07 -07001618 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001619
chaviwf1961f72017-09-18 16:41:07 -07001620 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001621 sp<Client> newParentClient(newParent->mClientRef.promote());
1622
chaviwf1961f72017-09-18 16:41:07 -07001623 if (client != newParentClient) {
1624 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001625 }
1626
chaviw06178942017-07-27 10:25:59 -07001627 return true;
1628}
1629
Robert Carr9524cb32017-02-13 11:32:32 -08001630bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001631 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001632 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001633 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001634 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001635 client->detachLayer(child.get());
1636 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001637 }
Robert Carr7f619b22017-11-06 12:56:35 -08001638 }
Robert Carr9524cb32017-02-13 11:32:32 -08001639
1640 return true;
1641}
1642
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001644 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645}
1646
1647void Layer::clearSyncPoints() {
1648 for (const auto& child : mCurrentChildren) {
1649 child->clearSyncPoints();
1650 }
1651
1652 Mutex::Autolock lock(mLocalSyncPointMutex);
1653 for (auto& point : mLocalSyncPoints) {
1654 point->setFrameAvailable();
1655 }
1656 mLocalSyncPoints.clear();
1657}
1658
1659int32_t Layer::getZ() const {
1660 return mDrawingState.z;
1661}
1662
Robert Carr29abff82017-12-04 13:51:20 -08001663bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1664 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1665 const State& state = useDrawing ? mDrawingState : mCurrentState;
1666 return state.zOrderRelativeOf != nullptr;
1667}
1668
David Sodman41fdfc92017-11-06 16:09:56 -08001669__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001670 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001671 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1672 "makeTraversalList received invalid stateSet");
1673 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1674 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1675 const State& state = useDrawing ? mDrawingState : mCurrentState;
1676
Robert Carr29abff82017-12-04 13:51:20 -08001677 if (state.zOrderRelatives.size() == 0) {
1678 *outSkipRelativeZUsers = true;
1679 return children;
1680 }
1681
Robert Carrdb66e622017-04-10 16:55:57 -07001682 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001683 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001684 sp<Layer> strongRelative = weakRelative.promote();
1685 if (strongRelative != nullptr) {
1686 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001687 }
1688 }
1689
Dan Stoza412903f2017-04-27 13:42:17 -07001690 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001691 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1692 if (childState.zOrderRelativeOf != nullptr) {
1693 continue;
1694 }
Robert Carrdb66e622017-04-10 16:55:57 -07001695 traverse.add(child);
1696 }
1697
1698 return traverse;
1699}
1700
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701/**
Robert Carrdb66e622017-04-10 16:55:57 -07001702 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703 */
Dan Stoza412903f2017-04-27 13:42:17 -07001704void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001705 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1706 // produce a new list for traversing, including our relatives, and not including our children
1707 // who are relatives of another surface. In the case that there are no relative Z,
1708 // makeTraversalList returns our children directly to avoid significant overhead.
1709 // However in this case we need to take the responsibility for filtering children which
1710 // are relatives of another surface here.
1711 bool skipRelativeZUsers = false;
1712 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001713
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001715 for (; i < list.size(); i++) {
1716 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001717 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1718 continue;
1719 }
1720
Robert Carrdb66e622017-04-10 16:55:57 -07001721 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001722 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001723 }
Dan Stoza412903f2017-04-27 13:42:17 -07001724 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725 }
Robert Carr29abff82017-12-04 13:51:20 -08001726
Dan Stoza412903f2017-04-27 13:42:17 -07001727 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001728 for (; i < list.size(); i++) {
1729 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001730
1731 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1732 continue;
1733 }
Dan Stoza412903f2017-04-27 13:42:17 -07001734 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001735 }
1736}
1737
1738/**
Robert Carrdb66e622017-04-10 16:55:57 -07001739 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 */
Dan Stoza412903f2017-04-27 13:42:17 -07001741void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1742 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001743 // See traverseInZOrder for documentation.
1744 bool skipRelativeZUsers = false;
1745 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001746
Robert Carr1f0a16a2016-10-24 16:27:39 -07001747 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001748 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001749 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001750
1751 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1752 continue;
1753 }
1754
Robert Carrdb66e622017-04-10 16:55:57 -07001755 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756 break;
1757 }
Dan Stoza412903f2017-04-27 13:42:17 -07001758 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 }
Dan Stoza412903f2017-04-27 13:42:17 -07001760 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001761 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001762 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001763
1764 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1765 continue;
1766 }
1767
Dan Stoza412903f2017-04-27 13:42:17 -07001768 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001769 }
1770}
1771
chaviwa76b2712017-09-20 12:02:26 -07001772/**
1773 * Traverse only children in z order, ignoring relative layers.
1774 */
1775void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1776 const LayerVector::Visitor& visitor) {
1777 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1778 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1779
1780 size_t i = 0;
1781 for (; i < children.size(); i++) {
1782 const auto& relative = children[i];
1783 if (relative->getZ() >= 0) {
1784 break;
1785 }
1786 relative->traverseChildrenInZOrder(stateSet, visitor);
1787 }
1788 visitor(this);
1789 for (; i < children.size(); i++) {
1790 const auto& relative = children[i];
1791 relative->traverseChildrenInZOrder(stateSet, visitor);
1792 }
1793}
1794
Robert Carr1f0a16a2016-10-24 16:27:39 -07001795Transform Layer::getTransform() const {
1796 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001797 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798 if (p != nullptr) {
1799 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001800
1801 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1802 // it isFixedSize) then there may be additional scaling not accounted
1803 // for in the transform. We need to mirror this scaling in child surfaces
1804 // or we will break the contract where WM can treat child surfaces as
1805 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001806 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001807 int bufferWidth;
1808 int bufferHeight;
1809 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001810 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1811 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001812 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001813 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1814 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001815 }
David Sodman41fdfc92017-11-06 16:09:56 -08001816 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1817 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001818 Transform extraParentScaling;
1819 extraParentScaling.set(sx, 0, 0, sy);
1820 t = t * extraParentScaling;
1821 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001822 }
1823 return t * getDrawingState().active.transform;
1824}
1825
chaviw13fdc492017-06-27 12:40:18 -07001826half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001827 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001828
chaviw13fdc492017-06-27 12:40:18 -07001829 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1830 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001831}
Robert Carr6452f122017-03-21 10:41:29 -07001832
chaviw13fdc492017-06-27 12:40:18 -07001833half4 Layer::getColor() const {
1834 const half4 color(getDrawingState().color);
1835 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001836}
Robert Carr6452f122017-03-21 10:41:29 -07001837
Robert Carr1f0a16a2016-10-24 16:27:39 -07001838void Layer::commitChildList() {
1839 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1840 const auto& child = mCurrentChildren[i];
1841 child->commitChildList();
1842 }
1843 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001844 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845}
1846
chaviw1d044282017-09-27 12:19:28 -07001847void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1848 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1849 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1850 const State& state = useDrawing ? mDrawingState : mCurrentState;
1851
1852 Transform requestedTransform = state.active.transform;
1853 Transform transform = getTransform();
1854
1855 layerInfo->set_id(sequence);
1856 layerInfo->set_name(getName().c_str());
1857 layerInfo->set_type(String8(getTypeId()));
1858
1859 for (const auto& child : children) {
1860 layerInfo->add_children(child->sequence);
1861 }
1862
1863 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1864 sp<Layer> strongRelative = weakRelative.promote();
1865 if (strongRelative != nullptr) {
1866 layerInfo->add_relatives(strongRelative->sequence);
1867 }
1868 }
1869
1870 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1871 layerInfo->mutable_transparent_region());
1872 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1873 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1874
1875 layerInfo->set_layer_stack(getLayerStack());
1876 layerInfo->set_z(state.z);
1877
1878 PositionProto* position = layerInfo->mutable_position();
1879 position->set_x(transform.tx());
1880 position->set_y(transform.ty());
1881
1882 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1883 requestedPosition->set_x(requestedTransform.tx());
1884 requestedPosition->set_y(requestedTransform.ty());
1885
1886 SizeProto* size = layerInfo->mutable_size();
1887 size->set_w(state.active.w);
1888 size->set_h(state.active.h);
1889
1890 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1891 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1892
1893 layerInfo->set_is_opaque(isOpaque(state));
1894 layerInfo->set_invalidate(contentDirty);
1895 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1896 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1897 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1898 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1899 layerInfo->set_flags(state.flags);
1900
1901 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1902 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1903
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001904 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001905 if (parent != nullptr) {
1906 layerInfo->set_parent(parent->sequence);
1907 }
1908
1909 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1910 if (zOrderRelativeOf != nullptr) {
1911 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1912 }
1913
David Sodman0cc69182017-11-17 12:12:07 -08001914 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001915 if (buffer != nullptr) {
1916 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001917 }
1918
1919 layerInfo->set_queued_frames(getQueuedFrameCount());
1920 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001921 layerInfo->set_window_type(state.type);
1922 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001923}
1924
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001925void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1926 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1927
1928 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1929
1930 const Rect& frame = hwcInfo.displayFrame;
1931 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1932
1933 const FloatRect& crop = hwcInfo.sourceCrop;
1934 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1935
1936 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1937 layerInfo->set_hwc_transform(transform);
1938}
1939
Mathias Agopian13127d82013-03-05 17:47:11 -08001940// ---------------------------------------------------------------------------
1941
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001943
1944#if defined(__gl_h_)
1945#error "don't include gl/gl.h in this file"
1946#endif
1947
1948#if defined(__gl2_h_)
1949#error "don't include gl2/gl2.h in this file"
1950#endif