blob: 136fbc1b210514c8672eb1d1b806f75d6481ca1a [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
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
David Sodman0c69cad2017-08-21 12:12:51 -070072 mSurfaceFlingerConsumer(NULL),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070074 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080079 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080080 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070082 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080084 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080085 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080086 mFiltering(false),
87 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070088 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000089#ifndef USE_HWC2
90 mIsGlesComposition(false),
91#endif
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
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000132#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 const auto& hwc = flinger->getHwComposer();
134 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
135 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000136#else
David Sodman41fdfc92017-11-06 16:09:56 -0800137 nsecs_t displayPeriod = flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000138#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700139 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800140
141 CompositorTiming compositorTiming;
142 flinger->getCompositorTiming(&compositorTiming);
143 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800144}
145
David Sodman41fdfc92017-11-06 16:09:56 -0800146void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700147
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700149 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700150}
151
Mathias Agopian13127d82013-03-05 17:47:11 -0800152// ---------------------------------------------------------------------------
153// callbacks
154// ---------------------------------------------------------------------------
155
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000156#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800157void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
158 if (mHwcLayers.empty()) {
159 return;
160 }
David Sodman0c69cad2017-08-21 12:12:51 -0700161 if (mSurfaceFlingerConsumer) {
162 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
163 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800164}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000165#else
166void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
David Sodman41fdfc92017-11-06 16:09:56 -0800167 HWComposer::HWCLayerInterface* layer) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000168 if (layer) {
169 layer->onDisplayed();
170 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
171 }
172}
173#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800174
Chia-I Wuc6657022017-08-15 11:18:17 -0700175void Layer::onRemovedFromCurrentState() {
176 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
177
chaviw8b3871a2017-11-01 17:41:01 -0700178 mPendingRemoval = true;
179
Robert Carr5edb1ad2017-04-25 10:54:24 -0700180 if (mCurrentState.zOrderRelativeOf != nullptr) {
181 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
182 if (strongRelative != nullptr) {
183 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700184 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700185 }
186 mCurrentState.zOrderRelativeOf = nullptr;
187 }
188
Chia-I Wuc6657022017-08-15 11:18:17 -0700189 for (const auto& child : mCurrentChildren) {
190 child->onRemovedFromCurrentState();
191 }
192}
Chia-I Wu38512252017-05-17 14:36:16 -0700193
Chia-I Wuc6657022017-08-15 11:18:17 -0700194void Layer::onRemoved() {
195 // the layer is removed from SF mLayersPendingRemoval
196
David Sodman0c69cad2017-08-21 12:12:51 -0700197 if (mSurfaceFlingerConsumer) {
198 mSurfaceFlingerConsumer->abandon();
199 }
Chia-I Wu38512252017-05-17 14:36:16 -0700200#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700201 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700202#endif
203
Robert Carr1f0a16a2016-10-24 16:27:39 -0700204 for (const auto& child : mCurrentChildren) {
205 child->onRemoved();
206 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700207}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700208
Mathias Agopian13127d82013-03-05 17:47:11 -0800209// ---------------------------------------------------------------------------
210// set-up
211// ---------------------------------------------------------------------------
212
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700213const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800214 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
chaviw13fdc492017-06-27 12:40:18 -0700217bool Layer::getPremultipledAlpha() const {
218 return mPremultipliedAlpha;
219}
220
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700221sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800222 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700223 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800224}
225
226// ---------------------------------------------------------------------------
227// h/w composer set-up
228// ---------------------------------------------------------------------------
229
Steven Thomasb02664d2017-07-26 18:48:28 -0700230#ifdef USE_HWC2
231bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman41fdfc92017-11-06 16:09:56 -0800232 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700233 HWC2::Layer* layer = hwc->createLayer(hwcId);
234 if (!layer) {
235 return false;
236 }
237 HWCInfo& hwcInfo = mHwcLayers[hwcId];
238 hwcInfo.hwc = hwc;
239 hwcInfo.layer = layer;
240 layer->setLayerDestroyedListener(
David Sodman41fdfc92017-11-06 16:09:56 -0800241 [this, hwcId](HWC2::Layer* /*layer*/) { mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700242 return true;
243}
244
245void Layer::destroyHwcLayer(int32_t hwcId) {
246 if (mHwcLayers.count(hwcId) == 0) {
247 return;
248 }
249 auto& hwcInfo = mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800250 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
252 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
253 // The layer destroyed listener should have cleared the entry from
254 // mHwcLayers. Verify that.
David Sodman41fdfc92017-11-06 16:09:56 -0800255 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Stale layer entry in mHwcLayers");
Steven Thomasb02664d2017-07-26 18:48:28 -0700256}
257
258void Layer::destroyAllHwcLayers() {
259 size_t numLayers = mHwcLayers.size();
260 for (size_t i = 0; i < numLayers; ++i) {
261 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
262 destroyHwcLayer(mHwcLayers.begin()->first);
263 }
264 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800265 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700266}
267#endif
268
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269Rect Layer::getContentCrop() const {
270 // this is the crop rectangle that applies to the buffer
271 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 Rect crop;
273 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800274 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800276 } else if (mActiveBuffer != NULL) {
277 // otherwise we use the whole buffer
278 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700279 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800280 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700281 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700282 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700283 return crop;
284}
285
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700286static Rect reduce(const Rect& win, const Region& exclude) {
287 if (CC_LIKELY(exclude.isEmpty())) {
288 return win;
289 }
290 if (exclude.isRect()) {
291 return win.reduce(exclude.getBounds());
292 }
293 return Region(win).subtract(exclude).getBounds();
294}
295
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
297 const Layer::State& s(getDrawingState());
298 Rect win(s.active.w, s.active.h);
299
300 if (!s.crop.isEmpty()) {
301 win.intersect(s.crop, &win);
302 }
303
304 Transform t = getTransform();
305 win = t.transform(win);
306
Robert Carr41b08b52017-06-01 16:11:34 -0700307 if (!s.finalCrop.isEmpty()) {
308 win.intersect(s.finalCrop, &win);
309 }
310
Chia-I Wue41dbe62017-06-13 14:10:56 -0700311 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700312 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
313 // When calculating the parent bounds for purposes of clipping,
314 // we don't need to constrain the parent to its transparent region.
315 // The transparent region is an optimization based on the
316 // buffer contents of the layer, but does not affect the space allocated to
317 // it by policy, and thus children should be allowed to extend into the
318 // parent's transparent region. In fact one of the main uses, is to reduce
319 // buffer allocation size in cases where a child window sits behind a main window
320 // (by marking the hole in the parent window as a transparent region)
321 if (p != nullptr) {
322 Rect bounds = p->computeScreenBounds(false);
323 bounds.intersect(win, &win);
324 }
325
326 if (reduceTransparentRegion) {
327 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
328 win = reduce(win, screenTransparentRegion);
329 }
330
331 return win;
332}
333
Mathias Agopian13127d82013-03-05 17:47:11 -0800334Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700335 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700336 return computeBounds(s.activeTransparentRegion);
337}
338
339Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
340 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700342
343 if (!s.crop.isEmpty()) {
344 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800345 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346
347 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700348 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800350 // Look in computeScreenBounds recursive call for explanation of
351 // why we pass false here.
352 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353 }
354
355 Transform t = getTransform();
356 if (p != nullptr) {
357 win = t.transform(win);
358 win.intersect(bounds, &win);
359 win = t.inverse().transform(win);
360 }
361
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700362 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700363 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800364}
365
Robert Carr1f0a16a2016-10-24 16:27:39 -0700366Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700367 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800368 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700369 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800370
371 // apply the projection's clipping to the window crop in
372 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700373 // if there are no window scaling involved, this operation will map to full
374 // pixels in the buffer.
375 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
376 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700377
378 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700379 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700380 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700381 }
382
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383 Transform t = getTransform();
384 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000385 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
386 activeCrop.clear();
387 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700388 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800389 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000390 activeCrop.clear();
391 }
392 }
chaviwb1154d12017-10-31 14:15:36 -0700393
394 const auto& p = mDrawingParent.promote();
395 if (p != nullptr) {
396 auto parentCrop = p->computeInitialCrop(hw);
397 activeCrop.intersect(parentCrop, &activeCrop);
398 }
399
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 return activeCrop;
401}
402
Dan Stoza5a423ea2017-02-16 14:10:39 -0800403FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 // the content crop is the area of the content that gets scaled to the
405 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800406 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407
408 // In addition there is a WM-specified crop we pull from our drawing state.
409 const State& s(getDrawingState());
410
411 // Screen space to make reduction to parent crop clearer.
412 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700413 Transform t = getTransform();
414 // Back to layer space to work with the content crop.
415 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800416
Michael Lentine28ea2172014-11-19 18:32:37 -0800417 // This needs to be here as transform.transform(Rect) computes the
418 // transformed rect and then takes the bounding box of the result before
419 // returning. This means
420 // transform.inverse().transform(transform.transform(Rect)) != Rect
421 // in which case we need to make sure the final rect is clipped to the
422 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
424 activeCrop.clear();
425 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800426
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700427 // subtract the transparent region and snap to the bounds
428 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
429
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 // Transform the window crop to match the buffer coordinate system,
431 // which means using the inverse of the current transform set on the
432 // SurfaceFlingerConsumer.
433 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700434 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700436 * the code below applies the primary display's inverse transform to the
437 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 */
David Sodman41fdfc92017-11-06 16:09:56 -0800439 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 // calculate the inverse transform
441 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800442 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800443 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800446 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447
448 int winWidth = s.active.w;
449 int winHeight = s.active.h;
450 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
451 // If the activeCrop has been rotate the ends are rotated but not
452 // the space itself so when transforming ends back we can't rely on
453 // a modification of the axes of rotation. To account for this we
454 // need to reorient the inverse rotation in terms of the current
455 // axes of rotation.
456 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
457 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
458 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800459 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 }
461 winWidth = s.active.h;
462 winHeight = s.active.w;
463 }
David Sodman41fdfc92017-11-06 16:09:56 -0800464 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465
466 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800467 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 float yScale = crop.getHeight() / float(winHeight);
469
David Sodman41fdfc92017-11-06 16:09:56 -0800470 float insetL = winCrop.left * xScale;
471 float insetT = winCrop.top * yScale;
472 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 float insetB = (winHeight - winCrop.bottom) * yScale;
474
David Sodman41fdfc92017-11-06 16:09:56 -0800475 crop.left += insetL;
476 crop.top += insetT;
477 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 crop.bottom -= insetB;
479
Mathias Agopian13127d82013-03-05 17:47:11 -0800480 return crop;
481}
482
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000483#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800484void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000485#else
David Sodman41fdfc92017-11-06 16:09:56 -0800486void Layer::setGeometry(const sp<const DisplayDevice>& hw, HWComposer::HWCLayerInterface& layer)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000487#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700488{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000489#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490 const auto hwcId = displayDevice->getHwcDisplayId();
491 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000492#else
493 layer.setDefaultState();
494#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700495
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700496 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000497#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 hwcInfo.forceClientComposition = false;
499
500 if (isSecure() && !displayDevice->isSecure()) {
501 hwcInfo.forceClientComposition = true;
502 }
503
504 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000505#else
506 layer.setSkip(false);
507
508 if (isSecure() && !hw->isSecure()) {
509 layer.setSkip(true);
510 }
511#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700512
Mathias Agopian13127d82013-03-05 17:47:11 -0800513 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700514 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000515#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500516 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700517 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800518 blendMode =
519 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 }
David Revemanecf0fa52017-03-03 11:32:44 -0500521 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800522 ALOGE_IF(error != HWC2::Error::None,
523 "[%s] Failed to set blend mode %s:"
524 " %s (%d)",
525 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
526 static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000527#else
chaviw13fdc492017-06-27 12:40:18 -0700528 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800529 layer.setBlending(mPremultipliedAlpha ? HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000530 }
531#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800532
533 // apply the layer's transform, followed by the display's global transform
534 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700536 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700537 if (!s.crop.isEmpty()) {
538 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700539 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000540#ifdef USE_HWC2
David Sodman41fdfc92017-11-06 16:09:56 -0800541 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000542#else
David Sodman41fdfc92017-11-06 16:09:56 -0800543 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000544#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000545 activeCrop.clear();
546 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700547 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800548 // This needs to be here as transform.transform(Rect) computes the
549 // transformed rect and then takes the bounding box of the result before
550 // returning. This means
551 // transform.inverse().transform(transform.transform(Rect)) != Rect
552 // in which case we need to make sure the final rect is clipped to the
553 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800554 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 activeCrop.clear();
556 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700557 // mark regions outside the crop as transparent
558 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800559 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
560 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
561 activeTransparentRegion.orSelf(
562 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700563 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700564
565 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700566 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800567 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000568 frame.clear();
569 }
570 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000571#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000572 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
573 frame.clear();
574 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575 const Transform& tr(displayDevice->getTransform());
576 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500577 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700578 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800579 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
580 transformedFrame.left, transformedFrame.top, transformedFrame.right,
581 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700582 } else {
583 hwcInfo.displayFrame = transformedFrame;
584 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
Dan Stoza5a423ea2017-02-16 14:10:39 -0800586 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700588 if (error != HWC2::Error::None) {
589 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800590 "%s (%d)",
591 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
592 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700593 } else {
594 hwcInfo.sourceCrop = sourceCrop;
595 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596
chaviw13fdc492017-06-27 12:40:18 -0700597 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700598 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800599 ALOGE_IF(error != HWC2::Error::None,
600 "[%s] Failed to set plane alpha %.3f: "
601 "%s (%d)",
602 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603
Robert Carrae060832016-11-28 10:51:00 -0800604 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800605 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
606 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500607
Albert Chaulk2a589632017-05-04 16:59:44 -0400608 int type = s.type;
609 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700610 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400611 if (parent.get()) {
612 auto& parentState = parent->getDrawingState();
613 type = parentState.type;
614 appId = parentState.appId;
615 }
616
617 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800618 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
619 static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000620#else
621 if (!frame.intersect(hw->getViewport(), &frame)) {
622 frame.clear();
623 }
624 const Transform& tr(hw->getTransform());
625 layer.setFrame(tr.transform(frame));
626 layer.setCrop(computeCrop(hw));
David Sodman41fdfc92017-11-06 16:09:56 -0800627 layer.setPlaneAlpha(static_cast<uint8_t>(std::round(255.0f * getAlpha())));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000628#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800629
Mathias Agopian29a367b2011-07-12 14:51:45 -0700630 /*
631 * Transformations are applied in this order:
632 * 1) buffer orientation/flip/mirror
633 * 2) state transformation (window manager)
634 * 3) layer orientation (screen orientation)
635 * (NOTE: the matrices are multiplied in reverse order)
636 */
637
638 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700639 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640
Robert Carrcae605c2017-03-29 12:10:31 -0700641 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700642 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700643 * the code below applies the primary display's inverse transform to the
644 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 */
David Sodman41fdfc92017-11-06 16:09:56 -0800646 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700647 // calculate the inverse transform
648 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800649 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650 }
Robert Carrcae605c2017-03-29 12:10:31 -0700651
652 /*
653 * Here we cancel out the orientation component of the WM transform.
654 * The scaling and translate components are already included in our bounds
655 * computation so it's enough to just omit it in the composition.
656 * See comment in onDraw with ref to b/36727915 for why.
657 */
658 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700659 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700660
661 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800662 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000663#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664 if (orientation & Transform::ROT_INVALID) {
665 // we can only handle simple transformation
666 hwcInfo.forceClientComposition = true;
667 } else {
668 auto transform = static_cast<HWC2::Transform>(orientation);
669 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800670 ALOGE_IF(error != HWC2::Error::None,
671 "[%s] Failed to set transform %s: "
672 "%s (%d)",
673 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
674 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000676#else
677 if (orientation & Transform::ROT_INVALID) {
678 // we can only handle simple transformation
679 layer.setSkip(true);
680 } else {
681 layer.setTransform(orientation);
682 }
683#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700684}
685
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000686#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687void Layer::forceClientComposition(int32_t hwcId) {
688 if (mHwcLayers.count(hwcId) == 0) {
689 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
690 return;
691 }
692
693 mHwcLayers[hwcId].forceClientComposition = true;
694}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695
Dan Stoza9e56aa02015-11-02 13:00:03 -0800696void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
697 // Apply this display's projection's viewport to the visible region
698 // before giving it to the HWC HAL.
699 const Transform& tr = displayDevice->getTransform();
700 const auto& viewport = displayDevice->getViewport();
701 Region visible = tr.transform(visibleRegion.intersect(viewport));
702 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800703 auto& hwcInfo = mHwcLayers[hwcId];
704 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800705 auto error = hwcLayer->setVisibleRegion(visible);
706 if (error != HWC2::Error::None) {
707 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
David Sodman41fdfc92017-11-06 16:09:56 -0800708 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709 visible.dump(LOG_TAG);
710 }
711
712 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
713 if (error != HWC2::Error::None) {
714 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
David Sodman41fdfc92017-11-06 16:09:56 -0800715 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 surfaceDamageRegion.dump(LOG_TAG);
717 }
718
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700719 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700721 setCompositionType(hwcId, HWC2::Composition::Sideband);
722 ALOGV("[%s] Requesting Sideband composition", mName.string());
723 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800724 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800725 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
726 mSidebandStream->handle(), to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700728 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800729 }
730
Dan Stoza0a21df72016-07-20 12:52:38 -0700731 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800732 if (hwcInfo.forceClientComposition ||
David Sodman41fdfc92017-11-06 16:09:56 -0800733 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700734 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700736 return;
737 }
738
Dan Stoza0a21df72016-07-20 12:52:38 -0700739 // SolidColor layers
740 if (mActiveBuffer == nullptr) {
741 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700742
chaviw13fdc492017-06-27 12:40:18 -0700743 half4 color = getColor();
David Sodman41fdfc92017-11-06 16:09:56 -0800744 error = hwcLayer->setColor({static_cast<uint8_t>(std::round(255.0f * color.r)),
745 static_cast<uint8_t>(std::round(255.0f * color.g)),
746 static_cast<uint8_t>(std::round(255.0f * color.b)), 255});
Dan Stoza0a21df72016-07-20 12:52:38 -0700747 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800748 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(), to_string(error).c_str(),
749 static_cast<int32_t>(error));
Dan Stoza0a21df72016-07-20 12:52:38 -0700750 }
Dan Stozac6c89542016-07-27 10:16:52 -0700751
752 // Clear out the transform, because it doesn't make sense absent a
753 // source buffer
754 error = hwcLayer->setTransform(HWC2::Transform::None);
755 if (error != HWC2::Error::None) {
756 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
David Sodman41fdfc92017-11-06 16:09:56 -0800757 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozac6c89542016-07-27 10:16:52 -0700758 }
759
Dan Stoza0a21df72016-07-20 12:52:38 -0700760 return;
761 }
762
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700763 // Device or Cursor layers
764 if (mPotentialCursor) {
765 ALOGV("[%s] Requesting Cursor composition", mName.string());
766 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 } else {
768 ALOGV("[%s] Requesting Device composition", mName.string());
769 setCompositionType(hwcId, HWC2::Composition::Device);
770 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700771
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700772 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
773 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
774 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800775 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentState.dataSpace,
776 to_string(error).c_str(), static_cast<int32_t>(error));
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700777 }
778
Chia-I Wu06d63de2017-01-04 14:58:51 +0800779 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400780 sp<GraphicBuffer> hwcBuffer;
David Sodman41fdfc92017-11-06 16:09:56 -0800781 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer, &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800782
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700783 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400784 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700785 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800786 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), mActiveBuffer->handle,
787 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700788 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600790
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000791#else
792void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
David Sodman41fdfc92017-11-06 16:09:56 -0800793 HWComposer::HWCLayerInterface& layer) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000794 // we have to set the visible region on every frame because
795 // we currently free it during onLayerDisplayed(), which is called
796 // after HWComposer::commit() -- every frame.
797 // Apply this display's projection's viewport to the visible region
798 // before giving it to the HWC HAL.
799 const Transform& tr = hw->getTransform();
800 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
801 layer.setVisibleRegionScreen(visible);
802 layer.setSurfaceDamage(surfaceDamageRegion);
803 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700804
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000805 if (mSidebandStream.get()) {
806 layer.setSidebandStream(mSidebandStream);
807 } else {
808 // NOTE: buffer can be NULL if the client never drew into this
809 // layer yet, or if we ran out of memory
810 layer.setBuffer(mActiveBuffer);
811 }
812}
813#endif
814
815#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800816void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
817 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman41fdfc92017-11-06 16:09:56 -0800818 if (mHwcLayers.count(hwcId) == 0 || getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800819 return;
820 }
821
822 // This gives us only the "orientation" component of the transform
823 const State& s(getCurrentState());
824
825 // Apply the layer's transform, followed by the display's global transform
826 // Here we're guaranteed that the layer's transform preserves rects
827 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700828 if (!s.crop.isEmpty()) {
829 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800830 }
831 // Subtract the transparent region and snap to the bounds
832 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700833 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800834 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700835 if (!s.finalCrop.isEmpty()) {
836 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000837 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800838 auto& displayTransform(displayDevice->getTransform());
839 auto position = displayTransform.transform(frame);
840
David Sodman41fdfc92017-11-06 16:09:56 -0800841 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, position.top);
842 ALOGE_IF(error != HWC2::Error::None,
843 "[%s] Failed to set cursor position "
844 "to (%d, %d): %s (%d)",
845 mName.string(), position.left, position.top, to_string(error).c_str(),
846 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800847}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000848#else
849void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
David Sodman41fdfc92017-11-06 16:09:56 -0800850 HWComposer::HWCLayerInterface& layer) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000851 int fenceFd = -1;
852
853 // TODO: there is a possible optimization here: we only need to set the
854 // acquire fence the first time a new buffer is acquired on EACH display.
855
David Sodman41fdfc92017-11-06 16:09:56 -0800856 if (layer.getCompositionType() == HWC_OVERLAY ||
857 layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000858 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
859 if (fence->isValid()) {
860 fenceFd = fence->dup();
861 if (fenceFd == -1) {
862 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
863 }
864 }
865 }
866 layer.setAcquireFenceFd(fenceFd);
867}
868
David Sodman41fdfc92017-11-06 16:09:56 -0800869Rect Layer::getPosition(const sp<const DisplayDevice>& hw) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000870 // this gives us only the "orientation" component of the transform
871 const State& s(getCurrentState());
872
873 // apply the layer's transform, followed by the display's global transform
874 // here we're guaranteed that the layer's transform preserves rects
875 Rect win(s.active.w, s.active.h);
876 if (!s.crop.isEmpty()) {
877 win.intersect(s.crop, &win);
878 }
879 // subtract the transparent region and snap to the bounds
880 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700881 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000882 frame.intersect(hw->getViewport(), &frame);
883 if (!s.finalCrop.isEmpty()) {
884 frame.intersect(s.finalCrop, &frame);
885 }
886 const Transform& tr(hw->getTransform());
887 return Rect(tr.transform(frame));
888}
889#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700890
Mathias Agopian13127d82013-03-05 17:47:11 -0800891// ---------------------------------------------------------------------------
892// drawing...
893// ---------------------------------------------------------------------------
894
chaviwa76b2712017-09-20 12:02:26 -0700895void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
896 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800897}
898
chaviwa76b2712017-09-20 12:02:26 -0700899void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
900 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800901}
902
chaviwa76b2712017-09-20 12:02:26 -0700903void Layer::draw(const RenderArea& renderArea) const {
904 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800905}
906
David Sodman41fdfc92017-11-06 16:09:56 -0800907void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
908 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700909 RenderEngine& engine(mFlinger->getRenderEngine());
chaviwa76b2712017-09-20 12:02:26 -0700910 computeGeometry(renderArea, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700911 engine.setupFillWithColor(red, green, blue, alpha);
912 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800913}
914
chaviwa76b2712017-09-20 12:02:26 -0700915void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800916 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800917}
918
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000919#ifdef USE_HWC2
David Sodman41fdfc92017-11-06 16:09:56 -0800920void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800921 if (mHwcLayers.count(hwcId) == 0) {
922 ALOGE("setCompositionType called without a valid HWC layer");
923 return;
924 }
925 auto& hwcInfo = mHwcLayers[hwcId];
926 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800927 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
928 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800929 if (hwcInfo.compositionType != type) {
930 ALOGV(" actually setting");
931 hwcInfo.compositionType = type;
932 if (callIntoHwc) {
933 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800934 ALOGE_IF(error != HWC2::Error::None,
935 "[%s] Failed to set "
936 "composition type %s: %s (%d)",
937 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
938 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800939 }
940 }
941}
942
943HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700944 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
945 // If we're querying the composition type for a display that does not
946 // have a HWC counterpart, then it will always be Client
947 return HWC2::Composition::Client;
948 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800949 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700950 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800951 return HWC2::Composition::Invalid;
952 }
953 return mHwcLayers.at(hwcId).compositionType;
954}
955
956void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
957 if (mHwcLayers.count(hwcId) == 0) {
958 ALOGE("setClearClientTarget called without a valid HWC layer");
959 return;
960 }
961 mHwcLayers[hwcId].clearClientTarget = clear;
962}
963
964bool Layer::getClearClientTarget(int32_t hwcId) const {
965 if (mHwcLayers.count(hwcId) == 0) {
966 ALOGE("getClearClientTarget called without a valid HWC layer");
967 return false;
968 }
969 return mHwcLayers.at(hwcId).clearClientTarget;
970}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000971#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800972
Dan Stozacac35382016-01-27 12:21:06 -0800973bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
974 if (point->getFrameNumber() <= mCurrentFrameNumber) {
975 // Don't bother with a SyncPoint, since we've already latched the
976 // relevant frame
977 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700978 }
979
Dan Stozacac35382016-01-27 12:21:06 -0800980 Mutex::Autolock lock(mLocalSyncPointMutex);
981 mLocalSyncPoints.push_back(point);
982 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700983}
984
Mathias Agopian13127d82013-03-05 17:47:11 -0800985void Layer::setFiltering(bool filtering) {
986 mFiltering = filtering;
987}
988
989bool Layer::getFiltering() const {
990 return mFiltering;
991}
992
Mathias Agopian13127d82013-03-05 17:47:11 -0800993// ----------------------------------------------------------------------------
994// local state
995// ----------------------------------------------------------------------------
996
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000997static void boundPoint(vec2* point, const Rect& crop) {
998 if (point->x < crop.left) {
999 point->x = crop.left;
1000 }
1001 if (point->x > crop.right) {
1002 point->x = crop.right;
1003 }
1004 if (point->y < crop.top) {
1005 point->y = crop.top;
1006 }
1007 if (point->y > crop.bottom) {
1008 point->y = crop.bottom;
1009 }
1010}
1011
chaviwa76b2712017-09-20 12:02:26 -07001012void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
1013 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001014 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -07001015 const Transform renderAreaTransform(renderArea.getTransform());
1016 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001017 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001018
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001019 vec2 lt = vec2(win.left, win.top);
1020 vec2 lb = vec2(win.left, win.bottom);
1021 vec2 rb = vec2(win.right, win.bottom);
1022 vec2 rt = vec2(win.right, win.top);
1023
Robert Carr1f0a16a2016-10-24 16:27:39 -07001024 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001025 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001026 lt = layerTransform.transform(lt);
1027 lb = layerTransform.transform(lb);
1028 rb = layerTransform.transform(rb);
1029 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001030 }
1031
Robert Carrb5d3d262016-03-25 15:08:13 -07001032 if (!s.finalCrop.isEmpty()) {
1033 boundPoint(&lt, s.finalCrop);
1034 boundPoint(&lb, s.finalCrop);
1035 boundPoint(&rb, s.finalCrop);
1036 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001037 }
1038
Mathias Agopianff2ed702013-09-01 21:36:12 -07001039 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -07001040 position[0] = renderAreaTransform.transform(lt);
1041 position[1] = renderAreaTransform.transform(lb);
1042 position[2] = renderAreaTransform.transform(rb);
1043 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -08001044 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -07001045 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 }
1047}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001048
David Sodman41fdfc92017-11-06 16:09:56 -08001049bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -07001050 const Layer::State& s(mDrawingState);
1051 return (s.flags & layer_state_t::eLayerSecure);
1052}
1053
Mathias Agopian13127d82013-03-05 17:47:11 -08001054void Layer::setVisibleRegion(const Region& visibleRegion) {
1055 // always called from main thread
1056 this->visibleRegion = visibleRegion;
1057}
1058
1059void Layer::setCoveredRegion(const Region& coveredRegion) {
1060 // always called from main thread
1061 this->coveredRegion = coveredRegion;
1062}
1063
David Sodman41fdfc92017-11-06 16:09:56 -08001064void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 // always called from main thread
1066 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1067}
1068
1069// ----------------------------------------------------------------------------
1070// transaction
1071// ----------------------------------------------------------------------------
1072
Dan Stoza7dde5992015-05-22 09:51:44 -07001073void Layer::pushPendingState() {
1074 if (!mCurrentState.modified) {
1075 return;
1076 }
1077
Dan Stoza7dde5992015-05-22 09:51:44 -07001078 // If this transaction is waiting on the receipt of a frame, generate a sync
1079 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001080 if (mCurrentState.barrierLayer != nullptr) {
1081 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1082 if (barrierLayer == nullptr) {
1083 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001084 // If we can't promote the layer we are intended to wait on,
1085 // then it is expired or otherwise invalid. Allow this transaction
1086 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001087 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001088 } else {
David Sodman41fdfc92017-11-06 16:09:56 -08001089 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001090 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001091 mRemoteSyncPoints.push_back(std::move(syncPoint));
1092 } else {
1093 // We already missed the frame we're supposed to synchronize
1094 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001095 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001096 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001097 }
1098
Dan Stoza7dde5992015-05-22 09:51:44 -07001099 // Wake us up to check if the frame has been received
1100 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001101 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001102 }
1103 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001104 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001105}
1106
Pablo Ceballos05289c22016-04-14 15:49:55 -07001107void Layer::popPendingState(State* stateToCommit) {
1108 auto oldFlags = stateToCommit->flags;
1109 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -08001110 stateToCommit->flags =
1111 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001112
1113 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001114 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001115}
1116
Pablo Ceballos05289c22016-04-14 15:49:55 -07001117bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001118 bool stateUpdateAvailable = false;
1119 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001120 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001121 if (mRemoteSyncPoints.empty()) {
1122 // If we don't have a sync point for this, apply it anyway. It
1123 // will be visually wrong, but it should keep us from getting
1124 // into too much trouble.
1125 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001126 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001127 stateUpdateAvailable = true;
1128 continue;
1129 }
1130
David Sodman41fdfc92017-11-06 16:09:56 -08001131 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
1132 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -08001133
1134 // Signal our end of the sync point and then dispose of it
1135 mRemoteSyncPoints.front()->setTransactionApplied();
1136 mRemoteSyncPoints.pop_front();
1137 continue;
1138 }
1139
Dan Stoza7dde5992015-05-22 09:51:44 -07001140 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1141 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001142 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001143 stateUpdateAvailable = true;
1144
1145 // Signal our end of the sync point and then dispose of it
1146 mRemoteSyncPoints.front()->setTransactionApplied();
1147 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001148 } else {
1149 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001150 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001151 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001152 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001153 stateUpdateAvailable = true;
1154 }
1155 }
1156
1157 // If we still have pending updates, wake SurfaceFlinger back up and point
1158 // it at this layer so we can process them
1159 if (!mPendingStates.empty()) {
1160 setTransactionFlags(eTransactionNeeded);
1161 mFlinger->setTransactionFlags(eTraversalNeeded);
1162 }
1163
1164 mCurrentState.modified = false;
1165 return stateUpdateAvailable;
1166}
1167
Mathias Agopian13127d82013-03-05 17:47:11 -08001168uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001169 ATRACE_CALL();
1170
Dan Stoza7dde5992015-05-22 09:51:44 -07001171 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001172 Layer::State c = getCurrentState();
1173 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001174 return 0;
1175 }
1176
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001177 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178
David Sodman41fdfc92017-11-06 16:09:56 -08001179 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001180
David Sodman0c69cad2017-08-21 12:12:51 -07001181 if (mSurfaceFlingerConsumer && sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001182 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001183 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001184 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1185 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1186 " requested={ wh={%4u,%4u} }}\n"
1187 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1188 " requested={ wh={%4u,%4u} }}\n",
1189 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(), c.active.w,
1190 c.active.h, c.crop.left, c.crop.top, c.crop.right, c.crop.bottom,
1191 c.crop.getWidth(), c.crop.getHeight(), c.requested.w, c.requested.h, s.active.w,
1192 s.active.h, s.crop.left, s.crop.top, s.crop.right, s.crop.bottom,
1193 s.crop.getWidth(), s.crop.getHeight(), s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001195 // record the new size, form this point on, when the client request
1196 // a buffer, it'll get the new size.
David Sodman41fdfc92017-11-06 16:09:56 -08001197 mSurfaceFlingerConsumer->setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001199
Robert Carre392b552017-09-19 12:16:05 -07001200 // Don't let Layer::doTransaction update the drawing state
1201 // if we have a pending resize, unless we are in fixed-size mode.
1202 // the drawing state will be updated only once we receive a buffer
1203 // with the correct size.
1204 //
1205 // In particular, we want to make sure the clip (which is part
1206 // of the geometry state) is latched together with the size but is
1207 // latched immediately when no resizing is involved.
1208 //
1209 // If a sideband stream is attached, however, we want to skip this
1210 // optimization so that transactions aren't missed when a buffer
1211 // never arrives
1212 //
1213 // In the case that we don't have a buffer we ignore other factors
1214 // and avoid entering the resizePending state. At a high level the
1215 // resizePending state is to avoid applying the state of the new buffer
1216 // to the old buffer. However in the state where we don't have an old buffer
1217 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001218 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
1219 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001220 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001221 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001222 flags |= eDontUpdateGeometryState;
1223 }
1224 }
1225
Robert Carr7bf247e2017-05-18 14:02:49 -07001226 // Here we apply various requested geometry states, depending on our
1227 // latching configuration. See Layer.h for a detailed discussion of
1228 // how geometry latching is controlled.
1229 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001230 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001231
1232 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1233 // mode, which causes attributes which normally latch regardless of scaling mode,
1234 // to be delayed. We copy the requested state to the active state making sure
1235 // to respect these rules (again see Layer.h for a detailed discussion).
1236 //
1237 // There is an awkward asymmetry in the handling of the crop states in the position
1238 // states, as can be seen below. Largely this arises from position and transform
1239 // being stored in the same data structure while having different latching rules.
1240 // b/38182305
1241 //
1242 // Careful that "c" and editCurrentState may not begin as equivalent due to
1243 // applyPendingStates in the presence of deferred transactions.
1244 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001245 float tx = c.active.transform.tx();
1246 float ty = c.active.transform.ty();
1247 c.active = c.requested;
1248 c.active.transform.set(tx, ty);
1249 editCurrentState.active = c.active;
1250 } else {
1251 editCurrentState.active = editCurrentState.requested;
1252 c.active = c.requested;
1253 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 }
1255
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001256 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001257 // invalidate and recompute the visible regions if needed
1258 flags |= Layer::eVisibleRegion;
1259 }
1260
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001261 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 // invalidate and recompute the visible regions if needed
1263 flags |= eVisibleRegion;
1264 this->contentDirty = true;
1265
1266 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001267 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001268 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 }
1270
Dan Stozac8145172016-04-28 16:29:06 -07001271 // If the layer is hidden, signal and clear out all local sync points so
1272 // that transactions for layers depending on this layer's frames becoming
1273 // visible are not blocked
1274 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001275 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001276 }
1277
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001279 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001280 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281}
1282
Pablo Ceballos05289c22016-04-14 15:49:55 -07001283void Layer::commitTransaction(const State& stateToCommit) {
1284 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001285}
1286
Mathias Agopian13127d82013-03-05 17:47:11 -08001287uint32_t Layer::getTransactionFlags(uint32_t flags) {
1288 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1289}
1290
1291uint32_t Layer::setTransactionFlags(uint32_t flags) {
1292 return android_atomic_or(flags, &mTransactionFlags);
1293}
1294
Robert Carr82364e32016-05-15 11:27:47 -07001295bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001296 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001297 return false;
1298 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001299
1300 // We update the requested and active position simultaneously because
1301 // we want to apply the position portion of the transform matrix immediately,
1302 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001303 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001304 if (immediate && !mFreezeGeometryUpdates) {
1305 // Here we directly update the active state
1306 // unlike other setters, because we store it within
1307 // the transform, but use different latching rules.
1308 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001309 mCurrentState.active.transform.set(x, y);
1310 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001311 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001312
Dan Stoza7dde5992015-05-22 09:51:44 -07001313 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001314 setTransactionFlags(eTransactionNeeded);
1315 return true;
1316}
Robert Carr82364e32016-05-15 11:27:47 -07001317
Robert Carr1f0a16a2016-10-24 16:27:39 -07001318bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1319 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1320 if (idx < 0) {
1321 return false;
1322 }
1323 if (childLayer->setLayer(z)) {
1324 mCurrentChildren.removeAt(idx);
1325 mCurrentChildren.add(childLayer);
1326 }
1327 return true;
1328}
1329
Robert Carrae060832016-11-28 10:51:00 -08001330bool Layer::setLayer(int32_t z) {
David Sodman41fdfc92017-11-06 16:09:56 -08001331 if (mCurrentState.z == z) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001332 mCurrentState.sequence++;
1333 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001334 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001335
1336 // Discard all relative layering.
1337 if (mCurrentState.zOrderRelativeOf != nullptr) {
1338 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1339 if (strongRelative != nullptr) {
1340 strongRelative->removeZOrderRelative(this);
1341 }
1342 mCurrentState.zOrderRelativeOf = nullptr;
1343 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 setTransactionFlags(eTransactionNeeded);
1345 return true;
1346}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001347
Robert Carrdb66e622017-04-10 16:55:57 -07001348void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1349 mCurrentState.zOrderRelatives.remove(relative);
1350 mCurrentState.sequence++;
1351 mCurrentState.modified = true;
1352 setTransactionFlags(eTransactionNeeded);
1353}
1354
1355void Layer::addZOrderRelative(const wp<Layer>& relative) {
1356 mCurrentState.zOrderRelatives.add(relative);
1357 mCurrentState.modified = true;
1358 mCurrentState.sequence++;
1359 setTransactionFlags(eTransactionNeeded);
1360}
1361
1362bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1363 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1364 if (handle == nullptr) {
1365 return false;
1366 }
1367 sp<Layer> relative = handle->owner.promote();
1368 if (relative == nullptr) {
1369 return false;
1370 }
1371
1372 mCurrentState.sequence++;
1373 mCurrentState.modified = true;
1374 mCurrentState.z = z;
1375
chaviw9ab4bd12017-11-03 13:11:00 -07001376 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1377 if (oldZOrderRelativeOf != nullptr) {
1378 oldZOrderRelativeOf->removeZOrderRelative(this);
1379 }
Robert Carrdb66e622017-04-10 16:55:57 -07001380 mCurrentState.zOrderRelativeOf = relative;
1381 relative->addZOrderRelative(this);
1382
1383 setTransactionFlags(eTransactionNeeded);
1384
1385 return true;
1386}
1387
Mathias Agopian13127d82013-03-05 17:47:11 -08001388bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001389 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001390 mCurrentState.requested.w = w;
1391 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001392 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001393 setTransactionFlags(eTransactionNeeded);
1394 return true;
1395}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001396bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001397 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001398 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001399 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001400 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001401 setTransactionFlags(eTransactionNeeded);
1402 return true;
1403}
chaviw13fdc492017-06-27 12:40:18 -07001404
1405bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001406 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1407 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001408 return false;
1409
1410 mCurrentState.sequence++;
1411 mCurrentState.color.r = color.r;
1412 mCurrentState.color.g = color.g;
1413 mCurrentState.color.b = color.b;
1414 mCurrentState.modified = true;
1415 setTransactionFlags(eTransactionNeeded);
1416 return true;
1417}
1418
Mathias Agopian13127d82013-03-05 17:47:11 -08001419bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1420 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001421 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001422 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001423 setTransactionFlags(eTransactionNeeded);
1424 return true;
1425}
1426bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001427 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001428 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001429 setTransactionFlags(eTransactionNeeded);
1430 return true;
1431}
1432bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1433 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001434 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001435 mCurrentState.sequence++;
1436 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001437 mCurrentState.mask = mask;
1438 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001439 setTransactionFlags(eTransactionNeeded);
1440 return true;
1441}
Robert Carr99e27f02016-06-16 15:18:02 -07001442
1443bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001444 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001445 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001446 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001447 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001448 mCurrentState.crop = crop;
1449 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001450 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1451
Dan Stoza7dde5992015-05-22 09:51:44 -07001452 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001453 setTransactionFlags(eTransactionNeeded);
1454 return true;
1455}
Robert Carr8d5227b2017-03-16 15:41:03 -07001456
1457bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001458 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001459 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001460 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001461 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001462 mCurrentState.finalCrop = crop;
1463 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001464 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1465
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001466 mCurrentState.modified = true;
1467 setTransactionFlags(eTransactionNeeded);
1468 return true;
1469}
Mathias Agopian13127d82013-03-05 17:47:11 -08001470
Robert Carrc3574f72016-03-24 12:19:32 -07001471bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001472 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001473 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001474 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001475 return true;
1476}
1477
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001478void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001479 mCurrentState.appId = appId;
1480 mCurrentState.type = type;
1481 mCurrentState.modified = true;
1482 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001483}
1484
Mathias Agopian13127d82013-03-05 17:47:11 -08001485bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001486 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001487 mCurrentState.sequence++;
1488 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001489 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001490 setTransactionFlags(eTransactionNeeded);
1491 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001492}
1493
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001494bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001495 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001496 mCurrentState.sequence++;
1497 mCurrentState.dataSpace = dataSpace;
1498 mCurrentState.modified = true;
1499 setTransactionFlags(eTransactionNeeded);
1500 return true;
1501}
1502
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001503android_dataspace Layer::getDataSpace() const {
1504 return mCurrentState.dataSpace;
1505}
1506
Robert Carr1f0a16a2016-10-24 16:27:39 -07001507uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001508 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001509 if (p == nullptr) {
1510 return getDrawingState().layerStack;
1511 }
1512 return p->getLayerStack();
1513}
1514
David Sodman41fdfc92017-11-06 16:09:56 -08001515void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001516 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001517 mCurrentState.frameNumber = frameNumber;
1518 // We don't set eTransactionNeeded, because just receiving a deferral
1519 // request without any other state updates shouldn't actually induce a delay
1520 mCurrentState.modified = true;
1521 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001522 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001523 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001524 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001525}
1526
David Sodman41fdfc92017-11-06 16:09:56 -08001527void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001528 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1529 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001530}
1531
Dan Stozaee44edd2015-03-23 15:50:23 -07001532void Layer::useSurfaceDamage() {
1533 if (mFlinger->mForceFullDamage) {
1534 surfaceDamageRegion = Region::INVALID_REGION;
1535 } else {
1536 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1537 }
1538}
1539
1540void Layer::useEmptyDamage() {
1541 surfaceDamageRegion.clear();
1542}
1543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544// ----------------------------------------------------------------------------
1545// pageflip handling...
1546// ----------------------------------------------------------------------------
1547
Dan Stoza6b9454d2014-11-07 16:00:59 -08001548bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001549 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001550 return true;
1551 }
1552
Dan Stoza6b9454d2014-11-07 16:00:59 -08001553 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001554 if (mQueueItems.empty()) {
1555 return false;
1556 }
1557 auto timestamp = mQueueItems[0].mTimestamp;
David Sodman41fdfc92017-11-06 16:09:56 -08001558 nsecs_t expectedPresent = mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001559
1560 // Ignore timestamps more than a second in the future
1561 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
David Sodman41fdfc92017-11-06 16:09:56 -08001562 ALOGW_IF(!isPlausible,
1563 "[%s] Timestamp %" PRId64 " seems implausible "
1564 "relative to expectedPresent %" PRId64,
1565 mName.string(), timestamp, expectedPresent);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001566
1567 bool isDue = timestamp < expectedPresent;
1568 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001569}
1570
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001571bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
David Sodman41fdfc92017-11-06 16:09:56 -08001572 const std::shared_ptr<FenceTime>& presentFence,
1573 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001574 // mFrameLatencyNeeded is true when a new frame was latched for the
1575 // composition.
David Sodman41fdfc92017-11-06 16:09:56 -08001576 if (!mFrameLatencyNeeded) return false;
Fabien Sanglard28e98082016-12-05 10:19:46 -08001577
Fabien Sanglard28e98082016-12-05 10:19:46 -08001578 // Update mFrameEventHistory.
1579 {
1580 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman41fdfc92017-11-06 16:09:56 -08001581 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
1582 compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001583 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08001584
1585 // Update mFrameTracker.
1586 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1587 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1588
David Sodman41fdfc92017-11-06 16:09:56 -08001589 std::shared_ptr<FenceTime> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08001590 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001591 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001592 } else {
1593 // There was no fence for this frame, so assume that it was ready
1594 // to be presented at the desired present time.
1595 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1596 }
1597
Brian Anderson3d4039d2016-09-23 16:31:30 -07001598 if (presentFence->isValid()) {
David Sodman41fdfc92017-11-06 16:09:56 -08001599 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001600 } else {
1601 // The HWC doesn't support present fences, so use the refresh
1602 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001603 mFrameTracker.setActualPresentTime(
David Sodman41fdfc92017-11-06 16:09:56 -08001604 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001605 }
1606
1607 mFrameTracker.advanceFrame();
1608 mFrameLatencyNeeded = false;
1609 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001610}
1611
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612bool Layer::isHiddenByPolicy() const {
1613 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001614 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615 if (parent != nullptr && parent->isHiddenByPolicy()) {
1616 return true;
1617 }
1618 return s.flags & layer_state_t::eLayerHidden;
1619}
1620
David Sodman41fdfc92017-11-06 16:09:56 -08001621uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001622 // TODO: should we do something special if mSecure is set?
1623 if (mProtectedByApp) {
1624 // need a hardware-protected path to external video sink
1625 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001626 }
Riley Andrews03414a12014-07-01 14:22:59 -07001627 if (mPotentialCursor) {
1628 usage |= GraphicBuffer::USAGE_CURSOR;
1629 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001630 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001631 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001632}
1633
Mathias Agopian84300952012-11-21 16:02:13 -08001634void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001635 uint32_t orientation = 0;
1636 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001637 // The transform hint is used to improve performance, but we can
1638 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001639 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001640 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001641 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001642 if (orientation & Transform::ROT_INVALID) {
1643 orientation = 0;
1644 }
1645 }
David Sodman0c69cad2017-08-21 12:12:51 -07001646 if (mSurfaceFlingerConsumer) {
1647 mSurfaceFlingerConsumer->setTransformHint(orientation);
1648 }
Mathias Agopiana4583642011-08-23 18:03:18 -07001649}
1650
Mathias Agopian13127d82013-03-05 17:47:11 -08001651// ----------------------------------------------------------------------------
1652// debugging
1653// ----------------------------------------------------------------------------
1654
Kalle Raitaa099a242017-01-11 11:17:29 -08001655LayerDebugInfo Layer::getLayerDebugInfo() const {
1656 LayerDebugInfo info;
1657 const Layer::State& ds = getDrawingState();
1658 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001659 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001660 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1661 info.mType = String8(getTypeId());
1662 info.mTransparentRegion = ds.activeTransparentRegion;
1663 info.mVisibleRegion = visibleRegion;
1664 info.mSurfaceDamageRegion = surfaceDamageRegion;
1665 info.mLayerStack = getLayerStack();
1666 info.mX = ds.active.transform.tx();
1667 info.mY = ds.active.transform.ty();
1668 info.mZ = ds.z;
1669 info.mWidth = ds.active.w;
1670 info.mHeight = ds.active.h;
1671 info.mCrop = ds.crop;
1672 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001673 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001674 info.mFlags = ds.flags;
1675 info.mPixelFormat = getPixelFormat();
1676 info.mDataSpace = getDataSpace();
1677 info.mMatrix[0][0] = ds.active.transform[0][0];
1678 info.mMatrix[0][1] = ds.active.transform[0][1];
1679 info.mMatrix[1][0] = ds.active.transform[1][0];
1680 info.mMatrix[1][1] = ds.active.transform[1][1];
1681 {
1682 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1683 if (activeBuffer != 0) {
1684 info.mActiveBufferWidth = activeBuffer->getWidth();
1685 info.mActiveBufferHeight = activeBuffer->getHeight();
1686 info.mActiveBufferStride = activeBuffer->getStride();
1687 info.mActiveBufferFormat = activeBuffer->format;
1688 } else {
1689 info.mActiveBufferWidth = 0;
1690 info.mActiveBufferHeight = 0;
1691 info.mActiveBufferStride = 0;
1692 info.mActiveBufferFormat = 0;
1693 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001694 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001695 info.mNumQueuedFrames = getQueuedFrameCount();
1696 info.mRefreshPending = isBufferLatched();
1697 info.mIsOpaque = isOpaque(ds);
1698 info.mContentDirty = contentDirty;
1699 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001700}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001701#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07001702void Layer::miniDumpHeader(String8& result) {
1703 result.append("----------------------------------------");
1704 result.append("---------------------------------------\n");
1705 result.append(" Layer name\n");
1706 result.append(" Z | ");
1707 result.append(" Comp Type | ");
1708 result.append(" Disp Frame (LTRB) | ");
1709 result.append(" Source Crop (LTRB)\n");
1710 result.append("----------------------------------------");
1711 result.append("---------------------------------------\n");
1712}
1713
1714void Layer::miniDump(String8& result, int32_t hwcId) const {
1715 if (mHwcLayers.count(hwcId) == 0) {
1716 return;
1717 }
1718
1719 String8 name;
1720 if (mName.length() > 77) {
1721 std::string shortened;
1722 shortened.append(mName.string(), 36);
1723 shortened.append("[...]");
1724 shortened.append(mName.string() + (mName.length() - 36), 36);
1725 name = shortened.c_str();
1726 } else {
1727 name = mName;
1728 }
1729
1730 result.appendFormat(" %s\n", name.string());
1731
1732 const Layer::State& layerState(getDrawingState());
1733 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001734 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001735 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001736 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001737 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001738 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001739 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 -07001740
1741 result.append("- - - - - - - - - - - - - - - - - - - - ");
1742 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1743}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001744#endif
Dan Stozae22aec72016-08-01 13:20:59 -07001745
Svetoslavd85084b2014-03-20 10:28:31 -07001746void Layer::dumpFrameStats(String8& result) const {
1747 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001748}
1749
Svetoslavd85084b2014-03-20 10:28:31 -07001750void Layer::clearFrameStats() {
1751 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001752}
1753
Jamie Gennis6547ff42013-07-16 20:12:42 -07001754void Layer::logFrameStats() {
1755 mFrameTracker.logAndResetStats(mName);
1756}
1757
Svetoslavd85084b2014-03-20 10:28:31 -07001758void Layer::getFrameStats(FrameStats* outStats) const {
1759 mFrameTracker.getStats(outStats);
1760}
1761
Brian Andersond6927fb2016-07-23 23:37:30 -07001762void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001763 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001764 Mutex::Autolock lock(mFrameEventHistoryMutex);
1765 mFrameEventHistory.checkFencesForCompletion();
1766 mFrameEventHistory.dump(result);
1767}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001768
Brian Anderson5ea5e592016-12-01 16:54:33 -08001769void Layer::onDisconnect() {
1770 Mutex::Autolock lock(mFrameEventHistoryMutex);
1771 mFrameEventHistory.onDisconnect();
1772}
1773
Brian Anderson3890c392016-07-25 12:48:08 -07001774void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001775 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001776 Mutex::Autolock lock(mFrameEventHistoryMutex);
1777 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001778 // If there are any unsignaled fences in the aquire timeline at this
1779 // point, the previously queued frame hasn't been latched yet. Go ahead
1780 // and try to get the signal time here so the syscall is taken out of
1781 // the main thread's critical path.
1782 mAcquireTimeline.updateSignalTimes();
1783 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001784 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001785 mFrameEventHistory.addQueue(*newTimestamps);
1786 }
1787
Brian Anderson3890c392016-07-25 12:48:08 -07001788 if (outDelta) {
1789 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001790 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001791}
Dan Stozae77c7662016-05-13 11:37:28 -07001792
David Sodman41fdfc92017-11-06 16:09:56 -08001793std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(bool forceFlush) {
Dan Stozae77c7662016-05-13 11:37:28 -07001794 std::vector<OccupancyTracker::Segment> history;
David Sodman0c69cad2017-08-21 12:12:51 -07001795
David Sodman41fdfc92017-11-06 16:09:56 -08001796 if (!mSurfaceFlingerConsumer) return {};
David Sodman0c69cad2017-08-21 12:12:51 -07001797
David Sodman41fdfc92017-11-06 16:09:56 -08001798 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush, &history);
Dan Stozae77c7662016-05-13 11:37:28 -07001799 if (result != NO_ERROR) {
David Sodman41fdfc92017-11-06 16:09:56 -08001800 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result);
Dan Stozae77c7662016-05-13 11:37:28 -07001801 return {};
1802 }
1803 return history;
1804}
1805
Robert Carr367c5682016-06-20 11:55:28 -07001806bool Layer::getTransformToDisplayInverse() const {
David Sodman0c69cad2017-08-21 12:12:51 -07001807 if (mSurfaceFlingerConsumer) {
1808 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
1809 }
1810 return false;
Robert Carr367c5682016-06-20 11:55:28 -07001811}
1812
Chia-I Wu98f1c102017-05-30 14:54:08 -07001813size_t Layer::getChildrenCount() const {
1814 size_t count = 0;
1815 for (const sp<Layer>& child : mCurrentChildren) {
1816 count += 1 + child->getChildrenCount();
1817 }
1818 return count;
1819}
1820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821void Layer::addChild(const sp<Layer>& layer) {
1822 mCurrentChildren.add(layer);
1823 layer->setParent(this);
1824}
1825
1826ssize_t Layer::removeChild(const sp<Layer>& layer) {
1827 layer->setParent(nullptr);
1828 return mCurrentChildren.remove(layer);
1829}
1830
Robert Carr1db73f62016-12-21 12:58:51 -08001831bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1832 sp<Handle> handle = nullptr;
1833 sp<Layer> newParent = nullptr;
1834 if (newParentHandle == nullptr) {
1835 return false;
1836 }
1837 handle = static_cast<Handle*>(newParentHandle.get());
1838 newParent = handle->owner.promote();
1839 if (newParent == nullptr) {
1840 ALOGE("Unable to promote Layer handle");
1841 return false;
1842 }
1843
1844 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001845 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001846
1847 sp<Client> client(child->mClientRef.promote());
1848 if (client != nullptr) {
1849 client->setParentLayer(newParent);
1850 }
1851 }
1852 mCurrentChildren.clear();
1853
1854 return true;
1855}
1856
chaviwf1961f72017-09-18 16:41:07 -07001857bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1858 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001859 return false;
1860 }
1861
1862 auto handle = static_cast<Handle*>(newParentHandle.get());
1863 sp<Layer> newParent = handle->owner.promote();
1864 if (newParent == nullptr) {
1865 ALOGE("Unable to promote Layer handle");
1866 return false;
1867 }
1868
chaviwf1961f72017-09-18 16:41:07 -07001869 sp<Layer> parent = getParent();
1870 if (parent != nullptr) {
1871 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001872 }
chaviwf1961f72017-09-18 16:41:07 -07001873 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001874
chaviwf1961f72017-09-18 16:41:07 -07001875 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001876 sp<Client> newParentClient(newParent->mClientRef.promote());
1877
chaviwf1961f72017-09-18 16:41:07 -07001878 if (client != newParentClient) {
1879 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001880 }
1881
chaviw06178942017-07-27 10:25:59 -07001882 return true;
1883}
1884
Robert Carr9524cb32017-02-13 11:32:32 -08001885bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07001886 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08001887 if (child == this) {
1888 return;
1889 }
1890
chaviw161410b02017-07-27 10:46:08 -07001891 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001892 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001893 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08001894 client->detachLayer(child);
1895 }
1896 });
1897
1898 return true;
1899}
1900
Robert Carr1f0a16a2016-10-24 16:27:39 -07001901void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001902 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001903}
1904
1905void Layer::clearSyncPoints() {
1906 for (const auto& child : mCurrentChildren) {
1907 child->clearSyncPoints();
1908 }
1909
1910 Mutex::Autolock lock(mLocalSyncPointMutex);
1911 for (auto& point : mLocalSyncPoints) {
1912 point->setFrameAvailable();
1913 }
1914 mLocalSyncPoints.clear();
1915}
1916
1917int32_t Layer::getZ() const {
1918 return mDrawingState.z;
1919}
1920
David Sodman41fdfc92017-11-06 16:09:56 -08001921__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
1922 LayerVector::StateSet stateSet) {
Dan Stoza412903f2017-04-27 13:42:17 -07001923 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1924 "makeTraversalList received invalid stateSet");
1925 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1926 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1927 const State& state = useDrawing ? mDrawingState : mCurrentState;
1928
1929 if (state.zOrderRelatives.size() == 0) {
1930 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07001931 }
1932 LayerVector traverse;
1933
Dan Stoza412903f2017-04-27 13:42:17 -07001934 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001935 sp<Layer> strongRelative = weakRelative.promote();
1936 if (strongRelative != nullptr) {
1937 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001938 }
1939 }
1940
Dan Stoza412903f2017-04-27 13:42:17 -07001941 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07001942 traverse.add(child);
1943 }
1944
1945 return traverse;
1946}
1947
Robert Carr1f0a16a2016-10-24 16:27:39 -07001948/**
Robert Carrdb66e622017-04-10 16:55:57 -07001949 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001950 */
Dan Stoza412903f2017-04-27 13:42:17 -07001951void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1952 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001953
Robert Carr1f0a16a2016-10-24 16:27:39 -07001954 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001955 for (; i < list.size(); i++) {
1956 const auto& relative = list[i];
1957 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001958 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001959 }
Dan Stoza412903f2017-04-27 13:42:17 -07001960 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001961 }
Dan Stoza412903f2017-04-27 13:42:17 -07001962 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001963 for (; i < list.size(); i++) {
1964 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001965 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001966 }
1967}
1968
1969/**
Robert Carrdb66e622017-04-10 16:55:57 -07001970 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001971 */
Dan Stoza412903f2017-04-27 13:42:17 -07001972void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1973 const LayerVector::Visitor& visitor) {
1974 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001975
Robert Carr1f0a16a2016-10-24 16:27:39 -07001976 int32_t i = 0;
David Sodman41fdfc92017-11-06 16:09:56 -08001977 for (i = list.size() - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001978 const auto& relative = list[i];
1979 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001980 break;
1981 }
Dan Stoza412903f2017-04-27 13:42:17 -07001982 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001983 }
Dan Stoza412903f2017-04-27 13:42:17 -07001984 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001985 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001986 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001987 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001988 }
1989}
1990
chaviwa76b2712017-09-20 12:02:26 -07001991/**
1992 * Traverse only children in z order, ignoring relative layers.
1993 */
1994void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1995 const LayerVector::Visitor& visitor) {
1996 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1997 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1998
1999 size_t i = 0;
2000 for (; i < children.size(); i++) {
2001 const auto& relative = children[i];
2002 if (relative->getZ() >= 0) {
2003 break;
2004 }
2005 relative->traverseChildrenInZOrder(stateSet, visitor);
2006 }
2007 visitor(this);
2008 for (; i < children.size(); i++) {
2009 const auto& relative = children[i];
2010 relative->traverseChildrenInZOrder(stateSet, visitor);
2011 }
2012}
2013
Robert Carr1f0a16a2016-10-24 16:27:39 -07002014Transform Layer::getTransform() const {
2015 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002016 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002017 if (p != nullptr) {
2018 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002019
2020 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2021 // it isFixedSize) then there may be additional scaling not accounted
2022 // for in the transform. We need to mirror this scaling in child surfaces
2023 // or we will break the contract where WM can treat child surfaces as
2024 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002025 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002026 int bufferWidth;
2027 int bufferHeight;
2028 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2029 bufferWidth = p->mActiveBuffer->getWidth();
2030 bufferHeight = p->mActiveBuffer->getHeight();
2031 } else {
2032 bufferHeight = p->mActiveBuffer->getWidth();
2033 bufferWidth = p->mActiveBuffer->getHeight();
2034 }
David Sodman41fdfc92017-11-06 16:09:56 -08002035 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
2036 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002037 Transform extraParentScaling;
2038 extraParentScaling.set(sx, 0, 0, sy);
2039 t = t * extraParentScaling;
2040 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002041 }
2042 return t * getDrawingState().active.transform;
2043}
2044
chaviw13fdc492017-06-27 12:40:18 -07002045half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002046 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002047
chaviw13fdc492017-06-27 12:40:18 -07002048 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2049 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002050}
Robert Carr6452f122017-03-21 10:41:29 -07002051
chaviw13fdc492017-06-27 12:40:18 -07002052half4 Layer::getColor() const {
2053 const half4 color(getDrawingState().color);
2054 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002055}
Robert Carr6452f122017-03-21 10:41:29 -07002056
Robert Carr1f0a16a2016-10-24 16:27:39 -07002057void Layer::commitChildList() {
2058 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2059 const auto& child = mCurrentChildren[i];
2060 child->commitChildList();
2061 }
2062 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002063 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002064}
2065
chaviw1d044282017-09-27 12:19:28 -07002066void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2067 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2068 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2069 const State& state = useDrawing ? mDrawingState : mCurrentState;
2070
2071 Transform requestedTransform = state.active.transform;
2072 Transform transform = getTransform();
2073
2074 layerInfo->set_id(sequence);
2075 layerInfo->set_name(getName().c_str());
2076 layerInfo->set_type(String8(getTypeId()));
2077
2078 for (const auto& child : children) {
2079 layerInfo->add_children(child->sequence);
2080 }
2081
2082 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2083 sp<Layer> strongRelative = weakRelative.promote();
2084 if (strongRelative != nullptr) {
2085 layerInfo->add_relatives(strongRelative->sequence);
2086 }
2087 }
2088
2089 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
2090 layerInfo->mutable_transparent_region());
2091 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2092 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2093
2094 layerInfo->set_layer_stack(getLayerStack());
2095 layerInfo->set_z(state.z);
2096
2097 PositionProto* position = layerInfo->mutable_position();
2098 position->set_x(transform.tx());
2099 position->set_y(transform.ty());
2100
2101 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2102 requestedPosition->set_x(requestedTransform.tx());
2103 requestedPosition->set_y(requestedTransform.ty());
2104
2105 SizeProto* size = layerInfo->mutable_size();
2106 size->set_w(state.active.w);
2107 size->set_h(state.active.h);
2108
2109 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
2110 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
2111
2112 layerInfo->set_is_opaque(isOpaque(state));
2113 layerInfo->set_invalidate(contentDirty);
2114 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
2115 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2116 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2117 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2118 layerInfo->set_flags(state.flags);
2119
2120 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2121 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2122
2123 auto parent = getParent();
2124 if (parent != nullptr) {
2125 layerInfo->set_parent(parent->sequence);
2126 }
2127
2128 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2129 if (zOrderRelativeOf != nullptr) {
2130 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2131 }
2132
2133 auto activeBuffer = getActiveBuffer();
2134 if (activeBuffer != nullptr) {
2135 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
2136 }
2137
2138 layerInfo->set_queued_frames(getQueuedFrameCount());
2139 layerInfo->set_refresh_pending(isBufferLatched());
2140}
2141
Mathias Agopian13127d82013-03-05 17:47:11 -08002142// ---------------------------------------------------------------------------
2143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002145
2146#if defined(__gl_h_)
2147#error "don't include gl/gl.h in this file"
2148#endif
2149
2150#if defined(__gl2_h_)
2151#error "don't include gl2/gl2.h in this file"
2152#endif