blob: ff5e6f6e36c20fef730c3c58795f36991894e2a1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <gui/BufferQueue.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080052#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
David Sodman9eeae692017-11-02 10:53:32 -070065LayerBE::LayerBE()
David Sodman5b4cffc2017-11-23 13:20:29 -080066 : mBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
67 mBuffer(nullptr),
68 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
David Sodman9eeae692017-11-02 10:53:32 -070069}
70
71
Mathias Agopian13127d82013-03-05 17:47:11 -080072int32_t Layer::sSequence = 1;
73
David Sodman41fdfc92017-11-06 16:09:56 -080074Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
75 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070076 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080077 sequence(uint32_t(android_atomic_inc(&sSequence))),
78 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080079 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070080 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080081 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070082 mPendingStateMutex(),
83 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070084 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080085 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080086 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070087 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070088 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070089 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080090 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080091 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mFiltering(false),
93 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080094 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070095 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070096 mPotentialCursor(false),
97 mQueueItemLock(),
98 mQueueItemCondition(),
99 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700100 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -0700101 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -0800102 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
106 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800107 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
108 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
109 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700112 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 mCurrentState.active.w = w;
115 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700116 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800117 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700118 mCurrentState.crop.makeInvalid();
119 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700120 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
121 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700123 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700127 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500128 mCurrentState.appId = 0;
129 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130
131 // drawing state & current state are identical
132 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700133
Dan Stoza9e56aa02015-11-02 13:00:03 -0800134 const auto& hwc = flinger->getHwComposer();
135 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
136 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700137 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800138
139 CompositorTiming compositorTiming;
140 flinger->getCompositorTiming(&compositorTiming);
141 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700142
Jamie Gennise8696a42012-01-15 18:54:57 -0800143}
144
David Sodman41fdfc92017-11-06 16:09:56 -0800145void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700146
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700147Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700149}
150
Mathias Agopian13127d82013-03-05 17:47:11 -0800151// ---------------------------------------------------------------------------
152// callbacks
153// ---------------------------------------------------------------------------
154
David Sodmaneb085e02017-10-05 18:49:04 -0700155/*
156 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
157 * Layer. So, the implementation is done in BufferLayer. When called on a
158 * ColorLayer object, it's essentially a NOP.
159 */
David Sodmaneb085e02017-10-05 18:49:04 -0700160void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800161
Chia-I Wuc6657022017-08-15 11:18:17 -0700162void Layer::onRemovedFromCurrentState() {
163 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
164
chaviw8b3871a2017-11-01 17:41:01 -0700165 mPendingRemoval = true;
166
Robert Carr5edb1ad2017-04-25 10:54:24 -0700167 if (mCurrentState.zOrderRelativeOf != nullptr) {
168 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
169 if (strongRelative != nullptr) {
170 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700171 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700172 }
173 mCurrentState.zOrderRelativeOf = nullptr;
174 }
175
Chia-I Wuc6657022017-08-15 11:18:17 -0700176 for (const auto& child : mCurrentChildren) {
177 child->onRemovedFromCurrentState();
178 }
179}
Chia-I Wu38512252017-05-17 14:36:16 -0700180
Chia-I Wuc6657022017-08-15 11:18:17 -0700181void Layer::onRemoved() {
182 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700183 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700184
Steven Thomasb02664d2017-07-26 18:48:28 -0700185 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700186
Robert Carr1f0a16a2016-10-24 16:27:39 -0700187 for (const auto& child : mCurrentChildren) {
188 child->onRemoved();
189 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700190}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700191
Mathias Agopian13127d82013-03-05 17:47:11 -0800192// ---------------------------------------------------------------------------
193// set-up
194// ---------------------------------------------------------------------------
195
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700196const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800197 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
chaviw13fdc492017-06-27 12:40:18 -0700200bool Layer::getPremultipledAlpha() const {
201 return mPremultipliedAlpha;
202}
203
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700204sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800205 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700206 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800207}
208
209// ---------------------------------------------------------------------------
210// h/w composer set-up
211// ---------------------------------------------------------------------------
212
Steven Thomasb02664d2017-07-26 18:48:28 -0700213bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700214 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700215 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700216 HWC2::Layer* layer = hwc->createLayer(hwcId);
217 if (!layer) {
218 return false;
219 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700220 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 hwcInfo.hwc = hwc;
222 hwcInfo.layer = layer;
223 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700224 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 return true;
226}
227
Chia-I Wu83806892017-11-16 10:50:20 -0800228bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700229 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800230 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800233 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
235 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
236 // The layer destroyed listener should have cleared the entry from
237 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700238 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
239 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800240 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700241}
242
243void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700244 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700245 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700246 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
247 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700248 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800250 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700251}
Steven Thomasb02664d2017-07-26 18:48:28 -0700252
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800253Rect Layer::getContentCrop() const {
254 // this is the crop rectangle that applies to the buffer
255 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700256 Rect crop;
257 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800258 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700259 crop = mCurrentCrop;
David Sodman5b4cffc2017-11-23 13:20:29 -0800260 } else if (getBE().mBuffer != NULL) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261 // otherwise we use the whole buffer
David Sodman5b4cffc2017-11-23 13:20:29 -0800262 crop = getBE().mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700263 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700265 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 return crop;
268}
269
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700270static Rect reduce(const Rect& win, const Region& exclude) {
271 if (CC_LIKELY(exclude.isEmpty())) {
272 return win;
273 }
274 if (exclude.isRect()) {
275 return win.reduce(exclude.getBounds());
276 }
277 return Region(win).subtract(exclude).getBounds();
278}
279
Robert Carr1f0a16a2016-10-24 16:27:39 -0700280Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
281 const Layer::State& s(getDrawingState());
282 Rect win(s.active.w, s.active.h);
283
284 if (!s.crop.isEmpty()) {
285 win.intersect(s.crop, &win);
286 }
287
288 Transform t = getTransform();
289 win = t.transform(win);
290
Robert Carr41b08b52017-06-01 16:11:34 -0700291 if (!s.finalCrop.isEmpty()) {
292 win.intersect(s.finalCrop, &win);
293 }
294
Chia-I Wue41dbe62017-06-13 14:10:56 -0700295 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
297 // When calculating the parent bounds for purposes of clipping,
298 // we don't need to constrain the parent to its transparent region.
299 // The transparent region is an optimization based on the
300 // buffer contents of the layer, but does not affect the space allocated to
301 // it by policy, and thus children should be allowed to extend into the
302 // parent's transparent region. In fact one of the main uses, is to reduce
303 // buffer allocation size in cases where a child window sits behind a main window
304 // (by marking the hole in the parent window as a transparent region)
305 if (p != nullptr) {
306 Rect bounds = p->computeScreenBounds(false);
307 bounds.intersect(win, &win);
308 }
309
310 if (reduceTransparentRegion) {
311 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
312 win = reduce(win, screenTransparentRegion);
313 }
314
315 return win;
316}
317
Mathias Agopian13127d82013-03-05 17:47:11 -0800318Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700319 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700320 return computeBounds(s.activeTransparentRegion);
321}
322
323Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
324 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800325 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700326
327 if (!s.crop.isEmpty()) {
328 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800329 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700330
331 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700332 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700333 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800334 // Look in computeScreenBounds recursive call for explanation of
335 // why we pass false here.
336 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700337 }
338
339 Transform t = getTransform();
340 if (p != nullptr) {
341 win = t.transform(win);
342 win.intersect(bounds, &win);
343 win = t.inverse().transform(win);
344 }
345
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700346 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700347 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800348}
349
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700351 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800352 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700353 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800354
355 // apply the projection's clipping to the window crop in
356 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700357 // if there are no window scaling involved, this operation will map to full
358 // pixels in the buffer.
359 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
360 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700361
362 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700363 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700364 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700365 }
366
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 Transform t = getTransform();
368 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000369 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
370 activeCrop.clear();
371 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700372 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800373 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000374 activeCrop.clear();
375 }
376 }
chaviwb1154d12017-10-31 14:15:36 -0700377
378 const auto& p = mDrawingParent.promote();
379 if (p != nullptr) {
380 auto parentCrop = p->computeInitialCrop(hw);
381 activeCrop.intersect(parentCrop, &activeCrop);
382 }
383
Robert Carr1f0a16a2016-10-24 16:27:39 -0700384 return activeCrop;
385}
386
Dan Stoza5a423ea2017-02-16 14:10:39 -0800387FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700388 // the content crop is the area of the content that gets scaled to the
389 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800390 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700391
392 // In addition there is a WM-specified crop we pull from our drawing state.
393 const State& s(getDrawingState());
394
395 // Screen space to make reduction to parent crop clearer.
396 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700397 Transform t = getTransform();
398 // Back to layer space to work with the content crop.
399 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800400
Michael Lentine28ea2172014-11-19 18:32:37 -0800401 // This needs to be here as transform.transform(Rect) computes the
402 // transformed rect and then takes the bounding box of the result before
403 // returning. This means
404 // transform.inverse().transform(transform.transform(Rect)) != Rect
405 // in which case we need to make sure the final rect is clipped to the
406 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000407 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
408 activeCrop.clear();
409 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800410
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700411 // subtract the transparent region and snap to the bounds
412 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
413
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 // Transform the window crop to match the buffer coordinate system,
415 // which means using the inverse of the current transform set on the
416 // SurfaceFlingerConsumer.
417 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700418 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700420 * the code below applies the primary display's inverse transform to the
421 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 */
David Sodman41fdfc92017-11-06 16:09:56 -0800423 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424 // calculate the inverse transform
425 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800426 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800427 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800429 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800430 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431
432 int winWidth = s.active.w;
433 int winHeight = s.active.h;
434 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
435 // If the activeCrop has been rotate the ends are rotated but not
436 // the space itself so when transforming ends back we can't rely on
437 // a modification of the axes of rotation. To account for this we
438 // need to reorient the inverse rotation in terms of the current
439 // axes of rotation.
440 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
441 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
442 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800443 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 }
445 winWidth = s.active.h;
446 winHeight = s.active.w;
447 }
David Sodman41fdfc92017-11-06 16:09:56 -0800448 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449
450 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800451 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 float yScale = crop.getHeight() / float(winHeight);
453
David Sodman41fdfc92017-11-06 16:09:56 -0800454 float insetL = winCrop.left * xScale;
455 float insetT = winCrop.top * yScale;
456 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 float insetB = (winHeight - winCrop.bottom) * yScale;
458
David Sodman41fdfc92017-11-06 16:09:56 -0800459 crop.left += insetL;
460 crop.top += insetT;
461 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 crop.bottom -= insetB;
463
Mathias Agopian13127d82013-03-05 17:47:11 -0800464 return crop;
465}
466
Robert Carrae060832016-11-28 10:51:00 -0800467void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700468{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700470 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700471
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700472 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 hwcInfo.forceClientComposition = false;
474
475 if (isSecure() && !displayDevice->isSecure()) {
476 hwcInfo.forceClientComposition = true;
477 }
478
479 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700480
Mathias Agopian13127d82013-03-05 17:47:11 -0800481 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700482 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500483 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700484 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800485 blendMode =
486 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 }
David Revemanecf0fa52017-03-03 11:32:44 -0500488 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800489 ALOGE_IF(error != HWC2::Error::None,
490 "[%s] Failed to set blend mode %s:"
491 " %s (%d)",
492 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
493 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800494
495 // apply the layer's transform, followed by the display's global transform
496 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700497 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700498 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700499 if (!s.crop.isEmpty()) {
500 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700501 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800502 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700505 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800506 // This needs to be here as transform.transform(Rect) computes the
507 // transformed rect and then takes the bounding box of the result before
508 // returning. This means
509 // transform.inverse().transform(transform.transform(Rect)) != Rect
510 // in which case we need to make sure the final rect is clipped to the
511 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800512 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000513 activeCrop.clear();
514 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700515 // mark regions outside the crop as transparent
516 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800517 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
518 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
519 activeTransparentRegion.orSelf(
520 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700521 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700522
523 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700524 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800525 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000526 frame.clear();
527 }
528 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
530 frame.clear();
531 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800532 const Transform& tr(displayDevice->getTransform());
533 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500534 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700535 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800536 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
537 transformedFrame.left, transformedFrame.top, transformedFrame.right,
538 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700539 } else {
540 hwcInfo.displayFrame = transformedFrame;
541 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542
Dan Stoza5a423ea2017-02-16 14:10:39 -0800543 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700545 if (error != HWC2::Error::None) {
546 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800547 "%s (%d)",
548 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
549 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700550 } else {
551 hwcInfo.sourceCrop = sourceCrop;
552 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553
chaviw13fdc492017-06-27 12:40:18 -0700554 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700555 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800556 ALOGE_IF(error != HWC2::Error::None,
557 "[%s] Failed to set plane alpha %.3f: "
558 "%s (%d)",
559 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560
Robert Carrae060832016-11-28 10:51:00 -0800561 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800562 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
563 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500564
Albert Chaulk2a589632017-05-04 16:59:44 -0400565 int type = s.type;
566 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700567 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400568 if (parent.get()) {
569 auto& parentState = parent->getDrawingState();
570 type = parentState.type;
571 appId = parentState.appId;
572 }
573
574 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800575 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
576 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800577
Mathias Agopian29a367b2011-07-12 14:51:45 -0700578 /*
579 * Transformations are applied in this order:
580 * 1) buffer orientation/flip/mirror
581 * 2) state transformation (window manager)
582 * 3) layer orientation (screen orientation)
583 * (NOTE: the matrices are multiplied in reverse order)
584 */
585
586 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700587 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700588
Robert Carrcae605c2017-03-29 12:10:31 -0700589 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700590 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700591 * the code below applies the primary display's inverse transform to the
592 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700593 */
David Sodman41fdfc92017-11-06 16:09:56 -0800594 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700595 // calculate the inverse transform
596 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800597 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598 }
Robert Carrcae605c2017-03-29 12:10:31 -0700599
600 /*
601 * Here we cancel out the orientation component of the WM transform.
602 * The scaling and translate components are already included in our bounds
603 * computation so it's enough to just omit it in the composition.
604 * See comment in onDraw with ref to b/36727915 for why.
605 */
606 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700607 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700608
609 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800610 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611 if (orientation & Transform::ROT_INVALID) {
612 // we can only handle simple transformation
613 hwcInfo.forceClientComposition = true;
614 } else {
615 auto transform = static_cast<HWC2::Transform>(orientation);
616 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800617 ALOGE_IF(error != HWC2::Error::None,
618 "[%s] Failed to set transform %s: "
619 "%s (%d)",
620 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
621 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700623}
624
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700626 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
628 return;
629 }
630
David Sodman6f65f3e2017-11-03 14:28:09 -0700631 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632}
Dan Stozaee44edd2015-03-23 15:50:23 -0700633
chaviwc9232ed2017-11-14 15:31:15 -0800634bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700635 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800636 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
637 return false;
638 }
639
David Sodman6f65f3e2017-11-03 14:28:09 -0700640 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800641}
642
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
644 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700645 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700646 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 return;
648 }
649
650 // This gives us only the "orientation" component of the transform
651 const State& s(getCurrentState());
652
653 // Apply the layer's transform, followed by the display's global transform
654 // Here we're guaranteed that the layer's transform preserves rects
655 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700656 if (!s.crop.isEmpty()) {
657 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 }
659 // Subtract the transparent region and snap to the bounds
660 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700661 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700663 if (!s.finalCrop.isEmpty()) {
664 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000665 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666 auto& displayTransform(displayDevice->getTransform());
667 auto position = displayTransform.transform(frame);
668
David Sodman6f65f3e2017-11-03 14:28:09 -0700669 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700670 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800671 ALOGE_IF(error != HWC2::Error::None,
672 "[%s] Failed to set cursor position "
673 "to (%d, %d): %s (%d)",
674 mName.string(), position.left, position.top, to_string(error).c_str(),
675 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676}
Riley Andrews03414a12014-07-01 14:22:59 -0700677
Mathias Agopian13127d82013-03-05 17:47:11 -0800678// ---------------------------------------------------------------------------
679// drawing...
680// ---------------------------------------------------------------------------
681
chaviwa76b2712017-09-20 12:02:26 -0700682void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
683 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800684}
685
chaviwa76b2712017-09-20 12:02:26 -0700686void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
687 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800688}
689
chaviwa76b2712017-09-20 12:02:26 -0700690void Layer::draw(const RenderArea& renderArea) const {
691 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800692}
693
David Sodman41fdfc92017-11-06 16:09:56 -0800694void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
695 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700696 RenderEngine& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700697 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700698 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700699 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800700}
701
chaviwa76b2712017-09-20 12:02:26 -0700702void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800703 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800704}
705
David Sodman41fdfc92017-11-06 16:09:56 -0800706void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700707 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 ALOGE("setCompositionType called without a valid HWC layer");
709 return;
710 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700711 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800713 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
714 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715 if (hwcInfo.compositionType != type) {
716 ALOGV(" actually setting");
717 hwcInfo.compositionType = type;
718 if (callIntoHwc) {
719 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800720 ALOGE_IF(error != HWC2::Error::None,
721 "[%s] Failed to set "
722 "composition type %s: %s (%d)",
723 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
724 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 }
726 }
727}
728
729HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700730 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
731 // If we're querying the composition type for a display that does not
732 // have a HWC counterpart, then it will always be Client
733 return HWC2::Composition::Client;
734 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700735 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700736 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 return HWC2::Composition::Invalid;
738 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700739 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740}
741
742void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700743 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800744 ALOGE("setClearClientTarget called without a valid HWC layer");
745 return;
746 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700747 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748}
749
750bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700751 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752 ALOGE("getClearClientTarget called without a valid HWC layer");
753 return false;
754 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700755 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800757
Dan Stozacac35382016-01-27 12:21:06 -0800758bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
759 if (point->getFrameNumber() <= mCurrentFrameNumber) {
760 // Don't bother with a SyncPoint, since we've already latched the
761 // relevant frame
762 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700763 }
764
Dan Stozacac35382016-01-27 12:21:06 -0800765 Mutex::Autolock lock(mLocalSyncPointMutex);
766 mLocalSyncPoints.push_back(point);
767 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700768}
769
Mathias Agopian13127d82013-03-05 17:47:11 -0800770void Layer::setFiltering(bool filtering) {
771 mFiltering = filtering;
772}
773
774bool Layer::getFiltering() const {
775 return mFiltering;
776}
777
Mathias Agopian13127d82013-03-05 17:47:11 -0800778// ----------------------------------------------------------------------------
779// local state
780// ----------------------------------------------------------------------------
781
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000782static void boundPoint(vec2* point, const Rect& crop) {
783 if (point->x < crop.left) {
784 point->x = crop.left;
785 }
786 if (point->x > crop.right) {
787 point->x = crop.right;
788 }
789 if (point->y < crop.top) {
790 point->y = crop.top;
791 }
792 if (point->y > crop.bottom) {
793 point->y = crop.bottom;
794 }
795}
796
chaviwa76b2712017-09-20 12:02:26 -0700797void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
798 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700799 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700800 const Transform renderAreaTransform(renderArea.getTransform());
801 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700802 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700803
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000804 vec2 lt = vec2(win.left, win.top);
805 vec2 lb = vec2(win.left, win.bottom);
806 vec2 rb = vec2(win.right, win.bottom);
807 vec2 rt = vec2(win.right, win.top);
808
Robert Carr1f0a16a2016-10-24 16:27:39 -0700809 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000810 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700811 lt = layerTransform.transform(lt);
812 lb = layerTransform.transform(lb);
813 rb = layerTransform.transform(rb);
814 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000815 }
816
Robert Carrb5d3d262016-03-25 15:08:13 -0700817 if (!s.finalCrop.isEmpty()) {
818 boundPoint(&lt, s.finalCrop);
819 boundPoint(&lb, s.finalCrop);
820 boundPoint(&rb, s.finalCrop);
821 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000822 }
823
Mathias Agopianff2ed702013-09-01 21:36:12 -0700824 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700825 position[0] = renderAreaTransform.transform(lt);
826 position[1] = renderAreaTransform.transform(lb);
827 position[2] = renderAreaTransform.transform(rb);
828 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800829 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700830 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800831 }
832}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800833
David Sodman41fdfc92017-11-06 16:09:56 -0800834bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700835 const Layer::State& s(mDrawingState);
836 return (s.flags & layer_state_t::eLayerSecure);
837}
838
Mathias Agopian13127d82013-03-05 17:47:11 -0800839void Layer::setVisibleRegion(const Region& visibleRegion) {
840 // always called from main thread
841 this->visibleRegion = visibleRegion;
842}
843
844void Layer::setCoveredRegion(const Region& coveredRegion) {
845 // always called from main thread
846 this->coveredRegion = coveredRegion;
847}
848
David Sodman41fdfc92017-11-06 16:09:56 -0800849void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800850 // always called from main thread
851 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
852}
853
854// ----------------------------------------------------------------------------
855// transaction
856// ----------------------------------------------------------------------------
857
Dan Stoza7dde5992015-05-22 09:51:44 -0700858void Layer::pushPendingState() {
859 if (!mCurrentState.modified) {
860 return;
861 }
862
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 // If this transaction is waiting on the receipt of a frame, generate a sync
864 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800865 if (mCurrentState.barrierLayer != nullptr) {
866 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
867 if (barrierLayer == nullptr) {
868 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700869 // If we can't promote the layer we are intended to wait on,
870 // then it is expired or otherwise invalid. Allow this transaction
871 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800872 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800873 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800874 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800875 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800876 mRemoteSyncPoints.push_back(std::move(syncPoint));
877 } else {
878 // We already missed the frame we're supposed to synchronize
879 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800880 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800881 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700882 }
883
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 // Wake us up to check if the frame has been received
885 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700886 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 }
888 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700889 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700890}
891
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892void Layer::popPendingState(State* stateToCommit) {
893 auto oldFlags = stateToCommit->flags;
894 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800895 stateToCommit->flags =
896 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700897
898 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700899 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700900}
901
Pablo Ceballos05289c22016-04-14 15:49:55 -0700902bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 bool stateUpdateAvailable = false;
904 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800905 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700906 if (mRemoteSyncPoints.empty()) {
907 // If we don't have a sync point for this, apply it anyway. It
908 // will be visually wrong, but it should keep us from getting
909 // into too much trouble.
910 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700911 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 stateUpdateAvailable = true;
913 continue;
914 }
915
David Sodman41fdfc92017-11-06 16:09:56 -0800916 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
917 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800918
919 // Signal our end of the sync point and then dispose of it
920 mRemoteSyncPoints.front()->setTransactionApplied();
921 mRemoteSyncPoints.pop_front();
922 continue;
923 }
924
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
926 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 stateUpdateAvailable = true;
929
930 // Signal our end of the sync point and then dispose of it
931 mRemoteSyncPoints.front()->setTransactionApplied();
932 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800933 } else {
934 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700937 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 stateUpdateAvailable = true;
939 }
940 }
941
942 // If we still have pending updates, wake SurfaceFlinger back up and point
943 // it at this layer so we can process them
944 if (!mPendingStates.empty()) {
945 setTransactionFlags(eTransactionNeeded);
946 mFlinger->setTransactionFlags(eTraversalNeeded);
947 }
948
949 mCurrentState.modified = false;
950 return stateUpdateAvailable;
951}
952
Mathias Agopian13127d82013-03-05 17:47:11 -0800953uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800954 ATRACE_CALL();
955
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700957 Layer::State c = getCurrentState();
958 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 return 0;
960 }
961
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700962 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963
David Sodman41fdfc92017-11-06 16:09:56 -0800964 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700965
David Sodmaneb085e02017-10-05 18:49:04 -0700966 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700967 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000968 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800969 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
970 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
971 " requested={ wh={%4u,%4u} }}\n"
972 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
973 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -0700974 this, getName().string(), mCurrentTransform,
975 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
976 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
977 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
978 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
979 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700981 // record the new size, form this point on, when the client request
982 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700983 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700985
Robert Carre392b552017-09-19 12:16:05 -0700986 // Don't let Layer::doTransaction update the drawing state
987 // if we have a pending resize, unless we are in fixed-size mode.
988 // the drawing state will be updated only once we receive a buffer
989 // with the correct size.
990 //
991 // In particular, we want to make sure the clip (which is part
992 // of the geometry state) is latched together with the size but is
993 // latched immediately when no resizing is involved.
994 //
995 // If a sideband stream is attached, however, we want to skip this
996 // optimization so that transactions aren't missed when a buffer
997 // never arrives
998 //
999 // In the case that we don't have a buffer we ignore other factors
1000 // and avoid entering the resizePending state. At a high level the
1001 // resizePending state is to avoid applying the state of the new buffer
1002 // to the old buffer. However in the state where we don't have an old buffer
1003 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001004 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman5b4cffc2017-11-23 13:20:29 -08001005 (getBE().mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001006 if (!isFixedSize()) {
David Sodman386c22e2017-11-09 16:34:46 -08001007 if (resizePending && getBE().mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001008 flags |= eDontUpdateGeometryState;
1009 }
1010 }
1011
Robert Carr7bf247e2017-05-18 14:02:49 -07001012 // Here we apply various requested geometry states, depending on our
1013 // latching configuration. See Layer.h for a detailed discussion of
1014 // how geometry latching is controlled.
1015 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001016 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001017
1018 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1019 // mode, which causes attributes which normally latch regardless of scaling mode,
1020 // to be delayed. We copy the requested state to the active state making sure
1021 // to respect these rules (again see Layer.h for a detailed discussion).
1022 //
1023 // There is an awkward asymmetry in the handling of the crop states in the position
1024 // states, as can be seen below. Largely this arises from position and transform
1025 // being stored in the same data structure while having different latching rules.
1026 // b/38182305
1027 //
1028 // Careful that "c" and editCurrentState may not begin as equivalent due to
1029 // applyPendingStates in the presence of deferred transactions.
1030 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001031 float tx = c.active.transform.tx();
1032 float ty = c.active.transform.ty();
1033 c.active = c.requested;
1034 c.active.transform.set(tx, ty);
1035 editCurrentState.active = c.active;
1036 } else {
1037 editCurrentState.active = editCurrentState.requested;
1038 c.active = c.requested;
1039 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 }
1041
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001042 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001043 // invalidate and recompute the visible regions if needed
1044 flags |= Layer::eVisibleRegion;
1045 }
1046
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001047 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 // invalidate and recompute the visible regions if needed
1049 flags |= eVisibleRegion;
1050 this->contentDirty = true;
1051
1052 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001053 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001054 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 }
1056
Dan Stozac8145172016-04-28 16:29:06 -07001057 // If the layer is hidden, signal and clear out all local sync points so
1058 // that transactions for layers depending on this layer's frames becoming
1059 // visible are not blocked
1060 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001061 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001062 }
1063
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001065 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067}
1068
Pablo Ceballos05289c22016-04-14 15:49:55 -07001069void Layer::commitTransaction(const State& stateToCommit) {
1070 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001071}
1072
Mathias Agopian13127d82013-03-05 17:47:11 -08001073uint32_t Layer::getTransactionFlags(uint32_t flags) {
1074 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1075}
1076
1077uint32_t Layer::setTransactionFlags(uint32_t flags) {
1078 return android_atomic_or(flags, &mTransactionFlags);
1079}
1080
Robert Carr82364e32016-05-15 11:27:47 -07001081bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001082 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 return false;
1084 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001085
1086 // We update the requested and active position simultaneously because
1087 // we want to apply the position portion of the transform matrix immediately,
1088 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001089 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001090 if (immediate && !mFreezeGeometryUpdates) {
1091 // Here we directly update the active state
1092 // unlike other setters, because we store it within
1093 // the transform, but use different latching rules.
1094 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001095 mCurrentState.active.transform.set(x, y);
1096 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001097 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001098
Dan Stoza7dde5992015-05-22 09:51:44 -07001099 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001100 setTransactionFlags(eTransactionNeeded);
1101 return true;
1102}
Robert Carr82364e32016-05-15 11:27:47 -07001103
Robert Carr1f0a16a2016-10-24 16:27:39 -07001104bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1105 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1106 if (idx < 0) {
1107 return false;
1108 }
1109 if (childLayer->setLayer(z)) {
1110 mCurrentChildren.removeAt(idx);
1111 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001112 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001113 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001114 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001115}
1116
Robert Carr503c7042017-09-27 15:06:08 -07001117bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1118 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1119 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1120 if (idx < 0) {
1121 return false;
1122 }
1123 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1124 mCurrentChildren.removeAt(idx);
1125 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001126 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001127 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001128 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001129}
1130
Robert Carrae060832016-11-28 10:51:00 -08001131bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001132 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 mCurrentState.sequence++;
1134 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001135 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001136
1137 // Discard all relative layering.
1138 if (mCurrentState.zOrderRelativeOf != nullptr) {
1139 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1140 if (strongRelative != nullptr) {
1141 strongRelative->removeZOrderRelative(this);
1142 }
1143 mCurrentState.zOrderRelativeOf = nullptr;
1144 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001145 setTransactionFlags(eTransactionNeeded);
1146 return true;
1147}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001148
Robert Carrdb66e622017-04-10 16:55:57 -07001149void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1150 mCurrentState.zOrderRelatives.remove(relative);
1151 mCurrentState.sequence++;
1152 mCurrentState.modified = true;
1153 setTransactionFlags(eTransactionNeeded);
1154}
1155
1156void Layer::addZOrderRelative(const wp<Layer>& relative) {
1157 mCurrentState.zOrderRelatives.add(relative);
1158 mCurrentState.modified = true;
1159 mCurrentState.sequence++;
1160 setTransactionFlags(eTransactionNeeded);
1161}
1162
Robert Carr503d2bd2017-12-04 15:49:47 -08001163bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001164 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1165 if (handle == nullptr) {
1166 return false;
1167 }
1168 sp<Layer> relative = handle->owner.promote();
1169 if (relative == nullptr) {
1170 return false;
1171 }
1172
Robert Carr503d2bd2017-12-04 15:49:47 -08001173 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1174 mCurrentState.zOrderRelativeOf == relative) {
1175 return false;
1176 }
1177
Robert Carrdb66e622017-04-10 16:55:57 -07001178 mCurrentState.sequence++;
1179 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001180 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001181
chaviw9ab4bd12017-11-03 13:11:00 -07001182 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1183 if (oldZOrderRelativeOf != nullptr) {
1184 oldZOrderRelativeOf->removeZOrderRelative(this);
1185 }
Robert Carrdb66e622017-04-10 16:55:57 -07001186 mCurrentState.zOrderRelativeOf = relative;
1187 relative->addZOrderRelative(this);
1188
1189 setTransactionFlags(eTransactionNeeded);
1190
1191 return true;
1192}
1193
Mathias Agopian13127d82013-03-05 17:47:11 -08001194bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001195 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001196 mCurrentState.requested.w = w;
1197 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001198 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001199 setTransactionFlags(eTransactionNeeded);
1200 return true;
1201}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001202bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001203 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001204 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001205 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001206 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001207 setTransactionFlags(eTransactionNeeded);
1208 return true;
1209}
chaviw13fdc492017-06-27 12:40:18 -07001210
1211bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001212 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1213 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001214 return false;
1215
1216 mCurrentState.sequence++;
1217 mCurrentState.color.r = color.r;
1218 mCurrentState.color.g = color.g;
1219 mCurrentState.color.b = color.b;
1220 mCurrentState.modified = true;
1221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
1224
Mathias Agopian13127d82013-03-05 17:47:11 -08001225bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1226 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001227 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001228 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 setTransactionFlags(eTransactionNeeded);
1230 return true;
1231}
1232bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001233 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001234 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 setTransactionFlags(eTransactionNeeded);
1236 return true;
1237}
1238bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1239 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001240 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 mCurrentState.sequence++;
1242 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001243 mCurrentState.mask = mask;
1244 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 setTransactionFlags(eTransactionNeeded);
1246 return true;
1247}
Robert Carr99e27f02016-06-16 15:18:02 -07001248
1249bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001250 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001251 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001252 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001253 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001254 mCurrentState.crop = crop;
1255 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001256 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1257
Dan Stoza7dde5992015-05-22 09:51:44 -07001258 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
Robert Carr8d5227b2017-03-16 15:41:03 -07001262
1263bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001264 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001265 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001266 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001267 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001268 mCurrentState.finalCrop = crop;
1269 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001270 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1271
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001272 mCurrentState.modified = true;
1273 setTransactionFlags(eTransactionNeeded);
1274 return true;
1275}
Mathias Agopian13127d82013-03-05 17:47:11 -08001276
Robert Carrc3574f72016-03-24 12:19:32 -07001277bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001278 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001279 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001280 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001281 return true;
1282}
1283
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001284void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001285 mCurrentState.appId = appId;
1286 mCurrentState.type = type;
1287 mCurrentState.modified = true;
1288 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001289}
1290
Mathias Agopian13127d82013-03-05 17:47:11 -08001291bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001292 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 mCurrentState.sequence++;
1294 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001295 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 setTransactionFlags(eTransactionNeeded);
1297 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001298}
1299
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001300bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001301 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001302 mCurrentState.sequence++;
1303 mCurrentState.dataSpace = dataSpace;
1304 mCurrentState.modified = true;
1305 setTransactionFlags(eTransactionNeeded);
1306 return true;
1307}
1308
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001309android_dataspace Layer::getDataSpace() const {
1310 return mCurrentState.dataSpace;
1311}
1312
Robert Carr1f0a16a2016-10-24 16:27:39 -07001313uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001314 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001315 if (p == nullptr) {
1316 return getDrawingState().layerStack;
1317 }
1318 return p->getLayerStack();
1319}
1320
David Sodman41fdfc92017-11-06 16:09:56 -08001321void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001322 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 mCurrentState.frameNumber = frameNumber;
1324 // We don't set eTransactionNeeded, because just receiving a deferral
1325 // request without any other state updates shouldn't actually induce a delay
1326 mCurrentState.modified = true;
1327 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001328 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001329 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001330 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001331}
1332
David Sodman41fdfc92017-11-06 16:09:56 -08001333void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001334 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1335 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001336}
1337
Dan Stozaee44edd2015-03-23 15:50:23 -07001338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339// ----------------------------------------------------------------------------
1340// pageflip handling...
1341// ----------------------------------------------------------------------------
1342
Robert Carr1f0a16a2016-10-24 16:27:39 -07001343bool Layer::isHiddenByPolicy() const {
1344 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001345 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001346 if (parent != nullptr && parent->isHiddenByPolicy()) {
1347 return true;
1348 }
1349 return s.flags & layer_state_t::eLayerHidden;
1350}
1351
David Sodman41fdfc92017-11-06 16:09:56 -08001352uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001353 // TODO: should we do something special if mSecure is set?
1354 if (mProtectedByApp) {
1355 // need a hardware-protected path to external video sink
1356 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001357 }
Riley Andrews03414a12014-07-01 14:22:59 -07001358 if (mPotentialCursor) {
1359 usage |= GraphicBuffer::USAGE_CURSOR;
1360 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001361 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001362 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001363}
1364
Mathias Agopian84300952012-11-21 16:02:13 -08001365void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001366 uint32_t orientation = 0;
1367 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001368 // The transform hint is used to improve performance, but we can
1369 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001370 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001371 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001372 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001373 if (orientation & Transform::ROT_INVALID) {
1374 orientation = 0;
1375 }
1376 }
David Sodmaneb085e02017-10-05 18:49:04 -07001377 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001378}
1379
Mathias Agopian13127d82013-03-05 17:47:11 -08001380// ----------------------------------------------------------------------------
1381// debugging
1382// ----------------------------------------------------------------------------
1383
Kalle Raitaa099a242017-01-11 11:17:29 -08001384LayerDebugInfo Layer::getLayerDebugInfo() const {
1385 LayerDebugInfo info;
1386 const Layer::State& ds = getDrawingState();
1387 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001388 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001389 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1390 info.mType = String8(getTypeId());
1391 info.mTransparentRegion = ds.activeTransparentRegion;
1392 info.mVisibleRegion = visibleRegion;
1393 info.mSurfaceDamageRegion = surfaceDamageRegion;
1394 info.mLayerStack = getLayerStack();
1395 info.mX = ds.active.transform.tx();
1396 info.mY = ds.active.transform.ty();
1397 info.mZ = ds.z;
1398 info.mWidth = ds.active.w;
1399 info.mHeight = ds.active.h;
1400 info.mCrop = ds.crop;
1401 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001402 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001403 info.mFlags = ds.flags;
1404 info.mPixelFormat = getPixelFormat();
1405 info.mDataSpace = getDataSpace();
1406 info.mMatrix[0][0] = ds.active.transform[0][0];
1407 info.mMatrix[0][1] = ds.active.transform[0][1];
1408 info.mMatrix[1][0] = ds.active.transform[1][0];
1409 info.mMatrix[1][1] = ds.active.transform[1][1];
1410 {
David Sodman5b4cffc2017-11-23 13:20:29 -08001411 sp<const GraphicBuffer> buffer = getBE().mBuffer;
1412 if (buffer != 0) {
1413 info.mActiveBufferWidth = buffer->getWidth();
1414 info.mActiveBufferHeight = buffer->getHeight();
1415 info.mActiveBufferStride = buffer->getStride();
1416 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001417 } else {
1418 info.mActiveBufferWidth = 0;
1419 info.mActiveBufferHeight = 0;
1420 info.mActiveBufferStride = 0;
1421 info.mActiveBufferFormat = 0;
1422 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001423 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001424 info.mNumQueuedFrames = getQueuedFrameCount();
1425 info.mRefreshPending = isBufferLatched();
1426 info.mIsOpaque = isOpaque(ds);
1427 info.mContentDirty = contentDirty;
1428 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001429}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001430
Dan Stozae22aec72016-08-01 13:20:59 -07001431void Layer::miniDumpHeader(String8& result) {
1432 result.append("----------------------------------------");
1433 result.append("---------------------------------------\n");
1434 result.append(" Layer name\n");
1435 result.append(" Z | ");
1436 result.append(" Comp Type | ");
1437 result.append(" Disp Frame (LTRB) | ");
1438 result.append(" Source Crop (LTRB)\n");
1439 result.append("----------------------------------------");
1440 result.append("---------------------------------------\n");
1441}
1442
1443void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001444 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001445 return;
1446 }
1447
1448 String8 name;
1449 if (mName.length() > 77) {
1450 std::string shortened;
1451 shortened.append(mName.string(), 36);
1452 shortened.append("[...]");
1453 shortened.append(mName.string() + (mName.length() - 36), 36);
1454 name = shortened.c_str();
1455 } else {
1456 name = mName;
1457 }
1458
1459 result.appendFormat(" %s\n", name.string());
1460
1461 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001462 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001463 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001464 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001465 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001466 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001467 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001468 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 -07001469
1470 result.append("- - - - - - - - - - - - - - - - - - - - ");
1471 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1472}
Dan Stozae22aec72016-08-01 13:20:59 -07001473
Svetoslavd85084b2014-03-20 10:28:31 -07001474void Layer::dumpFrameStats(String8& result) const {
1475 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001476}
1477
Svetoslavd85084b2014-03-20 10:28:31 -07001478void Layer::clearFrameStats() {
1479 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001480}
1481
Jamie Gennis6547ff42013-07-16 20:12:42 -07001482void Layer::logFrameStats() {
1483 mFrameTracker.logAndResetStats(mName);
1484}
1485
Svetoslavd85084b2014-03-20 10:28:31 -07001486void Layer::getFrameStats(FrameStats* outStats) const {
1487 mFrameTracker.getStats(outStats);
1488}
1489
Brian Andersond6927fb2016-07-23 23:37:30 -07001490void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001491 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001492 Mutex::Autolock lock(mFrameEventHistoryMutex);
1493 mFrameEventHistory.checkFencesForCompletion();
1494 mFrameEventHistory.dump(result);
1495}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001496
Brian Anderson5ea5e592016-12-01 16:54:33 -08001497void Layer::onDisconnect() {
1498 Mutex::Autolock lock(mFrameEventHistoryMutex);
1499 mFrameEventHistory.onDisconnect();
1500}
1501
Brian Anderson3890c392016-07-25 12:48:08 -07001502void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001503 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001504 Mutex::Autolock lock(mFrameEventHistoryMutex);
1505 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001506 // If there are any unsignaled fences in the aquire timeline at this
1507 // point, the previously queued frame hasn't been latched yet. Go ahead
1508 // and try to get the signal time here so the syscall is taken out of
1509 // the main thread's critical path.
1510 mAcquireTimeline.updateSignalTimes();
1511 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001512 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001513 mFrameEventHistory.addQueue(*newTimestamps);
1514 }
1515
Brian Anderson3890c392016-07-25 12:48:08 -07001516 if (outDelta) {
1517 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001518 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001519}
Dan Stozae77c7662016-05-13 11:37:28 -07001520
Chia-I Wu98f1c102017-05-30 14:54:08 -07001521size_t Layer::getChildrenCount() const {
1522 size_t count = 0;
1523 for (const sp<Layer>& child : mCurrentChildren) {
1524 count += 1 + child->getChildrenCount();
1525 }
1526 return count;
1527}
1528
Robert Carr1f0a16a2016-10-24 16:27:39 -07001529void Layer::addChild(const sp<Layer>& layer) {
1530 mCurrentChildren.add(layer);
1531 layer->setParent(this);
1532}
1533
1534ssize_t Layer::removeChild(const sp<Layer>& layer) {
1535 layer->setParent(nullptr);
1536 return mCurrentChildren.remove(layer);
1537}
1538
Robert Carr1db73f62016-12-21 12:58:51 -08001539bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1540 sp<Handle> handle = nullptr;
1541 sp<Layer> newParent = nullptr;
1542 if (newParentHandle == nullptr) {
1543 return false;
1544 }
1545 handle = static_cast<Handle*>(newParentHandle.get());
1546 newParent = handle->owner.promote();
1547 if (newParent == nullptr) {
1548 ALOGE("Unable to promote Layer handle");
1549 return false;
1550 }
1551
1552 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001553 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001554
1555 sp<Client> client(child->mClientRef.promote());
1556 if (client != nullptr) {
1557 client->setParentLayer(newParent);
1558 }
1559 }
1560 mCurrentChildren.clear();
1561
1562 return true;
1563}
1564
chaviwf1961f72017-09-18 16:41:07 -07001565bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1566 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001567 return false;
1568 }
1569
1570 auto handle = static_cast<Handle*>(newParentHandle.get());
1571 sp<Layer> newParent = handle->owner.promote();
1572 if (newParent == nullptr) {
1573 ALOGE("Unable to promote Layer handle");
1574 return false;
1575 }
1576
chaviwf1961f72017-09-18 16:41:07 -07001577 sp<Layer> parent = getParent();
1578 if (parent != nullptr) {
1579 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001580 }
chaviwf1961f72017-09-18 16:41:07 -07001581 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001582
chaviwf1961f72017-09-18 16:41:07 -07001583 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001584 sp<Client> newParentClient(newParent->mClientRef.promote());
1585
chaviwf1961f72017-09-18 16:41:07 -07001586 if (client != newParentClient) {
1587 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001588 }
1589
chaviw06178942017-07-27 10:25:59 -07001590 return true;
1591}
1592
Robert Carr9524cb32017-02-13 11:32:32 -08001593bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001594 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001595 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001596 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001597 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001598 client->detachLayer(child.get());
1599 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001600 }
Robert Carr7f619b22017-11-06 12:56:35 -08001601 }
Robert Carr9524cb32017-02-13 11:32:32 -08001602
1603 return true;
1604}
1605
Robert Carr1f0a16a2016-10-24 16:27:39 -07001606void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001607 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001608}
1609
1610void Layer::clearSyncPoints() {
1611 for (const auto& child : mCurrentChildren) {
1612 child->clearSyncPoints();
1613 }
1614
1615 Mutex::Autolock lock(mLocalSyncPointMutex);
1616 for (auto& point : mLocalSyncPoints) {
1617 point->setFrameAvailable();
1618 }
1619 mLocalSyncPoints.clear();
1620}
1621
1622int32_t Layer::getZ() const {
1623 return mDrawingState.z;
1624}
1625
Robert Carr29abff82017-12-04 13:51:20 -08001626bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1627 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1628 const State& state = useDrawing ? mDrawingState : mCurrentState;
1629 return state.zOrderRelativeOf != nullptr;
1630}
1631
David Sodman41fdfc92017-11-06 16:09:56 -08001632__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001633 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001634 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1635 "makeTraversalList received invalid stateSet");
1636 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1637 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1638 const State& state = useDrawing ? mDrawingState : mCurrentState;
1639
Robert Carr29abff82017-12-04 13:51:20 -08001640 if (state.zOrderRelatives.size() == 0) {
1641 *outSkipRelativeZUsers = true;
1642 return children;
1643 }
1644
Robert Carrdb66e622017-04-10 16:55:57 -07001645 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001646 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001647 sp<Layer> strongRelative = weakRelative.promote();
1648 if (strongRelative != nullptr) {
1649 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001650 }
1651 }
1652
Dan Stoza412903f2017-04-27 13:42:17 -07001653 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001654 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1655 if (childState.zOrderRelativeOf != nullptr) {
1656 continue;
1657 }
Robert Carrdb66e622017-04-10 16:55:57 -07001658 traverse.add(child);
1659 }
1660
1661 return traverse;
1662}
1663
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664/**
Robert Carrdb66e622017-04-10 16:55:57 -07001665 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666 */
Dan Stoza412903f2017-04-27 13:42:17 -07001667void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001668 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1669 // produce a new list for traversing, including our relatives, and not including our children
1670 // who are relatives of another surface. In the case that there are no relative Z,
1671 // makeTraversalList returns our children directly to avoid significant overhead.
1672 // However in this case we need to take the responsibility for filtering children which
1673 // are relatives of another surface here.
1674 bool skipRelativeZUsers = false;
1675 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001676
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001678 for (; i < list.size(); i++) {
1679 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001680 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1681 continue;
1682 }
1683
Robert Carrdb66e622017-04-10 16:55:57 -07001684 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001685 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001686 }
Dan Stoza412903f2017-04-27 13:42:17 -07001687 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001688 }
Robert Carr29abff82017-12-04 13:51:20 -08001689
Dan Stoza412903f2017-04-27 13:42:17 -07001690 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001691 for (; i < list.size(); i++) {
1692 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001693
1694 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1695 continue;
1696 }
Dan Stoza412903f2017-04-27 13:42:17 -07001697 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 }
1699}
1700
1701/**
Robert Carrdb66e622017-04-10 16:55:57 -07001702 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703 */
Dan Stoza412903f2017-04-27 13:42:17 -07001704void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1705 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001706 // See traverseInZOrder for documentation.
1707 bool skipRelativeZUsers = false;
1708 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001711 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001712 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001713
1714 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1715 continue;
1716 }
1717
Robert Carrdb66e622017-04-10 16:55:57 -07001718 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 break;
1720 }
Dan Stoza412903f2017-04-27 13:42:17 -07001721 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001722 }
Dan Stoza412903f2017-04-27 13:42:17 -07001723 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001724 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726
1727 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1728 continue;
1729 }
1730
Dan Stoza412903f2017-04-27 13:42:17 -07001731 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 }
1733}
1734
chaviwa76b2712017-09-20 12:02:26 -07001735/**
1736 * Traverse only children in z order, ignoring relative layers.
1737 */
1738void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1739 const LayerVector::Visitor& visitor) {
1740 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1741 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1742
1743 size_t i = 0;
1744 for (; i < children.size(); i++) {
1745 const auto& relative = children[i];
1746 if (relative->getZ() >= 0) {
1747 break;
1748 }
1749 relative->traverseChildrenInZOrder(stateSet, visitor);
1750 }
1751 visitor(this);
1752 for (; i < children.size(); i++) {
1753 const auto& relative = children[i];
1754 relative->traverseChildrenInZOrder(stateSet, visitor);
1755 }
1756}
1757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758Transform Layer::getTransform() const {
1759 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001760 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 if (p != nullptr) {
1762 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001763
1764 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1765 // it isFixedSize) then there may be additional scaling not accounted
1766 // for in the transform. We need to mirror this scaling in child surfaces
1767 // or we will break the contract where WM can treat child surfaces as
1768 // pixels in the parent surface.
David Sodman5b4cffc2017-11-23 13:20:29 -08001769 if (p->isFixedSize() && p->getBE().mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001770 int bufferWidth;
1771 int bufferHeight;
1772 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman5b4cffc2017-11-23 13:20:29 -08001773 bufferWidth = p->getBE().mBuffer->getWidth();
1774 bufferHeight = p->getBE().mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001775 } else {
David Sodman5b4cffc2017-11-23 13:20:29 -08001776 bufferHeight = p->getBE().mBuffer->getWidth();
1777 bufferWidth = p->getBE().mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001778 }
David Sodman41fdfc92017-11-06 16:09:56 -08001779 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1780 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001781 Transform extraParentScaling;
1782 extraParentScaling.set(sx, 0, 0, sy);
1783 t = t * extraParentScaling;
1784 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785 }
1786 return t * getDrawingState().active.transform;
1787}
1788
chaviw13fdc492017-06-27 12:40:18 -07001789half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001790 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001791
chaviw13fdc492017-06-27 12:40:18 -07001792 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1793 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001794}
Robert Carr6452f122017-03-21 10:41:29 -07001795
chaviw13fdc492017-06-27 12:40:18 -07001796half4 Layer::getColor() const {
1797 const half4 color(getDrawingState().color);
1798 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001799}
Robert Carr6452f122017-03-21 10:41:29 -07001800
Robert Carr1f0a16a2016-10-24 16:27:39 -07001801void Layer::commitChildList() {
1802 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1803 const auto& child = mCurrentChildren[i];
1804 child->commitChildList();
1805 }
1806 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001807 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001808}
1809
chaviw1d044282017-09-27 12:19:28 -07001810void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1811 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1812 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1813 const State& state = useDrawing ? mDrawingState : mCurrentState;
1814
1815 Transform requestedTransform = state.active.transform;
1816 Transform transform = getTransform();
1817
1818 layerInfo->set_id(sequence);
1819 layerInfo->set_name(getName().c_str());
1820 layerInfo->set_type(String8(getTypeId()));
1821
1822 for (const auto& child : children) {
1823 layerInfo->add_children(child->sequence);
1824 }
1825
1826 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1827 sp<Layer> strongRelative = weakRelative.promote();
1828 if (strongRelative != nullptr) {
1829 layerInfo->add_relatives(strongRelative->sequence);
1830 }
1831 }
1832
1833 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1834 layerInfo->mutable_transparent_region());
1835 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1836 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1837
1838 layerInfo->set_layer_stack(getLayerStack());
1839 layerInfo->set_z(state.z);
1840
1841 PositionProto* position = layerInfo->mutable_position();
1842 position->set_x(transform.tx());
1843 position->set_y(transform.ty());
1844
1845 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1846 requestedPosition->set_x(requestedTransform.tx());
1847 requestedPosition->set_y(requestedTransform.ty());
1848
1849 SizeProto* size = layerInfo->mutable_size();
1850 size->set_w(state.active.w);
1851 size->set_h(state.active.h);
1852
1853 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1854 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1855
1856 layerInfo->set_is_opaque(isOpaque(state));
1857 layerInfo->set_invalidate(contentDirty);
1858 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1859 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1860 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1861 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1862 layerInfo->set_flags(state.flags);
1863
1864 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1865 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1866
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001867 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001868 if (parent != nullptr) {
1869 layerInfo->set_parent(parent->sequence);
1870 }
1871
1872 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1873 if (zOrderRelativeOf != nullptr) {
1874 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1875 }
1876
David Sodman5b4cffc2017-11-23 13:20:29 -08001877 auto buffer = getBE().mBuffer;
1878 if (buffer != nullptr) {
1879 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001880 }
1881
1882 layerInfo->set_queued_frames(getQueuedFrameCount());
1883 layerInfo->set_refresh_pending(isBufferLatched());
1884}
1885
Mathias Agopian13127d82013-03-05 17:47:11 -08001886// ---------------------------------------------------------------------------
1887
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001889
1890#if defined(__gl_h_)
1891#error "don't include gl/gl.h in this file"
1892#endif
1893
1894#if defined(__gl2_h_)
1895#error "don't include gl2/gl2.h in this file"
1896#endif