blob: 038ece2e05ffdf7e18d69a7e48b51de076803329 [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>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware/HWComposer.h"
54
Mathias Agopian875d8e12013-06-07 15:35:48 -070055#include "RenderEngine/RenderEngine.h"
56
Dan Stozac5da2712016-07-20 15:38:12 -070057#include <mutex>
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#define DEBUG_RESIZE 0
60
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061namespace android {
62
63// ---------------------------------------------------------------------------
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)
Mathias Agopian13127d82013-03-05 17:47:11 -080069 : contentDirty(false),
70 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
74 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080076 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070077 mPendingStateMutex(),
78 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080080 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080081 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070083 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070084 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070085 mCurrentOpacity(true),
Brian Andersond6927fb2016-07-23 23:37:30 -070086 mBufferLatched(false),
Dan Stozacac35382016-01-27 12:21:06 -080087 mCurrentFrameNumber(0),
Brian Anderson8cc8b102016-10-21 12:43:09 -070088 mPreviousFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080089 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080090 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080091 mFiltering(false),
92 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070093 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000094#ifndef USE_HWC2
95 mIsGlesComposition(false),
96#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080097 mProtectedByApp(false),
98 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070099 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -0700100 mPotentialCursor(false),
101 mQueueItemLock(),
102 mQueueItemCondition(),
103 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700104 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -0800105 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700106 mAutoRefresh(false),
Robert Carr7bf247e2017-05-18 14:02:49 -0700107 mFreezeGeometryUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000109#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 ALOGV("Creating Layer %s", name.string());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000111#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800112
Mathias Agopiana67932f2011-04-20 14:20:59 -0700113 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700114 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700115 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116
117 uint32_t layerFlags = 0;
118 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800119 layerFlags |= layer_state_t::eLayerHidden;
120 if (flags & ISurfaceComposerClient::eOpaque)
121 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700122 if (flags & ISurfaceComposerClient::eSecure)
123 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 if (flags & ISurfaceComposerClient::eNonPremultiplied)
126 mPremultipliedAlpha = false;
127
128 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700129 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130
131 mCurrentState.active.w = w;
132 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800133 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700134 mCurrentState.crop.makeInvalid();
135 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700136 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
137 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138 mCurrentState.z = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000139#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140 mCurrentState.alpha = 1.0f;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000141#else
142 mCurrentState.alpha = 0xFF;
143#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 mCurrentState.layerStack = 0;
145 mCurrentState.flags = layerFlags;
146 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700147 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700148 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500149 mCurrentState.appId = 0;
150 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700151
152 // drawing state & current state are identical
153 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700154
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000155#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800156 const auto& hwc = flinger->getHwComposer();
157 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
158 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000159#else
160 nsecs_t displayPeriod =
161 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
162#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700163 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800164
165 CompositorTiming compositorTiming;
166 flinger->getCompositorTiming(&compositorTiming);
167 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800168}
169
Mathias Agopian3f844832013-08-07 21:24:32 -0700170void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800171 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700172 sp<IGraphicBufferProducer> producer;
173 sp<IGraphicBufferConsumer> consumer;
Mathias Agopian0556d792017-03-22 15:49:32 -0700174 BufferQueue::createBufferQueue(&producer, &consumer, true);
Robert Carr1db73f62016-12-21 12:58:51 -0800175 mProducer = new MonitoredProducer(producer, mFlinger, this);
Irvel468051e2016-06-13 16:44:44 -0700176 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800177 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800178 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700179 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800180
Fabien Sanglard63a5fcd2016-12-29 15:13:07 -0800181 if (mFlinger->isLayerTripleBufferingDisabled()) {
182 mProducer->setMaxDequeuedBufferCount(2);
183 }
Andy McFadden69052052012-09-14 16:10:11 -0700184
Mathias Agopian84300952012-11-21 16:02:13 -0800185 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
186 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700187}
188
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700189Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800190 sp<Client> c(mClientRef.promote());
191 if (c != 0) {
192 c->detachLayer(this);
193 }
194
Dan Stozacac35382016-01-27 12:21:06 -0800195 for (auto& point : mRemoteSyncPoints) {
196 point->setTransactionApplied();
197 }
Dan Stozac8145172016-04-28 16:29:06 -0700198 for (auto& point : mLocalSyncPoints) {
199 point->setFrameAvailable();
200 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700201 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700202 mFrameTracker.logAndResetStats(mName);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700203
204#ifdef USE_HWC2
Chia-I Wuc6657022017-08-15 11:18:17 -0700205 if (!mHwcLayers.empty()) {
206 ALOGE("Found stale hardware composer layers when destroying "
207 "surface flinger layer %s", mName.string());
208 destroyAllHwcLayers();
209 }
Steven Thomasd7f49c52017-07-26 18:48:28 -0700210#endif
Mathias Agopian96f08192010-06-02 23:28:45 -0700211}
212
Mathias Agopian13127d82013-03-05 17:47:11 -0800213// ---------------------------------------------------------------------------
214// callbacks
215// ---------------------------------------------------------------------------
216
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000217#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
219 if (mHwcLayers.empty()) {
220 return;
221 }
222 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
223}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000224#else
225void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
226 HWComposer::HWCLayerInterface* layer) {
227 if (layer) {
228 layer->onDisplayed();
229 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
230 }
231}
232#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800233
Dan Stoza6b9454d2014-11-07 16:00:59 -0800234void Layer::onFrameAvailable(const BufferItem& item) {
235 // Add this buffer from our internal queue tracker
236 { // Autolock scope
237 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700238 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
239 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700240 // Reset the frame number tracker when we receive the first buffer after
241 // a frame number reset
242 if (item.mFrameNumber == 1) {
243 mLastFrameNumberReceived = 0;
244 }
245
246 // Ensure that callbacks are handled in order
247 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
248 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
249 ms2ns(500));
250 if (result != NO_ERROR) {
251 ALOGE("[%s] Timed out waiting on callback", mName.string());
252 }
253 }
254
Dan Stoza6b9454d2014-11-07 16:00:59 -0800255 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700256 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700257
258 // Wake up any pending callbacks
259 mLastFrameNumberReceived = item.mFrameNumber;
260 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800261 }
262
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800263 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700264}
265
Dan Stoza6b9454d2014-11-07 16:00:59 -0800266void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700267 { // Autolock scope
268 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700269
Dan Stoza7dde5992015-05-22 09:51:44 -0700270 // Ensure that callbacks are handled in order
271 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
272 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
273 ms2ns(500));
274 if (result != NO_ERROR) {
275 ALOGE("[%s] Timed out waiting on callback", mName.string());
276 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700277 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700278
279 if (mQueueItems.empty()) {
280 ALOGE("Can't replace a frame on an empty queue");
281 return;
282 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700283 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700284
285 // Wake up any pending callbacks
286 mLastFrameNumberReceived = item.mFrameNumber;
287 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700288 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800289}
290
Jesse Hall399184a2014-03-03 15:42:54 -0800291void Layer::onSidebandStreamChanged() {
292 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
293 // mSidebandStreamChanged was false
294 mFlinger->signalLayerUpdate();
295 }
296}
297
Chia-I Wuc6657022017-08-15 11:18:17 -0700298void Layer::onRemovedFromCurrentState() {
299 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
300
Robert Carr5edb1ad2017-04-25 10:54:24 -0700301 if (mCurrentState.zOrderRelativeOf != nullptr) {
302 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
303 if (strongRelative != nullptr) {
304 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700305 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700306 }
307 mCurrentState.zOrderRelativeOf = nullptr;
308 }
309
Chia-I Wuc6657022017-08-15 11:18:17 -0700310 for (const auto& child : mCurrentChildren) {
311 child->onRemovedFromCurrentState();
312 }
313}
Chia-I Wu38512252017-05-17 14:36:16 -0700314
Chia-I Wuc6657022017-08-15 11:18:17 -0700315void Layer::onRemoved() {
316 // the layer is removed from SF mLayersPendingRemoval
317
318 mSurfaceFlingerConsumer->abandon();
Chia-I Wu38512252017-05-17 14:36:16 -0700319#ifdef USE_HWC2
Steven Thomasd7f49c52017-07-26 18:48:28 -0700320 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700321#endif
322
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 for (const auto& child : mCurrentChildren) {
324 child->onRemoved();
325 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700326}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327
Mathias Agopian13127d82013-03-05 17:47:11 -0800328// ---------------------------------------------------------------------------
329// set-up
330// ---------------------------------------------------------------------------
331
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700332const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800333 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}
335
Mathias Agopianf9d93272009-06-19 17:00:27 -0700336status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 PixelFormat format, uint32_t flags)
338{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700339 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700340 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700341
342 // never allow a surface larger than what our underlying GL implementation
343 // can handle.
344 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800345 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700346 return BAD_VALUE;
347 }
348
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700349 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700350
Riley Andrews03414a12014-07-01 14:22:59 -0700351 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700352 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700353 mCurrentOpacity = getOpacityForFormat(format);
354
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800355 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
356 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
357 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 return NO_ERROR;
360}
361
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700362sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800363 Mutex::Autolock _l(mLock);
364
365 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700366 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800367
368 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700369
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700370 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800371}
372
Dan Stozab9b08832014-03-13 11:55:57 -0700373sp<IGraphicBufferProducer> Layer::getProducer() const {
374 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700375}
376
Mathias Agopian13127d82013-03-05 17:47:11 -0800377// ---------------------------------------------------------------------------
378// h/w composer set-up
379// ---------------------------------------------------------------------------
380
Steven Thomasd7f49c52017-07-26 18:48:28 -0700381#ifdef USE_HWC2
382bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
383 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
384 "Already have a layer for hwcId %d", hwcId);
385 HWC2::Layer* layer = hwc->createLayer(hwcId);
386 if (!layer) {
387 return false;
388 }
389 HWCInfo& hwcInfo = mHwcLayers[hwcId];
390 hwcInfo.hwc = hwc;
391 hwcInfo.layer = layer;
392 layer->setLayerDestroyedListener(
393 [this, hwcId] (HWC2::Layer* /*layer*/){mHwcLayers.erase(hwcId);});
394 return true;
395}
396
397void Layer::destroyHwcLayer(int32_t hwcId) {
398 if (mHwcLayers.count(hwcId) == 0) {
399 return;
400 }
401 auto& hwcInfo = mHwcLayers[hwcId];
402 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr,
403 "Attempt to destroy null layer");
404 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
405 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
406 // The layer destroyed listener should have cleared the entry from
407 // mHwcLayers. Verify that.
408 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
409 "Stale layer entry in mHwcLayers");
410}
411
412void Layer::destroyAllHwcLayers() {
413 size_t numLayers = mHwcLayers.size();
414 for (size_t i = 0; i < numLayers; ++i) {
415 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
416 destroyHwcLayer(mHwcLayers.begin()->first);
417 }
418 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
419 "All hardware composer layers should have been destroyed");
420}
421#endif
422
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800423Rect Layer::getContentCrop() const {
424 // this is the crop rectangle that applies to the buffer
425 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700426 Rect crop;
427 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800428 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700429 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800430 } else if (mActiveBuffer != NULL) {
431 // otherwise we use the whole buffer
432 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700433 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800434 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700435 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700436 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437 return crop;
438}
439
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700440static Rect reduce(const Rect& win, const Region& exclude) {
441 if (CC_LIKELY(exclude.isEmpty())) {
442 return win;
443 }
444 if (exclude.isRect()) {
445 return win.reduce(exclude.getBounds());
446 }
447 return Region(win).subtract(exclude).getBounds();
448}
449
Robert Carr1f0a16a2016-10-24 16:27:39 -0700450Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
451 const Layer::State& s(getDrawingState());
452 Rect win(s.active.w, s.active.h);
453
454 if (!s.crop.isEmpty()) {
455 win.intersect(s.crop, &win);
456 }
457
458 Transform t = getTransform();
459 win = t.transform(win);
460
Robert Carr41b08b52017-06-01 16:11:34 -0700461 if (!s.finalCrop.isEmpty()) {
462 win.intersect(s.finalCrop, &win);
463 }
464
Chia-I Wue41dbe62017-06-13 14:10:56 -0700465 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700466 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
467 // When calculating the parent bounds for purposes of clipping,
468 // we don't need to constrain the parent to its transparent region.
469 // The transparent region is an optimization based on the
470 // buffer contents of the layer, but does not affect the space allocated to
471 // it by policy, and thus children should be allowed to extend into the
472 // parent's transparent region. In fact one of the main uses, is to reduce
473 // buffer allocation size in cases where a child window sits behind a main window
474 // (by marking the hole in the parent window as a transparent region)
475 if (p != nullptr) {
476 Rect bounds = p->computeScreenBounds(false);
477 bounds.intersect(win, &win);
478 }
479
480 if (reduceTransparentRegion) {
481 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
482 win = reduce(win, screenTransparentRegion);
483 }
484
485 return win;
486}
487
Mathias Agopian13127d82013-03-05 17:47:11 -0800488Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700489 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700490 return computeBounds(s.activeTransparentRegion);
491}
492
493Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
494 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800495 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700496
497 if (!s.crop.isEmpty()) {
498 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800499 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700500
501 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700502 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700503 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800504 // Look in computeScreenBounds recursive call for explanation of
505 // why we pass false here.
506 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700507 }
508
509 Transform t = getTransform();
510 if (p != nullptr) {
511 win = t.transform(win);
512 win.intersect(bounds, &win);
513 win = t.inverse().transform(win);
514 }
515
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700516 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700517 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800518}
519
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700521 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800522 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700523 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800524
525 // apply the projection's clipping to the window crop in
526 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700527 // if there are no window scaling involved, this operation will map to full
528 // pixels in the buffer.
529 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
530 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700531
532 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700533 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700534 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700535 }
536
Robert Carr1f0a16a2016-10-24 16:27:39 -0700537 Transform t = getTransform();
538 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000539 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
540 activeCrop.clear();
541 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700542 if (!s.finalCrop.isEmpty()) {
543 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000544 activeCrop.clear();
545 }
546 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700547 return activeCrop;
548}
549
Dan Stoza5a423ea2017-02-16 14:10:39 -0800550FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700551 // the content crop is the area of the content that gets scaled to the
552 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800553 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700554
555 // In addition there is a WM-specified crop we pull from our drawing state.
556 const State& s(getDrawingState());
557
558 // Screen space to make reduction to parent crop clearer.
559 Rect activeCrop = computeInitialCrop(hw);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700560 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700561 if (p != nullptr) {
562 auto parentCrop = p->computeInitialCrop(hw);
563 activeCrop.intersect(parentCrop, &activeCrop);
564 }
565 Transform t = getTransform();
566 // Back to layer space to work with the content crop.
567 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800568
Michael Lentine28ea2172014-11-19 18:32:37 -0800569 // This needs to be here as transform.transform(Rect) computes the
570 // transformed rect and then takes the bounding box of the result before
571 // returning. This means
572 // transform.inverse().transform(transform.transform(Rect)) != Rect
573 // in which case we need to make sure the final rect is clipped to the
574 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000575 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
576 activeCrop.clear();
577 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800578
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700579 // subtract the transparent region and snap to the bounds
580 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
581
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000582 // Transform the window crop to match the buffer coordinate system,
583 // which means using the inverse of the current transform set on the
584 // SurfaceFlingerConsumer.
585 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700586 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000587 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700588 * the code below applies the primary display's inverse transform to the
589 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000590 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700591 uint32_t invTransformOrient =
592 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000593 // calculate the inverse transform
594 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
595 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
596 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800597 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000598 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700599 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
600 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800601 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000602
603 int winWidth = s.active.w;
604 int winHeight = s.active.h;
605 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
606 // If the activeCrop has been rotate the ends are rotated but not
607 // the space itself so when transforming ends back we can't rely on
608 // a modification of the axes of rotation. To account for this we
609 // need to reorient the inverse rotation in terms of the current
610 // axes of rotation.
611 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
612 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
613 if (is_h_flipped == is_v_flipped) {
614 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
615 NATIVE_WINDOW_TRANSFORM_FLIP_H;
616 }
617 winWidth = s.active.h;
618 winHeight = s.active.w;
619 }
620 const Rect winCrop = activeCrop.transform(
621 invTransform, s.active.w, s.active.h);
622
623 // below, crop is intersected with winCrop expressed in crop's coordinate space
624 float xScale = crop.getWidth() / float(winWidth);
625 float yScale = crop.getHeight() / float(winHeight);
626
627 float insetL = winCrop.left * xScale;
628 float insetT = winCrop.top * yScale;
629 float insetR = (winWidth - winCrop.right ) * xScale;
630 float insetB = (winHeight - winCrop.bottom) * yScale;
631
632 crop.left += insetL;
633 crop.top += insetT;
634 crop.right -= insetR;
635 crop.bottom -= insetB;
636
Mathias Agopian13127d82013-03-05 17:47:11 -0800637 return crop;
638}
639
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000640#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800641void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000642#else
643void Layer::setGeometry(
644 const sp<const DisplayDevice>& hw,
645 HWComposer::HWCLayerInterface& layer)
646#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700647{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000648#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 const auto hwcId = displayDevice->getHwcDisplayId();
650 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000651#else
652 layer.setDefaultState();
653#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700654
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700655 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000656#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 hwcInfo.forceClientComposition = false;
658
659 if (isSecure() && !displayDevice->isSecure()) {
660 hwcInfo.forceClientComposition = true;
661 }
662
663 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000664#else
665 layer.setSkip(false);
666
667 if (isSecure() && !hw->isSecure()) {
668 layer.setSkip(true);
669 }
670#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700671
Mathias Agopian13127d82013-03-05 17:47:11 -0800672 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700673 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000674#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500675 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700676 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500677 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 }
David Revemanecf0fa52017-03-03 11:32:44 -0500680 auto error = hwcLayer->setBlendMode(blendMode);
681 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
682 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
683 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000684#else
Robert Carr6452f122017-03-21 10:41:29 -0700685 if (!isOpaque(s) || getAlpha() != 0xFF) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000686 layer.setBlending(mPremultipliedAlpha ?
687 HWC_BLENDING_PREMULT :
688 HWC_BLENDING_COVERAGE);
689 }
690#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800691
692 // apply the layer's transform, followed by the display's global transform
693 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700694 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700695 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700696 if (!s.crop.isEmpty()) {
697 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700698 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000699#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000700 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000701#else
702 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
703#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000704 activeCrop.clear();
705 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700706 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800707 // This needs to be here as transform.transform(Rect) computes the
708 // transformed rect and then takes the bounding box of the result before
709 // returning. This means
710 // transform.inverse().transform(transform.transform(Rect)) != Rect
711 // in which case we need to make sure the final rect is clipped to the
712 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000713 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
714 activeCrop.clear();
715 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700716 // mark regions outside the crop as transparent
717 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
718 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
719 s.active.w, s.active.h));
720 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
721 activeCrop.left, activeCrop.bottom));
722 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
723 s.active.w, activeCrop.bottom));
724 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700725
726 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700727 if (!s.finalCrop.isEmpty()) {
728 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000729 frame.clear();
730 }
731 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000732#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000733 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
734 frame.clear();
735 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736 const Transform& tr(displayDevice->getTransform());
737 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500738 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700739 if (error != HWC2::Error::None) {
740 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
741 mName.string(), transformedFrame.left, transformedFrame.top,
742 transformedFrame.right, transformedFrame.bottom,
743 to_string(error).c_str(), static_cast<int32_t>(error));
744 } else {
745 hwcInfo.displayFrame = transformedFrame;
746 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747
Dan Stoza5a423ea2017-02-16 14:10:39 -0800748 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700750 if (error != HWC2::Error::None) {
751 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
752 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
753 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
754 static_cast<int32_t>(error));
755 } else {
756 hwcInfo.sourceCrop = sourceCrop;
757 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758
Robert Carr6452f122017-03-21 10:41:29 -0700759 float alpha = getAlpha();
760 error = hwcLayer->setPlaneAlpha(alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
Robert Carr6452f122017-03-21 10:41:29 -0700762 "%s (%d)", mName.string(), alpha, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763 static_cast<int32_t>(error));
764
Robert Carrae060832016-11-28 10:51:00 -0800765 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800767 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500769
Albert Chaulk2a589632017-05-04 16:59:44 -0400770 int type = s.type;
771 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700772 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400773 if (parent.get()) {
774 auto& parentState = parent->getDrawingState();
775 type = parentState.type;
776 appId = parentState.appId;
777 }
778
779 error = hwcLayer->setInfo(type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500780 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
781 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000782#else
783 if (!frame.intersect(hw->getViewport(), &frame)) {
784 frame.clear();
785 }
786 const Transform& tr(hw->getTransform());
787 layer.setFrame(tr.transform(frame));
788 layer.setCrop(computeCrop(hw));
Robert Carr6452f122017-03-21 10:41:29 -0700789 layer.setPlaneAlpha(getAlpha());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000790#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800791
Mathias Agopian29a367b2011-07-12 14:51:45 -0700792 /*
793 * Transformations are applied in this order:
794 * 1) buffer orientation/flip/mirror
795 * 2) state transformation (window manager)
796 * 3) layer orientation (screen orientation)
797 * (NOTE: the matrices are multiplied in reverse order)
798 */
799
800 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700801 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700802
Robert Carrcae605c2017-03-29 12:10:31 -0700803 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700804 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700805 * the code below applies the primary display's inverse transform to the
806 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700807 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700808 uint32_t invTransform =
809 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700810 // calculate the inverse transform
811 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
812 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
813 NATIVE_WINDOW_TRANSFORM_FLIP_H;
814 }
Robert Carrcae605c2017-03-29 12:10:31 -0700815
816 /*
817 * Here we cancel out the orientation component of the WM transform.
818 * The scaling and translate components are already included in our bounds
819 * computation so it's enough to just omit it in the composition.
820 * See comment in onDraw with ref to b/36727915 for why.
821 */
822 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700823 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700824
825 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800826 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000827#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800828 if (orientation & Transform::ROT_INVALID) {
829 // we can only handle simple transformation
830 hwcInfo.forceClientComposition = true;
831 } else {
832 auto transform = static_cast<HWC2::Transform>(orientation);
833 auto error = hwcLayer->setTransform(transform);
834 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
835 "%s (%d)", mName.string(), to_string(transform).c_str(),
836 to_string(error).c_str(), static_cast<int32_t>(error));
837 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000838#else
839 if (orientation & Transform::ROT_INVALID) {
840 // we can only handle simple transformation
841 layer.setSkip(true);
842 } else {
843 layer.setTransform(orientation);
844 }
845#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700846}
847
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000848#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800849void Layer::forceClientComposition(int32_t hwcId) {
850 if (mHwcLayers.count(hwcId) == 0) {
851 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
852 return;
853 }
854
855 mHwcLayers[hwcId].forceClientComposition = true;
856}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800857
Dan Stoza9e56aa02015-11-02 13:00:03 -0800858void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
859 // Apply this display's projection's viewport to the visible region
860 // before giving it to the HWC HAL.
861 const Transform& tr = displayDevice->getTransform();
862 const auto& viewport = displayDevice->getViewport();
863 Region visible = tr.transform(visibleRegion.intersect(viewport));
864 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800865 auto& hwcInfo = mHwcLayers[hwcId];
866 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800867 auto error = hwcLayer->setVisibleRegion(visible);
868 if (error != HWC2::Error::None) {
869 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
870 to_string(error).c_str(), static_cast<int32_t>(error));
871 visible.dump(LOG_TAG);
872 }
873
874 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
875 if (error != HWC2::Error::None) {
876 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
877 to_string(error).c_str(), static_cast<int32_t>(error));
878 surfaceDamageRegion.dump(LOG_TAG);
879 }
880
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700881 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800882 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700883 setCompositionType(hwcId, HWC2::Composition::Sideband);
884 ALOGV("[%s] Requesting Sideband composition", mName.string());
885 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800886 if (error != HWC2::Error::None) {
887 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
888 mName.string(), mSidebandStream->handle(),
889 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800890 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700891 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800892 }
893
Dan Stoza0a21df72016-07-20 12:52:38 -0700894 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800895 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700896 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700897 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800898 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700899 return;
900 }
901
Dan Stoza0a21df72016-07-20 12:52:38 -0700902 // SolidColor layers
903 if (mActiveBuffer == nullptr) {
904 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700905
906 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700907 error = hwcLayer->setColor({0, 0, 0, 255});
908 if (error != HWC2::Error::None) {
909 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
910 to_string(error).c_str(), static_cast<int32_t>(error));
911 }
Dan Stozac6c89542016-07-27 10:16:52 -0700912
913 // Clear out the transform, because it doesn't make sense absent a
914 // source buffer
915 error = hwcLayer->setTransform(HWC2::Transform::None);
916 if (error != HWC2::Error::None) {
917 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
918 to_string(error).c_str(), static_cast<int32_t>(error));
919 }
920
Dan Stoza0a21df72016-07-20 12:52:38 -0700921 return;
922 }
923
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700924 // Device or Cursor layers
925 if (mPotentialCursor) {
926 ALOGV("[%s] Requesting Cursor composition", mName.string());
927 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800928 } else {
929 ALOGV("[%s] Requesting Device composition", mName.string());
930 setCompositionType(hwcId, HWC2::Composition::Device);
931 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700932
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700933 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
934 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
935 if (error != HWC2::Error::None) {
936 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
937 mCurrentState.dataSpace, to_string(error).c_str(),
938 static_cast<int32_t>(error));
939 }
940
Chia-I Wu06d63de2017-01-04 14:58:51 +0800941 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400942 sp<GraphicBuffer> hwcBuffer;
943 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
944 &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800945
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700946 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400947 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700948 if (error != HWC2::Error::None) {
949 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
950 mActiveBuffer->handle, to_string(error).c_str(),
951 static_cast<int32_t>(error));
952 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800953}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600954
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000955#else
956void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
957 HWComposer::HWCLayerInterface& layer) {
958 // we have to set the visible region on every frame because
959 // we currently free it during onLayerDisplayed(), which is called
960 // after HWComposer::commit() -- every frame.
961 // Apply this display's projection's viewport to the visible region
962 // before giving it to the HWC HAL.
963 const Transform& tr = hw->getTransform();
964 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
965 layer.setVisibleRegionScreen(visible);
966 layer.setSurfaceDamage(surfaceDamageRegion);
967 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700968
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000969 if (mSidebandStream.get()) {
970 layer.setSidebandStream(mSidebandStream);
971 } else {
972 // NOTE: buffer can be NULL if the client never drew into this
973 // layer yet, or if we ran out of memory
974 layer.setBuffer(mActiveBuffer);
975 }
976}
977#endif
978
979#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800980void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
981 auto hwcId = displayDevice->getHwcDisplayId();
982 if (mHwcLayers.count(hwcId) == 0 ||
983 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
984 return;
985 }
986
987 // This gives us only the "orientation" component of the transform
988 const State& s(getCurrentState());
989
990 // Apply the layer's transform, followed by the display's global transform
991 // Here we're guaranteed that the layer's transform preserves rects
992 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700993 if (!s.crop.isEmpty()) {
994 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800995 }
996 // Subtract the transparent region and snap to the bounds
997 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700998 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800999 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -07001000 if (!s.finalCrop.isEmpty()) {
1001 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001002 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001003 auto& displayTransform(displayDevice->getTransform());
1004 auto position = displayTransform.transform(frame);
1005
1006 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
1007 position.top);
1008 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
1009 "to (%d, %d): %s (%d)", mName.string(), position.left,
1010 position.top, to_string(error).c_str(),
1011 static_cast<int32_t>(error));
1012}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001013#else
1014void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
1015 HWComposer::HWCLayerInterface& layer) {
1016 int fenceFd = -1;
1017
1018 // TODO: there is a possible optimization here: we only need to set the
1019 // acquire fence the first time a new buffer is acquired on EACH display.
1020
1021 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
1022 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
1023 if (fence->isValid()) {
1024 fenceFd = fence->dup();
1025 if (fenceFd == -1) {
1026 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
1027 }
1028 }
1029 }
1030 layer.setAcquireFenceFd(fenceFd);
1031}
1032
1033Rect Layer::getPosition(
1034 const sp<const DisplayDevice>& hw)
1035{
1036 // this gives us only the "orientation" component of the transform
1037 const State& s(getCurrentState());
1038
1039 // apply the layer's transform, followed by the display's global transform
1040 // here we're guaranteed that the layer's transform preserves rects
1041 Rect win(s.active.w, s.active.h);
1042 if (!s.crop.isEmpty()) {
1043 win.intersect(s.crop, &win);
1044 }
1045 // subtract the transparent region and snap to the bounds
1046 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001047 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001048 frame.intersect(hw->getViewport(), &frame);
1049 if (!s.finalCrop.isEmpty()) {
1050 frame.intersect(s.finalCrop, &frame);
1051 }
1052 const Transform& tr(hw->getTransform());
1053 return Rect(tr.transform(frame));
1054}
1055#endif
Riley Andrews03414a12014-07-01 14:22:59 -07001056
Mathias Agopian13127d82013-03-05 17:47:11 -08001057// ---------------------------------------------------------------------------
1058// drawing...
1059// ---------------------------------------------------------------------------
1060
1061void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -08001062 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -08001063}
1064
Dan Stozac7014012014-02-14 15:03:43 -08001065void Layer::draw(const sp<const DisplayDevice>& hw,
1066 bool useIdentityTransform) const {
1067 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001068}
1069
Dan Stozac7014012014-02-14 15:03:43 -08001070void Layer::draw(const sp<const DisplayDevice>& hw) const {
1071 onDraw(hw, Region(hw->bounds()), false);
1072}
1073
Robert Carrcae605c2017-03-29 12:10:31 -07001074static constexpr mat4 inverseOrientation(uint32_t transform) {
1075 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1076 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1077 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1078 mat4 tr;
1079
1080 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
1081 tr = tr * rot90;
1082 }
1083 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
1084 tr = tr * flipH;
1085 }
1086 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
1087 tr = tr * flipV;
1088 }
1089 return inverse(tr);
1090}
1091
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001092/*
1093 * onDraw will draw the current layer onto the presentable buffer
1094 */
Dan Stozac7014012014-02-14 15:03:43 -08001095void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
1096 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001098 ATRACE_CALL();
1099
Mathias Agopiana67932f2011-04-20 14:20:59 -07001100 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001102 // in fact never been drawn into. This happens frequently with
1103 // SurfaceView because the WindowManager can't know when the client
1104 // has drawn the first time.
1105
1106 // If there is nothing under us, we paint the screen in black, otherwise
1107 // we just skip this update.
1108
1109 // figure out if there is something below us
1110 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001111 bool finished = false;
Dan Stoza412903f2017-04-27 13:42:17 -07001112 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001113 if (finished || layer == static_cast<Layer const*>(this)) {
1114 finished = true;
1115 return;
1116 }
Mathias Agopian42977342012-08-05 00:40:46 -07001117 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001118 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001119 // if not everything below us is covered, we plug the holes!
1120 Region holes(clip.subtract(under));
1121 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001122 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001123 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 return;
1125 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001126
Andy McFadden97eba892012-12-11 15:21:45 -08001127 // Bind the current buffer to the GL texture, and wait for it to be
1128 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001129 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1130 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001131 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001132 // Go ahead and draw the buffer anyway; no matter what we do the screen
1133 // is probably going to have something visibly wrong.
1134 }
1135
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001136 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1137
Mathias Agopian875d8e12013-06-07 15:35:48 -07001138 RenderEngine& engine(mFlinger->getRenderEngine());
1139
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001140 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001141 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001142 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001143
1144 // Query the texture matrix given our current filtering mode.
1145 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001146 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1147 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001148
Robert Carrcae605c2017-03-29 12:10:31 -07001149 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001150
1151 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001152 * the code below applies the primary display's inverse transform to
1153 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001154 */
Pablo Ceballos021623b2016-04-15 17:31:51 -07001155 uint32_t transform =
1156 DisplayDevice::getPrimaryDisplayOrientationTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001157 mat4 tr = inverseOrientation(transform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001158
Robert Carrcae605c2017-03-29 12:10:31 -07001159 /**
1160 * TODO(b/36727915): This is basically a hack.
1161 *
1162 * Ensure that regardless of the parent transformation,
1163 * this buffer is always transformed from native display
1164 * orientation to display orientation. For example, in the case
1165 * of a camera where the buffer remains in native orientation,
1166 * we want the pixels to always be upright.
1167 */
Chia-I Wue41dbe62017-06-13 14:10:56 -07001168 sp<Layer> p = mDrawingParent.promote();
1169 if (p != nullptr) {
1170 const auto parentTransform = p->getTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001171 tr = tr * inverseOrientation(parentTransform.getOrientation());
1172 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001173
1174 // and finally apply it to the original texture matrix
1175 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1176 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1177 }
1178
Jamie Genniscbb1a952012-05-08 17:05:52 -07001179 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001180 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1181 mTexture.setFiltering(useFiltering);
1182 mTexture.setMatrix(textureMatrix);
1183
1184 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001185 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001186 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001187 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001188 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001189 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190}
1191
Mathias Agopian13127d82013-03-05 17:47:11 -08001192
Dan Stozac7014012014-02-14 15:03:43 -08001193void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001194 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001195 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001196{
Mathias Agopian19733a32013-08-28 18:13:56 -07001197 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001198 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001199 engine.setupFillWithColor(red, green, blue, alpha);
1200 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001201}
1202
1203void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001204 const sp<const DisplayDevice>& hw) const {
1205 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001206}
1207
Dan Stozac7014012014-02-14 15:03:43 -08001208void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001209 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001210 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001211
Dan Stozac7014012014-02-14 15:03:43 -08001212 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001213
Mathias Agopian13127d82013-03-05 17:47:11 -08001214 /*
1215 * NOTE: the way we compute the texture coordinates here produces
1216 * different results than when we take the HWC path -- in the later case
1217 * the "source crop" is rounded to texel boundaries.
1218 * This can produce significantly different results when the texture
1219 * is scaled by a large amount.
1220 *
1221 * The GL code below is more logical (imho), and the difference with
1222 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001223 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 * GL composition when a buffer scaling is applied (maybe with some
1225 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1226 * like more of a hack.
1227 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001228 Rect win(computeBounds());
1229
Robert Carr1f0a16a2016-10-24 16:27:39 -07001230 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001231 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001232 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001233 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001234 win.clear();
1235 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001236 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001237 if (!win.intersect(computeBounds(), &win)) {
1238 win.clear();
1239 }
1240 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001241
Mathias Agopian3f844832013-08-07 21:24:32 -07001242 float left = float(win.left) / float(s.active.w);
1243 float top = float(win.top) / float(s.active.h);
1244 float right = float(win.right) / float(s.active.w);
1245 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001246
Mathias Agopian875d8e12013-06-07 15:35:48 -07001247 // TODO: we probably want to generate the texture coords with the mesh
1248 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001249 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1250 texCoords[0] = vec2(left, 1.0f - top);
1251 texCoords[1] = vec2(left, 1.0f - bottom);
1252 texCoords[2] = vec2(right, 1.0f - bottom);
1253 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001254
Mathias Agopian875d8e12013-06-07 15:35:48 -07001255 RenderEngine& engine(mFlinger->getRenderEngine());
Robert Carr6452f122017-03-21 10:41:29 -07001256 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), getAlpha());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001257#ifdef USE_HWC2
1258 engine.setSourceDataSpace(mCurrentState.dataSpace);
1259#endif
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001260 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001261 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001262}
1263
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001264#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001265void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1266 bool callIntoHwc) {
1267 if (mHwcLayers.count(hwcId) == 0) {
1268 ALOGE("setCompositionType called without a valid HWC layer");
1269 return;
1270 }
1271 auto& hwcInfo = mHwcLayers[hwcId];
1272 auto& hwcLayer = hwcInfo.layer;
1273 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1274 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1275 if (hwcInfo.compositionType != type) {
1276 ALOGV(" actually setting");
1277 hwcInfo.compositionType = type;
1278 if (callIntoHwc) {
1279 auto error = hwcLayer->setCompositionType(type);
1280 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1281 "composition type %s: %s (%d)", mName.string(),
1282 to_string(type).c_str(), to_string(error).c_str(),
1283 static_cast<int32_t>(error));
1284 }
1285 }
1286}
1287
1288HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001289 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1290 // If we're querying the composition type for a display that does not
1291 // have a HWC counterpart, then it will always be Client
1292 return HWC2::Composition::Client;
1293 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001294 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001295 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001296 return HWC2::Composition::Invalid;
1297 }
1298 return mHwcLayers.at(hwcId).compositionType;
1299}
1300
1301void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1302 if (mHwcLayers.count(hwcId) == 0) {
1303 ALOGE("setClearClientTarget called without a valid HWC layer");
1304 return;
1305 }
1306 mHwcLayers[hwcId].clearClientTarget = clear;
1307}
1308
1309bool Layer::getClearClientTarget(int32_t hwcId) const {
1310 if (mHwcLayers.count(hwcId) == 0) {
1311 ALOGE("getClearClientTarget called without a valid HWC layer");
1312 return false;
1313 }
1314 return mHwcLayers.at(hwcId).clearClientTarget;
1315}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001316#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001317
Ruben Brunk1681d952014-06-27 15:51:55 -07001318uint32_t Layer::getProducerStickyTransform() const {
1319 int producerStickyTransform = 0;
1320 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1321 if (ret != OK) {
1322 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1323 strerror(-ret), ret);
1324 return 0;
1325 }
1326 return static_cast<uint32_t>(producerStickyTransform);
1327}
1328
Dan Stozac5da2712016-07-20 15:38:12 -07001329bool Layer::latchUnsignaledBuffers() {
1330 static bool propertyLoaded = false;
1331 static bool latch = false;
1332 static std::mutex mutex;
1333 std::lock_guard<std::mutex> lock(mutex);
1334 if (!propertyLoaded) {
1335 char value[PROPERTY_VALUE_MAX] = {};
1336 property_get("debug.sf.latch_unsignaled", value, "0");
1337 latch = atoi(value);
1338 propertyLoaded = true;
1339 }
1340 return latch;
1341}
1342
Dan Stozacac35382016-01-27 12:21:06 -08001343uint64_t Layer::getHeadFrameNumber() const {
1344 Mutex::Autolock lock(mQueueItemLock);
1345 if (!mQueueItems.empty()) {
1346 return mQueueItems[0].mFrameNumber;
1347 } else {
1348 return mCurrentFrameNumber;
1349 }
1350}
1351
Dan Stoza1ce65812016-06-15 16:26:27 -07001352bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001353#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001354 if (latchUnsignaledBuffers()) {
1355 return true;
1356 }
1357
Dan Stoza1ce65812016-06-15 16:26:27 -07001358 Mutex::Autolock lock(mQueueItemLock);
1359 if (mQueueItems.empty()) {
1360 return true;
1361 }
1362 if (mQueueItems[0].mIsDroppable) {
1363 // Even though this buffer's fence may not have signaled yet, it could
1364 // be replaced by another buffer before it has a chance to, which means
1365 // that it's possible to get into a situation where a buffer is never
1366 // able to be latched. To avoid this, grab this buffer anyway.
1367 return true;
1368 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001369 return mQueueItems[0].mFenceTime->getSignalTime() !=
1370 Fence::SIGNAL_TIME_PENDING;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001371#else
1372 return true;
1373#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001374}
1375
Dan Stozacac35382016-01-27 12:21:06 -08001376bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1377 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1378 // Don't bother with a SyncPoint, since we've already latched the
1379 // relevant frame
1380 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001381 }
1382
Dan Stozacac35382016-01-27 12:21:06 -08001383 Mutex::Autolock lock(mLocalSyncPointMutex);
1384 mLocalSyncPoints.push_back(point);
1385 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001386}
1387
Mathias Agopian13127d82013-03-05 17:47:11 -08001388void Layer::setFiltering(bool filtering) {
1389 mFiltering = filtering;
1390}
1391
1392bool Layer::getFiltering() const {
1393 return mFiltering;
1394}
1395
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001396// As documented in libhardware header, formats in the range
1397// 0x100 - 0x1FF are specific to the HAL implementation, and
1398// are known to have no alpha channel
1399// TODO: move definition for device-specific range into
1400// hardware.h, instead of using hard-coded values here.
1401#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1402
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001403bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001404 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1405 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001406 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001407 switch (format) {
1408 case HAL_PIXEL_FORMAT_RGBA_8888:
1409 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001410 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001411 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001412 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001413 }
1414 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001415 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001416}
1417
Mathias Agopian13127d82013-03-05 17:47:11 -08001418// ----------------------------------------------------------------------------
1419// local state
1420// ----------------------------------------------------------------------------
1421
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001422static void boundPoint(vec2* point, const Rect& crop) {
1423 if (point->x < crop.left) {
1424 point->x = crop.left;
1425 }
1426 if (point->x > crop.right) {
1427 point->x = crop.right;
1428 }
1429 if (point->y < crop.top) {
1430 point->y = crop.top;
1431 }
1432 if (point->y > crop.bottom) {
1433 point->y = crop.bottom;
1434 }
1435}
1436
Dan Stozac7014012014-02-14 15:03:43 -08001437void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1438 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001439{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001440 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001441 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001442 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001443 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001444
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001445 vec2 lt = vec2(win.left, win.top);
1446 vec2 lb = vec2(win.left, win.bottom);
1447 vec2 rb = vec2(win.right, win.bottom);
1448 vec2 rt = vec2(win.right, win.top);
1449
Robert Carr1f0a16a2016-10-24 16:27:39 -07001450 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001451 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001452 lt = layerTransform.transform(lt);
1453 lb = layerTransform.transform(lb);
1454 rb = layerTransform.transform(rb);
1455 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001456 }
1457
Robert Carrb5d3d262016-03-25 15:08:13 -07001458 if (!s.finalCrop.isEmpty()) {
1459 boundPoint(&lt, s.finalCrop);
1460 boundPoint(&lb, s.finalCrop);
1461 boundPoint(&rb, s.finalCrop);
1462 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001463 }
1464
Mathias Agopianff2ed702013-09-01 21:36:12 -07001465 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001466 position[0] = hwTransform.transform(lt);
1467 position[1] = hwTransform.transform(lb);
1468 position[2] = hwTransform.transform(rb);
1469 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001470 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001471 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001472 }
1473}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001474
Andy McFadden4125a4f2014-01-29 17:17:11 -08001475bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001476{
neo.hee6fd41d2017-02-17 14:35:33 +08001477 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
Mathias Agopiana67932f2011-04-20 14:20:59 -07001478 // layer's opaque flag.
neo.hee6fd41d2017-02-17 14:35:33 +08001479 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001480 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001481 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001482
1483 // if the layer has the opaque flag, then we're always opaque,
1484 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001485 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001486}
1487
Dan Stoza23116082015-06-18 14:58:39 -07001488bool Layer::isSecure() const
1489{
1490 const Layer::State& s(mDrawingState);
1491 return (s.flags & layer_state_t::eLayerSecure);
1492}
1493
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001494bool Layer::isProtected() const
1495{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001496 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001497 return (activeBuffer != 0) &&
1498 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1499}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001500
Mathias Agopian13127d82013-03-05 17:47:11 -08001501bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001502 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001503}
1504
1505bool Layer::isCropped() const {
1506 return !mCurrentCrop.isEmpty();
1507}
1508
1509bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1510 return mNeedsFiltering || hw->needsFiltering();
1511}
1512
1513void Layer::setVisibleRegion(const Region& visibleRegion) {
1514 // always called from main thread
1515 this->visibleRegion = visibleRegion;
1516}
1517
1518void Layer::setCoveredRegion(const Region& coveredRegion) {
1519 // always called from main thread
1520 this->coveredRegion = coveredRegion;
1521}
1522
1523void Layer::setVisibleNonTransparentRegion(const Region&
1524 setVisibleNonTransparentRegion) {
1525 // always called from main thread
1526 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1527}
1528
1529// ----------------------------------------------------------------------------
1530// transaction
1531// ----------------------------------------------------------------------------
1532
Dan Stoza7dde5992015-05-22 09:51:44 -07001533void Layer::pushPendingState() {
1534 if (!mCurrentState.modified) {
1535 return;
1536 }
1537
Dan Stoza7dde5992015-05-22 09:51:44 -07001538 // If this transaction is waiting on the receipt of a frame, generate a sync
1539 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001540 if (mCurrentState.barrierLayer != nullptr) {
1541 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1542 if (barrierLayer == nullptr) {
1543 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001544 // If we can't promote the layer we are intended to wait on,
1545 // then it is expired or otherwise invalid. Allow this transaction
1546 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001547 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001548 } else {
1549 auto syncPoint = std::make_shared<SyncPoint>(
1550 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001551 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001552 mRemoteSyncPoints.push_back(std::move(syncPoint));
1553 } else {
1554 // We already missed the frame we're supposed to synchronize
1555 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001556 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001557 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001558 }
1559
Dan Stoza7dde5992015-05-22 09:51:44 -07001560 // Wake us up to check if the frame has been received
1561 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001562 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001563 }
1564 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001565 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001566}
1567
Pablo Ceballos05289c22016-04-14 15:49:55 -07001568void Layer::popPendingState(State* stateToCommit) {
1569 auto oldFlags = stateToCommit->flags;
1570 *stateToCommit = mPendingStates[0];
1571 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1572 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001573
1574 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001575 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001576}
1577
Pablo Ceballos05289c22016-04-14 15:49:55 -07001578bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001579 bool stateUpdateAvailable = false;
1580 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001581 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001582 if (mRemoteSyncPoints.empty()) {
1583 // If we don't have a sync point for this, apply it anyway. It
1584 // will be visually wrong, but it should keep us from getting
1585 // into too much trouble.
1586 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001587 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001588 stateUpdateAvailable = true;
1589 continue;
1590 }
1591
Dan Stozacac35382016-01-27 12:21:06 -08001592 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1593 mPendingStates[0].frameNumber) {
1594 ALOGE("[%s] Unexpected sync point frame number found",
1595 mName.string());
1596
1597 // Signal our end of the sync point and then dispose of it
1598 mRemoteSyncPoints.front()->setTransactionApplied();
1599 mRemoteSyncPoints.pop_front();
1600 continue;
1601 }
1602
Dan Stoza7dde5992015-05-22 09:51:44 -07001603 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1604 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001605 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001606 stateUpdateAvailable = true;
1607
1608 // Signal our end of the sync point and then dispose of it
1609 mRemoteSyncPoints.front()->setTransactionApplied();
1610 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001611 } else {
1612 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001613 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001614 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001615 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001616 stateUpdateAvailable = true;
1617 }
1618 }
1619
1620 // If we still have pending updates, wake SurfaceFlinger back up and point
1621 // it at this layer so we can process them
1622 if (!mPendingStates.empty()) {
1623 setTransactionFlags(eTransactionNeeded);
1624 mFlinger->setTransactionFlags(eTraversalNeeded);
1625 }
1626
1627 mCurrentState.modified = false;
1628 return stateUpdateAvailable;
1629}
1630
1631void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001632 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001633 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001634 Mutex::Autolock lock(mLocalSyncPointMutex);
1635 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001636 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001637 point->setFrameAvailable();
1638 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001639 }
1640}
1641
Mathias Agopian13127d82013-03-05 17:47:11 -08001642uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001643 ATRACE_CALL();
1644
Dan Stoza7dde5992015-05-22 09:51:44 -07001645 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001646 Layer::State c = getCurrentState();
1647 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001648 return 0;
1649 }
1650
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001651 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001653 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1654 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001655
1656 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001657 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001658 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001659 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001660 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001661 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001662 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001663 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001664 this, getName().string(), mCurrentTransform,
1665 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001666 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001667 c.crop.left,
1668 c.crop.top,
1669 c.crop.right,
1670 c.crop.bottom,
1671 c.crop.getWidth(),
1672 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001673 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001674 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001675 s.crop.left,
1676 s.crop.top,
1677 s.crop.right,
1678 s.crop.bottom,
1679 s.crop.getWidth(),
1680 s.crop.getHeight(),
1681 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001683 // record the new size, form this point on, when the client request
1684 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001685 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001686 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001688
Robert Carr82364e32016-05-15 11:27:47 -07001689 const bool resizePending = (c.requested.w != c.active.w) ||
1690 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001691 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001692 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001693 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001694 // if we have a pending resize, unless we are in fixed-size mode.
1695 // the drawing state will be updated only once we receive a buffer
1696 // with the correct size.
1697 //
1698 // in particular, we want to make sure the clip (which is part
1699 // of the geometry state) is latched together with the size but is
1700 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001701 //
1702 // If a sideband stream is attached, however, we want to skip this
1703 // optimization so that transactions aren't missed when a buffer
1704 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001705
1706 flags |= eDontUpdateGeometryState;
1707 }
1708 }
1709
Robert Carr7bf247e2017-05-18 14:02:49 -07001710 // Here we apply various requested geometry states, depending on our
1711 // latching configuration. See Layer.h for a detailed discussion of
1712 // how geometry latching is controlled.
1713 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001714 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001715
1716 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1717 // mode, which causes attributes which normally latch regardless of scaling mode,
1718 // to be delayed. We copy the requested state to the active state making sure
1719 // to respect these rules (again see Layer.h for a detailed discussion).
1720 //
1721 // There is an awkward asymmetry in the handling of the crop states in the position
1722 // states, as can be seen below. Largely this arises from position and transform
1723 // being stored in the same data structure while having different latching rules.
1724 // b/38182305
1725 //
1726 // Careful that "c" and editCurrentState may not begin as equivalent due to
1727 // applyPendingStates in the presence of deferred transactions.
1728 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001729 float tx = c.active.transform.tx();
1730 float ty = c.active.transform.ty();
1731 c.active = c.requested;
1732 c.active.transform.set(tx, ty);
1733 editCurrentState.active = c.active;
1734 } else {
1735 editCurrentState.active = editCurrentState.requested;
1736 c.active = c.requested;
1737 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001738 }
1739
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001740 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001741 // invalidate and recompute the visible regions if needed
1742 flags |= Layer::eVisibleRegion;
1743 }
1744
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001745 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001746 // invalidate and recompute the visible regions if needed
1747 flags |= eVisibleRegion;
1748 this->contentDirty = true;
1749
1750 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001751 const uint8_t type = c.active.transform.getType();
1752 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001753 (type >= Transform::SCALE));
1754 }
1755
Dan Stozac8145172016-04-28 16:29:06 -07001756 // If the layer is hidden, signal and clear out all local sync points so
1757 // that transactions for layers depending on this layer's frames becoming
1758 // visible are not blocked
1759 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001761 }
1762
Mathias Agopian13127d82013-03-05 17:47:11 -08001763 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001764 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001765 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766}
1767
Pablo Ceballos05289c22016-04-14 15:49:55 -07001768void Layer::commitTransaction(const State& stateToCommit) {
1769 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001770}
1771
Mathias Agopian13127d82013-03-05 17:47:11 -08001772uint32_t Layer::getTransactionFlags(uint32_t flags) {
1773 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1774}
1775
1776uint32_t Layer::setTransactionFlags(uint32_t flags) {
1777 return android_atomic_or(flags, &mTransactionFlags);
1778}
1779
Robert Carr82364e32016-05-15 11:27:47 -07001780bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001781 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001782 return false;
1783 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001784
1785 // We update the requested and active position simultaneously because
1786 // we want to apply the position portion of the transform matrix immediately,
1787 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001788 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001789 if (immediate && !mFreezeGeometryUpdates) {
1790 // Here we directly update the active state
1791 // unlike other setters, because we store it within
1792 // the transform, but use different latching rules.
1793 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001794 mCurrentState.active.transform.set(x, y);
1795 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001796 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001797
Dan Stoza7dde5992015-05-22 09:51:44 -07001798 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001799 setTransactionFlags(eTransactionNeeded);
1800 return true;
1801}
Robert Carr82364e32016-05-15 11:27:47 -07001802
Robert Carr1f0a16a2016-10-24 16:27:39 -07001803bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1804 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1805 if (idx < 0) {
1806 return false;
1807 }
1808 if (childLayer->setLayer(z)) {
1809 mCurrentChildren.removeAt(idx);
1810 mCurrentChildren.add(childLayer);
1811 }
1812 return true;
1813}
1814
Robert Carrae060832016-11-28 10:51:00 -08001815bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001816 if (mCurrentState.z == z)
1817 return false;
1818 mCurrentState.sequence++;
1819 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001820 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001821
1822 // Discard all relative layering.
1823 if (mCurrentState.zOrderRelativeOf != nullptr) {
1824 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1825 if (strongRelative != nullptr) {
1826 strongRelative->removeZOrderRelative(this);
1827 }
1828 mCurrentState.zOrderRelativeOf = nullptr;
1829 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001830 setTransactionFlags(eTransactionNeeded);
1831 return true;
1832}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001833
Robert Carrdb66e622017-04-10 16:55:57 -07001834void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1835 mCurrentState.zOrderRelatives.remove(relative);
1836 mCurrentState.sequence++;
1837 mCurrentState.modified = true;
1838 setTransactionFlags(eTransactionNeeded);
1839}
1840
1841void Layer::addZOrderRelative(const wp<Layer>& relative) {
1842 mCurrentState.zOrderRelatives.add(relative);
1843 mCurrentState.modified = true;
1844 mCurrentState.sequence++;
1845 setTransactionFlags(eTransactionNeeded);
1846}
1847
1848bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1849 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1850 if (handle == nullptr) {
1851 return false;
1852 }
1853 sp<Layer> relative = handle->owner.promote();
1854 if (relative == nullptr) {
1855 return false;
1856 }
1857
1858 mCurrentState.sequence++;
1859 mCurrentState.modified = true;
1860 mCurrentState.z = z;
1861
1862 mCurrentState.zOrderRelativeOf = relative;
1863 relative->addZOrderRelative(this);
1864
1865 setTransactionFlags(eTransactionNeeded);
1866
1867 return true;
1868}
1869
Mathias Agopian13127d82013-03-05 17:47:11 -08001870bool Layer::setSize(uint32_t w, uint32_t h) {
1871 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1872 return false;
1873 mCurrentState.requested.w = w;
1874 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001875 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001876 setTransactionFlags(eTransactionNeeded);
1877 return true;
1878}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001879#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001880bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001881#else
1882bool Layer::setAlpha(uint8_t alpha) {
1883#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001884 if (mCurrentState.alpha == alpha)
1885 return false;
1886 mCurrentState.sequence++;
1887 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001888 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001889 setTransactionFlags(eTransactionNeeded);
1890 return true;
1891}
1892bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1893 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001894 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001895 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001896 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001897 setTransactionFlags(eTransactionNeeded);
1898 return true;
1899}
1900bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001901 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001902 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001903 setTransactionFlags(eTransactionNeeded);
1904 return true;
1905}
1906bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1907 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1908 if (mCurrentState.flags == newFlags)
1909 return false;
1910 mCurrentState.sequence++;
1911 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001912 mCurrentState.mask = mask;
1913 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001914 setTransactionFlags(eTransactionNeeded);
1915 return true;
1916}
Robert Carr99e27f02016-06-16 15:18:02 -07001917
1918bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001919 if (mCurrentState.requestedCrop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001920 return false;
1921 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001922 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001923 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001924 mCurrentState.crop = crop;
1925 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001926 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1927
Dan Stoza7dde5992015-05-22 09:51:44 -07001928 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001929 setTransactionFlags(eTransactionNeeded);
1930 return true;
1931}
Robert Carr8d5227b2017-03-16 15:41:03 -07001932
1933bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001934 if (mCurrentState.requestedFinalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001935 return false;
1936 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001937 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001938 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001939 mCurrentState.finalCrop = crop;
1940 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001941 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1942
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001943 mCurrentState.modified = true;
1944 setTransactionFlags(eTransactionNeeded);
1945 return true;
1946}
Mathias Agopian13127d82013-03-05 17:47:11 -08001947
Robert Carrc3574f72016-03-24 12:19:32 -07001948bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1949 if (scalingMode == mOverrideScalingMode)
1950 return false;
1951 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001952 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001953 return true;
1954}
1955
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001956void Layer::setInfo(uint32_t type, uint32_t appId) {
1957 mCurrentState.appId = appId;
1958 mCurrentState.type = type;
1959 mCurrentState.modified = true;
1960 setTransactionFlags(eTransactionNeeded);
1961}
1962
Robert Carrc3574f72016-03-24 12:19:32 -07001963uint32_t Layer::getEffectiveScalingMode() const {
1964 if (mOverrideScalingMode >= 0) {
1965 return mOverrideScalingMode;
1966 }
1967 return mCurrentScalingMode;
1968}
1969
Mathias Agopian13127d82013-03-05 17:47:11 -08001970bool Layer::setLayerStack(uint32_t layerStack) {
1971 if (mCurrentState.layerStack == layerStack)
1972 return false;
1973 mCurrentState.sequence++;
1974 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001975 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001976 setTransactionFlags(eTransactionNeeded);
1977 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001978}
1979
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001980bool Layer::setDataSpace(android_dataspace dataSpace) {
1981 if (mCurrentState.dataSpace == dataSpace)
1982 return false;
1983 mCurrentState.sequence++;
1984 mCurrentState.dataSpace = dataSpace;
1985 mCurrentState.modified = true;
1986 setTransactionFlags(eTransactionNeeded);
1987 return true;
1988}
1989
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001990android_dataspace Layer::getDataSpace() const {
1991 return mCurrentState.dataSpace;
1992}
1993
Robert Carr1f0a16a2016-10-24 16:27:39 -07001994uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001995 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001996 if (p == nullptr) {
1997 return getDrawingState().layerStack;
1998 }
1999 return p->getLayerStack();
2000}
2001
Robert Carr0d480722017-01-10 16:42:54 -08002002void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07002003 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08002004 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07002005 mCurrentState.frameNumber = frameNumber;
2006 // We don't set eTransactionNeeded, because just receiving a deferral
2007 // request without any other state updates shouldn't actually induce a delay
2008 mCurrentState.modified = true;
2009 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08002010 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08002011 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07002012 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08002013}
2014
2015void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
2016 uint64_t frameNumber) {
2017 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
2018 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07002019}
2020
Dan Stozaee44edd2015-03-23 15:50:23 -07002021void Layer::useSurfaceDamage() {
2022 if (mFlinger->mForceFullDamage) {
2023 surfaceDamageRegion = Region::INVALID_REGION;
2024 } else {
2025 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
2026 }
2027}
2028
2029void Layer::useEmptyDamage() {
2030 surfaceDamageRegion.clear();
2031}
2032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002033// ----------------------------------------------------------------------------
2034// pageflip handling...
2035// ----------------------------------------------------------------------------
2036
Dan Stoza6b9454d2014-11-07 16:00:59 -08002037bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002038 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07002039 return true;
2040 }
2041
Dan Stoza6b9454d2014-11-07 16:00:59 -08002042 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002043 if (mQueueItems.empty()) {
2044 return false;
2045 }
2046 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002047 nsecs_t expectedPresent =
2048 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002049
2050 // Ignore timestamps more than a second in the future
2051 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
2052 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
2053 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
2054 expectedPresent);
2055
2056 bool isDue = timestamp < expectedPresent;
2057 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002058}
2059
Brian Andersond6927fb2016-07-23 23:37:30 -07002060bool Layer::onPreComposition(nsecs_t refreshStartTime) {
2061 if (mBufferLatched) {
2062 Mutex::Autolock lock(mFrameEventHistoryMutex);
2063 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
2064 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002065 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002066 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002067}
2068
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002069bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07002070 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002071 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002072 // mFrameLatencyNeeded is true when a new frame was latched for the
2073 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08002074 if (!mFrameLatencyNeeded)
2075 return false;
2076
Fabien Sanglard28e98082016-12-05 10:19:46 -08002077 // Update mFrameEventHistory.
2078 {
2079 Mutex::Autolock lock(mFrameEventHistoryMutex);
2080 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002081 glDoneFence, presentFence, compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002082 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08002083
2084 // Update mFrameTracker.
2085 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
2086 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
2087
Brian Anderson3d4039d2016-09-23 16:31:30 -07002088 std::shared_ptr<FenceTime> frameReadyFence =
2089 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08002090 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002091 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002092 } else {
2093 // There was no fence for this frame, so assume that it was ready
2094 // to be presented at the desired present time.
2095 mFrameTracker.setFrameReadyTime(desiredPresentTime);
2096 }
2097
Brian Anderson3d4039d2016-09-23 16:31:30 -07002098 if (presentFence->isValid()) {
2099 mFrameTracker.setActualPresentFence(
2100 std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002101 } else {
2102 // The HWC doesn't support present fences, so use the refresh
2103 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002104 mFrameTracker.setActualPresentTime(
2105 mFlinger->getHwComposer().getRefreshTimestamp(
2106 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002107 }
2108
2109 mFrameTracker.advanceFrame();
2110 mFrameLatencyNeeded = false;
2111 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002112}
2113
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002114#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07002115void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08002116 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
2117 return;
2118 }
2119
Brian Anderson3d4039d2016-09-23 16:31:30 -07002120 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002121 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002122 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002123 mReleaseTimeline.push(releaseFenceTime);
2124
2125 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002126 if (mPreviousFrameNumber != 0) {
2127 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2128 dequeueReadyTime, std::move(releaseFenceTime));
2129 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002130}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002131#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08002132
Robert Carr1f0a16a2016-10-24 16:27:39 -07002133bool Layer::isHiddenByPolicy() const {
2134 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07002135 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002136 if (parent != nullptr && parent->isHiddenByPolicy()) {
2137 return true;
2138 }
2139 return s.flags & layer_state_t::eLayerHidden;
2140}
2141
Mathias Agopianda27af92012-09-13 18:17:13 -07002142bool Layer::isVisible() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002143#ifdef USE_HWC2
Robert Carr6452f122017-03-21 10:41:29 -07002144 return !(isHiddenByPolicy()) && getAlpha() > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08002145 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002146#else
Robert Carr6452f122017-03-21 10:41:29 -07002147 return !(isHiddenByPolicy()) && getAlpha()
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002148 && (mActiveBuffer != NULL || mSidebandStream != NULL);
2149#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07002150}
2151
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002152bool Layer::allTransactionsSignaled() {
2153 auto headFrameNumber = getHeadFrameNumber();
2154 bool matchingFramesFound = false;
2155 bool allTransactionsApplied = true;
2156 Mutex::Autolock lock(mLocalSyncPointMutex);
2157
2158 for (auto& point : mLocalSyncPoints) {
2159 if (point->getFrameNumber() > headFrameNumber) {
2160 break;
2161 }
2162 matchingFramesFound = true;
2163
2164 if (!point->frameIsAvailable()) {
2165 // We haven't notified the remote layer that the frame for
2166 // this point is available yet. Notify it now, and then
2167 // abort this attempt to latch.
2168 point->setFrameAvailable();
2169 allTransactionsApplied = false;
2170 break;
2171 }
2172
2173 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
2174 }
2175 return !matchingFramesFound || allTransactionsApplied;
2176}
2177
Brian Andersond6927fb2016-07-23 23:37:30 -07002178Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002180 ATRACE_CALL();
2181
Jesse Hall399184a2014-03-03 15:42:54 -08002182 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2183 // mSidebandStreamChanged was true
2184 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002185 if (mSidebandStream != NULL) {
2186 setTransactionFlags(eTransactionNeeded);
2187 mFlinger->setTransactionFlags(eTraversalNeeded);
2188 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002189 recomputeVisibleRegions = true;
2190
2191 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002192 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002193 }
2194
Mathias Agopian4fec8732012-06-29 14:12:52 -07002195 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002196 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2197 return outDirtyRegion;
2198 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002199
Fabien Sanglard223eb912016-10-13 10:15:06 -07002200 // if we've already called updateTexImage() without going through
2201 // a composition step, we have to skip this layer at this point
2202 // because we cannot call updateTeximage() without a corresponding
2203 // compositionComplete() call.
2204 // we'll trigger an update in onPreComposition().
2205 if (mRefreshPending) {
2206 return outDirtyRegion;
2207 }
2208
2209 // If the head buffer's acquire fence hasn't signaled yet, return and
2210 // try again later
2211 if (!headFenceHasSignaled()) {
2212 mFlinger->signalLayerUpdate();
2213 return outDirtyRegion;
2214 }
2215
2216 // Capture the old state of the layer for comparisons later
2217 const State& s(getDrawingState());
2218 const bool oldOpacity = isOpaque(s);
2219 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2220
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002221 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002222 mFlinger->signalLayerUpdate();
2223 return outDirtyRegion;
2224 }
2225
2226 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2227 // of the buffer queue isn't modified when the buffer queue is returning
2228 // BufferItem's that weren't actually queued. This can happen in shared
2229 // buffer mode.
2230 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002231 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2232 getProducerStickyTransform() != 0, mName.string(),
Robert Carr7bf247e2017-05-18 14:02:49 -07002233 mOverrideScalingMode, mFreezeGeometryUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002234 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2235 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2236 mLastFrameNumberReceived);
2237 if (updateResult == BufferQueue::PRESENT_LATER) {
2238 // Producer doesn't want buffer to be displayed yet. Signal a
2239 // layer update so we check again at the next opportunity.
2240 mFlinger->signalLayerUpdate();
2241 return outDirtyRegion;
2242 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2243 // If the buffer has been rejected, remove it from the shadow queue
2244 // and return early
2245 if (queuedBuffer) {
2246 Mutex::Autolock lock(mQueueItemLock);
2247 mQueueItems.removeAt(0);
2248 android_atomic_dec(&mQueuedFrames);
2249 }
2250 return outDirtyRegion;
2251 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2252 // This can occur if something goes wrong when trying to create the
2253 // EGLImage for this buffer. If this happens, the buffer has already
2254 // been released, so we need to clean up the queue and bug out
2255 // early.
2256 if (queuedBuffer) {
2257 Mutex::Autolock lock(mQueueItemLock);
2258 mQueueItems.clear();
2259 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002260 }
2261
Fabien Sanglard223eb912016-10-13 10:15:06 -07002262 // Once we have hit this state, the shadow queue may no longer
2263 // correctly reflect the incoming BufferQueue's contents, so even if
2264 // updateTexImage starts working, the only safe course of action is
2265 // to continue to ignore updates.
2266 mUpdateTexImageFailed = true;
2267
2268 return outDirtyRegion;
2269 }
2270
2271 if (queuedBuffer) {
2272 // Autolock scope
2273 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2274
2275 Mutex::Autolock lock(mQueueItemLock);
2276
2277 // Remove any stale buffers that have been dropped during
2278 // updateTexImage
2279 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2280 mQueueItems.removeAt(0);
2281 android_atomic_dec(&mQueuedFrames);
2282 }
2283
2284 mQueueItems.removeAt(0);
2285 }
2286
2287
2288 // Decrement the queued-frames count. Signal another event if we
2289 // have more frames pending.
2290 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2291 || mAutoRefresh) {
2292 mFlinger->signalLayerUpdate();
2293 }
2294
Fabien Sanglard223eb912016-10-13 10:15:06 -07002295 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002296 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2297 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002298 if (mActiveBuffer == NULL) {
2299 // this can only happen if the very first buffer was rejected.
2300 return outDirtyRegion;
2301 }
2302
Brian Andersond6927fb2016-07-23 23:37:30 -07002303 mBufferLatched = true;
2304 mPreviousFrameNumber = mCurrentFrameNumber;
2305 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2306
2307 {
2308 Mutex::Autolock lock(mFrameEventHistoryMutex);
2309 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2310#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002311 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002312 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002313 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002314 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002315 if (mPreviousFrameNumber != 0) {
2316 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2317 latchTime, std::move(releaseFenceTime));
2318 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002319#endif
2320 }
2321
Fabien Sanglard223eb912016-10-13 10:15:06 -07002322 mRefreshPending = true;
2323 mFrameLatencyNeeded = true;
2324 if (oldActiveBuffer == NULL) {
2325 // the first time we receive a buffer, we need to trigger a
2326 // geometry invalidation.
2327 recomputeVisibleRegions = true;
2328 }
2329
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002330 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2331
Fabien Sanglard223eb912016-10-13 10:15:06 -07002332 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2333 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2334 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2335 if ((crop != mCurrentCrop) ||
2336 (transform != mCurrentTransform) ||
2337 (scalingMode != mCurrentScalingMode))
2338 {
2339 mCurrentCrop = crop;
2340 mCurrentTransform = transform;
2341 mCurrentScalingMode = scalingMode;
2342 recomputeVisibleRegions = true;
2343 }
2344
2345 if (oldActiveBuffer != NULL) {
2346 uint32_t bufWidth = mActiveBuffer->getWidth();
2347 uint32_t bufHeight = mActiveBuffer->getHeight();
2348 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2349 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002350 recomputeVisibleRegions = true;
2351 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002352 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002353
Fabien Sanglard223eb912016-10-13 10:15:06 -07002354 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2355 if (oldOpacity != isOpaque(s)) {
2356 recomputeVisibleRegions = true;
2357 }
Dan Stozacac35382016-01-27 12:21:06 -08002358
Fabien Sanglard223eb912016-10-13 10:15:06 -07002359 // Remove any sync points corresponding to the buffer which was just
2360 // latched
2361 {
2362 Mutex::Autolock lock(mLocalSyncPointMutex);
2363 auto point = mLocalSyncPoints.begin();
2364 while (point != mLocalSyncPoints.end()) {
2365 if (!(*point)->frameIsAvailable() ||
2366 !(*point)->transactionIsApplied()) {
2367 // This sync point must have been added since we started
2368 // latching. Don't drop it yet.
2369 ++point;
2370 continue;
2371 }
2372
2373 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2374 point = mLocalSyncPoints.erase(point);
2375 } else {
2376 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002377 }
2378 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002379 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002380
2381 // FIXME: postedRegion should be dirty & bounds
2382 Region dirtyRegion(Rect(s.active.w, s.active.h));
2383
2384 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002385 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002386
Mathias Agopian4fec8732012-06-29 14:12:52 -07002387 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002388}
2389
Mathias Agopiana67932f2011-04-20 14:20:59 -07002390uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002391{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002392 // TODO: should we do something special if mSecure is set?
2393 if (mProtectedByApp) {
2394 // need a hardware-protected path to external video sink
2395 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002396 }
Riley Andrews03414a12014-07-01 14:22:59 -07002397 if (mPotentialCursor) {
2398 usage |= GraphicBuffer::USAGE_CURSOR;
2399 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002400 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002401 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002402}
2403
Mathias Agopian84300952012-11-21 16:02:13 -08002404void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002405 uint32_t orientation = 0;
2406 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002407 // The transform hint is used to improve performance, but we can
2408 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002409 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002410 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002411 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002412 if (orientation & Transform::ROT_INVALID) {
2413 orientation = 0;
2414 }
2415 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002416 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002417}
2418
Mathias Agopian13127d82013-03-05 17:47:11 -08002419// ----------------------------------------------------------------------------
2420// debugging
2421// ----------------------------------------------------------------------------
2422
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002423void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002424{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002425 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002426
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002427 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002428 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002429 "+ %s %p (%s)\n",
2430 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002431 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002432
Mathias Agopian2ca79392013-04-02 18:30:32 -07002433 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002434 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002435 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002436 sp<Client> client(mClientRef.promote());
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -06002437 PixelFormat pf = PIXEL_FORMAT_UNKNOWN;
2438 const sp<GraphicBuffer>& buffer(getActiveBuffer());
2439 if (buffer != NULL) {
2440 pf = buffer->getPixelFormat();
2441 }
Mathias Agopian13127d82013-03-05 17:47:11 -08002442
Mathias Agopian74d211a2013-04-22 16:55:35 +02002443 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002444 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2445 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002446 "isOpaque=%1d, invalidate=%1d, "
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -06002447 "dataspace=%s, pixelformat=%s "
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002448#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08002449 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002450#else
2451 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2452#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002453 " client=%p\n",
Robert Carr1f0a16a2016-10-24 16:27:39 -07002454 getLayerStack(), s.z,
2455 s.active.transform.tx(), s.active.transform.ty(),
2456 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07002457 s.crop.left, s.crop.top,
2458 s.crop.right, s.crop.bottom,
2459 s.finalCrop.left, s.finalCrop.top,
2460 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002461 isOpaque(s), contentDirty,
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -06002462 dataspaceDetails(getDataSpace()).c_str(), decodePixelFormat(pf).c_str(),
Mathias Agopian13127d82013-03-05 17:47:11 -08002463 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002464 s.active.transform[0][0], s.active.transform[0][1],
2465 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002466 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002467
2468 sp<const GraphicBuffer> buf0(mActiveBuffer);
2469 uint32_t w0=0, h0=0, s0=0, f0=0;
2470 if (buf0 != 0) {
2471 w0 = buf0->getWidth();
2472 h0 = buf0->getHeight();
2473 s0 = buf0->getStride();
2474 f0 = buf0->format;
2475 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002476 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002477 " "
2478 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2479 " queued-frames=%d, mRefreshPending=%d\n",
2480 mFormat, w0, h0, s0,f0,
2481 mQueuedFrames, mRefreshPending);
2482
Mathias Agopian13127d82013-03-05 17:47:11 -08002483 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002484 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002485 }
2486}
2487
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002488#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002489void Layer::miniDumpHeader(String8& result) {
2490 result.append("----------------------------------------");
2491 result.append("---------------------------------------\n");
2492 result.append(" Layer name\n");
2493 result.append(" Z | ");
2494 result.append(" Comp Type | ");
2495 result.append(" Disp Frame (LTRB) | ");
2496 result.append(" Source Crop (LTRB)\n");
2497 result.append("----------------------------------------");
2498 result.append("---------------------------------------\n");
2499}
2500
2501void Layer::miniDump(String8& result, int32_t hwcId) const {
2502 if (mHwcLayers.count(hwcId) == 0) {
2503 return;
2504 }
2505
2506 String8 name;
2507 if (mName.length() > 77) {
2508 std::string shortened;
2509 shortened.append(mName.string(), 36);
2510 shortened.append("[...]");
2511 shortened.append(mName.string() + (mName.length() - 36), 36);
2512 name = shortened.c_str();
2513 } else {
2514 name = mName;
2515 }
2516
2517 result.appendFormat(" %s\n", name.string());
2518
2519 const Layer::State& layerState(getDrawingState());
2520 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2521 result.appendFormat(" %10u | ", layerState.z);
2522 result.appendFormat("%10s | ",
2523 to_string(getCompositionType(hwcId)).c_str());
2524 const Rect& frame = hwcInfo.displayFrame;
2525 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2526 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002527 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002528 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2529 crop.right, crop.bottom);
2530
2531 result.append("- - - - - - - - - - - - - - - - - - - - ");
2532 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2533}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002534#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002535
Svetoslavd85084b2014-03-20 10:28:31 -07002536void Layer::dumpFrameStats(String8& result) const {
2537 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002538}
2539
Svetoslavd85084b2014-03-20 10:28:31 -07002540void Layer::clearFrameStats() {
2541 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002542}
2543
Jamie Gennis6547ff42013-07-16 20:12:42 -07002544void Layer::logFrameStats() {
2545 mFrameTracker.logAndResetStats(mName);
2546}
2547
Svetoslavd85084b2014-03-20 10:28:31 -07002548void Layer::getFrameStats(FrameStats* outStats) const {
2549 mFrameTracker.getStats(outStats);
2550}
2551
Brian Andersond6927fb2016-07-23 23:37:30 -07002552void Layer::dumpFrameEvents(String8& result) {
2553 result.appendFormat("- Layer %s (%s, %p)\n",
2554 getName().string(), getTypeId(), this);
2555 Mutex::Autolock lock(mFrameEventHistoryMutex);
2556 mFrameEventHistory.checkFencesForCompletion();
2557 mFrameEventHistory.dump(result);
2558}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002559
Brian Anderson5ea5e592016-12-01 16:54:33 -08002560void Layer::onDisconnect() {
2561 Mutex::Autolock lock(mFrameEventHistoryMutex);
2562 mFrameEventHistory.onDisconnect();
2563}
2564
Brian Anderson3890c392016-07-25 12:48:08 -07002565void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2566 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002567 Mutex::Autolock lock(mFrameEventHistoryMutex);
2568 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002569 // If there are any unsignaled fences in the aquire timeline at this
2570 // point, the previously queued frame hasn't been latched yet. Go ahead
2571 // and try to get the signal time here so the syscall is taken out of
2572 // the main thread's critical path.
2573 mAcquireTimeline.updateSignalTimes();
2574 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002575 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002576 mFrameEventHistory.addQueue(*newTimestamps);
2577 }
2578
Brian Anderson3890c392016-07-25 12:48:08 -07002579 if (outDelta) {
2580 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002581 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002582}
Dan Stozae77c7662016-05-13 11:37:28 -07002583
2584std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2585 bool forceFlush) {
2586 std::vector<OccupancyTracker::Segment> history;
2587 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2588 &history);
2589 if (result != NO_ERROR) {
2590 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2591 result);
2592 return {};
2593 }
2594 return history;
2595}
2596
Robert Carr367c5682016-06-20 11:55:28 -07002597bool Layer::getTransformToDisplayInverse() const {
2598 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2599}
2600
Chia-I Wu98f1c102017-05-30 14:54:08 -07002601size_t Layer::getChildrenCount() const {
2602 size_t count = 0;
2603 for (const sp<Layer>& child : mCurrentChildren) {
2604 count += 1 + child->getChildrenCount();
2605 }
2606 return count;
2607}
2608
Robert Carr1f0a16a2016-10-24 16:27:39 -07002609void Layer::addChild(const sp<Layer>& layer) {
2610 mCurrentChildren.add(layer);
2611 layer->setParent(this);
2612}
2613
2614ssize_t Layer::removeChild(const sp<Layer>& layer) {
2615 layer->setParent(nullptr);
2616 return mCurrentChildren.remove(layer);
2617}
2618
Robert Carr1db73f62016-12-21 12:58:51 -08002619bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2620 sp<Handle> handle = nullptr;
2621 sp<Layer> newParent = nullptr;
2622 if (newParentHandle == nullptr) {
2623 return false;
2624 }
2625 handle = static_cast<Handle*>(newParentHandle.get());
2626 newParent = handle->owner.promote();
2627 if (newParent == nullptr) {
2628 ALOGE("Unable to promote Layer handle");
2629 return false;
2630 }
2631
2632 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002633 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08002634
2635 sp<Client> client(child->mClientRef.promote());
2636 if (client != nullptr) {
2637 client->setParentLayer(newParent);
2638 }
2639 }
2640 mCurrentChildren.clear();
2641
2642 return true;
2643}
2644
Robert Carr9524cb32017-02-13 11:32:32 -08002645bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07002646 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08002647 if (child == this) {
2648 return;
2649 }
2650
2651 sp<Client> client(child->mClientRef.promote());
2652 if (client != nullptr) {
2653 client->detachLayer(child);
2654 }
2655 });
2656
2657 return true;
2658}
2659
Robert Carr1f0a16a2016-10-24 16:27:39 -07002660void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002661 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002662}
2663
2664void Layer::clearSyncPoints() {
2665 for (const auto& child : mCurrentChildren) {
2666 child->clearSyncPoints();
2667 }
2668
2669 Mutex::Autolock lock(mLocalSyncPointMutex);
2670 for (auto& point : mLocalSyncPoints) {
2671 point->setFrameAvailable();
2672 }
2673 mLocalSyncPoints.clear();
2674}
2675
2676int32_t Layer::getZ() const {
2677 return mDrawingState.z;
2678}
2679
Dan Stoza412903f2017-04-27 13:42:17 -07002680LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) {
2681 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
2682 "makeTraversalList received invalid stateSet");
2683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2684 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2685 const State& state = useDrawing ? mDrawingState : mCurrentState;
2686
2687 if (state.zOrderRelatives.size() == 0) {
2688 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07002689 }
2690 LayerVector traverse;
2691
Dan Stoza412903f2017-04-27 13:42:17 -07002692 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07002693 sp<Layer> strongRelative = weakRelative.promote();
2694 if (strongRelative != nullptr) {
2695 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07002696 }
2697 }
2698
Dan Stoza412903f2017-04-27 13:42:17 -07002699 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07002700 traverse.add(child);
2701 }
2702
2703 return traverse;
2704}
2705
Robert Carr1f0a16a2016-10-24 16:27:39 -07002706/**
Robert Carrdb66e622017-04-10 16:55:57 -07002707 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002708 */
Dan Stoza412903f2017-04-27 13:42:17 -07002709void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
2710 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002711
Robert Carr1f0a16a2016-10-24 16:27:39 -07002712 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002713 for (; i < list.size(); i++) {
2714 const auto& relative = list[i];
2715 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002716 break;
Robert Carrdb66e622017-04-10 16:55:57 -07002717 }
Dan Stoza412903f2017-04-27 13:42:17 -07002718 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002719 }
Dan Stoza412903f2017-04-27 13:42:17 -07002720 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07002721 for (; i < list.size(); i++) {
2722 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002723 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002724 }
2725}
2726
2727/**
Robert Carrdb66e622017-04-10 16:55:57 -07002728 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002729 */
Dan Stoza412903f2017-04-27 13:42:17 -07002730void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
2731 const LayerVector::Visitor& visitor) {
2732 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002733
Robert Carr1f0a16a2016-10-24 16:27:39 -07002734 int32_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002735 for (i = list.size()-1; i>=0; i--) {
2736 const auto& relative = list[i];
2737 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002738 break;
2739 }
Dan Stoza412903f2017-04-27 13:42:17 -07002740 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002741 }
Dan Stoza412903f2017-04-27 13:42:17 -07002742 visitor(this);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002743 for (; i>=0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002744 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002745 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002746 }
2747}
2748
2749Transform Layer::getTransform() const {
2750 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002751 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002752 if (p != nullptr) {
2753 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002754
2755 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2756 // it isFixedSize) then there may be additional scaling not accounted
2757 // for in the transform. We need to mirror this scaling in child surfaces
2758 // or we will break the contract where WM can treat child surfaces as
2759 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002760 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002761 int bufferWidth;
2762 int bufferHeight;
2763 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2764 bufferWidth = p->mActiveBuffer->getWidth();
2765 bufferHeight = p->mActiveBuffer->getHeight();
2766 } else {
2767 bufferHeight = p->mActiveBuffer->getWidth();
2768 bufferWidth = p->mActiveBuffer->getHeight();
2769 }
Robert Carr9b429f42017-04-17 14:56:57 -07002770 float sx = p->getDrawingState().active.w /
Robert Carr1725eee2017-04-26 18:32:15 -07002771 static_cast<float>(bufferWidth);
Robert Carr9b429f42017-04-17 14:56:57 -07002772 float sy = p->getDrawingState().active.h /
Robert Carr1725eee2017-04-26 18:32:15 -07002773 static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002774 Transform extraParentScaling;
2775 extraParentScaling.set(sx, 0, 0, sy);
2776 t = t * extraParentScaling;
2777 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002778 }
2779 return t * getDrawingState().active.transform;
2780}
2781
Robert Carr6452f122017-03-21 10:41:29 -07002782#ifdef USE_HWC2
2783float Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002784 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002785
2786 float parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0;
2787 return parentAlpha * getDrawingState().alpha;
2788}
2789#else
2790uint8_t Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002791 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002792
2793 float parentAlpha = (p != nullptr) ? (p->getAlpha() / 255.0f) : 1.0;
2794 float drawingAlpha = getDrawingState().alpha / 255.0f;
2795 drawingAlpha = drawingAlpha * parentAlpha;
2796 return static_cast<uint8_t>(std::round(drawingAlpha * 255));
2797}
2798#endif
2799
Robert Carr1f0a16a2016-10-24 16:27:39 -07002800void Layer::commitChildList() {
2801 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2802 const auto& child = mCurrentChildren[i];
2803 child->commitChildList();
2804 }
2805 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002806 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002807}
2808
Mathias Agopian13127d82013-03-05 17:47:11 -08002809// ---------------------------------------------------------------------------
2810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002811}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002812
2813#if defined(__gl_h_)
2814#error "don't include gl/gl.h in this file"
2815#endif
2816
2817#if defined(__gl2_h_)
2818#error "don't include gl2/gl2.h in this file"
2819#endif