blob: 359b64fe7202550f7968d34bb9b6d0efbc2eb464 [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()
66 : mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
67}
68
69
Mathias Agopian13127d82013-03-05 17:47:11 -080070int32_t Layer::sSequence = 1;
71
David Sodman41fdfc92017-11-06 16:09:56 -080072Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
73 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070074 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 sequence(uint32_t(android_atomic_inc(&sSequence))),
76 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080077 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070078 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080079 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070080 mPendingStateMutex(),
81 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080083 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080084 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070085 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070086 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070087 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080088 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080089 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080090 mFiltering(false),
91 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070093 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070094 mPotentialCursor(false),
95 mQueueItemLock(),
96 mQueueItemCondition(),
97 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070098 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070099 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -0800100 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101
Mathias Agopiana67932f2011-04-20 14:20:59 -0700102 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103
104 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800105 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
106 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
107 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700110 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111
112 mCurrentState.active.w = w;
113 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700114 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800115 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700116 mCurrentState.crop.makeInvalid();
117 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700118 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
119 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700121 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700125 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500126 mCurrentState.appId = 0;
127 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128
129 // drawing state & current state are identical
130 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132 const auto& hwc = flinger->getHwComposer();
133 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
134 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700135 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800136
137 CompositorTiming compositorTiming;
138 flinger->getCompositorTiming(&compositorTiming);
139 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700140
Jamie Gennise8696a42012-01-15 18:54:57 -0800141}
142
David Sodman41fdfc92017-11-06 16:09:56 -0800143void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700147}
148
Mathias Agopian13127d82013-03-05 17:47:11 -0800149// ---------------------------------------------------------------------------
150// callbacks
151// ---------------------------------------------------------------------------
152
David Sodmaneb085e02017-10-05 18:49:04 -0700153/*
154 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
155 * Layer. So, the implementation is done in BufferLayer. When called on a
156 * ColorLayer object, it's essentially a NOP.
157 */
David Sodmaneb085e02017-10-05 18:49:04 -0700158void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800159
Chia-I Wuc6657022017-08-15 11:18:17 -0700160void Layer::onRemovedFromCurrentState() {
161 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
162
chaviw8b3871a2017-11-01 17:41:01 -0700163 mPendingRemoval = true;
164
Robert Carr5edb1ad2017-04-25 10:54:24 -0700165 if (mCurrentState.zOrderRelativeOf != nullptr) {
166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
167 if (strongRelative != nullptr) {
168 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700169 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700170 }
171 mCurrentState.zOrderRelativeOf = nullptr;
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
177}
Chia-I Wu38512252017-05-17 14:36:16 -0700178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179void Layer::onRemoved() {
180 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700181 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700182
Steven Thomasb02664d2017-07-26 18:48:28 -0700183 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700184
Robert Carr1f0a16a2016-10-24 16:27:39 -0700185 for (const auto& child : mCurrentChildren) {
186 child->onRemoved();
187 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700188}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700189
Mathias Agopian13127d82013-03-05 17:47:11 -0800190// ---------------------------------------------------------------------------
191// set-up
192// ---------------------------------------------------------------------------
193
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700194const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800195 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
chaviw13fdc492017-06-27 12:40:18 -0700198bool Layer::getPremultipledAlpha() const {
199 return mPremultipliedAlpha;
200}
201
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800203 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700204 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800205}
206
207// ---------------------------------------------------------------------------
208// h/w composer set-up
209// ---------------------------------------------------------------------------
210
Steven Thomasb02664d2017-07-26 18:48:28 -0700211bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700212 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700213 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700214 HWC2::Layer* layer = hwc->createLayer(hwcId);
215 if (!layer) {
216 return false;
217 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700218 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700219 hwcInfo.hwc = hwc;
220 hwcInfo.layer = layer;
221 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700222 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 return true;
224}
225
Chia-I Wu83806892017-11-16 10:50:20 -0800226bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700227 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800228 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700230 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800231 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700232 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
233 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
234 // The layer destroyed listener should have cleared the entry from
235 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700236 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
237 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800238 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700239}
240
241void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700242 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700244 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
245 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800248 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700249}
Steven Thomasb02664d2017-07-26 18:48:28 -0700250
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800251Rect Layer::getContentCrop() const {
252 // this is the crop rectangle that applies to the buffer
253 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700254 Rect crop;
255 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800258 } else if (mActiveBuffer != NULL) {
259 // otherwise we use the whole buffer
260 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700263 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 return crop;
266}
267
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700268static Rect reduce(const Rect& win, const Region& exclude) {
269 if (CC_LIKELY(exclude.isEmpty())) {
270 return win;
271 }
272 if (exclude.isRect()) {
273 return win.reduce(exclude.getBounds());
274 }
275 return Region(win).subtract(exclude).getBounds();
276}
277
Robert Carr1f0a16a2016-10-24 16:27:39 -0700278Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
279 const Layer::State& s(getDrawingState());
280 Rect win(s.active.w, s.active.h);
281
282 if (!s.crop.isEmpty()) {
283 win.intersect(s.crop, &win);
284 }
285
286 Transform t = getTransform();
287 win = t.transform(win);
288
Robert Carr41b08b52017-06-01 16:11:34 -0700289 if (!s.finalCrop.isEmpty()) {
290 win.intersect(s.finalCrop, &win);
291 }
292
Chia-I Wue41dbe62017-06-13 14:10:56 -0700293 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
295 // When calculating the parent bounds for purposes of clipping,
296 // we don't need to constrain the parent to its transparent region.
297 // The transparent region is an optimization based on the
298 // buffer contents of the layer, but does not affect the space allocated to
299 // it by policy, and thus children should be allowed to extend into the
300 // parent's transparent region. In fact one of the main uses, is to reduce
301 // buffer allocation size in cases where a child window sits behind a main window
302 // (by marking the hole in the parent window as a transparent region)
303 if (p != nullptr) {
304 Rect bounds = p->computeScreenBounds(false);
305 bounds.intersect(win, &win);
306 }
307
308 if (reduceTransparentRegion) {
309 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
310 win = reduce(win, screenTransparentRegion);
311 }
312
313 return win;
314}
315
Mathias Agopian13127d82013-03-05 17:47:11 -0800316Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700317 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700318 return computeBounds(s.activeTransparentRegion);
319}
320
321Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
322 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800323 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700324
325 if (!s.crop.isEmpty()) {
326 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800327 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700328
329 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700330 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700331 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800332 // Look in computeScreenBounds recursive call for explanation of
333 // why we pass false here.
334 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700335 }
336
337 Transform t = getTransform();
338 if (p != nullptr) {
339 win = t.transform(win);
340 win.intersect(bounds, &win);
341 win = t.inverse().transform(win);
342 }
343
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700344 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700345 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800346}
347
Robert Carr1f0a16a2016-10-24 16:27:39 -0700348Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700349 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800350 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700351 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800352
353 // apply the projection's clipping to the window crop in
354 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700355 // if there are no window scaling involved, this operation will map to full
356 // pixels in the buffer.
357 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
358 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700359
360 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700361 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700362 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700363 }
364
Robert Carr1f0a16a2016-10-24 16:27:39 -0700365 Transform t = getTransform();
366 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000367 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
368 activeCrop.clear();
369 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700370 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800371 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000372 activeCrop.clear();
373 }
374 }
chaviwb1154d12017-10-31 14:15:36 -0700375
376 const auto& p = mDrawingParent.promote();
377 if (p != nullptr) {
378 auto parentCrop = p->computeInitialCrop(hw);
379 activeCrop.intersect(parentCrop, &activeCrop);
380 }
381
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 return activeCrop;
383}
384
Dan Stoza5a423ea2017-02-16 14:10:39 -0800385FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700386 // the content crop is the area of the content that gets scaled to the
387 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800388 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700389
390 // In addition there is a WM-specified crop we pull from our drawing state.
391 const State& s(getDrawingState());
392
393 // Screen space to make reduction to parent crop clearer.
394 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395 Transform t = getTransform();
396 // Back to layer space to work with the content crop.
397 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800398
Michael Lentine28ea2172014-11-19 18:32:37 -0800399 // This needs to be here as transform.transform(Rect) computes the
400 // transformed rect and then takes the bounding box of the result before
401 // returning. This means
402 // transform.inverse().transform(transform.transform(Rect)) != Rect
403 // in which case we need to make sure the final rect is clipped to the
404 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000405 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
406 activeCrop.clear();
407 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800408
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700409 // subtract the transparent region and snap to the bounds
410 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
411
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 // Transform the window crop to match the buffer coordinate system,
413 // which means using the inverse of the current transform set on the
414 // SurfaceFlingerConsumer.
415 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700416 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000417 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700418 * the code below applies the primary display's inverse transform to the
419 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 */
David Sodman41fdfc92017-11-06 16:09:56 -0800421 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 // calculate the inverse transform
423 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800424 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800425 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800427 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800428 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429
430 int winWidth = s.active.w;
431 int winHeight = s.active.h;
432 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
433 // If the activeCrop has been rotate the ends are rotated but not
434 // the space itself so when transforming ends back we can't rely on
435 // a modification of the axes of rotation. To account for this we
436 // need to reorient the inverse rotation in terms of the current
437 // axes of rotation.
438 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
439 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
440 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800441 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 }
443 winWidth = s.active.h;
444 winHeight = s.active.w;
445 }
David Sodman41fdfc92017-11-06 16:09:56 -0800446 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447
448 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800449 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 float yScale = crop.getHeight() / float(winHeight);
451
David Sodman41fdfc92017-11-06 16:09:56 -0800452 float insetL = winCrop.left * xScale;
453 float insetT = winCrop.top * yScale;
454 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 float insetB = (winHeight - winCrop.bottom) * yScale;
456
David Sodman41fdfc92017-11-06 16:09:56 -0800457 crop.left += insetL;
458 crop.top += insetT;
459 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 crop.bottom -= insetB;
461
Mathias Agopian13127d82013-03-05 17:47:11 -0800462 return crop;
463}
464
Robert Carrae060832016-11-28 10:51:00 -0800465void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700466{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800467 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700468 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700469
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700470 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800471 hwcInfo.forceClientComposition = false;
472
473 if (isSecure() && !displayDevice->isSecure()) {
474 hwcInfo.forceClientComposition = true;
475 }
476
477 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700478
Mathias Agopian13127d82013-03-05 17:47:11 -0800479 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700480 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500481 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700482 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800483 blendMode =
484 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 }
David Revemanecf0fa52017-03-03 11:32:44 -0500486 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800487 ALOGE_IF(error != HWC2::Error::None,
488 "[%s] Failed to set blend mode %s:"
489 " %s (%d)",
490 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
491 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800492
493 // apply the layer's transform, followed by the display's global transform
494 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700495 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700496 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700497 if (!s.crop.isEmpty()) {
498 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700499 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800500 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000501 activeCrop.clear();
502 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700503 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800504 // This needs to be here as transform.transform(Rect) computes the
505 // transformed rect and then takes the bounding box of the result before
506 // returning. This means
507 // transform.inverse().transform(transform.transform(Rect)) != Rect
508 // in which case we need to make sure the final rect is clipped to the
509 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800510 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000511 activeCrop.clear();
512 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700513 // mark regions outside the crop as transparent
514 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800515 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
516 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
517 activeTransparentRegion.orSelf(
518 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700519 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520
521 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700522 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800523 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000524 frame.clear();
525 }
526 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000527 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
528 frame.clear();
529 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530 const Transform& tr(displayDevice->getTransform());
531 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500532 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700533 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800534 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
535 transformedFrame.left, transformedFrame.top, transformedFrame.right,
536 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700537 } else {
538 hwcInfo.displayFrame = transformedFrame;
539 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800540
Dan Stoza5a423ea2017-02-16 14:10:39 -0800541 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700543 if (error != HWC2::Error::None) {
544 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800545 "%s (%d)",
546 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
547 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700548 } else {
549 hwcInfo.sourceCrop = sourceCrop;
550 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551
chaviw13fdc492017-06-27 12:40:18 -0700552 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700553 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800554 ALOGE_IF(error != HWC2::Error::None,
555 "[%s] Failed to set plane alpha %.3f: "
556 "%s (%d)",
557 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558
Robert Carrae060832016-11-28 10:51:00 -0800559 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800560 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
561 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500562
Albert Chaulk2a589632017-05-04 16:59:44 -0400563 int type = s.type;
564 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700565 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400566 if (parent.get()) {
567 auto& parentState = parent->getDrawingState();
568 type = parentState.type;
569 appId = parentState.appId;
570 }
571
572 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800573 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
574 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800575
Mathias Agopian29a367b2011-07-12 14:51:45 -0700576 /*
577 * Transformations are applied in this order:
578 * 1) buffer orientation/flip/mirror
579 * 2) state transformation (window manager)
580 * 3) layer orientation (screen orientation)
581 * (NOTE: the matrices are multiplied in reverse order)
582 */
583
584 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700585 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700586
Robert Carrcae605c2017-03-29 12:10:31 -0700587 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700588 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700589 * the code below applies the primary display's inverse transform to the
590 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700591 */
David Sodman41fdfc92017-11-06 16:09:56 -0800592 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700593 // calculate the inverse transform
594 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800595 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700596 }
Robert Carrcae605c2017-03-29 12:10:31 -0700597
598 /*
599 * Here we cancel out the orientation component of the WM transform.
600 * The scaling and translate components are already included in our bounds
601 * computation so it's enough to just omit it in the composition.
602 * See comment in onDraw with ref to b/36727915 for why.
603 */
604 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700606
607 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800608 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800609 if (orientation & Transform::ROT_INVALID) {
610 // we can only handle simple transformation
611 hwcInfo.forceClientComposition = true;
612 } else {
613 auto transform = static_cast<HWC2::Transform>(orientation);
614 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800615 ALOGE_IF(error != HWC2::Error::None,
616 "[%s] Failed to set transform %s: "
617 "%s (%d)",
618 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
619 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700621}
622
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700624 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
626 return;
627 }
628
David Sodman6f65f3e2017-11-03 14:28:09 -0700629 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800630}
Dan Stozaee44edd2015-03-23 15:50:23 -0700631
chaviwc9232ed2017-11-14 15:31:15 -0800632bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700633 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800634 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
635 return false;
636 }
637
David Sodman6f65f3e2017-11-03 14:28:09 -0700638 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800639}
640
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
642 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700643 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700644 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645 return;
646 }
647
648 // This gives us only the "orientation" component of the transform
649 const State& s(getCurrentState());
650
651 // Apply the layer's transform, followed by the display's global transform
652 // Here we're guaranteed that the layer's transform preserves rects
653 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700654 if (!s.crop.isEmpty()) {
655 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656 }
657 // Subtract the transparent region and snap to the bounds
658 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700659 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700661 if (!s.finalCrop.isEmpty()) {
662 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000663 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664 auto& displayTransform(displayDevice->getTransform());
665 auto position = displayTransform.transform(frame);
666
David Sodman6f65f3e2017-11-03 14:28:09 -0700667 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700668 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800669 ALOGE_IF(error != HWC2::Error::None,
670 "[%s] Failed to set cursor position "
671 "to (%d, %d): %s (%d)",
672 mName.string(), position.left, position.top, to_string(error).c_str(),
673 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674}
Riley Andrews03414a12014-07-01 14:22:59 -0700675
Mathias Agopian13127d82013-03-05 17:47:11 -0800676// ---------------------------------------------------------------------------
677// drawing...
678// ---------------------------------------------------------------------------
679
chaviwa76b2712017-09-20 12:02:26 -0700680void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
681 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800682}
683
chaviwa76b2712017-09-20 12:02:26 -0700684void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
685 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800686}
687
chaviwa76b2712017-09-20 12:02:26 -0700688void Layer::draw(const RenderArea& renderArea) const {
689 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800690}
691
David Sodman41fdfc92017-11-06 16:09:56 -0800692void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
693 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700694 RenderEngine& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700695 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700696 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700697 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800698}
699
chaviwa76b2712017-09-20 12:02:26 -0700700void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800701 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800702}
703
David Sodman41fdfc92017-11-06 16:09:56 -0800704void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700705 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 ALOGE("setCompositionType called without a valid HWC layer");
707 return;
708 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700709 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800711 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
712 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713 if (hwcInfo.compositionType != type) {
714 ALOGV(" actually setting");
715 hwcInfo.compositionType = type;
716 if (callIntoHwc) {
717 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800718 ALOGE_IF(error != HWC2::Error::None,
719 "[%s] Failed to set "
720 "composition type %s: %s (%d)",
721 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
722 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723 }
724 }
725}
726
727HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700728 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
729 // If we're querying the composition type for a display that does not
730 // have a HWC counterpart, then it will always be Client
731 return HWC2::Composition::Client;
732 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700733 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700734 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 return HWC2::Composition::Invalid;
736 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700737 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738}
739
740void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700741 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 ALOGE("setClearClientTarget called without a valid HWC layer");
743 return;
744 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700745 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746}
747
748bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700749 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 ALOGE("getClearClientTarget called without a valid HWC layer");
751 return false;
752 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700753 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755
Dan Stozacac35382016-01-27 12:21:06 -0800756bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
757 if (point->getFrameNumber() <= mCurrentFrameNumber) {
758 // Don't bother with a SyncPoint, since we've already latched the
759 // relevant frame
760 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700761 }
762
Dan Stozacac35382016-01-27 12:21:06 -0800763 Mutex::Autolock lock(mLocalSyncPointMutex);
764 mLocalSyncPoints.push_back(point);
765 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700766}
767
Mathias Agopian13127d82013-03-05 17:47:11 -0800768void Layer::setFiltering(bool filtering) {
769 mFiltering = filtering;
770}
771
772bool Layer::getFiltering() const {
773 return mFiltering;
774}
775
Mathias Agopian13127d82013-03-05 17:47:11 -0800776// ----------------------------------------------------------------------------
777// local state
778// ----------------------------------------------------------------------------
779
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000780static void boundPoint(vec2* point, const Rect& crop) {
781 if (point->x < crop.left) {
782 point->x = crop.left;
783 }
784 if (point->x > crop.right) {
785 point->x = crop.right;
786 }
787 if (point->y < crop.top) {
788 point->y = crop.top;
789 }
790 if (point->y > crop.bottom) {
791 point->y = crop.bottom;
792 }
793}
794
chaviwa76b2712017-09-20 12:02:26 -0700795void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
796 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700797 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700798 const Transform renderAreaTransform(renderArea.getTransform());
799 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700800 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700801
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000802 vec2 lt = vec2(win.left, win.top);
803 vec2 lb = vec2(win.left, win.bottom);
804 vec2 rb = vec2(win.right, win.bottom);
805 vec2 rt = vec2(win.right, win.top);
806
Robert Carr1f0a16a2016-10-24 16:27:39 -0700807 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700809 lt = layerTransform.transform(lt);
810 lb = layerTransform.transform(lb);
811 rb = layerTransform.transform(rb);
812 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000813 }
814
Robert Carrb5d3d262016-03-25 15:08:13 -0700815 if (!s.finalCrop.isEmpty()) {
816 boundPoint(&lt, s.finalCrop);
817 boundPoint(&lb, s.finalCrop);
818 boundPoint(&rb, s.finalCrop);
819 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000820 }
821
Mathias Agopianff2ed702013-09-01 21:36:12 -0700822 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700823 position[0] = renderAreaTransform.transform(lt);
824 position[1] = renderAreaTransform.transform(lb);
825 position[2] = renderAreaTransform.transform(rb);
826 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800827 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700828 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800829 }
830}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800831
David Sodman41fdfc92017-11-06 16:09:56 -0800832bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700833 const Layer::State& s(mDrawingState);
834 return (s.flags & layer_state_t::eLayerSecure);
835}
836
Mathias Agopian13127d82013-03-05 17:47:11 -0800837void Layer::setVisibleRegion(const Region& visibleRegion) {
838 // always called from main thread
839 this->visibleRegion = visibleRegion;
840}
841
842void Layer::setCoveredRegion(const Region& coveredRegion) {
843 // always called from main thread
844 this->coveredRegion = coveredRegion;
845}
846
David Sodman41fdfc92017-11-06 16:09:56 -0800847void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800848 // always called from main thread
849 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
850}
851
852// ----------------------------------------------------------------------------
853// transaction
854// ----------------------------------------------------------------------------
855
Dan Stoza7dde5992015-05-22 09:51:44 -0700856void Layer::pushPendingState() {
857 if (!mCurrentState.modified) {
858 return;
859 }
860
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 // If this transaction is waiting on the receipt of a frame, generate a sync
862 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800863 if (mCurrentState.barrierLayer != nullptr) {
864 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
865 if (barrierLayer == nullptr) {
866 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700867 // If we can't promote the layer we are intended to wait on,
868 // then it is expired or otherwise invalid. Allow this transaction
869 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800870 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800871 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800872 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800873 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800874 mRemoteSyncPoints.push_back(std::move(syncPoint));
875 } else {
876 // We already missed the frame we're supposed to synchronize
877 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800878 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800879 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700880 }
881
Dan Stoza7dde5992015-05-22 09:51:44 -0700882 // Wake us up to check if the frame has been received
883 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700884 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 }
886 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700887 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700888}
889
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890void Layer::popPendingState(State* stateToCommit) {
891 auto oldFlags = stateToCommit->flags;
892 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800893 stateToCommit->flags =
894 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700895
896 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700897 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700898}
899
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 bool stateUpdateAvailable = false;
902 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800903 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 if (mRemoteSyncPoints.empty()) {
905 // If we don't have a sync point for this, apply it anyway. It
906 // will be visually wrong, but it should keep us from getting
907 // into too much trouble.
908 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 stateUpdateAvailable = true;
911 continue;
912 }
913
David Sodman41fdfc92017-11-06 16:09:56 -0800914 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
915 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800916
917 // Signal our end of the sync point and then dispose of it
918 mRemoteSyncPoints.front()->setTransactionApplied();
919 mRemoteSyncPoints.pop_front();
920 continue;
921 }
922
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
924 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 stateUpdateAvailable = true;
927
928 // Signal our end of the sync point and then dispose of it
929 mRemoteSyncPoints.front()->setTransactionApplied();
930 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800931 } else {
932 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700935 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 stateUpdateAvailable = true;
937 }
938 }
939
940 // If we still have pending updates, wake SurfaceFlinger back up and point
941 // it at this layer so we can process them
942 if (!mPendingStates.empty()) {
943 setTransactionFlags(eTransactionNeeded);
944 mFlinger->setTransactionFlags(eTraversalNeeded);
945 }
946
947 mCurrentState.modified = false;
948 return stateUpdateAvailable;
949}
950
Mathias Agopian13127d82013-03-05 17:47:11 -0800951uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800952 ATRACE_CALL();
953
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700955 Layer::State c = getCurrentState();
956 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 return 0;
958 }
959
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700960 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961
David Sodman41fdfc92017-11-06 16:09:56 -0800962 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700963
David Sodmaneb085e02017-10-05 18:49:04 -0700964 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700965 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000966 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800967 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
968 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
969 " requested={ wh={%4u,%4u} }}\n"
970 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
971 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -0700972 this, getName().string(), mCurrentTransform,
973 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
974 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
975 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
976 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
977 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700979 // record the new size, form this point on, when the client request
980 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700981 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700983
Robert Carre392b552017-09-19 12:16:05 -0700984 // Don't let Layer::doTransaction update the drawing state
985 // if we have a pending resize, unless we are in fixed-size mode.
986 // the drawing state will be updated only once we receive a buffer
987 // with the correct size.
988 //
989 // In particular, we want to make sure the clip (which is part
990 // of the geometry state) is latched together with the size but is
991 // latched immediately when no resizing is involved.
992 //
993 // If a sideband stream is attached, however, we want to skip this
994 // optimization so that transactions aren't missed when a buffer
995 // never arrives
996 //
997 // In the case that we don't have a buffer we ignore other factors
998 // and avoid entering the resizePending state. At a high level the
999 // resizePending state is to avoid applying the state of the new buffer
1000 // to the old buffer. However in the state where we don't have an old buffer
1001 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001002 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
1003 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001004 if (!isFixedSize()) {
David Sodman386c22e2017-11-09 16:34:46 -08001005 if (resizePending && getBE().mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001006 flags |= eDontUpdateGeometryState;
1007 }
1008 }
1009
Robert Carr7bf247e2017-05-18 14:02:49 -07001010 // Here we apply various requested geometry states, depending on our
1011 // latching configuration. See Layer.h for a detailed discussion of
1012 // how geometry latching is controlled.
1013 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001014 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001015
1016 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1017 // mode, which causes attributes which normally latch regardless of scaling mode,
1018 // to be delayed. We copy the requested state to the active state making sure
1019 // to respect these rules (again see Layer.h for a detailed discussion).
1020 //
1021 // There is an awkward asymmetry in the handling of the crop states in the position
1022 // states, as can be seen below. Largely this arises from position and transform
1023 // being stored in the same data structure while having different latching rules.
1024 // b/38182305
1025 //
1026 // Careful that "c" and editCurrentState may not begin as equivalent due to
1027 // applyPendingStates in the presence of deferred transactions.
1028 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001029 float tx = c.active.transform.tx();
1030 float ty = c.active.transform.ty();
1031 c.active = c.requested;
1032 c.active.transform.set(tx, ty);
1033 editCurrentState.active = c.active;
1034 } else {
1035 editCurrentState.active = editCurrentState.requested;
1036 c.active = c.requested;
1037 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001038 }
1039
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001040 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 // invalidate and recompute the visible regions if needed
1042 flags |= Layer::eVisibleRegion;
1043 }
1044
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001045 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 // invalidate and recompute the visible regions if needed
1047 flags |= eVisibleRegion;
1048 this->contentDirty = true;
1049
1050 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001051 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001052 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 }
1054
Dan Stozac8145172016-04-28 16:29:06 -07001055 // If the layer is hidden, signal and clear out all local sync points so
1056 // that transactions for layers depending on this layer's frames becoming
1057 // visible are not blocked
1058 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001059 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001060 }
1061
Mathias Agopian13127d82013-03-05 17:47:11 -08001062 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001063 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065}
1066
Pablo Ceballos05289c22016-04-14 15:49:55 -07001067void Layer::commitTransaction(const State& stateToCommit) {
1068 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001069}
1070
Mathias Agopian13127d82013-03-05 17:47:11 -08001071uint32_t Layer::getTransactionFlags(uint32_t flags) {
1072 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1073}
1074
1075uint32_t Layer::setTransactionFlags(uint32_t flags) {
1076 return android_atomic_or(flags, &mTransactionFlags);
1077}
1078
Robert Carr82364e32016-05-15 11:27:47 -07001079bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001080 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 return false;
1082 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001083
1084 // We update the requested and active position simultaneously because
1085 // we want to apply the position portion of the transform matrix immediately,
1086 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001087 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001088 if (immediate && !mFreezeGeometryUpdates) {
1089 // Here we directly update the active state
1090 // unlike other setters, because we store it within
1091 // the transform, but use different latching rules.
1092 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001093 mCurrentState.active.transform.set(x, y);
1094 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001095 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001096
Dan Stoza7dde5992015-05-22 09:51:44 -07001097 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001098 setTransactionFlags(eTransactionNeeded);
1099 return true;
1100}
Robert Carr82364e32016-05-15 11:27:47 -07001101
Robert Carr1f0a16a2016-10-24 16:27:39 -07001102bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1103 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1104 if (idx < 0) {
1105 return false;
1106 }
1107 if (childLayer->setLayer(z)) {
1108 mCurrentChildren.removeAt(idx);
1109 mCurrentChildren.add(childLayer);
1110 }
1111 return true;
1112}
1113
Robert Carr503c7042017-09-27 15:06:08 -07001114bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1115 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1116 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1117 if (idx < 0) {
1118 return false;
1119 }
1120 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1121 mCurrentChildren.removeAt(idx);
1122 mCurrentChildren.add(childLayer);
1123 }
1124 return true;
1125}
1126
Robert Carrae060832016-11-28 10:51:00 -08001127bool Layer::setLayer(int32_t z) {
David Sodman41fdfc92017-11-06 16:09:56 -08001128 if (mCurrentState.z == z) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 mCurrentState.sequence++;
1130 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001131 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001132
1133 // Discard all relative layering.
1134 if (mCurrentState.zOrderRelativeOf != nullptr) {
1135 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1136 if (strongRelative != nullptr) {
1137 strongRelative->removeZOrderRelative(this);
1138 }
1139 mCurrentState.zOrderRelativeOf = nullptr;
1140 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
1142 return true;
1143}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144
Robert Carrdb66e622017-04-10 16:55:57 -07001145void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1146 mCurrentState.zOrderRelatives.remove(relative);
1147 mCurrentState.sequence++;
1148 mCurrentState.modified = true;
1149 setTransactionFlags(eTransactionNeeded);
1150}
1151
1152void Layer::addZOrderRelative(const wp<Layer>& relative) {
1153 mCurrentState.zOrderRelatives.add(relative);
1154 mCurrentState.modified = true;
1155 mCurrentState.sequence++;
1156 setTransactionFlags(eTransactionNeeded);
1157}
1158
1159bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1160 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1161 if (handle == nullptr) {
1162 return false;
1163 }
1164 sp<Layer> relative = handle->owner.promote();
1165 if (relative == nullptr) {
1166 return false;
1167 }
1168
1169 mCurrentState.sequence++;
1170 mCurrentState.modified = true;
1171 mCurrentState.z = z;
1172
chaviw9ab4bd12017-11-03 13:11:00 -07001173 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1174 if (oldZOrderRelativeOf != nullptr) {
1175 oldZOrderRelativeOf->removeZOrderRelative(this);
1176 }
Robert Carrdb66e622017-04-10 16:55:57 -07001177 mCurrentState.zOrderRelativeOf = relative;
1178 relative->addZOrderRelative(this);
1179
1180 setTransactionFlags(eTransactionNeeded);
1181
1182 return true;
1183}
1184
Mathias Agopian13127d82013-03-05 17:47:11 -08001185bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001186 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 mCurrentState.requested.w = w;
1188 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001189 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001190 setTransactionFlags(eTransactionNeeded);
1191 return true;
1192}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001193bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001194 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001195 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001196 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001197 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001198 setTransactionFlags(eTransactionNeeded);
1199 return true;
1200}
chaviw13fdc492017-06-27 12:40:18 -07001201
1202bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001203 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1204 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001205 return false;
1206
1207 mCurrentState.sequence++;
1208 mCurrentState.color.r = color.r;
1209 mCurrentState.color.g = color.g;
1210 mCurrentState.color.b = color.b;
1211 mCurrentState.modified = true;
1212 setTransactionFlags(eTransactionNeeded);
1213 return true;
1214}
1215
Mathias Agopian13127d82013-03-05 17:47:11 -08001216bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1217 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001218 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001219 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 setTransactionFlags(eTransactionNeeded);
1221 return true;
1222}
1223bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001224 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001225 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
1227 return true;
1228}
1229bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1230 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001231 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 mCurrentState.sequence++;
1233 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001234 mCurrentState.mask = mask;
1235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
Robert Carr99e27f02016-06-16 15:18:02 -07001239
1240bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001241 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001243 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001244 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001245 mCurrentState.crop = crop;
1246 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001247 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1248
Dan Stoza7dde5992015-05-22 09:51:44 -07001249 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001250 setTransactionFlags(eTransactionNeeded);
1251 return true;
1252}
Robert Carr8d5227b2017-03-16 15:41:03 -07001253
1254bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001255 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001256 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001257 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001258 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001259 mCurrentState.finalCrop = crop;
1260 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001261 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1262
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001263 mCurrentState.modified = true;
1264 setTransactionFlags(eTransactionNeeded);
1265 return true;
1266}
Mathias Agopian13127d82013-03-05 17:47:11 -08001267
Robert Carrc3574f72016-03-24 12:19:32 -07001268bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001269 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001270 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001271 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001272 return true;
1273}
1274
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001275void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001276 mCurrentState.appId = appId;
1277 mCurrentState.type = type;
1278 mCurrentState.modified = true;
1279 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001280}
1281
Mathias Agopian13127d82013-03-05 17:47:11 -08001282bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001283 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 mCurrentState.sequence++;
1285 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001286 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 setTransactionFlags(eTransactionNeeded);
1288 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001289}
1290
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001291bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001292 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001293 mCurrentState.sequence++;
1294 mCurrentState.dataSpace = dataSpace;
1295 mCurrentState.modified = true;
1296 setTransactionFlags(eTransactionNeeded);
1297 return true;
1298}
1299
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001300android_dataspace Layer::getDataSpace() const {
1301 return mCurrentState.dataSpace;
1302}
1303
Robert Carr1f0a16a2016-10-24 16:27:39 -07001304uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001305 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001306 if (p == nullptr) {
1307 return getDrawingState().layerStack;
1308 }
1309 return p->getLayerStack();
1310}
1311
David Sodman41fdfc92017-11-06 16:09:56 -08001312void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001313 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001314 mCurrentState.frameNumber = frameNumber;
1315 // We don't set eTransactionNeeded, because just receiving a deferral
1316 // request without any other state updates shouldn't actually induce a delay
1317 mCurrentState.modified = true;
1318 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001319 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001320 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001321 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001322}
1323
David Sodman41fdfc92017-11-06 16:09:56 -08001324void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001325 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1326 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001327}
1328
Dan Stozaee44edd2015-03-23 15:50:23 -07001329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330// ----------------------------------------------------------------------------
1331// pageflip handling...
1332// ----------------------------------------------------------------------------
1333
Robert Carr1f0a16a2016-10-24 16:27:39 -07001334bool Layer::isHiddenByPolicy() const {
1335 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001336 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001337 if (parent != nullptr && parent->isHiddenByPolicy()) {
1338 return true;
1339 }
1340 return s.flags & layer_state_t::eLayerHidden;
1341}
1342
David Sodman41fdfc92017-11-06 16:09:56 -08001343uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001344 // TODO: should we do something special if mSecure is set?
1345 if (mProtectedByApp) {
1346 // need a hardware-protected path to external video sink
1347 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001348 }
Riley Andrews03414a12014-07-01 14:22:59 -07001349 if (mPotentialCursor) {
1350 usage |= GraphicBuffer::USAGE_CURSOR;
1351 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001352 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001353 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001354}
1355
Mathias Agopian84300952012-11-21 16:02:13 -08001356void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001357 uint32_t orientation = 0;
1358 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001359 // The transform hint is used to improve performance, but we can
1360 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001361 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001362 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001363 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001364 if (orientation & Transform::ROT_INVALID) {
1365 orientation = 0;
1366 }
1367 }
David Sodmaneb085e02017-10-05 18:49:04 -07001368 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001369}
1370
Mathias Agopian13127d82013-03-05 17:47:11 -08001371// ----------------------------------------------------------------------------
1372// debugging
1373// ----------------------------------------------------------------------------
1374
Kalle Raitaa099a242017-01-11 11:17:29 -08001375LayerDebugInfo Layer::getLayerDebugInfo() const {
1376 LayerDebugInfo info;
1377 const Layer::State& ds = getDrawingState();
1378 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001379 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1381 info.mType = String8(getTypeId());
1382 info.mTransparentRegion = ds.activeTransparentRegion;
1383 info.mVisibleRegion = visibleRegion;
1384 info.mSurfaceDamageRegion = surfaceDamageRegion;
1385 info.mLayerStack = getLayerStack();
1386 info.mX = ds.active.transform.tx();
1387 info.mY = ds.active.transform.ty();
1388 info.mZ = ds.z;
1389 info.mWidth = ds.active.w;
1390 info.mHeight = ds.active.h;
1391 info.mCrop = ds.crop;
1392 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001393 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001394 info.mFlags = ds.flags;
1395 info.mPixelFormat = getPixelFormat();
1396 info.mDataSpace = getDataSpace();
1397 info.mMatrix[0][0] = ds.active.transform[0][0];
1398 info.mMatrix[0][1] = ds.active.transform[0][1];
1399 info.mMatrix[1][0] = ds.active.transform[1][0];
1400 info.mMatrix[1][1] = ds.active.transform[1][1];
1401 {
1402 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1403 if (activeBuffer != 0) {
1404 info.mActiveBufferWidth = activeBuffer->getWidth();
1405 info.mActiveBufferHeight = activeBuffer->getHeight();
1406 info.mActiveBufferStride = activeBuffer->getStride();
1407 info.mActiveBufferFormat = activeBuffer->format;
1408 } else {
1409 info.mActiveBufferWidth = 0;
1410 info.mActiveBufferHeight = 0;
1411 info.mActiveBufferStride = 0;
1412 info.mActiveBufferFormat = 0;
1413 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001414 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001415 info.mNumQueuedFrames = getQueuedFrameCount();
1416 info.mRefreshPending = isBufferLatched();
1417 info.mIsOpaque = isOpaque(ds);
1418 info.mContentDirty = contentDirty;
1419 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001420}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001421
Dan Stozae22aec72016-08-01 13:20:59 -07001422void Layer::miniDumpHeader(String8& result) {
1423 result.append("----------------------------------------");
1424 result.append("---------------------------------------\n");
1425 result.append(" Layer name\n");
1426 result.append(" Z | ");
1427 result.append(" Comp Type | ");
1428 result.append(" Disp Frame (LTRB) | ");
1429 result.append(" Source Crop (LTRB)\n");
1430 result.append("----------------------------------------");
1431 result.append("---------------------------------------\n");
1432}
1433
1434void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001435 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001436 return;
1437 }
1438
1439 String8 name;
1440 if (mName.length() > 77) {
1441 std::string shortened;
1442 shortened.append(mName.string(), 36);
1443 shortened.append("[...]");
1444 shortened.append(mName.string() + (mName.length() - 36), 36);
1445 name = shortened.c_str();
1446 } else {
1447 name = mName;
1448 }
1449
1450 result.appendFormat(" %s\n", name.string());
1451
1452 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001453 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001454 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001455 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001456 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001457 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001458 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001459 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 -07001460
1461 result.append("- - - - - - - - - - - - - - - - - - - - ");
1462 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1463}
Dan Stozae22aec72016-08-01 13:20:59 -07001464
Svetoslavd85084b2014-03-20 10:28:31 -07001465void Layer::dumpFrameStats(String8& result) const {
1466 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001467}
1468
Svetoslavd85084b2014-03-20 10:28:31 -07001469void Layer::clearFrameStats() {
1470 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001471}
1472
Jamie Gennis6547ff42013-07-16 20:12:42 -07001473void Layer::logFrameStats() {
1474 mFrameTracker.logAndResetStats(mName);
1475}
1476
Svetoslavd85084b2014-03-20 10:28:31 -07001477void Layer::getFrameStats(FrameStats* outStats) const {
1478 mFrameTracker.getStats(outStats);
1479}
1480
Brian Andersond6927fb2016-07-23 23:37:30 -07001481void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001482 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001483 Mutex::Autolock lock(mFrameEventHistoryMutex);
1484 mFrameEventHistory.checkFencesForCompletion();
1485 mFrameEventHistory.dump(result);
1486}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001487
Brian Anderson5ea5e592016-12-01 16:54:33 -08001488void Layer::onDisconnect() {
1489 Mutex::Autolock lock(mFrameEventHistoryMutex);
1490 mFrameEventHistory.onDisconnect();
1491}
1492
Brian Anderson3890c392016-07-25 12:48:08 -07001493void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001494 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001495 Mutex::Autolock lock(mFrameEventHistoryMutex);
1496 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001497 // If there are any unsignaled fences in the aquire timeline at this
1498 // point, the previously queued frame hasn't been latched yet. Go ahead
1499 // and try to get the signal time here so the syscall is taken out of
1500 // the main thread's critical path.
1501 mAcquireTimeline.updateSignalTimes();
1502 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001503 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001504 mFrameEventHistory.addQueue(*newTimestamps);
1505 }
1506
Brian Anderson3890c392016-07-25 12:48:08 -07001507 if (outDelta) {
1508 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001509 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001510}
Dan Stozae77c7662016-05-13 11:37:28 -07001511
Chia-I Wu98f1c102017-05-30 14:54:08 -07001512size_t Layer::getChildrenCount() const {
1513 size_t count = 0;
1514 for (const sp<Layer>& child : mCurrentChildren) {
1515 count += 1 + child->getChildrenCount();
1516 }
1517 return count;
1518}
1519
Robert Carr1f0a16a2016-10-24 16:27:39 -07001520void Layer::addChild(const sp<Layer>& layer) {
1521 mCurrentChildren.add(layer);
1522 layer->setParent(this);
1523}
1524
1525ssize_t Layer::removeChild(const sp<Layer>& layer) {
1526 layer->setParent(nullptr);
1527 return mCurrentChildren.remove(layer);
1528}
1529
Robert Carr1db73f62016-12-21 12:58:51 -08001530bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1531 sp<Handle> handle = nullptr;
1532 sp<Layer> newParent = nullptr;
1533 if (newParentHandle == nullptr) {
1534 return false;
1535 }
1536 handle = static_cast<Handle*>(newParentHandle.get());
1537 newParent = handle->owner.promote();
1538 if (newParent == nullptr) {
1539 ALOGE("Unable to promote Layer handle");
1540 return false;
1541 }
1542
1543 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001544 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001545
1546 sp<Client> client(child->mClientRef.promote());
1547 if (client != nullptr) {
1548 client->setParentLayer(newParent);
1549 }
1550 }
1551 mCurrentChildren.clear();
1552
1553 return true;
1554}
1555
chaviwf1961f72017-09-18 16:41:07 -07001556bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1557 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001558 return false;
1559 }
1560
1561 auto handle = static_cast<Handle*>(newParentHandle.get());
1562 sp<Layer> newParent = handle->owner.promote();
1563 if (newParent == nullptr) {
1564 ALOGE("Unable to promote Layer handle");
1565 return false;
1566 }
1567
chaviwf1961f72017-09-18 16:41:07 -07001568 sp<Layer> parent = getParent();
1569 if (parent != nullptr) {
1570 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001571 }
chaviwf1961f72017-09-18 16:41:07 -07001572 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001573
chaviwf1961f72017-09-18 16:41:07 -07001574 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001575 sp<Client> newParentClient(newParent->mClientRef.promote());
1576
chaviwf1961f72017-09-18 16:41:07 -07001577 if (client != newParentClient) {
1578 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001579 }
1580
chaviw06178942017-07-27 10:25:59 -07001581 return true;
1582}
1583
Robert Carr9524cb32017-02-13 11:32:32 -08001584bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001585 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001586 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001587 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001588 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001589 client->detachLayer(child.get());
1590 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001591 }
Robert Carr7f619b22017-11-06 12:56:35 -08001592 }
Robert Carr9524cb32017-02-13 11:32:32 -08001593
1594 return true;
1595}
1596
Robert Carr1f0a16a2016-10-24 16:27:39 -07001597void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001598 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599}
1600
1601void Layer::clearSyncPoints() {
1602 for (const auto& child : mCurrentChildren) {
1603 child->clearSyncPoints();
1604 }
1605
1606 Mutex::Autolock lock(mLocalSyncPointMutex);
1607 for (auto& point : mLocalSyncPoints) {
1608 point->setFrameAvailable();
1609 }
1610 mLocalSyncPoints.clear();
1611}
1612
1613int32_t Layer::getZ() const {
1614 return mDrawingState.z;
1615}
1616
David Sodman41fdfc92017-11-06 16:09:56 -08001617__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
1618 LayerVector::StateSet stateSet) {
Dan Stoza412903f2017-04-27 13:42:17 -07001619 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1620 "makeTraversalList received invalid stateSet");
1621 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1622 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1623 const State& state = useDrawing ? mDrawingState : mCurrentState;
1624
Robert Carrdb66e622017-04-10 16:55:57 -07001625 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001626 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001627 sp<Layer> strongRelative = weakRelative.promote();
1628 if (strongRelative != nullptr) {
1629 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001630 }
1631 }
1632
Dan Stoza412903f2017-04-27 13:42:17 -07001633 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001634 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1635 if (childState.zOrderRelativeOf != nullptr) {
1636 continue;
1637 }
Robert Carrdb66e622017-04-10 16:55:57 -07001638 traverse.add(child);
1639 }
1640
1641 return traverse;
1642}
1643
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644/**
Robert Carrdb66e622017-04-10 16:55:57 -07001645 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 */
Dan Stoza412903f2017-04-27 13:42:17 -07001647void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1648 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001649
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001651 for (; i < list.size(); i++) {
1652 const auto& relative = list[i];
1653 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001655 }
Dan Stoza412903f2017-04-27 13:42:17 -07001656 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 }
Dan Stoza412903f2017-04-27 13:42:17 -07001658 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001659 for (; i < list.size(); i++) {
1660 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001661 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 }
1663}
1664
1665/**
Robert Carrdb66e622017-04-10 16:55:57 -07001666 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 */
Dan Stoza412903f2017-04-27 13:42:17 -07001668void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1669 const LayerVector::Visitor& visitor) {
1670 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001671
Robert Carr1f0a16a2016-10-24 16:27:39 -07001672 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001673 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001674 const auto& relative = list[i];
1675 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 break;
1677 }
Dan Stoza412903f2017-04-27 13:42:17 -07001678 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 }
Dan Stoza412903f2017-04-27 13:42:17 -07001680 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001681 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001682 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001683 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684 }
1685}
1686
chaviwa76b2712017-09-20 12:02:26 -07001687/**
1688 * Traverse only children in z order, ignoring relative layers.
1689 */
1690void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1691 const LayerVector::Visitor& visitor) {
1692 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1693 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1694
1695 size_t i = 0;
1696 for (; i < children.size(); i++) {
1697 const auto& relative = children[i];
1698 if (relative->getZ() >= 0) {
1699 break;
1700 }
1701 relative->traverseChildrenInZOrder(stateSet, visitor);
1702 }
1703 visitor(this);
1704 for (; i < children.size(); i++) {
1705 const auto& relative = children[i];
1706 relative->traverseChildrenInZOrder(stateSet, visitor);
1707 }
1708}
1709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710Transform Layer::getTransform() const {
1711 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001712 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713 if (p != nullptr) {
1714 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001715
1716 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1717 // it isFixedSize) then there may be additional scaling not accounted
1718 // for in the transform. We need to mirror this scaling in child surfaces
1719 // or we will break the contract where WM can treat child surfaces as
1720 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07001721 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001722 int bufferWidth;
1723 int bufferHeight;
1724 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
1725 bufferWidth = p->mActiveBuffer->getWidth();
1726 bufferHeight = p->mActiveBuffer->getHeight();
1727 } else {
1728 bufferHeight = p->mActiveBuffer->getWidth();
1729 bufferWidth = p->mActiveBuffer->getHeight();
1730 }
David Sodman41fdfc92017-11-06 16:09:56 -08001731 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1732 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001733 Transform extraParentScaling;
1734 extraParentScaling.set(sx, 0, 0, sy);
1735 t = t * extraParentScaling;
1736 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 }
1738 return t * getDrawingState().active.transform;
1739}
1740
chaviw13fdc492017-06-27 12:40:18 -07001741half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001742 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001743
chaviw13fdc492017-06-27 12:40:18 -07001744 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1745 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001746}
Robert Carr6452f122017-03-21 10:41:29 -07001747
chaviw13fdc492017-06-27 12:40:18 -07001748half4 Layer::getColor() const {
1749 const half4 color(getDrawingState().color);
1750 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001751}
Robert Carr6452f122017-03-21 10:41:29 -07001752
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753void Layer::commitChildList() {
1754 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1755 const auto& child = mCurrentChildren[i];
1756 child->commitChildList();
1757 }
1758 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001759 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760}
1761
chaviw1d044282017-09-27 12:19:28 -07001762void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1763 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1764 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1765 const State& state = useDrawing ? mDrawingState : mCurrentState;
1766
1767 Transform requestedTransform = state.active.transform;
1768 Transform transform = getTransform();
1769
1770 layerInfo->set_id(sequence);
1771 layerInfo->set_name(getName().c_str());
1772 layerInfo->set_type(String8(getTypeId()));
1773
1774 for (const auto& child : children) {
1775 layerInfo->add_children(child->sequence);
1776 }
1777
1778 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1779 sp<Layer> strongRelative = weakRelative.promote();
1780 if (strongRelative != nullptr) {
1781 layerInfo->add_relatives(strongRelative->sequence);
1782 }
1783 }
1784
1785 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1786 layerInfo->mutable_transparent_region());
1787 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1788 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1789
1790 layerInfo->set_layer_stack(getLayerStack());
1791 layerInfo->set_z(state.z);
1792
1793 PositionProto* position = layerInfo->mutable_position();
1794 position->set_x(transform.tx());
1795 position->set_y(transform.ty());
1796
1797 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1798 requestedPosition->set_x(requestedTransform.tx());
1799 requestedPosition->set_y(requestedTransform.ty());
1800
1801 SizeProto* size = layerInfo->mutable_size();
1802 size->set_w(state.active.w);
1803 size->set_h(state.active.h);
1804
1805 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1806 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1807
1808 layerInfo->set_is_opaque(isOpaque(state));
1809 layerInfo->set_invalidate(contentDirty);
1810 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1811 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1812 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1813 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1814 layerInfo->set_flags(state.flags);
1815
1816 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1817 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1818
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001819 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001820 if (parent != nullptr) {
1821 layerInfo->set_parent(parent->sequence);
1822 }
1823
1824 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1825 if (zOrderRelativeOf != nullptr) {
1826 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1827 }
1828
1829 auto activeBuffer = getActiveBuffer();
1830 if (activeBuffer != nullptr) {
1831 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
1832 }
1833
1834 layerInfo->set_queued_frames(getQueuedFrameCount());
1835 layerInfo->set_refresh_pending(isBufferLatched());
1836}
1837
Mathias Agopian13127d82013-03-05 17:47:11 -08001838// ---------------------------------------------------------------------------
1839
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001841
1842#if defined(__gl_h_)
1843#error "don't include gl/gl.h in this file"
1844#endif
1845
1846#if defined(__gl2_h_)
1847#error "don't include gl2/gl2.h in this file"
1848#endif