blob: 43bef60504341570398948ecaab626776b7d201d [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
Yiwei Zhang60d1a192018-03-07 14:52:28 -080045#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080052#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070073 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080078 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080079 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070081 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080083 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080084 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080085 mFiltering(false),
86 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080087 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070088 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070089 mPotentialCursor(false),
90 mQueueItemLock(),
91 mQueueItemCondition(),
92 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070093 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070094 mAutoRefresh(false),
David Sodmanb8af7922017-12-21 15:17:55 -080095 mFreezeGeometryUpdates(false),
David Sodman2b727ac2017-12-21 14:28:08 -080096 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080097
Mathias Agopiana67932f2011-04-20 14:20:59 -070098 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099
100 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800101 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
102 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
103 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700106 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 mCurrentState.active.w = w;
109 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700110 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800111 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700112 mCurrentState.crop.makeInvalid();
113 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700114 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
115 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700117 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700121 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500122 mCurrentState.appId = 0;
123 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 // drawing state & current state are identical
126 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127
Dan Stoza9e56aa02015-11-02 13:00:03 -0800128 const auto& hwc = flinger->getHwComposer();
129 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
130 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132
133 CompositorTiming compositorTiming;
134 flinger->getCompositorTiming(&compositorTiming);
135 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700136
Jamie Gennise8696a42012-01-15 18:54:57 -0800137}
138
David Sodman41fdfc92017-11-06 16:09:56 -0800139void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700140
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700141Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800142 sp<Client> c(mClientRef.promote());
143 if (c != 0) {
144 c->detachLayer(this);
145 }
146
147 for (auto& point : mRemoteSyncPoints) {
148 point->setTransactionApplied();
149 }
150 for (auto& point : mLocalSyncPoints) {
151 point->setFrameAvailable();
152 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700153 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700154}
155
Mathias Agopian13127d82013-03-05 17:47:11 -0800156// ---------------------------------------------------------------------------
157// callbacks
158// ---------------------------------------------------------------------------
159
David Sodmaneb085e02017-10-05 18:49:04 -0700160/*
161 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
162 * Layer. So, the implementation is done in BufferLayer. When called on a
163 * ColorLayer object, it's essentially a NOP.
164 */
David Sodmaneb085e02017-10-05 18:49:04 -0700165void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800166
Chia-I Wuc6657022017-08-15 11:18:17 -0700167void Layer::onRemovedFromCurrentState() {
168 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
169
chaviw8b3871a2017-11-01 17:41:01 -0700170 mPendingRemoval = true;
171
Robert Carr5edb1ad2017-04-25 10:54:24 -0700172 if (mCurrentState.zOrderRelativeOf != nullptr) {
173 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
174 if (strongRelative != nullptr) {
175 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700176 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700177 }
178 mCurrentState.zOrderRelativeOf = nullptr;
179 }
180
Chia-I Wuc6657022017-08-15 11:18:17 -0700181 for (const auto& child : mCurrentChildren) {
182 child->onRemovedFromCurrentState();
183 }
184}
Chia-I Wu38512252017-05-17 14:36:16 -0700185
Chia-I Wuc6657022017-08-15 11:18:17 -0700186void Layer::onRemoved() {
187 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700188 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700189
Steven Thomasb02664d2017-07-26 18:48:28 -0700190 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700191
Robert Carr1f0a16a2016-10-24 16:27:39 -0700192 for (const auto& child : mCurrentChildren) {
193 child->onRemoved();
194 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700195}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700196
Mathias Agopian13127d82013-03-05 17:47:11 -0800197// ---------------------------------------------------------------------------
198// set-up
199// ---------------------------------------------------------------------------
200
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700201const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203}
204
chaviw13fdc492017-06-27 12:40:18 -0700205bool Layer::getPremultipledAlpha() const {
206 return mPremultipliedAlpha;
207}
208
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700209sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800210 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700211 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800212}
213
214// ---------------------------------------------------------------------------
215// h/w composer set-up
216// ---------------------------------------------------------------------------
217
Steven Thomasb02664d2017-07-26 18:48:28 -0700218bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700219 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700220 "Already have a layer for hwcId %d", hwcId);
David Sodman5d89c1d2017-12-14 15:54:51 -0800221
222 std::shared_ptr<LayerContainer> layer(new LayerContainer(hwc, hwcId));
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 if (!layer) {
224 return false;
225 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700226 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 hwcInfo.hwc = hwc;
228 hwcInfo.layer = layer;
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 return true;
230}
231
Chia-I Wu83806892017-11-16 10:50:20 -0800232bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700233 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800234 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700236 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800237 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodman5d89c1d2017-12-14 15:54:51 -0800239 hwcInfo.layer = nullptr;
240
241 if (getBE().mHwcLayers.count(hwcId) == 1) {
242 getBE().mHwcLayers.erase(hwcId);
243 }
244
Chia-I Wu83806892017-11-16 10:50:20 -0800245 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700246}
247
248void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700251 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
252 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700253 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800255 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700256}
Steven Thomasb02664d2017-07-26 18:48:28 -0700257
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800258Rect Layer::getContentCrop() const {
259 // this is the crop rectangle that applies to the buffer
260 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 Rect crop;
262 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800265 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800267 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700270 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 return crop;
273}
274
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700275static Rect reduce(const Rect& win, const Region& exclude) {
276 if (CC_LIKELY(exclude.isEmpty())) {
277 return win;
278 }
279 if (exclude.isRect()) {
280 return win.reduce(exclude.getBounds());
281 }
282 return Region(win).subtract(exclude).getBounds();
283}
284
Dan Stoza80d61162017-12-20 15:57:52 -0800285static FloatRect reduce(const FloatRect& win, const Region& exclude) {
286 if (CC_LIKELY(exclude.isEmpty())) {
287 return win;
288 }
289 // Convert through Rect (by rounding) for lack of FloatRegion
290 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
291}
292
Robert Carr1f0a16a2016-10-24 16:27:39 -0700293Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
294 const Layer::State& s(getDrawingState());
295 Rect win(s.active.w, s.active.h);
296
297 if (!s.crop.isEmpty()) {
298 win.intersect(s.crop, &win);
299 }
300
301 Transform t = getTransform();
302 win = t.transform(win);
303
Robert Carr41b08b52017-06-01 16:11:34 -0700304 if (!s.finalCrop.isEmpty()) {
305 win.intersect(s.finalCrop, &win);
306 }
307
Chia-I Wue41dbe62017-06-13 14:10:56 -0700308 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700309 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
310 // When calculating the parent bounds for purposes of clipping,
311 // we don't need to constrain the parent to its transparent region.
312 // The transparent region is an optimization based on the
313 // buffer contents of the layer, but does not affect the space allocated to
314 // it by policy, and thus children should be allowed to extend into the
315 // parent's transparent region. In fact one of the main uses, is to reduce
316 // buffer allocation size in cases where a child window sits behind a main window
317 // (by marking the hole in the parent window as a transparent region)
318 if (p != nullptr) {
319 Rect bounds = p->computeScreenBounds(false);
320 bounds.intersect(win, &win);
321 }
322
323 if (reduceTransparentRegion) {
324 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
325 win = reduce(win, screenTransparentRegion);
326 }
327
328 return win;
329}
330
Dan Stoza80d61162017-12-20 15:57:52 -0800331FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700332 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700333 return computeBounds(s.activeTransparentRegion);
334}
335
Dan Stoza80d61162017-12-20 15:57:52 -0800336FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700337 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800338 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700339
340 if (!s.crop.isEmpty()) {
341 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343
344 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700345 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800347 // Look in computeScreenBounds recursive call for explanation of
348 // why we pass false here.
349 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
351
352 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800353
354 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700355 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800356 floatWin = t.transform(floatWin);
357 floatWin = floatWin.intersect(bounds.toFloatRect());
358 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 }
360
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700361 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800362 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800363}
364
Robert Carr1f0a16a2016-10-24 16:27:39 -0700365Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700366 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800367 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700368 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800369
370 // apply the projection's clipping to the window crop in
371 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700372 // if there are no window scaling involved, this operation will map to full
373 // pixels in the buffer.
374 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
375 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700376
377 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700378 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700379 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380 }
381
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 Transform t = getTransform();
383 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000384 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
385 activeCrop.clear();
386 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700387 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800388 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000389 activeCrop.clear();
390 }
391 }
chaviwb1154d12017-10-31 14:15:36 -0700392
393 const auto& p = mDrawingParent.promote();
394 if (p != nullptr) {
395 auto parentCrop = p->computeInitialCrop(hw);
396 activeCrop.intersect(parentCrop, &activeCrop);
397 }
398
Robert Carr1f0a16a2016-10-24 16:27:39 -0700399 return activeCrop;
400}
401
Dan Stoza5a423ea2017-02-16 14:10:39 -0800402FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 // the content crop is the area of the content that gets scaled to the
404 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800405 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406
407 // In addition there is a WM-specified crop we pull from our drawing state.
408 const State& s(getDrawingState());
409
410 // Screen space to make reduction to parent crop clearer.
411 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700412 Transform t = getTransform();
413 // Back to layer space to work with the content crop.
414 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800415
Michael Lentine28ea2172014-11-19 18:32:37 -0800416 // This needs to be here as transform.transform(Rect) computes the
417 // transformed rect and then takes the bounding box of the result before
418 // returning. This means
419 // transform.inverse().transform(transform.transform(Rect)) != Rect
420 // in which case we need to make sure the final rect is clipped to the
421 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
423 activeCrop.clear();
424 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800425
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700426 // subtract the transparent region and snap to the bounds
427 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
428
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 // Transform the window crop to match the buffer coordinate system,
430 // which means using the inverse of the current transform set on the
431 // SurfaceFlingerConsumer.
432 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700433 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700435 * the code below applies the primary display's inverse transform to the
436 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 */
David Sodman41fdfc92017-11-06 16:09:56 -0800438 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 // calculate the inverse transform
440 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800441 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800442 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446
447 int winWidth = s.active.w;
448 int winHeight = s.active.h;
449 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
450 // If the activeCrop has been rotate the ends are rotated but not
451 // the space itself so when transforming ends back we can't rely on
452 // a modification of the axes of rotation. To account for this we
453 // need to reorient the inverse rotation in terms of the current
454 // axes of rotation.
455 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
456 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
457 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800458 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 }
460 winWidth = s.active.h;
461 winHeight = s.active.w;
462 }
David Sodman41fdfc92017-11-06 16:09:56 -0800463 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464
465 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800466 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 float yScale = crop.getHeight() / float(winHeight);
468
David Sodman41fdfc92017-11-06 16:09:56 -0800469 float insetL = winCrop.left * xScale;
470 float insetT = winCrop.top * yScale;
471 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 float insetB = (winHeight - winCrop.bottom) * yScale;
473
David Sodman41fdfc92017-11-06 16:09:56 -0800474 crop.left += insetL;
475 crop.top += insetT;
476 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 crop.bottom -= insetB;
478
Mathias Agopian13127d82013-03-05 17:47:11 -0800479 return crop;
480}
481
Robert Carrae060832016-11-28 10:51:00 -0800482void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700483{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700485 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700486
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700487 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488 hwcInfo.forceClientComposition = false;
489
490 if (isSecure() && !displayDevice->isSecure()) {
491 hwcInfo.forceClientComposition = true;
492 }
493
Chia-I Wu30505fb2018-03-26 16:20:31 -0700494 auto& hwcLayer = hwcInfo.layer;
495
Mathias Agopian13127d82013-03-05 17:47:11 -0800496 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700497 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500498 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700499 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800500 blendMode =
501 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700503 auto error = (*hwcLayer)->setBlendMode(blendMode);
504 ALOGE_IF(error != HWC2::Error::None,
505 "[%s] Failed to set blend mode %s:"
506 " %s (%d)",
507 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
508 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800509
510 // apply the layer's transform, followed by the display's global transform
511 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700512 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700513 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700514 if (!s.crop.isEmpty()) {
515 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700516 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800517 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000518 activeCrop.clear();
519 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800521 // This needs to be here as transform.transform(Rect) computes the
522 // transformed rect and then takes the bounding box of the result before
523 // returning. This means
524 // transform.inverse().transform(transform.transform(Rect)) != Rect
525 // in which case we need to make sure the final rect is clipped to the
526 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800527 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528 activeCrop.clear();
529 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700530 // mark regions outside the crop as transparent
531 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800532 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
533 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
534 activeTransparentRegion.orSelf(
535 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700536 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700537
Dan Stoza80d61162017-12-20 15:57:52 -0800538 // computeBounds returns a FloatRect to provide more accuracy during the
539 // transformation. We then round upon constructing 'frame'.
540 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700541 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800542 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000543 frame.clear();
544 }
545 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000546 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
547 frame.clear();
548 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549 const Transform& tr(displayDevice->getTransform());
550 Rect transformedFrame = tr.transform(frame);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700551 error = (*hwcLayer)->setDisplayFrame(transformedFrame);
552 if (error != HWC2::Error::None) {
553 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
554 transformedFrame.left, transformedFrame.top, transformedFrame.right,
555 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
556 } else {
557 hwcInfo.displayFrame = transformedFrame;
558 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559
Dan Stoza5a423ea2017-02-16 14:10:39 -0800560 FloatRect sourceCrop = computeCrop(displayDevice);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700561 error = (*hwcLayer)->setSourceCrop(sourceCrop);
562 if (error != HWC2::Error::None) {
563 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
564 "%s (%d)",
565 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
566 to_string(error).c_str(), static_cast<int32_t>(error));
567 } else {
568 hwcInfo.sourceCrop = sourceCrop;
569 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570
chaviw13fdc492017-06-27 12:40:18 -0700571 float alpha = static_cast<float>(getAlpha());
Chia-I Wu30505fb2018-03-26 16:20:31 -0700572 error = (*hwcLayer)->setPlaneAlpha(alpha);
573 ALOGE_IF(error != HWC2::Error::None,
574 "[%s] Failed to set plane alpha %.3f: "
575 "%s (%d)",
576 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577
Chia-I Wu30505fb2018-03-26 16:20:31 -0700578 error = (*hwcLayer)->setZOrder(z);
579 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
580 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500581
Albert Chaulk2a589632017-05-04 16:59:44 -0400582 int type = s.type;
583 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700584 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400585 if (parent.get()) {
586 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700587 if (parentState.type >= 0 || parentState.appId >= 0) {
588 type = parentState.type;
589 appId = parentState.appId;
590 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400591 }
592
Chia-I Wu30505fb2018-03-26 16:20:31 -0700593 error = (*hwcLayer)->setInfo(type, appId);
594 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
595 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800596
Mathias Agopian29a367b2011-07-12 14:51:45 -0700597 /*
598 * Transformations are applied in this order:
599 * 1) buffer orientation/flip/mirror
600 * 2) state transformation (window manager)
601 * 3) layer orientation (screen orientation)
602 * (NOTE: the matrices are multiplied in reverse order)
603 */
604
605 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700606 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700607
Robert Carrcae605c2017-03-29 12:10:31 -0700608 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700609 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700610 * the code below applies the primary display's inverse transform to the
611 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700612 */
David Sodman41fdfc92017-11-06 16:09:56 -0800613 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 // calculate the inverse transform
615 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800616 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 }
Robert Carrcae605c2017-03-29 12:10:31 -0700618
619 /*
620 * Here we cancel out the orientation component of the WM transform.
621 * The scaling and translate components are already included in our bounds
622 * computation so it's enough to just omit it in the composition.
623 * See comment in onDraw with ref to b/36727915 for why.
624 */
625 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700627
Jorim Jaggi5a963262018-01-17 15:57:44 +0100628 // STOPSHIP (b/72106793): If we have less than 25% scaling, HWC usually needs to use the rotator
629 // to handle it. However, there is one guaranteed frame of jank when we switch to using the
630 // rotator. In the meantime, we force GL composition instead until we have a better fix for the
631 // HWC issue.
632 bool extremeScaling = abs(t[0][0]) <= 0.25 || abs(t[1][1]) <= 0.25;
633
Mathias Agopian29a367b2011-07-12 14:51:45 -0700634 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800635 const uint32_t orientation = transform.getOrientation();
Jorim Jaggi5a963262018-01-17 15:57:44 +0100636 if (orientation & Transform::ROT_INVALID || extremeScaling) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 // we can only handle simple transformation
Chia-I Wu30505fb2018-03-26 16:20:31 -0700638 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 } else {
640 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800641 hwcInfo.transform = transform;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700642 auto error = (*hwcLayer)->setTransform(transform);
643 ALOGE_IF(error != HWC2::Error::None,
644 "[%s] Failed to set transform %s: "
645 "%s (%d)",
646 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
647 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800648 }
649}
650
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700652 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
654 return;
655 }
656
David Sodman6f65f3e2017-11-03 14:28:09 -0700657 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658}
Dan Stozaee44edd2015-03-23 15:50:23 -0700659
chaviwc9232ed2017-11-14 15:31:15 -0800660bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700661 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800662 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
663 return false;
664 }
665
David Sodman6f65f3e2017-11-03 14:28:09 -0700666 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800667}
668
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
670 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700671 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700672 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673 return;
674 }
675
676 // This gives us only the "orientation" component of the transform
677 const State& s(getCurrentState());
678
679 // Apply the layer's transform, followed by the display's global transform
680 // Here we're guaranteed that the layer's transform preserves rects
681 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700682 if (!s.crop.isEmpty()) {
683 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 }
685 // Subtract the transparent region and snap to the bounds
686 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700687 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700689 if (!s.finalCrop.isEmpty()) {
690 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000691 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692 auto& displayTransform(displayDevice->getTransform());
693 auto position = displayTransform.transform(frame);
694
David Sodman5d89c1d2017-12-14 15:54:51 -0800695 auto error = (*getBE().mHwcLayers[hwcId].layer)->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700696 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800697 ALOGE_IF(error != HWC2::Error::None,
698 "[%s] Failed to set cursor position "
699 "to (%d, %d): %s (%d)",
700 mName.string(), position.left, position.top, to_string(error).c_str(),
701 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702}
Riley Andrews03414a12014-07-01 14:22:59 -0700703
Mathias Agopian13127d82013-03-05 17:47:11 -0800704// ---------------------------------------------------------------------------
705// drawing...
706// ---------------------------------------------------------------------------
707
chaviwa76b2712017-09-20 12:02:26 -0700708void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
709 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800710}
711
chaviwa76b2712017-09-20 12:02:26 -0700712void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
713 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800714}
715
chaviwa76b2712017-09-20 12:02:26 -0700716void Layer::draw(const RenderArea& renderArea) const {
717 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800718}
719
David Sodman41fdfc92017-11-06 16:09:56 -0800720void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
721 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800722 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700723 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700724 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700725 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800726}
727
chaviwa76b2712017-09-20 12:02:26 -0700728void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800729 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800730}
731
Chia-I Wu30505fb2018-03-26 16:20:31 -0700732void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
733 if (getBE().mHwcLayers.count(hwcId) == 0) {
734 ALOGE("setCompositionType called without a valid HWC layer");
735 return;
736 }
737 auto& hwcInfo = getBE().mHwcLayers[hwcId];
738 auto& hwcLayer = hwcInfo.layer;
739 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (*hwcLayer)->getId(), to_string(type).c_str(),
740 static_cast<int>(callIntoHwc));
741 if (hwcInfo.compositionType != type) {
742 ALOGV(" actually setting");
743 hwcInfo.compositionType = type;
744 if (callIntoHwc) {
745 auto error = (*hwcLayer)->setCompositionType(type);
746 ALOGE_IF(error != HWC2::Error::None,
747 "[%s] Failed to set "
748 "composition type %s: %s (%d)",
749 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
750 static_cast<int32_t>(error));
751 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752 }
753}
754
755HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700756 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
757 // If we're querying the composition type for a display that does not
758 // have a HWC counterpart, then it will always be Client
759 return HWC2::Composition::Client;
760 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700761 if (getBE().mHwcLayers.count(hwcId) == 0) {
762 ALOGE("getCompositionType called with an invalid HWC layer");
763 return HWC2::Composition::Invalid;
764 }
765 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766}
767
768void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700769 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770 ALOGE("setClearClientTarget called without a valid HWC layer");
771 return;
772 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700773 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774}
775
776bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700777 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 ALOGE("getClearClientTarget called without a valid HWC layer");
779 return false;
780 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700781 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783
Dan Stozacac35382016-01-27 12:21:06 -0800784bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
785 if (point->getFrameNumber() <= mCurrentFrameNumber) {
786 // Don't bother with a SyncPoint, since we've already latched the
787 // relevant frame
788 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700789 }
790
Dan Stozacac35382016-01-27 12:21:06 -0800791 Mutex::Autolock lock(mLocalSyncPointMutex);
792 mLocalSyncPoints.push_back(point);
793 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700794}
795
Mathias Agopian13127d82013-03-05 17:47:11 -0800796void Layer::setFiltering(bool filtering) {
797 mFiltering = filtering;
798}
799
800bool Layer::getFiltering() const {
801 return mFiltering;
802}
803
Mathias Agopian13127d82013-03-05 17:47:11 -0800804// ----------------------------------------------------------------------------
805// local state
806// ----------------------------------------------------------------------------
807
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808static void boundPoint(vec2* point, const Rect& crop) {
809 if (point->x < crop.left) {
810 point->x = crop.left;
811 }
812 if (point->x > crop.right) {
813 point->x = crop.right;
814 }
815 if (point->y < crop.top) {
816 point->y = crop.top;
817 }
818 if (point->y > crop.bottom) {
819 point->y = crop.bottom;
820 }
821}
822
chaviwa76b2712017-09-20 12:02:26 -0700823void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
824 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700826 const Transform renderAreaTransform(renderArea.getTransform());
827 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800828 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700829
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000830 vec2 lt = vec2(win.left, win.top);
831 vec2 lb = vec2(win.left, win.bottom);
832 vec2 rb = vec2(win.right, win.bottom);
833 vec2 rt = vec2(win.right, win.top);
834
Robert Carr1f0a16a2016-10-24 16:27:39 -0700835 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000836 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700837 lt = layerTransform.transform(lt);
838 lb = layerTransform.transform(lb);
839 rb = layerTransform.transform(rb);
840 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000841 }
842
Robert Carrb5d3d262016-03-25 15:08:13 -0700843 if (!s.finalCrop.isEmpty()) {
844 boundPoint(&lt, s.finalCrop);
845 boundPoint(&lb, s.finalCrop);
846 boundPoint(&rb, s.finalCrop);
847 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000848 }
849
Mathias Agopianff2ed702013-09-01 21:36:12 -0700850 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700851 position[0] = renderAreaTransform.transform(lt);
852 position[1] = renderAreaTransform.transform(lb);
853 position[2] = renderAreaTransform.transform(rb);
854 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800855 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700856 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 }
858}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800859
David Sodman41fdfc92017-11-06 16:09:56 -0800860bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700861 const Layer::State& s(mDrawingState);
862 return (s.flags & layer_state_t::eLayerSecure);
863}
864
Mathias Agopian13127d82013-03-05 17:47:11 -0800865void Layer::setVisibleRegion(const Region& visibleRegion) {
866 // always called from main thread
867 this->visibleRegion = visibleRegion;
868}
869
870void Layer::setCoveredRegion(const Region& coveredRegion) {
871 // always called from main thread
872 this->coveredRegion = coveredRegion;
873}
874
David Sodman41fdfc92017-11-06 16:09:56 -0800875void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800876 // always called from main thread
877 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
878}
879
Robert Carre5f4f692018-01-12 13:12:28 -0800880void Layer::clearVisibilityRegions() {
881 visibleRegion.clear();
882 visibleNonTransparentRegion.clear();
883 coveredRegion.clear();
884}
885
Mathias Agopian13127d82013-03-05 17:47:11 -0800886// ----------------------------------------------------------------------------
887// transaction
888// ----------------------------------------------------------------------------
889
Dan Stoza7dde5992015-05-22 09:51:44 -0700890void Layer::pushPendingState() {
891 if (!mCurrentState.modified) {
892 return;
893 }
894
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 // If this transaction is waiting on the receipt of a frame, generate a sync
896 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800897 if (mCurrentState.barrierLayer != nullptr) {
898 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
899 if (barrierLayer == nullptr) {
900 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 // If we can't promote the layer we are intended to wait on,
902 // then it is expired or otherwise invalid. Allow this transaction
903 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800904 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800905 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800906 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800907 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800908 mRemoteSyncPoints.push_back(std::move(syncPoint));
909 } else {
910 // We already missed the frame we're supposed to synchronize
911 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800912 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800913 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 }
915
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 // Wake us up to check if the frame has been received
917 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700918 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 }
920 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700921 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700922}
923
Pablo Ceballos05289c22016-04-14 15:49:55 -0700924void Layer::popPendingState(State* stateToCommit) {
925 auto oldFlags = stateToCommit->flags;
926 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800927 stateToCommit->flags =
928 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700929
930 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700931 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700932}
933
Pablo Ceballos05289c22016-04-14 15:49:55 -0700934bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 bool stateUpdateAvailable = false;
936 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800937 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 if (mRemoteSyncPoints.empty()) {
939 // If we don't have a sync point for this, apply it anyway. It
940 // will be visually wrong, but it should keep us from getting
941 // into too much trouble.
942 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700943 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 stateUpdateAvailable = true;
945 continue;
946 }
947
David Sodman41fdfc92017-11-06 16:09:56 -0800948 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
949 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800950
951 // Signal our end of the sync point and then dispose of it
952 mRemoteSyncPoints.front()->setTransactionApplied();
953 mRemoteSyncPoints.pop_front();
954 continue;
955 }
956
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
958 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700959 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 stateUpdateAvailable = true;
961
962 // Signal our end of the sync point and then dispose of it
963 mRemoteSyncPoints.front()->setTransactionApplied();
964 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800965 } else {
966 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700969 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 stateUpdateAvailable = true;
971 }
972 }
973
974 // If we still have pending updates, wake SurfaceFlinger back up and point
975 // it at this layer so we can process them
976 if (!mPendingStates.empty()) {
977 setTransactionFlags(eTransactionNeeded);
978 mFlinger->setTransactionFlags(eTraversalNeeded);
979 }
980
981 mCurrentState.modified = false;
982 return stateUpdateAvailable;
983}
984
Mathias Agopian13127d82013-03-05 17:47:11 -0800985uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800986 ATRACE_CALL();
987
Dan Stoza7dde5992015-05-22 09:51:44 -0700988 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700989 Layer::State c = getCurrentState();
990 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700991 return 0;
992 }
993
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995
David Sodman41fdfc92017-11-06 16:09:56 -0800996 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700997
David Sodmaneb085e02017-10-05 18:49:04 -0700998 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700999 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001000 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001001 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1002 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1003 " requested={ wh={%4u,%4u} }}\n"
1004 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1005 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001006 this, getName().string(), mCurrentTransform,
1007 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1008 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1009 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1010 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1011 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001013 // record the new size, form this point on, when the client request
1014 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001015 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001017
Robert Carre392b552017-09-19 12:16:05 -07001018 // Don't let Layer::doTransaction update the drawing state
1019 // if we have a pending resize, unless we are in fixed-size mode.
1020 // the drawing state will be updated only once we receive a buffer
1021 // with the correct size.
1022 //
1023 // In particular, we want to make sure the clip (which is part
1024 // of the geometry state) is latched together with the size but is
1025 // latched immediately when no resizing is involved.
1026 //
1027 // If a sideband stream is attached, however, we want to skip this
1028 // optimization so that transactions aren't missed when a buffer
1029 // never arrives
1030 //
1031 // In the case that we don't have a buffer we ignore other factors
1032 // and avoid entering the resizePending state. At a high level the
1033 // resizePending state is to avoid applying the state of the new buffer
1034 // to the old buffer. However in the state where we don't have an old buffer
1035 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001036 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001037 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001038 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001039 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001040 flags |= eDontUpdateGeometryState;
1041 }
1042 }
1043
Robert Carr7bf247e2017-05-18 14:02:49 -07001044 // Here we apply various requested geometry states, depending on our
1045 // latching configuration. See Layer.h for a detailed discussion of
1046 // how geometry latching is controlled.
1047 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001048 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001049
1050 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1051 // mode, which causes attributes which normally latch regardless of scaling mode,
1052 // to be delayed. We copy the requested state to the active state making sure
1053 // to respect these rules (again see Layer.h for a detailed discussion).
1054 //
1055 // There is an awkward asymmetry in the handling of the crop states in the position
1056 // states, as can be seen below. Largely this arises from position and transform
1057 // being stored in the same data structure while having different latching rules.
1058 // b/38182305
1059 //
1060 // Careful that "c" and editCurrentState may not begin as equivalent due to
1061 // applyPendingStates in the presence of deferred transactions.
1062 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001063 float tx = c.active.transform.tx();
1064 float ty = c.active.transform.ty();
1065 c.active = c.requested;
1066 c.active.transform.set(tx, ty);
1067 editCurrentState.active = c.active;
1068 } else {
1069 editCurrentState.active = editCurrentState.requested;
1070 c.active = c.requested;
1071 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001074 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 // invalidate and recompute the visible regions if needed
1076 flags |= Layer::eVisibleRegion;
1077 }
1078
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001079 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 // invalidate and recompute the visible regions if needed
1081 flags |= eVisibleRegion;
1082 this->contentDirty = true;
1083
1084 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001085 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001086 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 }
1088
Dan Stozac8145172016-04-28 16:29:06 -07001089 // If the layer is hidden, signal and clear out all local sync points so
1090 // that transactions for layers depending on this layer's frames becoming
1091 // visible are not blocked
1092 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001093 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001094 }
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001097 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001098 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099}
1100
Pablo Ceballos05289c22016-04-14 15:49:55 -07001101void Layer::commitTransaction(const State& stateToCommit) {
1102 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001103}
1104
Mathias Agopian13127d82013-03-05 17:47:11 -08001105uint32_t Layer::getTransactionFlags(uint32_t flags) {
1106 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1107}
1108
1109uint32_t Layer::setTransactionFlags(uint32_t flags) {
1110 return android_atomic_or(flags, &mTransactionFlags);
1111}
1112
Robert Carr82364e32016-05-15 11:27:47 -07001113bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001114 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 return false;
1116 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001117
1118 // We update the requested and active position simultaneously because
1119 // we want to apply the position portion of the transform matrix immediately,
1120 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001121 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001122 if (immediate && !mFreezeGeometryUpdates) {
1123 // Here we directly update the active state
1124 // unlike other setters, because we store it within
1125 // the transform, but use different latching rules.
1126 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001127 mCurrentState.active.transform.set(x, y);
1128 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001129 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001130
Dan Stoza7dde5992015-05-22 09:51:44 -07001131 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr82364e32016-05-15 11:27:47 -07001135
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1137 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1138 if (idx < 0) {
1139 return false;
1140 }
1141 if (childLayer->setLayer(z)) {
1142 mCurrentChildren.removeAt(idx);
1143 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001146 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001147}
1148
Robert Carr503c7042017-09-27 15:06:08 -07001149bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1150 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1151 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1152 if (idx < 0) {
1153 return false;
1154 }
1155 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1156 mCurrentChildren.removeAt(idx);
1157 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001159 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001160 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001161}
1162
Robert Carrae060832016-11-28 10:51:00 -08001163bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001164 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 mCurrentState.sequence++;
1166 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001167 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001168
1169 // Discard all relative layering.
1170 if (mCurrentState.zOrderRelativeOf != nullptr) {
1171 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1172 if (strongRelative != nullptr) {
1173 strongRelative->removeZOrderRelative(this);
1174 }
1175 mCurrentState.zOrderRelativeOf = nullptr;
1176 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001180
Robert Carrdb66e622017-04-10 16:55:57 -07001181void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1182 mCurrentState.zOrderRelatives.remove(relative);
1183 mCurrentState.sequence++;
1184 mCurrentState.modified = true;
1185 setTransactionFlags(eTransactionNeeded);
1186}
1187
1188void Layer::addZOrderRelative(const wp<Layer>& relative) {
1189 mCurrentState.zOrderRelatives.add(relative);
1190 mCurrentState.modified = true;
1191 mCurrentState.sequence++;
1192 setTransactionFlags(eTransactionNeeded);
1193}
1194
Robert Carr503d2bd2017-12-04 15:49:47 -08001195bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001196 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1197 if (handle == nullptr) {
1198 return false;
1199 }
1200 sp<Layer> relative = handle->owner.promote();
1201 if (relative == nullptr) {
1202 return false;
1203 }
1204
Robert Carr503d2bd2017-12-04 15:49:47 -08001205 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1206 mCurrentState.zOrderRelativeOf == relative) {
1207 return false;
1208 }
1209
Robert Carrdb66e622017-04-10 16:55:57 -07001210 mCurrentState.sequence++;
1211 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001212 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001213
chaviw9ab4bd12017-11-03 13:11:00 -07001214 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1215 if (oldZOrderRelativeOf != nullptr) {
1216 oldZOrderRelativeOf->removeZOrderRelative(this);
1217 }
Robert Carrdb66e622017-04-10 16:55:57 -07001218 mCurrentState.zOrderRelativeOf = relative;
1219 relative->addZOrderRelative(this);
1220
1221 setTransactionFlags(eTransactionNeeded);
1222
1223 return true;
1224}
1225
Mathias Agopian13127d82013-03-05 17:47:11 -08001226bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001227 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 mCurrentState.requested.w = w;
1229 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001234bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001235 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001237 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239 setTransactionFlags(eTransactionNeeded);
1240 return true;
1241}
chaviw13fdc492017-06-27 12:40:18 -07001242
1243bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001244 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1245 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001246 return false;
1247
1248 mCurrentState.sequence++;
1249 mCurrentState.color.r = color.r;
1250 mCurrentState.color.g = color.g;
1251 mCurrentState.color.b = color.b;
1252 mCurrentState.modified = true;
1253 setTransactionFlags(eTransactionNeeded);
1254 return true;
1255}
1256
Mathias Agopian13127d82013-03-05 17:47:11 -08001257bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1258 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001259 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001260 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
1264bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001265 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001266 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001267 setTransactionFlags(eTransactionNeeded);
1268 return true;
1269}
1270bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1271 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001272 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001273 mCurrentState.sequence++;
1274 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001275 mCurrentState.mask = mask;
1276 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 setTransactionFlags(eTransactionNeeded);
1278 return true;
1279}
Robert Carr99e27f02016-06-16 15:18:02 -07001280
1281bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001282 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001283 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001284 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001285 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001286 mCurrentState.crop = crop;
1287 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001288 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1289
Dan Stoza7dde5992015-05-22 09:51:44 -07001290 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001291 setTransactionFlags(eTransactionNeeded);
1292 return true;
1293}
Robert Carr8d5227b2017-03-16 15:41:03 -07001294
1295bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001296 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001297 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001298 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001299 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001300 mCurrentState.finalCrop = crop;
1301 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001302 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1303
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001304 mCurrentState.modified = true;
1305 setTransactionFlags(eTransactionNeeded);
1306 return true;
1307}
Mathias Agopian13127d82013-03-05 17:47:11 -08001308
Robert Carrc3574f72016-03-24 12:19:32 -07001309bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001310 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001311 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001312 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001313 return true;
1314}
1315
rongliucfb187b2018-03-14 12:26:23 -07001316void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001317 mCurrentState.appId = appId;
1318 mCurrentState.type = type;
1319 mCurrentState.modified = true;
1320 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001321}
1322
Mathias Agopian13127d82013-03-05 17:47:11 -08001323bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001324 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 mCurrentState.sequence++;
1326 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001327 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001328 setTransactionFlags(eTransactionNeeded);
1329 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001330}
1331
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001332bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001333 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001334 mCurrentState.sequence++;
1335 mCurrentState.dataSpace = dataSpace;
1336 mCurrentState.modified = true;
1337 setTransactionFlags(eTransactionNeeded);
1338 return true;
1339}
1340
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001341android_dataspace Layer::getDataSpace() const {
1342 return mCurrentState.dataSpace;
1343}
1344
Robert Carr1f0a16a2016-10-24 16:27:39 -07001345uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001346 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001347 if (p == nullptr) {
1348 return getDrawingState().layerStack;
1349 }
1350 return p->getLayerStack();
1351}
1352
David Sodman41fdfc92017-11-06 16:09:56 -08001353void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001354 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001355 mCurrentState.frameNumber = frameNumber;
1356 // We don't set eTransactionNeeded, because just receiving a deferral
1357 // request without any other state updates shouldn't actually induce a delay
1358 mCurrentState.modified = true;
1359 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001360 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001361 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001362 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001363}
1364
David Sodman41fdfc92017-11-06 16:09:56 -08001365void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001366 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1367 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001368}
1369
Dan Stozaee44edd2015-03-23 15:50:23 -07001370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371// ----------------------------------------------------------------------------
1372// pageflip handling...
1373// ----------------------------------------------------------------------------
1374
Robert Carr1f0a16a2016-10-24 16:27:39 -07001375bool Layer::isHiddenByPolicy() const {
1376 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001377 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001378 if (parent != nullptr && parent->isHiddenByPolicy()) {
1379 return true;
1380 }
1381 return s.flags & layer_state_t::eLayerHidden;
1382}
1383
David Sodman41fdfc92017-11-06 16:09:56 -08001384uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001385 // TODO: should we do something special if mSecure is set?
1386 if (mProtectedByApp) {
1387 // need a hardware-protected path to external video sink
1388 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001389 }
Riley Andrews03414a12014-07-01 14:22:59 -07001390 if (mPotentialCursor) {
1391 usage |= GraphicBuffer::USAGE_CURSOR;
1392 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001393 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001394 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001395}
1396
Mathias Agopian84300952012-11-21 16:02:13 -08001397void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001398 uint32_t orientation = 0;
1399 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001400 // The transform hint is used to improve performance, but we can
1401 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001403 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001405 if (orientation & Transform::ROT_INVALID) {
1406 orientation = 0;
1407 }
1408 }
David Sodmaneb085e02017-10-05 18:49:04 -07001409 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001410}
1411
Mathias Agopian13127d82013-03-05 17:47:11 -08001412// ----------------------------------------------------------------------------
1413// debugging
1414// ----------------------------------------------------------------------------
1415
Kalle Raitaa099a242017-01-11 11:17:29 -08001416LayerDebugInfo Layer::getLayerDebugInfo() const {
1417 LayerDebugInfo info;
1418 const Layer::State& ds = getDrawingState();
1419 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001420 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001421 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1422 info.mType = String8(getTypeId());
1423 info.mTransparentRegion = ds.activeTransparentRegion;
1424 info.mVisibleRegion = visibleRegion;
1425 info.mSurfaceDamageRegion = surfaceDamageRegion;
1426 info.mLayerStack = getLayerStack();
1427 info.mX = ds.active.transform.tx();
1428 info.mY = ds.active.transform.ty();
1429 info.mZ = ds.z;
1430 info.mWidth = ds.active.w;
1431 info.mHeight = ds.active.h;
1432 info.mCrop = ds.crop;
1433 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001434 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001435 info.mFlags = ds.flags;
1436 info.mPixelFormat = getPixelFormat();
1437 info.mDataSpace = getDataSpace();
1438 info.mMatrix[0][0] = ds.active.transform[0][0];
1439 info.mMatrix[0][1] = ds.active.transform[0][1];
1440 info.mMatrix[1][0] = ds.active.transform[1][0];
1441 info.mMatrix[1][1] = ds.active.transform[1][1];
1442 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001443 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001444 if (buffer != 0) {
1445 info.mActiveBufferWidth = buffer->getWidth();
1446 info.mActiveBufferHeight = buffer->getHeight();
1447 info.mActiveBufferStride = buffer->getStride();
1448 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001449 } else {
1450 info.mActiveBufferWidth = 0;
1451 info.mActiveBufferHeight = 0;
1452 info.mActiveBufferStride = 0;
1453 info.mActiveBufferFormat = 0;
1454 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001456 info.mNumQueuedFrames = getQueuedFrameCount();
1457 info.mRefreshPending = isBufferLatched();
1458 info.mIsOpaque = isOpaque(ds);
1459 info.mContentDirty = contentDirty;
1460 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001461}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001462
Dan Stozae22aec72016-08-01 13:20:59 -07001463void Layer::miniDumpHeader(String8& result) {
1464 result.append("----------------------------------------");
1465 result.append("---------------------------------------\n");
1466 result.append(" Layer name\n");
1467 result.append(" Z | ");
1468 result.append(" Comp Type | ");
1469 result.append(" Disp Frame (LTRB) | ");
1470 result.append(" Source Crop (LTRB)\n");
1471 result.append("----------------------------------------");
1472 result.append("---------------------------------------\n");
1473}
1474
1475void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001476 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001477 return;
1478 }
1479
1480 String8 name;
1481 if (mName.length() > 77) {
1482 std::string shortened;
1483 shortened.append(mName.string(), 36);
1484 shortened.append("[...]");
1485 shortened.append(mName.string() + (mName.length() - 36), 36);
1486 name = shortened.c_str();
1487 } else {
1488 name = mName;
1489 }
1490
1491 result.appendFormat(" %s\n", name.string());
1492
1493 const Layer::State& layerState(getDrawingState());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001494 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001495 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001496 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001497 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001498 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Chia-I Wu30505fb2018-03-26 16:20:31 -07001499 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001500 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 -07001501
1502 result.append("- - - - - - - - - - - - - - - - - - - - ");
1503 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1504}
Dan Stozae22aec72016-08-01 13:20:59 -07001505
Svetoslavd85084b2014-03-20 10:28:31 -07001506void Layer::dumpFrameStats(String8& result) const {
1507 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001508}
1509
Svetoslavd85084b2014-03-20 10:28:31 -07001510void Layer::clearFrameStats() {
1511 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001512}
1513
Jamie Gennis6547ff42013-07-16 20:12:42 -07001514void Layer::logFrameStats() {
1515 mFrameTracker.logAndResetStats(mName);
1516}
1517
Svetoslavd85084b2014-03-20 10:28:31 -07001518void Layer::getFrameStats(FrameStats* outStats) const {
1519 mFrameTracker.getStats(outStats);
1520}
1521
Brian Andersond6927fb2016-07-23 23:37:30 -07001522void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001523 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001524 Mutex::Autolock lock(mFrameEventHistoryMutex);
1525 mFrameEventHistory.checkFencesForCompletion();
1526 mFrameEventHistory.dump(result);
1527}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001528
Brian Anderson5ea5e592016-12-01 16:54:33 -08001529void Layer::onDisconnect() {
1530 Mutex::Autolock lock(mFrameEventHistoryMutex);
1531 mFrameEventHistory.onDisconnect();
1532}
1533
Brian Anderson3890c392016-07-25 12:48:08 -07001534void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001535 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001536 Mutex::Autolock lock(mFrameEventHistoryMutex);
1537 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001538 // If there are any unsignaled fences in the aquire timeline at this
1539 // point, the previously queued frame hasn't been latched yet. Go ahead
1540 // and try to get the signal time here so the syscall is taken out of
1541 // the main thread's critical path.
1542 mAcquireTimeline.updateSignalTimes();
1543 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001544 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001545 mFrameEventHistory.addQueue(*newTimestamps);
1546 }
1547
Brian Anderson3890c392016-07-25 12:48:08 -07001548 if (outDelta) {
1549 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001550 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001551}
Dan Stozae77c7662016-05-13 11:37:28 -07001552
Chia-I Wu98f1c102017-05-30 14:54:08 -07001553size_t Layer::getChildrenCount() const {
1554 size_t count = 0;
1555 for (const sp<Layer>& child : mCurrentChildren) {
1556 count += 1 + child->getChildrenCount();
1557 }
1558 return count;
1559}
1560
Robert Carr1f0a16a2016-10-24 16:27:39 -07001561void Layer::addChild(const sp<Layer>& layer) {
1562 mCurrentChildren.add(layer);
1563 layer->setParent(this);
1564}
1565
1566ssize_t Layer::removeChild(const sp<Layer>& layer) {
1567 layer->setParent(nullptr);
1568 return mCurrentChildren.remove(layer);
1569}
1570
Robert Carr1db73f62016-12-21 12:58:51 -08001571bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1572 sp<Handle> handle = nullptr;
1573 sp<Layer> newParent = nullptr;
1574 if (newParentHandle == nullptr) {
1575 return false;
1576 }
1577 handle = static_cast<Handle*>(newParentHandle.get());
1578 newParent = handle->owner.promote();
1579 if (newParent == nullptr) {
1580 ALOGE("Unable to promote Layer handle");
1581 return false;
1582 }
1583
1584 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001585 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001586
1587 sp<Client> client(child->mClientRef.promote());
1588 if (client != nullptr) {
1589 client->setParentLayer(newParent);
1590 }
1591 }
1592 mCurrentChildren.clear();
1593
1594 return true;
1595}
1596
Robert Carr15eae092018-03-23 13:43:53 -07001597void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001598 for (const sp<Layer>& child : mDrawingChildren) {
1599 child->mDrawingParent = newParent;
1600 }
1601}
1602
chaviwf1961f72017-09-18 16:41:07 -07001603bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1604 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001605 return false;
1606 }
1607
1608 auto handle = static_cast<Handle*>(newParentHandle.get());
1609 sp<Layer> newParent = handle->owner.promote();
1610 if (newParent == nullptr) {
1611 ALOGE("Unable to promote Layer handle");
1612 return false;
1613 }
1614
chaviwf1961f72017-09-18 16:41:07 -07001615 sp<Layer> parent = getParent();
1616 if (parent != nullptr) {
1617 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001618 }
chaviwf1961f72017-09-18 16:41:07 -07001619 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001620
chaviwf1961f72017-09-18 16:41:07 -07001621 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001622 sp<Client> newParentClient(newParent->mClientRef.promote());
1623
chaviwf1961f72017-09-18 16:41:07 -07001624 if (client != newParentClient) {
1625 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001626 }
1627
chaviw06178942017-07-27 10:25:59 -07001628 return true;
1629}
1630
Robert Carr9524cb32017-02-13 11:32:32 -08001631bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001632 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001633 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001634 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001635 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001636 client->detachLayer(child.get());
1637 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001638 }
Robert Carr7f619b22017-11-06 12:56:35 -08001639 }
Robert Carr9524cb32017-02-13 11:32:32 -08001640
1641 return true;
1642}
1643
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001645 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646}
1647
1648void Layer::clearSyncPoints() {
1649 for (const auto& child : mCurrentChildren) {
1650 child->clearSyncPoints();
1651 }
1652
1653 Mutex::Autolock lock(mLocalSyncPointMutex);
1654 for (auto& point : mLocalSyncPoints) {
1655 point->setFrameAvailable();
1656 }
1657 mLocalSyncPoints.clear();
1658}
1659
1660int32_t Layer::getZ() const {
1661 return mDrawingState.z;
1662}
1663
Robert Carr29abff82017-12-04 13:51:20 -08001664bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1665 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1666 const State& state = useDrawing ? mDrawingState : mCurrentState;
1667 return state.zOrderRelativeOf != nullptr;
1668}
1669
David Sodman41fdfc92017-11-06 16:09:56 -08001670__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001671 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001672 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1673 "makeTraversalList received invalid stateSet");
1674 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1675 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1676 const State& state = useDrawing ? mDrawingState : mCurrentState;
1677
Robert Carr29abff82017-12-04 13:51:20 -08001678 if (state.zOrderRelatives.size() == 0) {
1679 *outSkipRelativeZUsers = true;
1680 return children;
1681 }
1682
Robert Carrdb66e622017-04-10 16:55:57 -07001683 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001684 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001685 sp<Layer> strongRelative = weakRelative.promote();
1686 if (strongRelative != nullptr) {
1687 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001688 }
1689 }
1690
Dan Stoza412903f2017-04-27 13:42:17 -07001691 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001692 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1693 if (childState.zOrderRelativeOf != nullptr) {
1694 continue;
1695 }
Robert Carrdb66e622017-04-10 16:55:57 -07001696 traverse.add(child);
1697 }
1698
1699 return traverse;
1700}
1701
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702/**
Robert Carrdb66e622017-04-10 16:55:57 -07001703 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 */
Dan Stoza412903f2017-04-27 13:42:17 -07001705void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001706 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1707 // produce a new list for traversing, including our relatives, and not including our children
1708 // who are relatives of another surface. In the case that there are no relative Z,
1709 // makeTraversalList returns our children directly to avoid significant overhead.
1710 // However in this case we need to take the responsibility for filtering children which
1711 // are relatives of another surface here.
1712 bool skipRelativeZUsers = false;
1713 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001714
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001716 for (; i < list.size(); i++) {
1717 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001718 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1719 continue;
1720 }
1721
Robert Carrdb66e622017-04-10 16:55:57 -07001722 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001724 }
Dan Stoza412903f2017-04-27 13:42:17 -07001725 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726 }
Robert Carr29abff82017-12-04 13:51:20 -08001727
Dan Stoza412903f2017-04-27 13:42:17 -07001728 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001729 for (; i < list.size(); i++) {
1730 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001731
1732 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1733 continue;
1734 }
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
1737}
1738
1739/**
Robert Carrdb66e622017-04-10 16:55:57 -07001740 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 */
Dan Stoza412903f2017-04-27 13:42:17 -07001742void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1743 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001744 // See traverseInZOrder for documentation.
1745 bool skipRelativeZUsers = false;
1746 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001747
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001749 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001750 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001751
1752 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1753 continue;
1754 }
1755
Robert Carrdb66e622017-04-10 16:55:57 -07001756 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757 break;
1758 }
Dan Stoza412903f2017-04-27 13:42:17 -07001759 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 }
Dan Stoza412903f2017-04-27 13:42:17 -07001761 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001762 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001763 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001764
1765 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1766 continue;
1767 }
1768
Dan Stoza412903f2017-04-27 13:42:17 -07001769 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 }
1771}
1772
chaviwa76b2712017-09-20 12:02:26 -07001773/**
1774 * Traverse only children in z order, ignoring relative layers.
1775 */
1776void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1777 const LayerVector::Visitor& visitor) {
1778 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1779 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1780
1781 size_t i = 0;
1782 for (; i < children.size(); i++) {
1783 const auto& relative = children[i];
1784 if (relative->getZ() >= 0) {
1785 break;
1786 }
1787 relative->traverseChildrenInZOrder(stateSet, visitor);
1788 }
1789 visitor(this);
1790 for (; i < children.size(); i++) {
1791 const auto& relative = children[i];
1792 relative->traverseChildrenInZOrder(stateSet, visitor);
1793 }
1794}
1795
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796Transform Layer::getTransform() const {
1797 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001798 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001799 if (p != nullptr) {
1800 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001801
1802 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1803 // it isFixedSize) then there may be additional scaling not accounted
1804 // for in the transform. We need to mirror this scaling in child surfaces
1805 // or we will break the contract where WM can treat child surfaces as
1806 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001807 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001808 int bufferWidth;
1809 int bufferHeight;
1810 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001811 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1812 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001813 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001814 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1815 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001816 }
David Sodman41fdfc92017-11-06 16:09:56 -08001817 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1818 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001819 Transform extraParentScaling;
1820 extraParentScaling.set(sx, 0, 0, sy);
1821 t = t * extraParentScaling;
1822 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 }
1824 return t * getDrawingState().active.transform;
1825}
1826
chaviw13fdc492017-06-27 12:40:18 -07001827half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001828 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001829
chaviw13fdc492017-06-27 12:40:18 -07001830 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1831 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001832}
Robert Carr6452f122017-03-21 10:41:29 -07001833
chaviw13fdc492017-06-27 12:40:18 -07001834half4 Layer::getColor() const {
1835 const half4 color(getDrawingState().color);
1836 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001837}
Robert Carr6452f122017-03-21 10:41:29 -07001838
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839void Layer::commitChildList() {
1840 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1841 const auto& child = mCurrentChildren[i];
1842 child->commitChildList();
1843 }
1844 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001845 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846}
1847
chaviw1d044282017-09-27 12:19:28 -07001848void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1849 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1850 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1851 const State& state = useDrawing ? mDrawingState : mCurrentState;
1852
1853 Transform requestedTransform = state.active.transform;
1854 Transform transform = getTransform();
1855
1856 layerInfo->set_id(sequence);
1857 layerInfo->set_name(getName().c_str());
1858 layerInfo->set_type(String8(getTypeId()));
1859
1860 for (const auto& child : children) {
1861 layerInfo->add_children(child->sequence);
1862 }
1863
1864 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1865 sp<Layer> strongRelative = weakRelative.promote();
1866 if (strongRelative != nullptr) {
1867 layerInfo->add_relatives(strongRelative->sequence);
1868 }
1869 }
1870
1871 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1872 layerInfo->mutable_transparent_region());
1873 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1874 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1875
1876 layerInfo->set_layer_stack(getLayerStack());
1877 layerInfo->set_z(state.z);
1878
1879 PositionProto* position = layerInfo->mutable_position();
1880 position->set_x(transform.tx());
1881 position->set_y(transform.ty());
1882
1883 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1884 requestedPosition->set_x(requestedTransform.tx());
1885 requestedPosition->set_y(requestedTransform.ty());
1886
1887 SizeProto* size = layerInfo->mutable_size();
1888 size->set_w(state.active.w);
1889 size->set_h(state.active.h);
1890
1891 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1892 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1893
1894 layerInfo->set_is_opaque(isOpaque(state));
1895 layerInfo->set_invalidate(contentDirty);
1896 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1897 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1898 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1899 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1900 layerInfo->set_flags(state.flags);
1901
1902 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1903 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1904
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001905 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001906 if (parent != nullptr) {
1907 layerInfo->set_parent(parent->sequence);
1908 }
1909
1910 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1911 if (zOrderRelativeOf != nullptr) {
1912 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1913 }
1914
David Sodman0cc69182017-11-17 12:12:07 -08001915 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001916 if (buffer != nullptr) {
1917 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001918 }
1919
1920 layerInfo->set_queued_frames(getQueuedFrameCount());
1921 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001922 layerInfo->set_window_type(state.type);
1923 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001924}
1925
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001926void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1927 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1928
1929 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1930
1931 const Rect& frame = hwcInfo.displayFrame;
1932 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1933
1934 const FloatRect& crop = hwcInfo.sourceCrop;
1935 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1936
1937 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1938 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001939
1940 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1941 layerInfo->set_hwc_composition_type(compositionType);
1942
1943 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1944 static_cast<BufferLayer*>(this)->isProtected()) {
1945 layerInfo->set_is_protected(true);
1946 } else {
1947 layerInfo->set_is_protected(false);
1948 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001949}
1950
Mathias Agopian13127d82013-03-05 17:47:11 -08001951// ---------------------------------------------------------------------------
1952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001954
1955#if defined(__gl_h_)
1956#error "don't include gl/gl.h in this file"
1957#endif
1958
1959#if defined(__gl2_h_)
1960#error "don't include gl2/gl2.h in this file"
1961#endif