blob: 763488c8f5e969607bf388e3910189bff5886982 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.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
46#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#define DEBUG_RESIZE 0
62
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
Mathias Agopian4d9b8222013-03-12 17:11:48 -070067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
68 const String8& name, uint32_t w, 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),
Robert Carr7bf247e2017-05-18 14:02:49 -0700100 mFreezeGeometryUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
105 uint32_t layerFlags = 0;
106 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800107 layerFlags |= layer_state_t::eLayerHidden;
108 if (flags & ISurfaceComposerClient::eOpaque)
109 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700110 if (flags & ISurfaceComposerClient::eSecure)
111 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700114 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 mCurrentState.active.w = w;
117 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700118 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800119 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700120 mCurrentState.crop.makeInvalid();
121 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700122 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
123 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700125 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700129 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500130 mCurrentState.appId = 0;
131 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132
133 // drawing state & current state are identical
134 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700135
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000136#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800137 const auto& hwc = flinger->getHwComposer();
138 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
139 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000140#else
141 nsecs_t displayPeriod =
142 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
143#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700144 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800145
146 CompositorTiming compositorTiming;
147 flinger->getCompositorTiming(&compositorTiming);
148 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800149}
150
Mathias Agopian3f844832013-08-07 21:24:32 -0700151void Layer::onFirstRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700152}
153
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700154Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700155 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700156}
157
Mathias Agopian13127d82013-03-05 17:47:11 -0800158// ---------------------------------------------------------------------------
159// callbacks
160// ---------------------------------------------------------------------------
161
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000162#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800163void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
164 if (mHwcLayers.empty()) {
165 return;
166 }
David Sodman0c69cad2017-08-21 12:12:51 -0700167 if (mSurfaceFlingerConsumer) {
168 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
169 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000171#else
172void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
173 HWComposer::HWCLayerInterface* layer) {
174 if (layer) {
175 layer->onDisplayed();
176 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
177 }
178}
179#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800180
Chia-I Wuc6657022017-08-15 11:18:17 -0700181void Layer::onRemovedFromCurrentState() {
182 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
183
Robert Carr5edb1ad2017-04-25 10:54:24 -0700184 if (mCurrentState.zOrderRelativeOf != nullptr) {
185 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
186 if (strongRelative != nullptr) {
187 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700188 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700189 }
190 mCurrentState.zOrderRelativeOf = nullptr;
191 }
192
Chia-I Wuc6657022017-08-15 11:18:17 -0700193 for (const auto& child : mCurrentChildren) {
194 child->onRemovedFromCurrentState();
195 }
196}
Chia-I Wu38512252017-05-17 14:36:16 -0700197
Chia-I Wuc6657022017-08-15 11:18:17 -0700198void Layer::onRemoved() {
199 // the layer is removed from SF mLayersPendingRemoval
200
David Sodman0c69cad2017-08-21 12:12:51 -0700201 if (mSurfaceFlingerConsumer) {
202 mSurfaceFlingerConsumer->abandon();
203 }
Chia-I Wu38512252017-05-17 14:36:16 -0700204#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700205 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700206#endif
207
Robert Carr1f0a16a2016-10-24 16:27:39 -0700208 for (const auto& child : mCurrentChildren) {
209 child->onRemoved();
210 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700211}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700212
Mathias Agopian13127d82013-03-05 17:47:11 -0800213// ---------------------------------------------------------------------------
214// set-up
215// ---------------------------------------------------------------------------
216
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700217const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800218 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219}
220
chaviw13fdc492017-06-27 12:40:18 -0700221bool Layer::getPremultipledAlpha() const {
222 return mPremultipliedAlpha;
223}
224
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700225sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800226 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700227 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800228}
229
230// ---------------------------------------------------------------------------
231// h/w composer set-up
232// ---------------------------------------------------------------------------
233
Steven Thomasb02664d2017-07-26 18:48:28 -0700234#ifdef USE_HWC2
235bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
236 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
237 "Already have a layer for hwcId %d", hwcId);
238 HWC2::Layer* layer = hwc->createLayer(hwcId);
239 if (!layer) {
240 return false;
241 }
242 HWCInfo& hwcInfo = mHwcLayers[hwcId];
243 hwcInfo.hwc = hwc;
244 hwcInfo.layer = layer;
245 layer->setLayerDestroyedListener(
246 [this, hwcId] (HWC2::Layer* /*layer*/){mHwcLayers.erase(hwcId);});
247 return true;
248}
249
250void Layer::destroyHwcLayer(int32_t hwcId) {
251 if (mHwcLayers.count(hwcId) == 0) {
252 return;
253 }
254 auto& hwcInfo = mHwcLayers[hwcId];
255 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr,
256 "Attempt to destroy null layer");
257 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
258 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
259 // The layer destroyed listener should have cleared the entry from
260 // mHwcLayers. Verify that.
261 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
262 "Stale layer entry in mHwcLayers");
263}
264
265void Layer::destroyAllHwcLayers() {
266 size_t numLayers = mHwcLayers.size();
267 for (size_t i = 0; i < numLayers; ++i) {
268 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
269 destroyHwcLayer(mHwcLayers.begin()->first);
270 }
271 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
272 "All hardware composer layers should have been destroyed");
273}
274#endif
275
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800276Rect Layer::getContentCrop() const {
277 // this is the crop rectangle that applies to the buffer
278 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700279 Rect crop;
280 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800281 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700282 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800283 } else if (mActiveBuffer != NULL) {
284 // otherwise we use the whole buffer
285 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700286 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800287 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700288 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700289 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700290 return crop;
291}
292
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700293static Rect reduce(const Rect& win, const Region& exclude) {
294 if (CC_LIKELY(exclude.isEmpty())) {
295 return win;
296 }
297 if (exclude.isRect()) {
298 return win.reduce(exclude.getBounds());
299 }
300 return Region(win).subtract(exclude).getBounds();
301}
302
Robert Carr1f0a16a2016-10-24 16:27:39 -0700303Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
304 const Layer::State& s(getDrawingState());
305 Rect win(s.active.w, s.active.h);
306
307 if (!s.crop.isEmpty()) {
308 win.intersect(s.crop, &win);
309 }
310
311 Transform t = getTransform();
312 win = t.transform(win);
313
Robert Carr41b08b52017-06-01 16:11:34 -0700314 if (!s.finalCrop.isEmpty()) {
315 win.intersect(s.finalCrop, &win);
316 }
317
Chia-I Wue41dbe62017-06-13 14:10:56 -0700318 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700319 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
320 // When calculating the parent bounds for purposes of clipping,
321 // we don't need to constrain the parent to its transparent region.
322 // The transparent region is an optimization based on the
323 // buffer contents of the layer, but does not affect the space allocated to
324 // it by policy, and thus children should be allowed to extend into the
325 // parent's transparent region. In fact one of the main uses, is to reduce
326 // buffer allocation size in cases where a child window sits behind a main window
327 // (by marking the hole in the parent window as a transparent region)
328 if (p != nullptr) {
329 Rect bounds = p->computeScreenBounds(false);
330 bounds.intersect(win, &win);
331 }
332
333 if (reduceTransparentRegion) {
334 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
335 win = reduce(win, screenTransparentRegion);
336 }
337
338 return win;
339}
340
Mathias Agopian13127d82013-03-05 17:47:11 -0800341Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700342 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700343 return computeBounds(s.activeTransparentRegion);
344}
345
346Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
347 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700349
350 if (!s.crop.isEmpty()) {
351 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800352 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353
354 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700355 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700356 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800357 // Look in computeScreenBounds recursive call for explanation of
358 // why we pass false here.
359 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 }
361
362 Transform t = getTransform();
363 if (p != nullptr) {
364 win = t.transform(win);
365 win.intersect(bounds, &win);
366 win = t.inverse().transform(win);
367 }
368
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700369 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700370 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800371}
372
Robert Carr1f0a16a2016-10-24 16:27:39 -0700373Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700374 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800375 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700376 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800377
378 // apply the projection's clipping to the window crop in
379 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700380 // if there are no window scaling involved, this operation will map to full
381 // pixels in the buffer.
382 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
383 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384
385 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700386 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700387 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700388 }
389
Robert Carr1f0a16a2016-10-24 16:27:39 -0700390 Transform t = getTransform();
391 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000392 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
393 activeCrop.clear();
394 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700395 if (!s.finalCrop.isEmpty()) {
396 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000397 activeCrop.clear();
398 }
399 }
chaviwb1154d12017-10-31 14:15:36 -0700400
401 const auto& p = mDrawingParent.promote();
402 if (p != nullptr) {
403 auto parentCrop = p->computeInitialCrop(hw);
404 activeCrop.intersect(parentCrop, &activeCrop);
405 }
406
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407 return activeCrop;
408}
409
Dan Stoza5a423ea2017-02-16 14:10:39 -0800410FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411 // the content crop is the area of the content that gets scaled to the
412 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800413 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700414
415 // In addition there is a WM-specified crop we pull from our drawing state.
416 const State& s(getDrawingState());
417
418 // Screen space to make reduction to parent crop clearer.
419 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700420 Transform t = getTransform();
421 // Back to layer space to work with the content crop.
422 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800423
Michael Lentine28ea2172014-11-19 18:32:37 -0800424 // This needs to be here as transform.transform(Rect) computes the
425 // transformed rect and then takes the bounding box of the result before
426 // returning. This means
427 // transform.inverse().transform(transform.transform(Rect)) != Rect
428 // in which case we need to make sure the final rect is clipped to the
429 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
431 activeCrop.clear();
432 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800433
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700434 // subtract the transparent region and snap to the bounds
435 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
436
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 // Transform the window crop to match the buffer coordinate system,
438 // which means using the inverse of the current transform set on the
439 // SurfaceFlingerConsumer.
440 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700441 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700443 * the code below applies the primary display's inverse transform to the
444 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700446 uint32_t invTransformOrient =
447 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // calculate the inverse transform
449 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
450 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
451 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800452 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700454 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
455 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457
458 int winWidth = s.active.w;
459 int winHeight = s.active.h;
460 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
461 // If the activeCrop has been rotate the ends are rotated but not
462 // the space itself so when transforming ends back we can't rely on
463 // a modification of the axes of rotation. To account for this we
464 // need to reorient the inverse rotation in terms of the current
465 // axes of rotation.
466 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
467 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
468 if (is_h_flipped == is_v_flipped) {
469 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
470 NATIVE_WINDOW_TRANSFORM_FLIP_H;
471 }
472 winWidth = s.active.h;
473 winHeight = s.active.w;
474 }
475 const Rect winCrop = activeCrop.transform(
476 invTransform, s.active.w, s.active.h);
477
478 // below, crop is intersected with winCrop expressed in crop's coordinate space
479 float xScale = crop.getWidth() / float(winWidth);
480 float yScale = crop.getHeight() / float(winHeight);
481
482 float insetL = winCrop.left * xScale;
483 float insetT = winCrop.top * yScale;
484 float insetR = (winWidth - winCrop.right ) * xScale;
485 float insetB = (winHeight - winCrop.bottom) * yScale;
486
487 crop.left += insetL;
488 crop.top += insetT;
489 crop.right -= insetR;
490 crop.bottom -= insetB;
491
Mathias Agopian13127d82013-03-05 17:47:11 -0800492 return crop;
493}
494
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000495#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800496void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000497#else
498void Layer::setGeometry(
499 const sp<const DisplayDevice>& hw,
500 HWComposer::HWCLayerInterface& layer)
501#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700502{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000503#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504 const auto hwcId = displayDevice->getHwcDisplayId();
505 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000506#else
507 layer.setDefaultState();
508#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700509
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700510 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000511#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512 hwcInfo.forceClientComposition = false;
513
514 if (isSecure() && !displayDevice->isSecure()) {
515 hwcInfo.forceClientComposition = true;
516 }
517
518 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000519#else
520 layer.setSkip(false);
521
522 if (isSecure() && !hw->isSecure()) {
523 layer.setSkip(true);
524 }
525#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700526
Mathias Agopian13127d82013-03-05 17:47:11 -0800527 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700528 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000529#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500530 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700531 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500532 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800533 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534 }
David Revemanecf0fa52017-03-03 11:32:44 -0500535 auto error = hwcLayer->setBlendMode(blendMode);
536 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
537 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
538 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000539#else
chaviw13fdc492017-06-27 12:40:18 -0700540 if (!isOpaque(s) || getAlpha() != 1.0f) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000541 layer.setBlending(mPremultipliedAlpha ?
542 HWC_BLENDING_PREMULT :
543 HWC_BLENDING_COVERAGE);
544 }
545#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800546
547 // apply the layer's transform, followed by the display's global transform
548 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700549 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700550 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700551 if (!s.crop.isEmpty()) {
552 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700553 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000554#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000556#else
557 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
558#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000559 activeCrop.clear();
560 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700561 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800562 // This needs to be here as transform.transform(Rect) computes the
563 // transformed rect and then takes the bounding box of the result before
564 // returning. This means
565 // transform.inverse().transform(transform.transform(Rect)) != Rect
566 // in which case we need to make sure the final rect is clipped to the
567 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000568 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
569 activeCrop.clear();
570 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700571 // mark regions outside the crop as transparent
572 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
573 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
574 s.active.w, s.active.h));
575 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
576 activeCrop.left, activeCrop.bottom));
577 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
578 s.active.w, activeCrop.bottom));
579 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700580
581 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700582 if (!s.finalCrop.isEmpty()) {
583 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000584 frame.clear();
585 }
586 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000587#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000588 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
589 frame.clear();
590 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 const Transform& tr(displayDevice->getTransform());
592 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500593 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700594 if (error != HWC2::Error::None) {
595 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
596 mName.string(), transformedFrame.left, transformedFrame.top,
597 transformedFrame.right, transformedFrame.bottom,
598 to_string(error).c_str(), static_cast<int32_t>(error));
599 } else {
600 hwcInfo.displayFrame = transformedFrame;
601 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602
Dan Stoza5a423ea2017-02-16 14:10:39 -0800603 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700605 if (error != HWC2::Error::None) {
606 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
607 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
608 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
609 static_cast<int32_t>(error));
610 } else {
611 hwcInfo.sourceCrop = sourceCrop;
612 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613
chaviw13fdc492017-06-27 12:40:18 -0700614 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700615 error = hwcLayer->setPlaneAlpha(alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800616 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
Robert Carr6452f122017-03-21 10:41:29 -0700617 "%s (%d)", mName.string(), alpha, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618 static_cast<int32_t>(error));
619
Robert Carrae060832016-11-28 10:51:00 -0800620 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800622 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500624
Albert Chaulk2a589632017-05-04 16:59:44 -0400625 int type = s.type;
626 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700627 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400628 if (parent.get()) {
629 auto& parentState = parent->getDrawingState();
630 type = parentState.type;
631 appId = parentState.appId;
632 }
633
634 error = hwcLayer->setInfo(type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500635 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
636 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000637#else
638 if (!frame.intersect(hw->getViewport(), &frame)) {
639 frame.clear();
640 }
641 const Transform& tr(hw->getTransform());
642 layer.setFrame(tr.transform(frame));
643 layer.setCrop(computeCrop(hw));
chaviw13fdc492017-06-27 12:40:18 -0700644 layer.setPlaneAlpha(static_cast<uint8_t>(std::round(255.0f*getAlpha())));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000645#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800646
Mathias Agopian29a367b2011-07-12 14:51:45 -0700647 /*
648 * Transformations are applied in this order:
649 * 1) buffer orientation/flip/mirror
650 * 2) state transformation (window manager)
651 * 3) layer orientation (screen orientation)
652 * (NOTE: the matrices are multiplied in reverse order)
653 */
654
655 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700656 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700657
Robert Carrcae605c2017-03-29 12:10:31 -0700658 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700659 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700660 * the code below applies the primary display's inverse transform to the
661 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700662 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700663 uint32_t invTransform =
664 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700665 // calculate the inverse transform
666 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
667 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
668 NATIVE_WINDOW_TRANSFORM_FLIP_H;
669 }
Robert Carrcae605c2017-03-29 12:10:31 -0700670
671 /*
672 * Here we cancel out the orientation component of the WM transform.
673 * The scaling and translate components are already included in our bounds
674 * computation so it's enough to just omit it in the composition.
675 * See comment in onDraw with ref to b/36727915 for why.
676 */
677 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700678 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700679
680 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800681 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000682#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 if (orientation & Transform::ROT_INVALID) {
684 // we can only handle simple transformation
685 hwcInfo.forceClientComposition = true;
686 } else {
687 auto transform = static_cast<HWC2::Transform>(orientation);
688 auto error = hwcLayer->setTransform(transform);
689 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
690 "%s (%d)", mName.string(), to_string(transform).c_str(),
691 to_string(error).c_str(), static_cast<int32_t>(error));
692 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000693#else
694 if (orientation & Transform::ROT_INVALID) {
695 // we can only handle simple transformation
696 layer.setSkip(true);
697 } else {
698 layer.setTransform(orientation);
699 }
700#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700701}
702
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000703#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704void Layer::forceClientComposition(int32_t hwcId) {
705 if (mHwcLayers.count(hwcId) == 0) {
706 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
707 return;
708 }
709
710 mHwcLayers[hwcId].forceClientComposition = true;
711}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
714 // Apply this display's projection's viewport to the visible region
715 // before giving it to the HWC HAL.
716 const Transform& tr = displayDevice->getTransform();
717 const auto& viewport = displayDevice->getViewport();
718 Region visible = tr.transform(visibleRegion.intersect(viewport));
719 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800720 auto& hwcInfo = mHwcLayers[hwcId];
721 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722 auto error = hwcLayer->setVisibleRegion(visible);
723 if (error != HWC2::Error::None) {
724 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
725 to_string(error).c_str(), static_cast<int32_t>(error));
726 visible.dump(LOG_TAG);
727 }
728
729 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
730 if (error != HWC2::Error::None) {
731 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
732 to_string(error).c_str(), static_cast<int32_t>(error));
733 surfaceDamageRegion.dump(LOG_TAG);
734 }
735
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700736 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700738 setCompositionType(hwcId, HWC2::Composition::Sideband);
739 ALOGV("[%s] Requesting Sideband composition", mName.string());
740 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 if (error != HWC2::Error::None) {
742 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
743 mName.string(), mSidebandStream->handle(),
744 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700746 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747 }
748
Dan Stoza0a21df72016-07-20 12:52:38 -0700749 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800750 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700751 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700752 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700754 return;
755 }
756
Dan Stoza0a21df72016-07-20 12:52:38 -0700757 // SolidColor layers
758 if (mActiveBuffer == nullptr) {
759 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700760
chaviw13fdc492017-06-27 12:40:18 -0700761 half4 color = getColor();
762 error = hwcLayer->setColor({static_cast<uint8_t>(std::round(255.0f*color.r)),
763 static_cast<uint8_t>(std::round(255.0f * color.g)),
764 static_cast<uint8_t>(std::round(255.0f * color.b)),
765 255});
Dan Stoza0a21df72016-07-20 12:52:38 -0700766 if (error != HWC2::Error::None) {
767 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
768 to_string(error).c_str(), static_cast<int32_t>(error));
769 }
Dan Stozac6c89542016-07-27 10:16:52 -0700770
771 // Clear out the transform, because it doesn't make sense absent a
772 // source buffer
773 error = hwcLayer->setTransform(HWC2::Transform::None);
774 if (error != HWC2::Error::None) {
775 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
776 to_string(error).c_str(), static_cast<int32_t>(error));
777 }
778
Dan Stoza0a21df72016-07-20 12:52:38 -0700779 return;
780 }
781
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700782 // Device or Cursor layers
783 if (mPotentialCursor) {
784 ALOGV("[%s] Requesting Cursor composition", mName.string());
785 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786 } else {
787 ALOGV("[%s] Requesting Device composition", mName.string());
788 setCompositionType(hwcId, HWC2::Composition::Device);
789 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700790
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700791 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
792 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
793 if (error != HWC2::Error::None) {
794 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
795 mCurrentState.dataSpace, to_string(error).c_str(),
796 static_cast<int32_t>(error));
797 }
798
Chia-I Wu06d63de2017-01-04 14:58:51 +0800799 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400800 sp<GraphicBuffer> hwcBuffer;
801 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
802 &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800803
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700804 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400805 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700806 if (error != HWC2::Error::None) {
807 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
808 mActiveBuffer->handle, to_string(error).c_str(),
809 static_cast<int32_t>(error));
810 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800811}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600812
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000813#else
814void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
815 HWComposer::HWCLayerInterface& layer) {
816 // we have to set the visible region on every frame because
817 // we currently free it during onLayerDisplayed(), which is called
818 // after HWComposer::commit() -- every frame.
819 // Apply this display's projection's viewport to the visible region
820 // before giving it to the HWC HAL.
821 const Transform& tr = hw->getTransform();
822 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
823 layer.setVisibleRegionScreen(visible);
824 layer.setSurfaceDamage(surfaceDamageRegion);
825 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700826
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000827 if (mSidebandStream.get()) {
828 layer.setSidebandStream(mSidebandStream);
829 } else {
830 // NOTE: buffer can be NULL if the client never drew into this
831 // layer yet, or if we ran out of memory
832 layer.setBuffer(mActiveBuffer);
833 }
834}
835#endif
836
837#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800838void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
839 auto hwcId = displayDevice->getHwcDisplayId();
840 if (mHwcLayers.count(hwcId) == 0 ||
841 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
842 return;
843 }
844
845 // This gives us only the "orientation" component of the transform
846 const State& s(getCurrentState());
847
848 // Apply the layer's transform, followed by the display's global transform
849 // Here we're guaranteed that the layer's transform preserves rects
850 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700851 if (!s.crop.isEmpty()) {
852 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800853 }
854 // Subtract the transparent region and snap to the bounds
855 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700856 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800857 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700858 if (!s.finalCrop.isEmpty()) {
859 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000860 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800861 auto& displayTransform(displayDevice->getTransform());
862 auto position = displayTransform.transform(frame);
863
864 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
865 position.top);
866 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
867 "to (%d, %d): %s (%d)", mName.string(), position.left,
868 position.top, to_string(error).c_str(),
869 static_cast<int32_t>(error));
870}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000871#else
872void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
873 HWComposer::HWCLayerInterface& layer) {
874 int fenceFd = -1;
875
876 // TODO: there is a possible optimization here: we only need to set the
877 // acquire fence the first time a new buffer is acquired on EACH display.
878
879 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
880 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
881 if (fence->isValid()) {
882 fenceFd = fence->dup();
883 if (fenceFd == -1) {
884 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
885 }
886 }
887 }
888 layer.setAcquireFenceFd(fenceFd);
889}
890
891Rect Layer::getPosition(
892 const sp<const DisplayDevice>& hw)
893{
894 // this gives us only the "orientation" component of the transform
895 const State& s(getCurrentState());
896
897 // apply the layer's transform, followed by the display's global transform
898 // here we're guaranteed that the layer's transform preserves rects
899 Rect win(s.active.w, s.active.h);
900 if (!s.crop.isEmpty()) {
901 win.intersect(s.crop, &win);
902 }
903 // subtract the transparent region and snap to the bounds
904 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700905 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000906 frame.intersect(hw->getViewport(), &frame);
907 if (!s.finalCrop.isEmpty()) {
908 frame.intersect(s.finalCrop, &frame);
909 }
910 const Transform& tr(hw->getTransform());
911 return Rect(tr.transform(frame));
912}
913#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700914
Mathias Agopian13127d82013-03-05 17:47:11 -0800915// ---------------------------------------------------------------------------
916// drawing...
917// ---------------------------------------------------------------------------
918
chaviwa76b2712017-09-20 12:02:26 -0700919void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
920 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800921}
922
chaviwa76b2712017-09-20 12:02:26 -0700923void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
924 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800925}
926
chaviwa76b2712017-09-20 12:02:26 -0700927void Layer::draw(const RenderArea& renderArea) const {
928 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800929}
930
chaviwa76b2712017-09-20 12:02:26 -0700931void Layer::clearWithOpenGL(const RenderArea& renderArea,
Fabien Sanglard17487192016-12-07 13:03:32 -0800932 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -0800933 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800934{
Mathias Agopian19733a32013-08-28 18:13:56 -0700935 RenderEngine& engine(mFlinger->getRenderEngine());
chaviwa76b2712017-09-20 12:02:26 -0700936 computeGeometry(renderArea, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700937 engine.setupFillWithColor(red, green, blue, alpha);
938 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800939}
940
chaviwa76b2712017-09-20 12:02:26 -0700941void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
942 clearWithOpenGL(renderArea, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800943}
944
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000945#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800946void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
947 bool callIntoHwc) {
948 if (mHwcLayers.count(hwcId) == 0) {
949 ALOGE("setCompositionType called without a valid HWC layer");
950 return;
951 }
952 auto& hwcInfo = mHwcLayers[hwcId];
953 auto& hwcLayer = hwcInfo.layer;
954 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
955 to_string(type).c_str(), static_cast<int>(callIntoHwc));
956 if (hwcInfo.compositionType != type) {
957 ALOGV(" actually setting");
958 hwcInfo.compositionType = type;
959 if (callIntoHwc) {
960 auto error = hwcLayer->setCompositionType(type);
961 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
962 "composition type %s: %s (%d)", mName.string(),
963 to_string(type).c_str(), to_string(error).c_str(),
964 static_cast<int32_t>(error));
965 }
966 }
967}
968
969HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700970 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
971 // If we're querying the composition type for a display that does not
972 // have a HWC counterpart, then it will always be Client
973 return HWC2::Composition::Client;
974 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800975 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700976 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800977 return HWC2::Composition::Invalid;
978 }
979 return mHwcLayers.at(hwcId).compositionType;
980}
981
982void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
983 if (mHwcLayers.count(hwcId) == 0) {
984 ALOGE("setClearClientTarget called without a valid HWC layer");
985 return;
986 }
987 mHwcLayers[hwcId].clearClientTarget = clear;
988}
989
990bool Layer::getClearClientTarget(int32_t hwcId) const {
991 if (mHwcLayers.count(hwcId) == 0) {
992 ALOGE("getClearClientTarget called without a valid HWC layer");
993 return false;
994 }
995 return mHwcLayers.at(hwcId).clearClientTarget;
996}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000997#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800998
Dan Stozacac35382016-01-27 12:21:06 -0800999bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1000 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1001 // Don't bother with a SyncPoint, since we've already latched the
1002 // relevant frame
1003 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001004 }
1005
Dan Stozacac35382016-01-27 12:21:06 -08001006 Mutex::Autolock lock(mLocalSyncPointMutex);
1007 mLocalSyncPoints.push_back(point);
1008 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001009}
1010
Mathias Agopian13127d82013-03-05 17:47:11 -08001011void Layer::setFiltering(bool filtering) {
1012 mFiltering = filtering;
1013}
1014
1015bool Layer::getFiltering() const {
1016 return mFiltering;
1017}
1018
Mathias Agopian13127d82013-03-05 17:47:11 -08001019// ----------------------------------------------------------------------------
1020// local state
1021// ----------------------------------------------------------------------------
1022
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001023static void boundPoint(vec2* point, const Rect& crop) {
1024 if (point->x < crop.left) {
1025 point->x = crop.left;
1026 }
1027 if (point->x > crop.right) {
1028 point->x = crop.right;
1029 }
1030 if (point->y < crop.top) {
1031 point->y = crop.top;
1032 }
1033 if (point->y > crop.bottom) {
1034 point->y = crop.bottom;
1035 }
1036}
1037
chaviwa76b2712017-09-20 12:02:26 -07001038void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
1039 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001040 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -07001041 const Transform renderAreaTransform(renderArea.getTransform());
1042 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001043 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001044
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001045 vec2 lt = vec2(win.left, win.top);
1046 vec2 lb = vec2(win.left, win.bottom);
1047 vec2 rb = vec2(win.right, win.bottom);
1048 vec2 rt = vec2(win.right, win.top);
1049
Robert Carr1f0a16a2016-10-24 16:27:39 -07001050 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001051 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001052 lt = layerTransform.transform(lt);
1053 lb = layerTransform.transform(lb);
1054 rb = layerTransform.transform(rb);
1055 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001056 }
1057
Robert Carrb5d3d262016-03-25 15:08:13 -07001058 if (!s.finalCrop.isEmpty()) {
1059 boundPoint(&lt, s.finalCrop);
1060 boundPoint(&lb, s.finalCrop);
1061 boundPoint(&rb, s.finalCrop);
1062 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001063 }
1064
Mathias Agopianff2ed702013-09-01 21:36:12 -07001065 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -07001066 position[0] = renderAreaTransform.transform(lt);
1067 position[1] = renderAreaTransform.transform(lb);
1068 position[2] = renderAreaTransform.transform(rb);
1069 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001070 for (size_t i=0 ; i<4 ; i++) {
chaviwa76b2712017-09-20 12:02:26 -07001071 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001074
Dan Stoza23116082015-06-18 14:58:39 -07001075bool Layer::isSecure() const
1076{
1077 const Layer::State& s(mDrawingState);
1078 return (s.flags & layer_state_t::eLayerSecure);
1079}
1080
Mathias Agopian13127d82013-03-05 17:47:11 -08001081void Layer::setVisibleRegion(const Region& visibleRegion) {
1082 // always called from main thread
1083 this->visibleRegion = visibleRegion;
1084}
1085
1086void Layer::setCoveredRegion(const Region& coveredRegion) {
1087 // always called from main thread
1088 this->coveredRegion = coveredRegion;
1089}
1090
1091void Layer::setVisibleNonTransparentRegion(const Region&
1092 setVisibleNonTransparentRegion) {
1093 // always called from main thread
1094 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1095}
1096
1097// ----------------------------------------------------------------------------
1098// transaction
1099// ----------------------------------------------------------------------------
1100
Dan Stoza7dde5992015-05-22 09:51:44 -07001101void Layer::pushPendingState() {
1102 if (!mCurrentState.modified) {
1103 return;
1104 }
1105
Dan Stoza7dde5992015-05-22 09:51:44 -07001106 // If this transaction is waiting on the receipt of a frame, generate a sync
1107 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001108 if (mCurrentState.barrierLayer != nullptr) {
1109 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1110 if (barrierLayer == nullptr) {
1111 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001112 // If we can't promote the layer we are intended to wait on,
1113 // then it is expired or otherwise invalid. Allow this transaction
1114 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001115 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001116 } else {
1117 auto syncPoint = std::make_shared<SyncPoint>(
1118 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001119 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001120 mRemoteSyncPoints.push_back(std::move(syncPoint));
1121 } else {
1122 // We already missed the frame we're supposed to synchronize
1123 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001124 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001125 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001126 }
1127
Dan Stoza7dde5992015-05-22 09:51:44 -07001128 // Wake us up to check if the frame has been received
1129 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001130 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001131 }
1132 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001133 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001134}
1135
Pablo Ceballos05289c22016-04-14 15:49:55 -07001136void Layer::popPendingState(State* stateToCommit) {
1137 auto oldFlags = stateToCommit->flags;
1138 *stateToCommit = mPendingStates[0];
1139 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1140 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001141
1142 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001143 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001144}
1145
Pablo Ceballos05289c22016-04-14 15:49:55 -07001146bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001147 bool stateUpdateAvailable = false;
1148 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001149 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001150 if (mRemoteSyncPoints.empty()) {
1151 // If we don't have a sync point for this, apply it anyway. It
1152 // will be visually wrong, but it should keep us from getting
1153 // into too much trouble.
1154 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001155 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001156 stateUpdateAvailable = true;
1157 continue;
1158 }
1159
Dan Stozacac35382016-01-27 12:21:06 -08001160 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1161 mPendingStates[0].frameNumber) {
1162 ALOGE("[%s] Unexpected sync point frame number found",
1163 mName.string());
1164
1165 // Signal our end of the sync point and then dispose of it
1166 mRemoteSyncPoints.front()->setTransactionApplied();
1167 mRemoteSyncPoints.pop_front();
1168 continue;
1169 }
1170
Dan Stoza7dde5992015-05-22 09:51:44 -07001171 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1172 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001173 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001174 stateUpdateAvailable = true;
1175
1176 // Signal our end of the sync point and then dispose of it
1177 mRemoteSyncPoints.front()->setTransactionApplied();
1178 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001179 } else {
1180 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001181 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001182 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001183 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001184 stateUpdateAvailable = true;
1185 }
1186 }
1187
1188 // If we still have pending updates, wake SurfaceFlinger back up and point
1189 // it at this layer so we can process them
1190 if (!mPendingStates.empty()) {
1191 setTransactionFlags(eTransactionNeeded);
1192 mFlinger->setTransactionFlags(eTraversalNeeded);
1193 }
1194
1195 mCurrentState.modified = false;
1196 return stateUpdateAvailable;
1197}
1198
Mathias Agopian13127d82013-03-05 17:47:11 -08001199uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001200 ATRACE_CALL();
1201
Dan Stoza7dde5992015-05-22 09:51:44 -07001202 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001203 Layer::State c = getCurrentState();
1204 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001205 return 0;
1206 }
1207
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001208 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001210 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1211 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001212
David Sodman0c69cad2017-08-21 12:12:51 -07001213 if (mSurfaceFlingerConsumer && sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001214 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001215 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001216 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001217 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001218 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001219 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001220 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001221 this, getName().string(), mCurrentTransform,
1222 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001223 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001224 c.crop.left,
1225 c.crop.top,
1226 c.crop.right,
1227 c.crop.bottom,
1228 c.crop.getWidth(),
1229 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001230 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001231 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001232 s.crop.left,
1233 s.crop.top,
1234 s.crop.right,
1235 s.crop.bottom,
1236 s.crop.getWidth(),
1237 s.crop.getHeight(),
1238 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001240 // record the new size, form this point on, when the client request
1241 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001242 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001243 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001245
Robert Carre392b552017-09-19 12:16:05 -07001246 // Don't let Layer::doTransaction update the drawing state
1247 // if we have a pending resize, unless we are in fixed-size mode.
1248 // the drawing state will be updated only once we receive a buffer
1249 // with the correct size.
1250 //
1251 // In particular, we want to make sure the clip (which is part
1252 // of the geometry state) is latched together with the size but is
1253 // latched immediately when no resizing is involved.
1254 //
1255 // If a sideband stream is attached, however, we want to skip this
1256 // optimization so that transactions aren't missed when a buffer
1257 // never arrives
1258 //
1259 // In the case that we don't have a buffer we ignore other factors
1260 // and avoid entering the resizePending state. At a high level the
1261 // resizePending state is to avoid applying the state of the new buffer
1262 // to the old buffer. However in the state where we don't have an old buffer
1263 // there is no such concern but we may still be being used as a parent layer.
1264 const bool resizePending = ((c.requested.w != c.active.w) ||
1265 (c.requested.h != c.active.h)) && (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001266 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001267 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001268 flags |= eDontUpdateGeometryState;
1269 }
1270 }
1271
Robert Carr7bf247e2017-05-18 14:02:49 -07001272 // Here we apply various requested geometry states, depending on our
1273 // latching configuration. See Layer.h for a detailed discussion of
1274 // how geometry latching is controlled.
1275 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001276 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001277
1278 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1279 // mode, which causes attributes which normally latch regardless of scaling mode,
1280 // to be delayed. We copy the requested state to the active state making sure
1281 // to respect these rules (again see Layer.h for a detailed discussion).
1282 //
1283 // There is an awkward asymmetry in the handling of the crop states in the position
1284 // states, as can be seen below. Largely this arises from position and transform
1285 // being stored in the same data structure while having different latching rules.
1286 // b/38182305
1287 //
1288 // Careful that "c" and editCurrentState may not begin as equivalent due to
1289 // applyPendingStates in the presence of deferred transactions.
1290 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001291 float tx = c.active.transform.tx();
1292 float ty = c.active.transform.ty();
1293 c.active = c.requested;
1294 c.active.transform.set(tx, ty);
1295 editCurrentState.active = c.active;
1296 } else {
1297 editCurrentState.active = editCurrentState.requested;
1298 c.active = c.requested;
1299 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 }
1301
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001302 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001303 // invalidate and recompute the visible regions if needed
1304 flags |= Layer::eVisibleRegion;
1305 }
1306
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001307 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 // invalidate and recompute the visible regions if needed
1309 flags |= eVisibleRegion;
1310 this->contentDirty = true;
1311
1312 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001313 const uint8_t type = c.active.transform.getType();
1314 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001315 (type >= Transform::SCALE));
1316 }
1317
Dan Stozac8145172016-04-28 16:29:06 -07001318 // If the layer is hidden, signal and clear out all local sync points so
1319 // that transactions for layers depending on this layer's frames becoming
1320 // visible are not blocked
1321 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001322 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001323 }
1324
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001326 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001327 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328}
1329
Pablo Ceballos05289c22016-04-14 15:49:55 -07001330void Layer::commitTransaction(const State& stateToCommit) {
1331 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332}
1333
Mathias Agopian13127d82013-03-05 17:47:11 -08001334uint32_t Layer::getTransactionFlags(uint32_t flags) {
1335 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1336}
1337
1338uint32_t Layer::setTransactionFlags(uint32_t flags) {
1339 return android_atomic_or(flags, &mTransactionFlags);
1340}
1341
Robert Carr82364e32016-05-15 11:27:47 -07001342bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001343 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 return false;
1345 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001346
1347 // We update the requested and active position simultaneously because
1348 // we want to apply the position portion of the transform matrix immediately,
1349 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001350 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001351 if (immediate && !mFreezeGeometryUpdates) {
1352 // Here we directly update the active state
1353 // unlike other setters, because we store it within
1354 // the transform, but use different latching rules.
1355 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001356 mCurrentState.active.transform.set(x, y);
1357 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001358 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001359
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001361 setTransactionFlags(eTransactionNeeded);
1362 return true;
1363}
Robert Carr82364e32016-05-15 11:27:47 -07001364
Robert Carr1f0a16a2016-10-24 16:27:39 -07001365bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1366 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1367 if (idx < 0) {
1368 return false;
1369 }
1370 if (childLayer->setLayer(z)) {
1371 mCurrentChildren.removeAt(idx);
1372 mCurrentChildren.add(childLayer);
1373 }
1374 return true;
1375}
1376
Robert Carrae060832016-11-28 10:51:00 -08001377bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001378 if (mCurrentState.z == z)
1379 return false;
1380 mCurrentState.sequence++;
1381 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001382 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001383
1384 // Discard all relative layering.
1385 if (mCurrentState.zOrderRelativeOf != nullptr) {
1386 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1387 if (strongRelative != nullptr) {
1388 strongRelative->removeZOrderRelative(this);
1389 }
1390 mCurrentState.zOrderRelativeOf = nullptr;
1391 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001392 setTransactionFlags(eTransactionNeeded);
1393 return true;
1394}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001395
Robert Carrdb66e622017-04-10 16:55:57 -07001396void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1397 mCurrentState.zOrderRelatives.remove(relative);
1398 mCurrentState.sequence++;
1399 mCurrentState.modified = true;
1400 setTransactionFlags(eTransactionNeeded);
1401}
1402
1403void Layer::addZOrderRelative(const wp<Layer>& relative) {
1404 mCurrentState.zOrderRelatives.add(relative);
1405 mCurrentState.modified = true;
1406 mCurrentState.sequence++;
1407 setTransactionFlags(eTransactionNeeded);
1408}
1409
1410bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1411 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1412 if (handle == nullptr) {
1413 return false;
1414 }
1415 sp<Layer> relative = handle->owner.promote();
1416 if (relative == nullptr) {
1417 return false;
1418 }
1419
1420 mCurrentState.sequence++;
1421 mCurrentState.modified = true;
1422 mCurrentState.z = z;
1423
chaviw9ab4bd12017-11-03 13:11:00 -07001424 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1425 if (oldZOrderRelativeOf != nullptr) {
1426 oldZOrderRelativeOf->removeZOrderRelative(this);
1427 }
Robert Carrdb66e622017-04-10 16:55:57 -07001428 mCurrentState.zOrderRelativeOf = relative;
1429 relative->addZOrderRelative(this);
1430
1431 setTransactionFlags(eTransactionNeeded);
1432
1433 return true;
1434}
1435
Mathias Agopian13127d82013-03-05 17:47:11 -08001436bool Layer::setSize(uint32_t w, uint32_t h) {
1437 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1438 return false;
1439 mCurrentState.requested.w = w;
1440 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001441 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001442 setTransactionFlags(eTransactionNeeded);
1443 return true;
1444}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001445bool Layer::setAlpha(float alpha) {
chaviw13fdc492017-06-27 12:40:18 -07001446 if (mCurrentState.color.a == alpha)
Mathias Agopian13127d82013-03-05 17:47:11 -08001447 return false;
1448 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001449 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001450 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001451 setTransactionFlags(eTransactionNeeded);
1452 return true;
1453}
chaviw13fdc492017-06-27 12:40:18 -07001454
1455bool Layer::setColor(const half3& color) {
1456 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g
1457 && color.b == mCurrentState.color.b)
1458 return false;
1459
1460 mCurrentState.sequence++;
1461 mCurrentState.color.r = color.r;
1462 mCurrentState.color.g = color.g;
1463 mCurrentState.color.b = color.b;
1464 mCurrentState.modified = true;
1465 setTransactionFlags(eTransactionNeeded);
1466 return true;
1467}
1468
Mathias Agopian13127d82013-03-05 17:47:11 -08001469bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1470 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001471 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001472 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001473 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 setTransactionFlags(eTransactionNeeded);
1475 return true;
1476}
1477bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001478 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001479 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001480 setTransactionFlags(eTransactionNeeded);
1481 return true;
1482}
1483bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1484 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1485 if (mCurrentState.flags == newFlags)
1486 return false;
1487 mCurrentState.sequence++;
1488 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001489 mCurrentState.mask = mask;
1490 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001491 setTransactionFlags(eTransactionNeeded);
1492 return true;
1493}
Robert Carr99e27f02016-06-16 15:18:02 -07001494
1495bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001496 if (mCurrentState.requestedCrop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001497 return false;
1498 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001499 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001500 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001501 mCurrentState.crop = crop;
1502 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001503 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1504
Dan Stoza7dde5992015-05-22 09:51:44 -07001505 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001506 setTransactionFlags(eTransactionNeeded);
1507 return true;
1508}
Robert Carr8d5227b2017-03-16 15:41:03 -07001509
1510bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001511 if (mCurrentState.requestedFinalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001512 return false;
1513 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001514 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001515 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001516 mCurrentState.finalCrop = crop;
1517 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001518 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1519
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001520 mCurrentState.modified = true;
1521 setTransactionFlags(eTransactionNeeded);
1522 return true;
1523}
Mathias Agopian13127d82013-03-05 17:47:11 -08001524
Robert Carrc3574f72016-03-24 12:19:32 -07001525bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1526 if (scalingMode == mOverrideScalingMode)
1527 return false;
1528 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001529 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001530 return true;
1531}
1532
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001533void Layer::setInfo(uint32_t type, uint32_t appId) {
1534 mCurrentState.appId = appId;
1535 mCurrentState.type = type;
1536 mCurrentState.modified = true;
1537 setTransactionFlags(eTransactionNeeded);
1538}
1539
Mathias Agopian13127d82013-03-05 17:47:11 -08001540bool Layer::setLayerStack(uint32_t layerStack) {
1541 if (mCurrentState.layerStack == layerStack)
1542 return false;
1543 mCurrentState.sequence++;
1544 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001545 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001546 setTransactionFlags(eTransactionNeeded);
1547 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001548}
1549
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001550bool Layer::setDataSpace(android_dataspace dataSpace) {
1551 if (mCurrentState.dataSpace == dataSpace)
1552 return false;
1553 mCurrentState.sequence++;
1554 mCurrentState.dataSpace = dataSpace;
1555 mCurrentState.modified = true;
1556 setTransactionFlags(eTransactionNeeded);
1557 return true;
1558}
1559
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001560android_dataspace Layer::getDataSpace() const {
1561 return mCurrentState.dataSpace;
1562}
1563
Robert Carr1f0a16a2016-10-24 16:27:39 -07001564uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001565 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001566 if (p == nullptr) {
1567 return getDrawingState().layerStack;
1568 }
1569 return p->getLayerStack();
1570}
1571
Robert Carr0d480722017-01-10 16:42:54 -08001572void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07001573 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001574 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001575 mCurrentState.frameNumber = frameNumber;
1576 // We don't set eTransactionNeeded, because just receiving a deferral
1577 // request without any other state updates shouldn't actually induce a delay
1578 mCurrentState.modified = true;
1579 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001580 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001581 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001582 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001583}
1584
1585void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
1586 uint64_t frameNumber) {
1587 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1588 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001589}
1590
Dan Stozaee44edd2015-03-23 15:50:23 -07001591void Layer::useSurfaceDamage() {
1592 if (mFlinger->mForceFullDamage) {
1593 surfaceDamageRegion = Region::INVALID_REGION;
1594 } else {
1595 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1596 }
1597}
1598
1599void Layer::useEmptyDamage() {
1600 surfaceDamageRegion.clear();
1601}
1602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603// ----------------------------------------------------------------------------
1604// pageflip handling...
1605// ----------------------------------------------------------------------------
1606
Dan Stoza6b9454d2014-11-07 16:00:59 -08001607bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001608 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001609 return true;
1610 }
1611
Dan Stoza6b9454d2014-11-07 16:00:59 -08001612 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001613 if (mQueueItems.empty()) {
1614 return false;
1615 }
1616 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001617 nsecs_t expectedPresent =
1618 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001619
1620 // Ignore timestamps more than a second in the future
1621 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1622 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1623 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1624 expectedPresent);
1625
1626 bool isDue = timestamp < expectedPresent;
1627 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001628}
1629
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001630bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001631 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001632 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001633 // mFrameLatencyNeeded is true when a new frame was latched for the
1634 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08001635 if (!mFrameLatencyNeeded)
1636 return false;
1637
Fabien Sanglard28e98082016-12-05 10:19:46 -08001638 // Update mFrameEventHistory.
1639 {
1640 Mutex::Autolock lock(mFrameEventHistoryMutex);
1641 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001642 glDoneFence, presentFence, compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001643 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08001644
1645 // Update mFrameTracker.
1646 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1647 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1648
Brian Anderson3d4039d2016-09-23 16:31:30 -07001649 std::shared_ptr<FenceTime> frameReadyFence =
1650 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08001651 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001652 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001653 } else {
1654 // There was no fence for this frame, so assume that it was ready
1655 // to be presented at the desired present time.
1656 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1657 }
1658
Brian Anderson3d4039d2016-09-23 16:31:30 -07001659 if (presentFence->isValid()) {
1660 mFrameTracker.setActualPresentFence(
1661 std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001662 } else {
1663 // The HWC doesn't support present fences, so use the refresh
1664 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001665 mFrameTracker.setActualPresentTime(
1666 mFlinger->getHwComposer().getRefreshTimestamp(
1667 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001668 }
1669
1670 mFrameTracker.advanceFrame();
1671 mFrameLatencyNeeded = false;
1672 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001673}
1674
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675bool Layer::isHiddenByPolicy() const {
1676 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001677 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001678 if (parent != nullptr && parent->isHiddenByPolicy()) {
1679 return true;
1680 }
1681 return s.flags & layer_state_t::eLayerHidden;
1682}
1683
Mathias Agopiana67932f2011-04-20 14:20:59 -07001684uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001685{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001686 // TODO: should we do something special if mSecure is set?
1687 if (mProtectedByApp) {
1688 // need a hardware-protected path to external video sink
1689 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001690 }
Riley Andrews03414a12014-07-01 14:22:59 -07001691 if (mPotentialCursor) {
1692 usage |= GraphicBuffer::USAGE_CURSOR;
1693 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001694 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001695 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001696}
1697
Mathias Agopian84300952012-11-21 16:02:13 -08001698void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001699 uint32_t orientation = 0;
1700 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001701 // The transform hint is used to improve performance, but we can
1702 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001703 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001704 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001705 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001706 if (orientation & Transform::ROT_INVALID) {
1707 orientation = 0;
1708 }
1709 }
David Sodman0c69cad2017-08-21 12:12:51 -07001710 if (mSurfaceFlingerConsumer) {
1711 mSurfaceFlingerConsumer->setTransformHint(orientation);
1712 }
Mathias Agopiana4583642011-08-23 18:03:18 -07001713}
1714
Mathias Agopian13127d82013-03-05 17:47:11 -08001715// ----------------------------------------------------------------------------
1716// debugging
1717// ----------------------------------------------------------------------------
1718
Kalle Raitaa099a242017-01-11 11:17:29 -08001719LayerDebugInfo Layer::getLayerDebugInfo() const {
1720 LayerDebugInfo info;
1721 const Layer::State& ds = getDrawingState();
1722 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001723 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001724 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1725 info.mType = String8(getTypeId());
1726 info.mTransparentRegion = ds.activeTransparentRegion;
1727 info.mVisibleRegion = visibleRegion;
1728 info.mSurfaceDamageRegion = surfaceDamageRegion;
1729 info.mLayerStack = getLayerStack();
1730 info.mX = ds.active.transform.tx();
1731 info.mY = ds.active.transform.ty();
1732 info.mZ = ds.z;
1733 info.mWidth = ds.active.w;
1734 info.mHeight = ds.active.h;
1735 info.mCrop = ds.crop;
1736 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001737 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001738 info.mFlags = ds.flags;
1739 info.mPixelFormat = getPixelFormat();
1740 info.mDataSpace = getDataSpace();
1741 info.mMatrix[0][0] = ds.active.transform[0][0];
1742 info.mMatrix[0][1] = ds.active.transform[0][1];
1743 info.mMatrix[1][0] = ds.active.transform[1][0];
1744 info.mMatrix[1][1] = ds.active.transform[1][1];
1745 {
1746 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1747 if (activeBuffer != 0) {
1748 info.mActiveBufferWidth = activeBuffer->getWidth();
1749 info.mActiveBufferHeight = activeBuffer->getHeight();
1750 info.mActiveBufferStride = activeBuffer->getStride();
1751 info.mActiveBufferFormat = activeBuffer->format;
1752 } else {
1753 info.mActiveBufferWidth = 0;
1754 info.mActiveBufferHeight = 0;
1755 info.mActiveBufferStride = 0;
1756 info.mActiveBufferFormat = 0;
1757 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001758 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001759 info.mNumQueuedFrames = getQueuedFrameCount();
1760 info.mRefreshPending = isBufferLatched();
1761 info.mIsOpaque = isOpaque(ds);
1762 info.mContentDirty = contentDirty;
1763 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001764}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001765#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07001766void Layer::miniDumpHeader(String8& result) {
1767 result.append("----------------------------------------");
1768 result.append("---------------------------------------\n");
1769 result.append(" Layer name\n");
1770 result.append(" Z | ");
1771 result.append(" Comp Type | ");
1772 result.append(" Disp Frame (LTRB) | ");
1773 result.append(" Source Crop (LTRB)\n");
1774 result.append("----------------------------------------");
1775 result.append("---------------------------------------\n");
1776}
1777
1778void Layer::miniDump(String8& result, int32_t hwcId) const {
1779 if (mHwcLayers.count(hwcId) == 0) {
1780 return;
1781 }
1782
1783 String8 name;
1784 if (mName.length() > 77) {
1785 std::string shortened;
1786 shortened.append(mName.string(), 36);
1787 shortened.append("[...]");
1788 shortened.append(mName.string() + (mName.length() - 36), 36);
1789 name = shortened.c_str();
1790 } else {
1791 name = mName;
1792 }
1793
1794 result.appendFormat(" %s\n", name.string());
1795
1796 const Layer::State& layerState(getDrawingState());
1797 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001798 result.appendFormat(" %10d | ", layerState.z);
Dan Stozae22aec72016-08-01 13:20:59 -07001799 result.appendFormat("%10s | ",
1800 to_string(getCompositionType(hwcId)).c_str());
1801 const Rect& frame = hwcInfo.displayFrame;
1802 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
1803 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001804 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07001805 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
1806 crop.right, crop.bottom);
1807
1808 result.append("- - - - - - - - - - - - - - - - - - - - ");
1809 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1810}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001811#endif
Dan Stozae22aec72016-08-01 13:20:59 -07001812
Svetoslavd85084b2014-03-20 10:28:31 -07001813void Layer::dumpFrameStats(String8& result) const {
1814 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001815}
1816
Svetoslavd85084b2014-03-20 10:28:31 -07001817void Layer::clearFrameStats() {
1818 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001819}
1820
Jamie Gennis6547ff42013-07-16 20:12:42 -07001821void Layer::logFrameStats() {
1822 mFrameTracker.logAndResetStats(mName);
1823}
1824
Svetoslavd85084b2014-03-20 10:28:31 -07001825void Layer::getFrameStats(FrameStats* outStats) const {
1826 mFrameTracker.getStats(outStats);
1827}
1828
Brian Andersond6927fb2016-07-23 23:37:30 -07001829void Layer::dumpFrameEvents(String8& result) {
1830 result.appendFormat("- Layer %s (%s, %p)\n",
1831 getName().string(), getTypeId(), this);
1832 Mutex::Autolock lock(mFrameEventHistoryMutex);
1833 mFrameEventHistory.checkFencesForCompletion();
1834 mFrameEventHistory.dump(result);
1835}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001836
Brian Anderson5ea5e592016-12-01 16:54:33 -08001837void Layer::onDisconnect() {
1838 Mutex::Autolock lock(mFrameEventHistoryMutex);
1839 mFrameEventHistory.onDisconnect();
1840}
1841
Brian Anderson3890c392016-07-25 12:48:08 -07001842void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
1843 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001844 Mutex::Autolock lock(mFrameEventHistoryMutex);
1845 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001846 // If there are any unsignaled fences in the aquire timeline at this
1847 // point, the previously queued frame hasn't been latched yet. Go ahead
1848 // and try to get the signal time here so the syscall is taken out of
1849 // the main thread's critical path.
1850 mAcquireTimeline.updateSignalTimes();
1851 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001852 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001853 mFrameEventHistory.addQueue(*newTimestamps);
1854 }
1855
Brian Anderson3890c392016-07-25 12:48:08 -07001856 if (outDelta) {
1857 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001858 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001859}
Dan Stozae77c7662016-05-13 11:37:28 -07001860
1861std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
1862 bool forceFlush) {
1863 std::vector<OccupancyTracker::Segment> history;
David Sodman0c69cad2017-08-21 12:12:51 -07001864
1865 if (!mSurfaceFlingerConsumer)
1866 return {};
1867
Dan Stozae77c7662016-05-13 11:37:28 -07001868 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
1869 &history);
1870 if (result != NO_ERROR) {
1871 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
1872 result);
1873 return {};
1874 }
1875 return history;
1876}
1877
Robert Carr367c5682016-06-20 11:55:28 -07001878bool Layer::getTransformToDisplayInverse() const {
David Sodman0c69cad2017-08-21 12:12:51 -07001879 if (mSurfaceFlingerConsumer) {
1880 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
1881 }
1882 return false;
Robert Carr367c5682016-06-20 11:55:28 -07001883}
1884
Chia-I Wu98f1c102017-05-30 14:54:08 -07001885size_t Layer::getChildrenCount() const {
1886 size_t count = 0;
1887 for (const sp<Layer>& child : mCurrentChildren) {
1888 count += 1 + child->getChildrenCount();
1889 }
1890 return count;
1891}
1892
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893void Layer::addChild(const sp<Layer>& layer) {
1894 mCurrentChildren.add(layer);
1895 layer->setParent(this);
1896}
1897
1898ssize_t Layer::removeChild(const sp<Layer>& layer) {
1899 layer->setParent(nullptr);
1900 return mCurrentChildren.remove(layer);
1901}
1902
Robert Carr1db73f62016-12-21 12:58:51 -08001903bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1904 sp<Handle> handle = nullptr;
1905 sp<Layer> newParent = nullptr;
1906 if (newParentHandle == nullptr) {
1907 return false;
1908 }
1909 handle = static_cast<Handle*>(newParentHandle.get());
1910 newParent = handle->owner.promote();
1911 if (newParent == nullptr) {
1912 ALOGE("Unable to promote Layer handle");
1913 return false;
1914 }
1915
1916 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001917 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001918
1919 sp<Client> client(child->mClientRef.promote());
1920 if (client != nullptr) {
1921 client->setParentLayer(newParent);
1922 }
1923 }
1924 mCurrentChildren.clear();
1925
1926 return true;
1927}
1928
chaviwf1961f72017-09-18 16:41:07 -07001929bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1930 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001931 return false;
1932 }
1933
1934 auto handle = static_cast<Handle*>(newParentHandle.get());
1935 sp<Layer> newParent = handle->owner.promote();
1936 if (newParent == nullptr) {
1937 ALOGE("Unable to promote Layer handle");
1938 return false;
1939 }
1940
chaviwf1961f72017-09-18 16:41:07 -07001941 sp<Layer> parent = getParent();
1942 if (parent != nullptr) {
1943 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001944 }
chaviwf1961f72017-09-18 16:41:07 -07001945 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001946
chaviwf1961f72017-09-18 16:41:07 -07001947 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001948 sp<Client> newParentClient(newParent->mClientRef.promote());
1949
chaviwf1961f72017-09-18 16:41:07 -07001950 if (client != newParentClient) {
1951 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001952 }
1953
chaviw06178942017-07-27 10:25:59 -07001954 return true;
1955}
1956
Robert Carr9524cb32017-02-13 11:32:32 -08001957bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07001958 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08001959 if (child == this) {
1960 return;
1961 }
1962
chaviw161410b02017-07-27 10:46:08 -07001963 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001964 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001965 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08001966 client->detachLayer(child);
1967 }
1968 });
1969
1970 return true;
1971}
1972
Robert Carr1f0a16a2016-10-24 16:27:39 -07001973void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001974 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001975}
1976
1977void Layer::clearSyncPoints() {
1978 for (const auto& child : mCurrentChildren) {
1979 child->clearSyncPoints();
1980 }
1981
1982 Mutex::Autolock lock(mLocalSyncPointMutex);
1983 for (auto& point : mLocalSyncPoints) {
1984 point->setFrameAvailable();
1985 }
1986 mLocalSyncPoints.clear();
1987}
1988
1989int32_t Layer::getZ() const {
1990 return mDrawingState.z;
1991}
1992
Ivan Lozano80de6f32017-10-30 11:24:08 -07001993__attribute__((no_sanitize("unsigned-integer-overflow")))
Dan Stoza412903f2017-04-27 13:42:17 -07001994LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) {
1995 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1996 "makeTraversalList received invalid stateSet");
1997 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1998 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1999 const State& state = useDrawing ? mDrawingState : mCurrentState;
2000
2001 if (state.zOrderRelatives.size() == 0) {
2002 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07002003 }
2004 LayerVector traverse;
2005
Dan Stoza412903f2017-04-27 13:42:17 -07002006 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07002007 sp<Layer> strongRelative = weakRelative.promote();
2008 if (strongRelative != nullptr) {
2009 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07002010 }
2011 }
2012
Dan Stoza412903f2017-04-27 13:42:17 -07002013 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07002014 traverse.add(child);
2015 }
2016
2017 return traverse;
2018}
2019
Robert Carr1f0a16a2016-10-24 16:27:39 -07002020/**
Robert Carrdb66e622017-04-10 16:55:57 -07002021 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002022 */
Dan Stoza412903f2017-04-27 13:42:17 -07002023void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
2024 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002025
Robert Carr1f0a16a2016-10-24 16:27:39 -07002026 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002027 for (; i < list.size(); i++) {
2028 const auto& relative = list[i];
2029 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002030 break;
Robert Carrdb66e622017-04-10 16:55:57 -07002031 }
Dan Stoza412903f2017-04-27 13:42:17 -07002032 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002033 }
Dan Stoza412903f2017-04-27 13:42:17 -07002034 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07002035 for (; i < list.size(); i++) {
2036 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002037 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002038 }
2039}
2040
2041/**
Robert Carrdb66e622017-04-10 16:55:57 -07002042 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002043 */
Dan Stoza412903f2017-04-27 13:42:17 -07002044void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
2045 const LayerVector::Visitor& visitor) {
2046 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002047
Robert Carr1f0a16a2016-10-24 16:27:39 -07002048 int32_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002049 for (i = list.size()-1; i>=0; i--) {
2050 const auto& relative = list[i];
2051 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002052 break;
2053 }
Dan Stoza412903f2017-04-27 13:42:17 -07002054 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002055 }
Dan Stoza412903f2017-04-27 13:42:17 -07002056 visitor(this);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002057 for (; i>=0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002058 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002059 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002060 }
2061}
2062
chaviwa76b2712017-09-20 12:02:26 -07002063/**
2064 * Traverse only children in z order, ignoring relative layers.
2065 */
2066void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2067 const LayerVector::Visitor& visitor) {
2068 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2069 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2070
2071 size_t i = 0;
2072 for (; i < children.size(); i++) {
2073 const auto& relative = children[i];
2074 if (relative->getZ() >= 0) {
2075 break;
2076 }
2077 relative->traverseChildrenInZOrder(stateSet, visitor);
2078 }
2079 visitor(this);
2080 for (; i < children.size(); i++) {
2081 const auto& relative = children[i];
2082 relative->traverseChildrenInZOrder(stateSet, visitor);
2083 }
2084}
2085
Robert Carr1f0a16a2016-10-24 16:27:39 -07002086Transform Layer::getTransform() const {
2087 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002088 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002089 if (p != nullptr) {
2090 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002091
2092 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2093 // it isFixedSize) then there may be additional scaling not accounted
2094 // for in the transform. We need to mirror this scaling in child surfaces
2095 // or we will break the contract where WM can treat child surfaces as
2096 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002097 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002098 int bufferWidth;
2099 int bufferHeight;
2100 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2101 bufferWidth = p->mActiveBuffer->getWidth();
2102 bufferHeight = p->mActiveBuffer->getHeight();
2103 } else {
2104 bufferHeight = p->mActiveBuffer->getWidth();
2105 bufferWidth = p->mActiveBuffer->getHeight();
2106 }
Robert Carr9b429f42017-04-17 14:56:57 -07002107 float sx = p->getDrawingState().active.w /
Robert Carr1725eee2017-04-26 18:32:15 -07002108 static_cast<float>(bufferWidth);
Robert Carr9b429f42017-04-17 14:56:57 -07002109 float sy = p->getDrawingState().active.h /
Robert Carr1725eee2017-04-26 18:32:15 -07002110 static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002111 Transform extraParentScaling;
2112 extraParentScaling.set(sx, 0, 0, sy);
2113 t = t * extraParentScaling;
2114 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002115 }
2116 return t * getDrawingState().active.transform;
2117}
2118
chaviw13fdc492017-06-27 12:40:18 -07002119half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002120 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002121
chaviw13fdc492017-06-27 12:40:18 -07002122 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2123 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002124}
Robert Carr6452f122017-03-21 10:41:29 -07002125
chaviw13fdc492017-06-27 12:40:18 -07002126half4 Layer::getColor() const {
2127 const half4 color(getDrawingState().color);
2128 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002129}
Robert Carr6452f122017-03-21 10:41:29 -07002130
Robert Carr1f0a16a2016-10-24 16:27:39 -07002131void Layer::commitChildList() {
2132 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2133 const auto& child = mCurrentChildren[i];
2134 child->commitChildList();
2135 }
2136 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002137 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002138}
2139
chaviw1d044282017-09-27 12:19:28 -07002140void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2141 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2142 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2143 const State& state = useDrawing ? mDrawingState : mCurrentState;
2144
2145 Transform requestedTransform = state.active.transform;
2146 Transform transform = getTransform();
2147
2148 layerInfo->set_id(sequence);
2149 layerInfo->set_name(getName().c_str());
2150 layerInfo->set_type(String8(getTypeId()));
2151
2152 for (const auto& child : children) {
2153 layerInfo->add_children(child->sequence);
2154 }
2155
2156 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2157 sp<Layer> strongRelative = weakRelative.promote();
2158 if (strongRelative != nullptr) {
2159 layerInfo->add_relatives(strongRelative->sequence);
2160 }
2161 }
2162
2163 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
2164 layerInfo->mutable_transparent_region());
2165 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2166 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2167
2168 layerInfo->set_layer_stack(getLayerStack());
2169 layerInfo->set_z(state.z);
2170
2171 PositionProto* position = layerInfo->mutable_position();
2172 position->set_x(transform.tx());
2173 position->set_y(transform.ty());
2174
2175 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2176 requestedPosition->set_x(requestedTransform.tx());
2177 requestedPosition->set_y(requestedTransform.ty());
2178
2179 SizeProto* size = layerInfo->mutable_size();
2180 size->set_w(state.active.w);
2181 size->set_h(state.active.h);
2182
2183 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
2184 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
2185
2186 layerInfo->set_is_opaque(isOpaque(state));
2187 layerInfo->set_invalidate(contentDirty);
2188 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
2189 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2190 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2191 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2192 layerInfo->set_flags(state.flags);
2193
2194 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2195 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2196
2197 auto parent = getParent();
2198 if (parent != nullptr) {
2199 layerInfo->set_parent(parent->sequence);
2200 }
2201
2202 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2203 if (zOrderRelativeOf != nullptr) {
2204 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2205 }
2206
2207 auto activeBuffer = getActiveBuffer();
2208 if (activeBuffer != nullptr) {
2209 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
2210 }
2211
2212 layerInfo->set_queued_frames(getQueuedFrameCount());
2213 layerInfo->set_refresh_pending(isBufferLatched());
2214}
2215
Mathias Agopian13127d82013-03-05 17:47:11 -08002216// ---------------------------------------------------------------------------
2217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002218}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002219
2220#if defined(__gl_h_)
2221#error "don't include gl/gl.h in this file"
2222#endif
2223
2224#if defined(__gl2_h_)
2225#error "don't include gl2/gl2.h in this file"
2226#endif