blob: 9cc1021057a29931871feeedd6b1356fce1ba7be [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <gui/BufferQueue.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#define DEBUG_RESIZE 0
61
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062namespace android {
63
64// ---------------------------------------------------------------------------
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
Mathias Agopian4d9b8222013-03-12 17:11:48 -070068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
69 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080070 : contentDirty(false),
71 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mPremultipliedAlpha(true),
75 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080076 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080077 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070078 mPendingStateMutex(),
79 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080081 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080082 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070084 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070085 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 mCurrentOpacity(true),
Brian Andersond6927fb2016-07-23 23:37:30 -070087 mBufferLatched(false),
Dan Stozacac35382016-01-27 12:21:06 -080088 mCurrentFrameNumber(0),
Brian Anderson8cc8b102016-10-21 12:43:09 -070089 mPreviousFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080090 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080091 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mFiltering(false),
93 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070094 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000095#ifndef USE_HWC2
96 mIsGlesComposition(false),
97#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080098 mProtectedByApp(false),
99 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -0700100 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -0700101 mPotentialCursor(false),
102 mQueueItemLock(),
103 mQueueItemCondition(),
104 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700105 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -0800106 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700107 mAutoRefresh(false),
Robert Carr7bf247e2017-05-18 14:02:49 -0700108 mFreezeGeometryUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000110#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111 ALOGV("Creating Layer %s", name.string());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000112#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800113
Mathias Agopiana67932f2011-04-20 14:20:59 -0700114 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700115 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700116 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117
118 uint32_t layerFlags = 0;
119 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800120 layerFlags |= layer_state_t::eLayerHidden;
121 if (flags & ISurfaceComposerClient::eOpaque)
122 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700123 if (flags & ISurfaceComposerClient::eSecure)
124 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125
126 if (flags & ISurfaceComposerClient::eNonPremultiplied)
127 mPremultipliedAlpha = false;
128
129 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700130 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131
132 mCurrentState.active.w = w;
133 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800134 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700135 mCurrentState.crop.makeInvalid();
136 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700137 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
138 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139 mCurrentState.z = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000140#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800141 mCurrentState.alpha = 1.0f;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000142#else
143 mCurrentState.alpha = 0xFF;
144#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145 mCurrentState.layerStack = 0;
146 mCurrentState.flags = layerFlags;
147 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700149 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500150 mCurrentState.appId = 0;
151 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700152
153 // drawing state & current state are identical
154 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700155
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000156#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800157 const auto& hwc = flinger->getHwComposer();
158 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
159 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000160#else
161 nsecs_t displayPeriod =
162 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
163#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700164 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800165
166 CompositorTiming compositorTiming;
167 flinger->getCompositorTiming(&compositorTiming);
168 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800169}
170
Mathias Agopian3f844832013-08-07 21:24:32 -0700171void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800172 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700173 sp<IGraphicBufferProducer> producer;
174 sp<IGraphicBufferConsumer> consumer;
Mathias Agopian0556d792017-03-22 15:49:32 -0700175 BufferQueue::createBufferQueue(&producer, &consumer, true);
Robert Carr1db73f62016-12-21 12:58:51 -0800176 mProducer = new MonitoredProducer(producer, mFlinger, this);
Irvel468051e2016-06-13 16:44:44 -0700177 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800178 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800179 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700180 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800181
Fabien Sanglard63a5fcd2016-12-29 15:13:07 -0800182 if (mFlinger->isLayerTripleBufferingDisabled()) {
183 mProducer->setMaxDequeuedBufferCount(2);
184 }
Andy McFadden69052052012-09-14 16:10:11 -0700185
Mathias Agopian84300952012-11-21 16:02:13 -0800186 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
187 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700188}
189
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700190Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800191 sp<Client> c(mClientRef.promote());
192 if (c != 0) {
193 c->detachLayer(this);
194 }
195
Dan Stozacac35382016-01-27 12:21:06 -0800196 for (auto& point : mRemoteSyncPoints) {
197 point->setTransactionApplied();
198 }
Dan Stozac8145172016-04-28 16:29:06 -0700199 for (auto& point : mLocalSyncPoints) {
200 point->setFrameAvailable();
201 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700202 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700203 mFrameTracker.logAndResetStats(mName);
Steven Thomasb02664d2017-07-26 18:48:28 -0700204
205#ifdef USE_HWC2
Chia-I Wuc6657022017-08-15 11:18:17 -0700206 if (!mHwcLayers.empty()) {
207 ALOGE("Found stale hardware composer layers when destroying "
208 "surface flinger layer %s", mName.string());
209 destroyAllHwcLayers();
210 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700211#endif
Mathias Agopian96f08192010-06-02 23:28:45 -0700212}
213
Mathias Agopian13127d82013-03-05 17:47:11 -0800214// ---------------------------------------------------------------------------
215// callbacks
216// ---------------------------------------------------------------------------
217
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000218#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
220 if (mHwcLayers.empty()) {
221 return;
222 }
223 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
224}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000225#else
226void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
227 HWComposer::HWCLayerInterface* layer) {
228 if (layer) {
229 layer->onDisplayed();
230 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
231 }
232}
233#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800234
Dan Stoza6b9454d2014-11-07 16:00:59 -0800235void Layer::onFrameAvailable(const BufferItem& item) {
236 // Add this buffer from our internal queue tracker
237 { // Autolock scope
238 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700239 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
240 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700241 // Reset the frame number tracker when we receive the first buffer after
242 // a frame number reset
243 if (item.mFrameNumber == 1) {
244 mLastFrameNumberReceived = 0;
245 }
246
247 // Ensure that callbacks are handled in order
248 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
249 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
250 ms2ns(500));
251 if (result != NO_ERROR) {
252 ALOGE("[%s] Timed out waiting on callback", mName.string());
253 }
254 }
255
Dan Stoza6b9454d2014-11-07 16:00:59 -0800256 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700257 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700258
259 // Wake up any pending callbacks
260 mLastFrameNumberReceived = item.mFrameNumber;
261 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800262 }
263
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800264 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700265}
266
Dan Stoza6b9454d2014-11-07 16:00:59 -0800267void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700268 { // Autolock scope
269 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700270
Dan Stoza7dde5992015-05-22 09:51:44 -0700271 // Ensure that callbacks are handled in order
272 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
273 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
274 ms2ns(500));
275 if (result != NO_ERROR) {
276 ALOGE("[%s] Timed out waiting on callback", mName.string());
277 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700278 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700279
280 if (mQueueItems.empty()) {
281 ALOGE("Can't replace a frame on an empty queue");
282 return;
283 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700284 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700285
286 // Wake up any pending callbacks
287 mLastFrameNumberReceived = item.mFrameNumber;
288 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700289 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800290}
291
Jesse Hall399184a2014-03-03 15:42:54 -0800292void Layer::onSidebandStreamChanged() {
293 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
294 // mSidebandStreamChanged was false
295 mFlinger->signalLayerUpdate();
296 }
297}
298
Chia-I Wuc6657022017-08-15 11:18:17 -0700299void Layer::onRemovedFromCurrentState() {
300 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
301
Robert Carr5edb1ad2017-04-25 10:54:24 -0700302 if (mCurrentState.zOrderRelativeOf != nullptr) {
303 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
304 if (strongRelative != nullptr) {
305 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700306 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700307 }
308 mCurrentState.zOrderRelativeOf = nullptr;
309 }
310
Chia-I Wuc6657022017-08-15 11:18:17 -0700311 for (const auto& child : mCurrentChildren) {
312 child->onRemovedFromCurrentState();
313 }
314}
Chia-I Wu38512252017-05-17 14:36:16 -0700315
Chia-I Wuc6657022017-08-15 11:18:17 -0700316void Layer::onRemoved() {
317 // the layer is removed from SF mLayersPendingRemoval
318
319 mSurfaceFlingerConsumer->abandon();
Chia-I Wu38512252017-05-17 14:36:16 -0700320#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700321 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700322#endif
323
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324 for (const auto& child : mCurrentChildren) {
325 child->onRemoved();
326 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700327}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700328
Mathias Agopian13127d82013-03-05 17:47:11 -0800329// ---------------------------------------------------------------------------
330// set-up
331// ---------------------------------------------------------------------------
332
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700333const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800334 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335}
336
Mathias Agopianf9d93272009-06-19 17:00:27 -0700337status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 PixelFormat format, uint32_t flags)
339{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700340 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700341 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700342
343 // never allow a surface larger than what our underlying GL implementation
344 // can handle.
345 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800346 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700347 return BAD_VALUE;
348 }
349
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700350 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700351
Riley Andrews03414a12014-07-01 14:22:59 -0700352 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700353 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354 mCurrentOpacity = getOpacityForFormat(format);
355
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800356 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
357 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
358 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 return NO_ERROR;
361}
362
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700363sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800364 Mutex::Autolock _l(mLock);
365
366 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700367 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800368
369 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700370
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700371 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800372}
373
Dan Stozab9b08832014-03-13 11:55:57 -0700374sp<IGraphicBufferProducer> Layer::getProducer() const {
375 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700376}
377
Mathias Agopian13127d82013-03-05 17:47:11 -0800378// ---------------------------------------------------------------------------
379// h/w composer set-up
380// ---------------------------------------------------------------------------
381
Steven Thomasb02664d2017-07-26 18:48:28 -0700382#ifdef USE_HWC2
383bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
384 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
385 "Already have a layer for hwcId %d", hwcId);
386 HWC2::Layer* layer = hwc->createLayer(hwcId);
387 if (!layer) {
388 return false;
389 }
390 HWCInfo& hwcInfo = mHwcLayers[hwcId];
391 hwcInfo.hwc = hwc;
392 hwcInfo.layer = layer;
393 layer->setLayerDestroyedListener(
394 [this, hwcId] (HWC2::Layer* /*layer*/){mHwcLayers.erase(hwcId);});
395 return true;
396}
397
398void Layer::destroyHwcLayer(int32_t hwcId) {
399 if (mHwcLayers.count(hwcId) == 0) {
400 return;
401 }
402 auto& hwcInfo = mHwcLayers[hwcId];
403 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr,
404 "Attempt to destroy null layer");
405 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
406 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
407 // The layer destroyed listener should have cleared the entry from
408 // mHwcLayers. Verify that.
409 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
410 "Stale layer entry in mHwcLayers");
411}
412
413void Layer::destroyAllHwcLayers() {
414 size_t numLayers = mHwcLayers.size();
415 for (size_t i = 0; i < numLayers; ++i) {
416 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
417 destroyHwcLayer(mHwcLayers.begin()->first);
418 }
419 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
420 "All hardware composer layers should have been destroyed");
421}
422#endif
423
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800424Rect Layer::getContentCrop() const {
425 // this is the crop rectangle that applies to the buffer
426 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700427 Rect crop;
428 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800429 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700430 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800431 } else if (mActiveBuffer != NULL) {
432 // otherwise we use the whole buffer
433 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700434 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800435 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700436 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700438 return crop;
439}
440
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700441static Rect reduce(const Rect& win, const Region& exclude) {
442 if (CC_LIKELY(exclude.isEmpty())) {
443 return win;
444 }
445 if (exclude.isRect()) {
446 return win.reduce(exclude.getBounds());
447 }
448 return Region(win).subtract(exclude).getBounds();
449}
450
Robert Carr1f0a16a2016-10-24 16:27:39 -0700451Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
452 const Layer::State& s(getDrawingState());
453 Rect win(s.active.w, s.active.h);
454
455 if (!s.crop.isEmpty()) {
456 win.intersect(s.crop, &win);
457 }
458
459 Transform t = getTransform();
460 win = t.transform(win);
461
Robert Carr41b08b52017-06-01 16:11:34 -0700462 if (!s.finalCrop.isEmpty()) {
463 win.intersect(s.finalCrop, &win);
464 }
465
Chia-I Wue41dbe62017-06-13 14:10:56 -0700466 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700467 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
468 // When calculating the parent bounds for purposes of clipping,
469 // we don't need to constrain the parent to its transparent region.
470 // The transparent region is an optimization based on the
471 // buffer contents of the layer, but does not affect the space allocated to
472 // it by policy, and thus children should be allowed to extend into the
473 // parent's transparent region. In fact one of the main uses, is to reduce
474 // buffer allocation size in cases where a child window sits behind a main window
475 // (by marking the hole in the parent window as a transparent region)
476 if (p != nullptr) {
477 Rect bounds = p->computeScreenBounds(false);
478 bounds.intersect(win, &win);
479 }
480
481 if (reduceTransparentRegion) {
482 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
483 win = reduce(win, screenTransparentRegion);
484 }
485
486 return win;
487}
488
Mathias Agopian13127d82013-03-05 17:47:11 -0800489Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700490 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700491 return computeBounds(s.activeTransparentRegion);
492}
493
494Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
495 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800496 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700497
498 if (!s.crop.isEmpty()) {
499 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800500 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700501
502 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700503 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700504 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800505 // Look in computeScreenBounds recursive call for explanation of
506 // why we pass false here.
507 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508 }
509
510 Transform t = getTransform();
511 if (p != nullptr) {
512 win = t.transform(win);
513 win.intersect(bounds, &win);
514 win = t.inverse().transform(win);
515 }
516
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700517 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700518 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800519}
520
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700522 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800523 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700524 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800525
526 // apply the projection's clipping to the window crop in
527 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700528 // if there are no window scaling involved, this operation will map to full
529 // pixels in the buffer.
530 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
531 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700532
533 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700534 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700535 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700536 }
537
Robert Carr1f0a16a2016-10-24 16:27:39 -0700538 Transform t = getTransform();
539 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000540 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
541 activeCrop.clear();
542 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700543 if (!s.finalCrop.isEmpty()) {
544 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000545 activeCrop.clear();
546 }
547 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700548 return activeCrop;
549}
550
Dan Stoza5a423ea2017-02-16 14:10:39 -0800551FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700552 // the content crop is the area of the content that gets scaled to the
553 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800554 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700555
556 // In addition there is a WM-specified crop we pull from our drawing state.
557 const State& s(getDrawingState());
558
559 // Screen space to make reduction to parent crop clearer.
560 Rect activeCrop = computeInitialCrop(hw);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700561 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700562 if (p != nullptr) {
563 auto parentCrop = p->computeInitialCrop(hw);
564 activeCrop.intersect(parentCrop, &activeCrop);
565 }
566 Transform t = getTransform();
567 // Back to layer space to work with the content crop.
568 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800569
Michael Lentine28ea2172014-11-19 18:32:37 -0800570 // This needs to be here as transform.transform(Rect) computes the
571 // transformed rect and then takes the bounding box of the result before
572 // returning. This means
573 // transform.inverse().transform(transform.transform(Rect)) != Rect
574 // in which case we need to make sure the final rect is clipped to the
575 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000576 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
577 activeCrop.clear();
578 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800579
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700580 // subtract the transparent region and snap to the bounds
581 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
582
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000583 // Transform the window crop to match the buffer coordinate system,
584 // which means using the inverse of the current transform set on the
585 // SurfaceFlingerConsumer.
586 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700587 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000588 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700589 * the code below applies the primary display's inverse transform to the
590 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000591 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700592 uint32_t invTransformOrient =
593 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000594 // calculate the inverse transform
595 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
596 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
597 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800598 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000599 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700600 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
601 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800602 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000603
604 int winWidth = s.active.w;
605 int winHeight = s.active.h;
606 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
607 // If the activeCrop has been rotate the ends are rotated but not
608 // the space itself so when transforming ends back we can't rely on
609 // a modification of the axes of rotation. To account for this we
610 // need to reorient the inverse rotation in terms of the current
611 // axes of rotation.
612 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
613 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
614 if (is_h_flipped == is_v_flipped) {
615 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
616 NATIVE_WINDOW_TRANSFORM_FLIP_H;
617 }
618 winWidth = s.active.h;
619 winHeight = s.active.w;
620 }
621 const Rect winCrop = activeCrop.transform(
622 invTransform, s.active.w, s.active.h);
623
624 // below, crop is intersected with winCrop expressed in crop's coordinate space
625 float xScale = crop.getWidth() / float(winWidth);
626 float yScale = crop.getHeight() / float(winHeight);
627
628 float insetL = winCrop.left * xScale;
629 float insetT = winCrop.top * yScale;
630 float insetR = (winWidth - winCrop.right ) * xScale;
631 float insetB = (winHeight - winCrop.bottom) * yScale;
632
633 crop.left += insetL;
634 crop.top += insetT;
635 crop.right -= insetR;
636 crop.bottom -= insetB;
637
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 return crop;
639}
640
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000641#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800642void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000643#else
644void Layer::setGeometry(
645 const sp<const DisplayDevice>& hw,
646 HWComposer::HWCLayerInterface& layer)
647#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700648{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000649#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 const auto hwcId = displayDevice->getHwcDisplayId();
651 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000652#else
653 layer.setDefaultState();
654#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700655
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700656 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000657#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 hwcInfo.forceClientComposition = false;
659
660 if (isSecure() && !displayDevice->isSecure()) {
661 hwcInfo.forceClientComposition = true;
662 }
663
664 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000665#else
666 layer.setSkip(false);
667
668 if (isSecure() && !hw->isSecure()) {
669 layer.setSkip(true);
670 }
671#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700672
Mathias Agopian13127d82013-03-05 17:47:11 -0800673 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700674 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000675#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500676 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700677 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500678 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 }
David Revemanecf0fa52017-03-03 11:32:44 -0500681 auto error = hwcLayer->setBlendMode(blendMode);
682 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
683 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
684 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000685#else
Robert Carr6452f122017-03-21 10:41:29 -0700686 if (!isOpaque(s) || getAlpha() != 0xFF) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000687 layer.setBlending(mPremultipliedAlpha ?
688 HWC_BLENDING_PREMULT :
689 HWC_BLENDING_COVERAGE);
690 }
691#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800692
693 // apply the layer's transform, followed by the display's global transform
694 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700695 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700696 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700697 if (!s.crop.isEmpty()) {
698 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700699 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000700#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000701 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000702#else
703 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
704#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000705 activeCrop.clear();
706 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700707 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800708 // This needs to be here as transform.transform(Rect) computes the
709 // transformed rect and then takes the bounding box of the result before
710 // returning. This means
711 // transform.inverse().transform(transform.transform(Rect)) != Rect
712 // in which case we need to make sure the final rect is clipped to the
713 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000714 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
715 activeCrop.clear();
716 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700717 // mark regions outside the crop as transparent
718 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
719 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
720 s.active.w, s.active.h));
721 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
722 activeCrop.left, activeCrop.bottom));
723 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
724 s.active.w, activeCrop.bottom));
725 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700726
727 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700728 if (!s.finalCrop.isEmpty()) {
729 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000730 frame.clear();
731 }
732 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000733#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000734 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
735 frame.clear();
736 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 const Transform& tr(displayDevice->getTransform());
738 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500739 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700740 if (error != HWC2::Error::None) {
741 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
742 mName.string(), transformedFrame.left, transformedFrame.top,
743 transformedFrame.right, transformedFrame.bottom,
744 to_string(error).c_str(), static_cast<int32_t>(error));
745 } else {
746 hwcInfo.displayFrame = transformedFrame;
747 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748
Dan Stoza5a423ea2017-02-16 14:10:39 -0800749 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700751 if (error != HWC2::Error::None) {
752 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
753 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
754 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
755 static_cast<int32_t>(error));
756 } else {
757 hwcInfo.sourceCrop = sourceCrop;
758 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759
Robert Carr6452f122017-03-21 10:41:29 -0700760 float alpha = getAlpha();
761 error = hwcLayer->setPlaneAlpha(alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
Robert Carr6452f122017-03-21 10:41:29 -0700763 "%s (%d)", mName.string(), alpha, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 static_cast<int32_t>(error));
765
Robert Carrae060832016-11-28 10:51:00 -0800766 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800768 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500770
Albert Chaulk2a589632017-05-04 16:59:44 -0400771 int type = s.type;
772 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700773 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400774 if (parent.get()) {
775 auto& parentState = parent->getDrawingState();
776 type = parentState.type;
777 appId = parentState.appId;
778 }
779
780 error = hwcLayer->setInfo(type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500781 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
782 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000783#else
784 if (!frame.intersect(hw->getViewport(), &frame)) {
785 frame.clear();
786 }
787 const Transform& tr(hw->getTransform());
788 layer.setFrame(tr.transform(frame));
789 layer.setCrop(computeCrop(hw));
Robert Carr6452f122017-03-21 10:41:29 -0700790 layer.setPlaneAlpha(getAlpha());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000791#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800792
Mathias Agopian29a367b2011-07-12 14:51:45 -0700793 /*
794 * Transformations are applied in this order:
795 * 1) buffer orientation/flip/mirror
796 * 2) state transformation (window manager)
797 * 3) layer orientation (screen orientation)
798 * (NOTE: the matrices are multiplied in reverse order)
799 */
800
801 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700802 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700803
Robert Carrcae605c2017-03-29 12:10:31 -0700804 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700805 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700806 * the code below applies the primary display's inverse transform to the
807 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700808 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700809 uint32_t invTransform =
810 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700811 // calculate the inverse transform
812 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
813 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
814 NATIVE_WINDOW_TRANSFORM_FLIP_H;
815 }
Robert Carrcae605c2017-03-29 12:10:31 -0700816
817 /*
818 * Here we cancel out the orientation component of the WM transform.
819 * The scaling and translate components are already included in our bounds
820 * computation so it's enough to just omit it in the composition.
821 * See comment in onDraw with ref to b/36727915 for why.
822 */
823 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700824 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700825
826 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000828#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800829 if (orientation & Transform::ROT_INVALID) {
830 // we can only handle simple transformation
831 hwcInfo.forceClientComposition = true;
832 } else {
833 auto transform = static_cast<HWC2::Transform>(orientation);
834 auto error = hwcLayer->setTransform(transform);
835 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
836 "%s (%d)", mName.string(), to_string(transform).c_str(),
837 to_string(error).c_str(), static_cast<int32_t>(error));
838 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000839#else
840 if (orientation & Transform::ROT_INVALID) {
841 // we can only handle simple transformation
842 layer.setSkip(true);
843 } else {
844 layer.setTransform(orientation);
845 }
846#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700847}
848
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000849#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800850void Layer::forceClientComposition(int32_t hwcId) {
851 if (mHwcLayers.count(hwcId) == 0) {
852 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
853 return;
854 }
855
856 mHwcLayers[hwcId].forceClientComposition = true;
857}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800858
Dan Stoza9e56aa02015-11-02 13:00:03 -0800859void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
860 // Apply this display's projection's viewport to the visible region
861 // before giving it to the HWC HAL.
862 const Transform& tr = displayDevice->getTransform();
863 const auto& viewport = displayDevice->getViewport();
864 Region visible = tr.transform(visibleRegion.intersect(viewport));
865 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800866 auto& hwcInfo = mHwcLayers[hwcId];
867 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800868 auto error = hwcLayer->setVisibleRegion(visible);
869 if (error != HWC2::Error::None) {
870 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
871 to_string(error).c_str(), static_cast<int32_t>(error));
872 visible.dump(LOG_TAG);
873 }
874
875 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
876 if (error != HWC2::Error::None) {
877 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
878 to_string(error).c_str(), static_cast<int32_t>(error));
879 surfaceDamageRegion.dump(LOG_TAG);
880 }
881
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700882 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800883 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700884 setCompositionType(hwcId, HWC2::Composition::Sideband);
885 ALOGV("[%s] Requesting Sideband composition", mName.string());
886 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800887 if (error != HWC2::Error::None) {
888 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
889 mName.string(), mSidebandStream->handle(),
890 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800891 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700892 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800893 }
894
Dan Stoza0a21df72016-07-20 12:52:38 -0700895 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800896 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700897 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700898 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800899 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700900 return;
901 }
902
Dan Stoza0a21df72016-07-20 12:52:38 -0700903 // SolidColor layers
904 if (mActiveBuffer == nullptr) {
905 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700906
907 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700908 error = hwcLayer->setColor({0, 0, 0, 255});
909 if (error != HWC2::Error::None) {
910 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
911 to_string(error).c_str(), static_cast<int32_t>(error));
912 }
Dan Stozac6c89542016-07-27 10:16:52 -0700913
914 // Clear out the transform, because it doesn't make sense absent a
915 // source buffer
916 error = hwcLayer->setTransform(HWC2::Transform::None);
917 if (error != HWC2::Error::None) {
918 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
919 to_string(error).c_str(), static_cast<int32_t>(error));
920 }
921
Dan Stoza0a21df72016-07-20 12:52:38 -0700922 return;
923 }
924
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700925 // Device or Cursor layers
926 if (mPotentialCursor) {
927 ALOGV("[%s] Requesting Cursor composition", mName.string());
928 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800929 } else {
930 ALOGV("[%s] Requesting Device composition", mName.string());
931 setCompositionType(hwcId, HWC2::Composition::Device);
932 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700933
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700934 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
935 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
936 if (error != HWC2::Error::None) {
937 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
938 mCurrentState.dataSpace, to_string(error).c_str(),
939 static_cast<int32_t>(error));
940 }
941
Chia-I Wu06d63de2017-01-04 14:58:51 +0800942 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400943 sp<GraphicBuffer> hwcBuffer;
944 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
945 &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800946
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700947 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400948 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700949 if (error != HWC2::Error::None) {
950 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
951 mActiveBuffer->handle, to_string(error).c_str(),
952 static_cast<int32_t>(error));
953 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800954}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600955
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000956#else
957void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
958 HWComposer::HWCLayerInterface& layer) {
959 // we have to set the visible region on every frame because
960 // we currently free it during onLayerDisplayed(), which is called
961 // after HWComposer::commit() -- every frame.
962 // Apply this display's projection's viewport to the visible region
963 // before giving it to the HWC HAL.
964 const Transform& tr = hw->getTransform();
965 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
966 layer.setVisibleRegionScreen(visible);
967 layer.setSurfaceDamage(surfaceDamageRegion);
968 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700969
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000970 if (mSidebandStream.get()) {
971 layer.setSidebandStream(mSidebandStream);
972 } else {
973 // NOTE: buffer can be NULL if the client never drew into this
974 // layer yet, or if we ran out of memory
975 layer.setBuffer(mActiveBuffer);
976 }
977}
978#endif
979
980#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800981void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
982 auto hwcId = displayDevice->getHwcDisplayId();
983 if (mHwcLayers.count(hwcId) == 0 ||
984 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
985 return;
986 }
987
988 // This gives us only the "orientation" component of the transform
989 const State& s(getCurrentState());
990
991 // Apply the layer's transform, followed by the display's global transform
992 // Here we're guaranteed that the layer's transform preserves rects
993 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700994 if (!s.crop.isEmpty()) {
995 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800996 }
997 // Subtract the transparent region and snap to the bounds
998 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700999 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -08001000 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -07001001 if (!s.finalCrop.isEmpty()) {
1002 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001003 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001004 auto& displayTransform(displayDevice->getTransform());
1005 auto position = displayTransform.transform(frame);
1006
1007 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
1008 position.top);
1009 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
1010 "to (%d, %d): %s (%d)", mName.string(), position.left,
1011 position.top, to_string(error).c_str(),
1012 static_cast<int32_t>(error));
1013}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001014#else
1015void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
1016 HWComposer::HWCLayerInterface& layer) {
1017 int fenceFd = -1;
1018
1019 // TODO: there is a possible optimization here: we only need to set the
1020 // acquire fence the first time a new buffer is acquired on EACH display.
1021
1022 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
1023 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
1024 if (fence->isValid()) {
1025 fenceFd = fence->dup();
1026 if (fenceFd == -1) {
1027 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
1028 }
1029 }
1030 }
1031 layer.setAcquireFenceFd(fenceFd);
1032}
1033
1034Rect Layer::getPosition(
1035 const sp<const DisplayDevice>& hw)
1036{
1037 // this gives us only the "orientation" component of the transform
1038 const State& s(getCurrentState());
1039
1040 // apply the layer's transform, followed by the display's global transform
1041 // here we're guaranteed that the layer's transform preserves rects
1042 Rect win(s.active.w, s.active.h);
1043 if (!s.crop.isEmpty()) {
1044 win.intersect(s.crop, &win);
1045 }
1046 // subtract the transparent region and snap to the bounds
1047 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001048 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001049 frame.intersect(hw->getViewport(), &frame);
1050 if (!s.finalCrop.isEmpty()) {
1051 frame.intersect(s.finalCrop, &frame);
1052 }
1053 const Transform& tr(hw->getTransform());
1054 return Rect(tr.transform(frame));
1055}
1056#endif
Riley Andrews03414a12014-07-01 14:22:59 -07001057
Mathias Agopian13127d82013-03-05 17:47:11 -08001058// ---------------------------------------------------------------------------
1059// drawing...
1060// ---------------------------------------------------------------------------
1061
1062void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -08001063 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -08001064}
1065
Dan Stozac7014012014-02-14 15:03:43 -08001066void Layer::draw(const sp<const DisplayDevice>& hw,
1067 bool useIdentityTransform) const {
1068 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001069}
1070
Dan Stozac7014012014-02-14 15:03:43 -08001071void Layer::draw(const sp<const DisplayDevice>& hw) const {
1072 onDraw(hw, Region(hw->bounds()), false);
1073}
1074
Robert Carrcae605c2017-03-29 12:10:31 -07001075static constexpr mat4 inverseOrientation(uint32_t transform) {
1076 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1077 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1078 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1079 mat4 tr;
1080
1081 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
1082 tr = tr * rot90;
1083 }
1084 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
1085 tr = tr * flipH;
1086 }
1087 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
1088 tr = tr * flipV;
1089 }
1090 return inverse(tr);
1091}
1092
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001093/*
1094 * onDraw will draw the current layer onto the presentable buffer
1095 */
Dan Stozac7014012014-02-14 15:03:43 -08001096void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
1097 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001099 ATRACE_CALL();
1100
Mathias Agopiana67932f2011-04-20 14:20:59 -07001101 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001103 // in fact never been drawn into. This happens frequently with
1104 // SurfaceView because the WindowManager can't know when the client
1105 // has drawn the first time.
1106
1107 // If there is nothing under us, we paint the screen in black, otherwise
1108 // we just skip this update.
1109
1110 // figure out if there is something below us
1111 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001112 bool finished = false;
Dan Stoza412903f2017-04-27 13:42:17 -07001113 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001114 if (finished || layer == static_cast<Layer const*>(this)) {
1115 finished = true;
1116 return;
1117 }
Mathias Agopian42977342012-08-05 00:40:46 -07001118 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001119 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001120 // if not everything below us is covered, we plug the holes!
1121 Region holes(clip.subtract(under));
1122 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001123 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001124 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 return;
1126 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001127
Andy McFadden97eba892012-12-11 15:21:45 -08001128 // Bind the current buffer to the GL texture, and wait for it to be
1129 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001130 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1131 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001132 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001133 // Go ahead and draw the buffer anyway; no matter what we do the screen
1134 // is probably going to have something visibly wrong.
1135 }
1136
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001137 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1138
Mathias Agopian875d8e12013-06-07 15:35:48 -07001139 RenderEngine& engine(mFlinger->getRenderEngine());
1140
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001141 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001142 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001143 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001144
1145 // Query the texture matrix given our current filtering mode.
1146 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001147 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1148 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001149
Robert Carrcae605c2017-03-29 12:10:31 -07001150 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001151
1152 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001153 * the code below applies the primary display's inverse transform to
1154 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001155 */
Pablo Ceballos021623b2016-04-15 17:31:51 -07001156 uint32_t transform =
1157 DisplayDevice::getPrimaryDisplayOrientationTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001158 mat4 tr = inverseOrientation(transform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001159
Robert Carrcae605c2017-03-29 12:10:31 -07001160 /**
1161 * TODO(b/36727915): This is basically a hack.
1162 *
1163 * Ensure that regardless of the parent transformation,
1164 * this buffer is always transformed from native display
1165 * orientation to display orientation. For example, in the case
1166 * of a camera where the buffer remains in native orientation,
1167 * we want the pixels to always be upright.
1168 */
Chia-I Wue41dbe62017-06-13 14:10:56 -07001169 sp<Layer> p = mDrawingParent.promote();
1170 if (p != nullptr) {
1171 const auto parentTransform = p->getTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001172 tr = tr * inverseOrientation(parentTransform.getOrientation());
1173 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001174
1175 // and finally apply it to the original texture matrix
1176 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1177 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1178 }
1179
Jamie Genniscbb1a952012-05-08 17:05:52 -07001180 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001181 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1182 mTexture.setFiltering(useFiltering);
1183 mTexture.setMatrix(textureMatrix);
1184
1185 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001186 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001187 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001188 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001189 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001190 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191}
1192
Mathias Agopian13127d82013-03-05 17:47:11 -08001193
Dan Stozac7014012014-02-14 15:03:43 -08001194void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001195 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001196 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001197{
Mathias Agopian19733a32013-08-28 18:13:56 -07001198 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001199 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001200 engine.setupFillWithColor(red, green, blue, alpha);
1201 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001202}
1203
1204void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001205 const sp<const DisplayDevice>& hw) const {
1206 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001207}
1208
Dan Stozac7014012014-02-14 15:03:43 -08001209void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001210 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001211 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001212
Dan Stozac7014012014-02-14 15:03:43 -08001213 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001214
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 /*
1216 * NOTE: the way we compute the texture coordinates here produces
1217 * different results than when we take the HWC path -- in the later case
1218 * the "source crop" is rounded to texel boundaries.
1219 * This can produce significantly different results when the texture
1220 * is scaled by a large amount.
1221 *
1222 * The GL code below is more logical (imho), and the difference with
1223 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001224 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001225 * GL composition when a buffer scaling is applied (maybe with some
1226 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1227 * like more of a hack.
1228 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001229 Rect win(computeBounds());
1230
Robert Carr1f0a16a2016-10-24 16:27:39 -07001231 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001232 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001233 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001234 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001235 win.clear();
1236 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001237 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001238 if (!win.intersect(computeBounds(), &win)) {
1239 win.clear();
1240 }
1241 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001242
Mathias Agopian3f844832013-08-07 21:24:32 -07001243 float left = float(win.left) / float(s.active.w);
1244 float top = float(win.top) / float(s.active.h);
1245 float right = float(win.right) / float(s.active.w);
1246 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001247
Mathias Agopian875d8e12013-06-07 15:35:48 -07001248 // TODO: we probably want to generate the texture coords with the mesh
1249 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001250 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1251 texCoords[0] = vec2(left, 1.0f - top);
1252 texCoords[1] = vec2(left, 1.0f - bottom);
1253 texCoords[2] = vec2(right, 1.0f - bottom);
1254 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001255
Mathias Agopian875d8e12013-06-07 15:35:48 -07001256 RenderEngine& engine(mFlinger->getRenderEngine());
Robert Carr6452f122017-03-21 10:41:29 -07001257 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), getAlpha());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001258#ifdef USE_HWC2
1259 engine.setSourceDataSpace(mCurrentState.dataSpace);
1260#endif
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001261 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001262 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001263}
1264
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001265#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001266void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1267 bool callIntoHwc) {
1268 if (mHwcLayers.count(hwcId) == 0) {
1269 ALOGE("setCompositionType called without a valid HWC layer");
1270 return;
1271 }
1272 auto& hwcInfo = mHwcLayers[hwcId];
1273 auto& hwcLayer = hwcInfo.layer;
1274 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1275 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1276 if (hwcInfo.compositionType != type) {
1277 ALOGV(" actually setting");
1278 hwcInfo.compositionType = type;
1279 if (callIntoHwc) {
1280 auto error = hwcLayer->setCompositionType(type);
1281 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1282 "composition type %s: %s (%d)", mName.string(),
1283 to_string(type).c_str(), to_string(error).c_str(),
1284 static_cast<int32_t>(error));
1285 }
1286 }
1287}
1288
1289HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001290 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1291 // If we're querying the composition type for a display that does not
1292 // have a HWC counterpart, then it will always be Client
1293 return HWC2::Composition::Client;
1294 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001295 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001296 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001297 return HWC2::Composition::Invalid;
1298 }
1299 return mHwcLayers.at(hwcId).compositionType;
1300}
1301
1302void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1303 if (mHwcLayers.count(hwcId) == 0) {
1304 ALOGE("setClearClientTarget called without a valid HWC layer");
1305 return;
1306 }
1307 mHwcLayers[hwcId].clearClientTarget = clear;
1308}
1309
1310bool Layer::getClearClientTarget(int32_t hwcId) const {
1311 if (mHwcLayers.count(hwcId) == 0) {
1312 ALOGE("getClearClientTarget called without a valid HWC layer");
1313 return false;
1314 }
1315 return mHwcLayers.at(hwcId).clearClientTarget;
1316}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001317#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001318
Ruben Brunk1681d952014-06-27 15:51:55 -07001319uint32_t Layer::getProducerStickyTransform() const {
1320 int producerStickyTransform = 0;
1321 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1322 if (ret != OK) {
1323 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1324 strerror(-ret), ret);
1325 return 0;
1326 }
1327 return static_cast<uint32_t>(producerStickyTransform);
1328}
1329
Dan Stozac5da2712016-07-20 15:38:12 -07001330bool Layer::latchUnsignaledBuffers() {
1331 static bool propertyLoaded = false;
1332 static bool latch = false;
1333 static std::mutex mutex;
1334 std::lock_guard<std::mutex> lock(mutex);
1335 if (!propertyLoaded) {
1336 char value[PROPERTY_VALUE_MAX] = {};
1337 property_get("debug.sf.latch_unsignaled", value, "0");
1338 latch = atoi(value);
1339 propertyLoaded = true;
1340 }
1341 return latch;
1342}
1343
Dan Stozacac35382016-01-27 12:21:06 -08001344uint64_t Layer::getHeadFrameNumber() const {
1345 Mutex::Autolock lock(mQueueItemLock);
1346 if (!mQueueItems.empty()) {
1347 return mQueueItems[0].mFrameNumber;
1348 } else {
1349 return mCurrentFrameNumber;
1350 }
1351}
1352
Dan Stoza1ce65812016-06-15 16:26:27 -07001353bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001354#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001355 if (latchUnsignaledBuffers()) {
1356 return true;
1357 }
1358
Dan Stoza1ce65812016-06-15 16:26:27 -07001359 Mutex::Autolock lock(mQueueItemLock);
1360 if (mQueueItems.empty()) {
1361 return true;
1362 }
1363 if (mQueueItems[0].mIsDroppable) {
1364 // Even though this buffer's fence may not have signaled yet, it could
1365 // be replaced by another buffer before it has a chance to, which means
1366 // that it's possible to get into a situation where a buffer is never
1367 // able to be latched. To avoid this, grab this buffer anyway.
1368 return true;
1369 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001370 return mQueueItems[0].mFenceTime->getSignalTime() !=
1371 Fence::SIGNAL_TIME_PENDING;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001372#else
1373 return true;
1374#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001375}
1376
Dan Stozacac35382016-01-27 12:21:06 -08001377bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1378 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1379 // Don't bother with a SyncPoint, since we've already latched the
1380 // relevant frame
1381 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001382 }
1383
Dan Stozacac35382016-01-27 12:21:06 -08001384 Mutex::Autolock lock(mLocalSyncPointMutex);
1385 mLocalSyncPoints.push_back(point);
1386 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001387}
1388
Mathias Agopian13127d82013-03-05 17:47:11 -08001389void Layer::setFiltering(bool filtering) {
1390 mFiltering = filtering;
1391}
1392
1393bool Layer::getFiltering() const {
1394 return mFiltering;
1395}
1396
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001397// As documented in libhardware header, formats in the range
1398// 0x100 - 0x1FF are specific to the HAL implementation, and
1399// are known to have no alpha channel
1400// TODO: move definition for device-specific range into
1401// hardware.h, instead of using hard-coded values here.
1402#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1403
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001404bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001405 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1406 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001407 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001408 switch (format) {
1409 case HAL_PIXEL_FORMAT_RGBA_8888:
1410 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001411 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001412 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001413 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001414 }
1415 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001416 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001417}
1418
Mathias Agopian13127d82013-03-05 17:47:11 -08001419// ----------------------------------------------------------------------------
1420// local state
1421// ----------------------------------------------------------------------------
1422
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001423static void boundPoint(vec2* point, const Rect& crop) {
1424 if (point->x < crop.left) {
1425 point->x = crop.left;
1426 }
1427 if (point->x > crop.right) {
1428 point->x = crop.right;
1429 }
1430 if (point->y < crop.top) {
1431 point->y = crop.top;
1432 }
1433 if (point->y > crop.bottom) {
1434 point->y = crop.bottom;
1435 }
1436}
1437
Dan Stozac7014012014-02-14 15:03:43 -08001438void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1439 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001440{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001441 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001442 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001443 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001444 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001445
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001446 vec2 lt = vec2(win.left, win.top);
1447 vec2 lb = vec2(win.left, win.bottom);
1448 vec2 rb = vec2(win.right, win.bottom);
1449 vec2 rt = vec2(win.right, win.top);
1450
Robert Carr1f0a16a2016-10-24 16:27:39 -07001451 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001452 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001453 lt = layerTransform.transform(lt);
1454 lb = layerTransform.transform(lb);
1455 rb = layerTransform.transform(rb);
1456 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001457 }
1458
Robert Carrb5d3d262016-03-25 15:08:13 -07001459 if (!s.finalCrop.isEmpty()) {
1460 boundPoint(&lt, s.finalCrop);
1461 boundPoint(&lb, s.finalCrop);
1462 boundPoint(&rb, s.finalCrop);
1463 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001464 }
1465
Mathias Agopianff2ed702013-09-01 21:36:12 -07001466 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001467 position[0] = hwTransform.transform(lt);
1468 position[1] = hwTransform.transform(lb);
1469 position[2] = hwTransform.transform(rb);
1470 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001471 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001472 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001473 }
1474}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001475
Andy McFadden4125a4f2014-01-29 17:17:11 -08001476bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001477{
neo.hee6fd41d2017-02-17 14:35:33 +08001478 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
Mathias Agopiana67932f2011-04-20 14:20:59 -07001479 // layer's opaque flag.
neo.hee6fd41d2017-02-17 14:35:33 +08001480 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001481 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001482 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001483
1484 // if the layer has the opaque flag, then we're always opaque,
1485 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001486 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001487}
1488
Dan Stoza23116082015-06-18 14:58:39 -07001489bool Layer::isSecure() const
1490{
1491 const Layer::State& s(mDrawingState);
1492 return (s.flags & layer_state_t::eLayerSecure);
1493}
1494
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001495bool Layer::isProtected() const
1496{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001497 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001498 return (activeBuffer != 0) &&
1499 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1500}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001501
Mathias Agopian13127d82013-03-05 17:47:11 -08001502bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001503 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001504}
1505
1506bool Layer::isCropped() const {
1507 return !mCurrentCrop.isEmpty();
1508}
1509
1510bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1511 return mNeedsFiltering || hw->needsFiltering();
1512}
1513
1514void Layer::setVisibleRegion(const Region& visibleRegion) {
1515 // always called from main thread
1516 this->visibleRegion = visibleRegion;
1517}
1518
1519void Layer::setCoveredRegion(const Region& coveredRegion) {
1520 // always called from main thread
1521 this->coveredRegion = coveredRegion;
1522}
1523
1524void Layer::setVisibleNonTransparentRegion(const Region&
1525 setVisibleNonTransparentRegion) {
1526 // always called from main thread
1527 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1528}
1529
1530// ----------------------------------------------------------------------------
1531// transaction
1532// ----------------------------------------------------------------------------
1533
Dan Stoza7dde5992015-05-22 09:51:44 -07001534void Layer::pushPendingState() {
1535 if (!mCurrentState.modified) {
1536 return;
1537 }
1538
Dan Stoza7dde5992015-05-22 09:51:44 -07001539 // If this transaction is waiting on the receipt of a frame, generate a sync
1540 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001541 if (mCurrentState.barrierLayer != nullptr) {
1542 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1543 if (barrierLayer == nullptr) {
1544 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001545 // If we can't promote the layer we are intended to wait on,
1546 // then it is expired or otherwise invalid. Allow this transaction
1547 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001548 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001549 } else {
1550 auto syncPoint = std::make_shared<SyncPoint>(
1551 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001552 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001553 mRemoteSyncPoints.push_back(std::move(syncPoint));
1554 } else {
1555 // We already missed the frame we're supposed to synchronize
1556 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001557 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001558 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001559 }
1560
Dan Stoza7dde5992015-05-22 09:51:44 -07001561 // Wake us up to check if the frame has been received
1562 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001563 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001564 }
1565 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001566 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001567}
1568
Pablo Ceballos05289c22016-04-14 15:49:55 -07001569void Layer::popPendingState(State* stateToCommit) {
1570 auto oldFlags = stateToCommit->flags;
1571 *stateToCommit = mPendingStates[0];
1572 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1573 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001574
1575 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001576 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001577}
1578
Pablo Ceballos05289c22016-04-14 15:49:55 -07001579bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001580 bool stateUpdateAvailable = false;
1581 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001582 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001583 if (mRemoteSyncPoints.empty()) {
1584 // If we don't have a sync point for this, apply it anyway. It
1585 // will be visually wrong, but it should keep us from getting
1586 // into too much trouble.
1587 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001588 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001589 stateUpdateAvailable = true;
1590 continue;
1591 }
1592
Dan Stozacac35382016-01-27 12:21:06 -08001593 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1594 mPendingStates[0].frameNumber) {
1595 ALOGE("[%s] Unexpected sync point frame number found",
1596 mName.string());
1597
1598 // Signal our end of the sync point and then dispose of it
1599 mRemoteSyncPoints.front()->setTransactionApplied();
1600 mRemoteSyncPoints.pop_front();
1601 continue;
1602 }
1603
Dan Stoza7dde5992015-05-22 09:51:44 -07001604 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1605 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001606 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001607 stateUpdateAvailable = true;
1608
1609 // Signal our end of the sync point and then dispose of it
1610 mRemoteSyncPoints.front()->setTransactionApplied();
1611 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001612 } else {
1613 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001614 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001615 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001616 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001617 stateUpdateAvailable = true;
1618 }
1619 }
1620
1621 // If we still have pending updates, wake SurfaceFlinger back up and point
1622 // it at this layer so we can process them
1623 if (!mPendingStates.empty()) {
1624 setTransactionFlags(eTransactionNeeded);
1625 mFlinger->setTransactionFlags(eTraversalNeeded);
1626 }
1627
1628 mCurrentState.modified = false;
1629 return stateUpdateAvailable;
1630}
1631
1632void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001633 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001634 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001635 Mutex::Autolock lock(mLocalSyncPointMutex);
1636 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001637 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001638 point->setFrameAvailable();
1639 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001640 }
1641}
1642
Mathias Agopian13127d82013-03-05 17:47:11 -08001643uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001644 ATRACE_CALL();
1645
Dan Stoza7dde5992015-05-22 09:51:44 -07001646 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001647 Layer::State c = getCurrentState();
1648 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001649 return 0;
1650 }
1651
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001652 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001654 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1655 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001656
1657 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001658 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001659 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001660 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001661 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001662 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001663 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001664 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001665 this, getName().string(), mCurrentTransform,
1666 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001667 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001668 c.crop.left,
1669 c.crop.top,
1670 c.crop.right,
1671 c.crop.bottom,
1672 c.crop.getWidth(),
1673 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001674 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001675 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001676 s.crop.left,
1677 s.crop.top,
1678 s.crop.right,
1679 s.crop.bottom,
1680 s.crop.getWidth(),
1681 s.crop.getHeight(),
1682 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001684 // record the new size, form this point on, when the client request
1685 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001686 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001687 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001689
Robert Carre392b552017-09-19 12:16:05 -07001690 // Don't let Layer::doTransaction update the drawing state
1691 // if we have a pending resize, unless we are in fixed-size mode.
1692 // the drawing state will be updated only once we receive a buffer
1693 // with the correct size.
1694 //
1695 // In particular, we want to make sure the clip (which is part
1696 // of the geometry state) is latched together with the size but is
1697 // latched immediately when no resizing is involved.
1698 //
1699 // If a sideband stream is attached, however, we want to skip this
1700 // optimization so that transactions aren't missed when a buffer
1701 // never arrives
1702 //
1703 // In the case that we don't have a buffer we ignore other factors
1704 // and avoid entering the resizePending state. At a high level the
1705 // resizePending state is to avoid applying the state of the new buffer
1706 // to the old buffer. However in the state where we don't have an old buffer
1707 // there is no such concern but we may still be being used as a parent layer.
1708 const bool resizePending = ((c.requested.w != c.active.w) ||
1709 (c.requested.h != c.active.h)) && (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001710 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001711 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001712 flags |= eDontUpdateGeometryState;
1713 }
1714 }
1715
Robert Carr7bf247e2017-05-18 14:02:49 -07001716 // Here we apply various requested geometry states, depending on our
1717 // latching configuration. See Layer.h for a detailed discussion of
1718 // how geometry latching is controlled.
1719 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001720 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001721
1722 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1723 // mode, which causes attributes which normally latch regardless of scaling mode,
1724 // to be delayed. We copy the requested state to the active state making sure
1725 // to respect these rules (again see Layer.h for a detailed discussion).
1726 //
1727 // There is an awkward asymmetry in the handling of the crop states in the position
1728 // states, as can be seen below. Largely this arises from position and transform
1729 // being stored in the same data structure while having different latching rules.
1730 // b/38182305
1731 //
1732 // Careful that "c" and editCurrentState may not begin as equivalent due to
1733 // applyPendingStates in the presence of deferred transactions.
1734 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001735 float tx = c.active.transform.tx();
1736 float ty = c.active.transform.ty();
1737 c.active = c.requested;
1738 c.active.transform.set(tx, ty);
1739 editCurrentState.active = c.active;
1740 } else {
1741 editCurrentState.active = editCurrentState.requested;
1742 c.active = c.requested;
1743 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001744 }
1745
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001746 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001747 // invalidate and recompute the visible regions if needed
1748 flags |= Layer::eVisibleRegion;
1749 }
1750
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001751 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001752 // invalidate and recompute the visible regions if needed
1753 flags |= eVisibleRegion;
1754 this->contentDirty = true;
1755
1756 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001757 const uint8_t type = c.active.transform.getType();
1758 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001759 (type >= Transform::SCALE));
1760 }
1761
Dan Stozac8145172016-04-28 16:29:06 -07001762 // If the layer is hidden, signal and clear out all local sync points so
1763 // that transactions for layers depending on this layer's frames becoming
1764 // visible are not blocked
1765 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001767 }
1768
Mathias Agopian13127d82013-03-05 17:47:11 -08001769 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001770 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001771 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772}
1773
Pablo Ceballos05289c22016-04-14 15:49:55 -07001774void Layer::commitTransaction(const State& stateToCommit) {
1775 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001776}
1777
Mathias Agopian13127d82013-03-05 17:47:11 -08001778uint32_t Layer::getTransactionFlags(uint32_t flags) {
1779 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1780}
1781
1782uint32_t Layer::setTransactionFlags(uint32_t flags) {
1783 return android_atomic_or(flags, &mTransactionFlags);
1784}
1785
Robert Carr82364e32016-05-15 11:27:47 -07001786bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001787 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001788 return false;
1789 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001790
1791 // We update the requested and active position simultaneously because
1792 // we want to apply the position portion of the transform matrix immediately,
1793 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001794 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001795 if (immediate && !mFreezeGeometryUpdates) {
1796 // Here we directly update the active state
1797 // unlike other setters, because we store it within
1798 // the transform, but use different latching rules.
1799 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001800 mCurrentState.active.transform.set(x, y);
1801 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001802 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001803
Dan Stoza7dde5992015-05-22 09:51:44 -07001804 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001805 setTransactionFlags(eTransactionNeeded);
1806 return true;
1807}
Robert Carr82364e32016-05-15 11:27:47 -07001808
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1810 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1811 if (idx < 0) {
1812 return false;
1813 }
1814 if (childLayer->setLayer(z)) {
1815 mCurrentChildren.removeAt(idx);
1816 mCurrentChildren.add(childLayer);
1817 }
1818 return true;
1819}
1820
Robert Carrae060832016-11-28 10:51:00 -08001821bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001822 if (mCurrentState.z == z)
1823 return false;
1824 mCurrentState.sequence++;
1825 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001826 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001827
1828 // Discard all relative layering.
1829 if (mCurrentState.zOrderRelativeOf != nullptr) {
1830 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1831 if (strongRelative != nullptr) {
1832 strongRelative->removeZOrderRelative(this);
1833 }
1834 mCurrentState.zOrderRelativeOf = nullptr;
1835 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001836 setTransactionFlags(eTransactionNeeded);
1837 return true;
1838}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839
Robert Carrdb66e622017-04-10 16:55:57 -07001840void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1841 mCurrentState.zOrderRelatives.remove(relative);
1842 mCurrentState.sequence++;
1843 mCurrentState.modified = true;
1844 setTransactionFlags(eTransactionNeeded);
1845}
1846
1847void Layer::addZOrderRelative(const wp<Layer>& relative) {
1848 mCurrentState.zOrderRelatives.add(relative);
1849 mCurrentState.modified = true;
1850 mCurrentState.sequence++;
1851 setTransactionFlags(eTransactionNeeded);
1852}
1853
1854bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1855 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1856 if (handle == nullptr) {
1857 return false;
1858 }
1859 sp<Layer> relative = handle->owner.promote();
1860 if (relative == nullptr) {
1861 return false;
1862 }
1863
1864 mCurrentState.sequence++;
1865 mCurrentState.modified = true;
1866 mCurrentState.z = z;
1867
1868 mCurrentState.zOrderRelativeOf = relative;
1869 relative->addZOrderRelative(this);
1870
1871 setTransactionFlags(eTransactionNeeded);
1872
1873 return true;
1874}
1875
Mathias Agopian13127d82013-03-05 17:47:11 -08001876bool Layer::setSize(uint32_t w, uint32_t h) {
1877 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1878 return false;
1879 mCurrentState.requested.w = w;
1880 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001881 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001882 setTransactionFlags(eTransactionNeeded);
1883 return true;
1884}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001885#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001886bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001887#else
1888bool Layer::setAlpha(uint8_t alpha) {
1889#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001890 if (mCurrentState.alpha == alpha)
1891 return false;
1892 mCurrentState.sequence++;
1893 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001894 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001895 setTransactionFlags(eTransactionNeeded);
1896 return true;
1897}
1898bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1899 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001900 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001901 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
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::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001907 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001908 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001909 setTransactionFlags(eTransactionNeeded);
1910 return true;
1911}
1912bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1913 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1914 if (mCurrentState.flags == newFlags)
1915 return false;
1916 mCurrentState.sequence++;
1917 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001918 mCurrentState.mask = mask;
1919 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001920 setTransactionFlags(eTransactionNeeded);
1921 return true;
1922}
Robert Carr99e27f02016-06-16 15:18:02 -07001923
1924bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001925 if (mCurrentState.requestedCrop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001926 return false;
1927 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001928 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001929 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001930 mCurrentState.crop = crop;
1931 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001932 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1933
Dan Stoza7dde5992015-05-22 09:51:44 -07001934 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001935 setTransactionFlags(eTransactionNeeded);
1936 return true;
1937}
Robert Carr8d5227b2017-03-16 15:41:03 -07001938
1939bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001940 if (mCurrentState.requestedFinalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001941 return false;
1942 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001943 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001944 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001945 mCurrentState.finalCrop = crop;
1946 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001947 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1948
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001949 mCurrentState.modified = true;
1950 setTransactionFlags(eTransactionNeeded);
1951 return true;
1952}
Mathias Agopian13127d82013-03-05 17:47:11 -08001953
Robert Carrc3574f72016-03-24 12:19:32 -07001954bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1955 if (scalingMode == mOverrideScalingMode)
1956 return false;
1957 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001958 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001959 return true;
1960}
1961
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001962void Layer::setInfo(uint32_t type, uint32_t appId) {
1963 mCurrentState.appId = appId;
1964 mCurrentState.type = type;
1965 mCurrentState.modified = true;
1966 setTransactionFlags(eTransactionNeeded);
1967}
1968
Robert Carrc3574f72016-03-24 12:19:32 -07001969uint32_t Layer::getEffectiveScalingMode() const {
1970 if (mOverrideScalingMode >= 0) {
1971 return mOverrideScalingMode;
1972 }
1973 return mCurrentScalingMode;
1974}
1975
Mathias Agopian13127d82013-03-05 17:47:11 -08001976bool Layer::setLayerStack(uint32_t layerStack) {
1977 if (mCurrentState.layerStack == layerStack)
1978 return false;
1979 mCurrentState.sequence++;
1980 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001981 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001982 setTransactionFlags(eTransactionNeeded);
1983 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001984}
1985
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001986bool Layer::setDataSpace(android_dataspace dataSpace) {
1987 if (mCurrentState.dataSpace == dataSpace)
1988 return false;
1989 mCurrentState.sequence++;
1990 mCurrentState.dataSpace = dataSpace;
1991 mCurrentState.modified = true;
1992 setTransactionFlags(eTransactionNeeded);
1993 return true;
1994}
1995
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001996android_dataspace Layer::getDataSpace() const {
1997 return mCurrentState.dataSpace;
1998}
1999
Robert Carr1f0a16a2016-10-24 16:27:39 -07002000uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002001 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002002 if (p == nullptr) {
2003 return getDrawingState().layerStack;
2004 }
2005 return p->getLayerStack();
2006}
2007
Robert Carr0d480722017-01-10 16:42:54 -08002008void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07002009 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08002010 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07002011 mCurrentState.frameNumber = frameNumber;
2012 // We don't set eTransactionNeeded, because just receiving a deferral
2013 // request without any other state updates shouldn't actually induce a delay
2014 mCurrentState.modified = true;
2015 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08002016 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08002017 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07002018 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08002019}
2020
2021void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
2022 uint64_t frameNumber) {
2023 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
2024 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07002025}
2026
Dan Stozaee44edd2015-03-23 15:50:23 -07002027void Layer::useSurfaceDamage() {
2028 if (mFlinger->mForceFullDamage) {
2029 surfaceDamageRegion = Region::INVALID_REGION;
2030 } else {
2031 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
2032 }
2033}
2034
2035void Layer::useEmptyDamage() {
2036 surfaceDamageRegion.clear();
2037}
2038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039// ----------------------------------------------------------------------------
2040// pageflip handling...
2041// ----------------------------------------------------------------------------
2042
Dan Stoza6b9454d2014-11-07 16:00:59 -08002043bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002044 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07002045 return true;
2046 }
2047
Dan Stoza6b9454d2014-11-07 16:00:59 -08002048 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002049 if (mQueueItems.empty()) {
2050 return false;
2051 }
2052 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002053 nsecs_t expectedPresent =
2054 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002055
2056 // Ignore timestamps more than a second in the future
2057 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
2058 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
2059 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
2060 expectedPresent);
2061
2062 bool isDue = timestamp < expectedPresent;
2063 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002064}
2065
Brian Andersond6927fb2016-07-23 23:37:30 -07002066bool Layer::onPreComposition(nsecs_t refreshStartTime) {
2067 if (mBufferLatched) {
2068 Mutex::Autolock lock(mFrameEventHistoryMutex);
2069 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
2070 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002071 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002072 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002073}
2074
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002075bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07002076 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002077 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002078 // mFrameLatencyNeeded is true when a new frame was latched for the
2079 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08002080 if (!mFrameLatencyNeeded)
2081 return false;
2082
Fabien Sanglard28e98082016-12-05 10:19:46 -08002083 // Update mFrameEventHistory.
2084 {
2085 Mutex::Autolock lock(mFrameEventHistoryMutex);
2086 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002087 glDoneFence, presentFence, compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002088 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08002089
2090 // Update mFrameTracker.
2091 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
2092 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
2093
Brian Anderson3d4039d2016-09-23 16:31:30 -07002094 std::shared_ptr<FenceTime> frameReadyFence =
2095 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08002096 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002097 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002098 } else {
2099 // There was no fence for this frame, so assume that it was ready
2100 // to be presented at the desired present time.
2101 mFrameTracker.setFrameReadyTime(desiredPresentTime);
2102 }
2103
Brian Anderson3d4039d2016-09-23 16:31:30 -07002104 if (presentFence->isValid()) {
2105 mFrameTracker.setActualPresentFence(
2106 std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002107 } else {
2108 // The HWC doesn't support present fences, so use the refresh
2109 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002110 mFrameTracker.setActualPresentTime(
2111 mFlinger->getHwComposer().getRefreshTimestamp(
2112 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002113 }
2114
2115 mFrameTracker.advanceFrame();
2116 mFrameLatencyNeeded = false;
2117 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002118}
2119
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002120#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07002121void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08002122 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
2123 return;
2124 }
2125
Brian Anderson3d4039d2016-09-23 16:31:30 -07002126 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002127 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002128 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002129 mReleaseTimeline.push(releaseFenceTime);
2130
2131 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002132 if (mPreviousFrameNumber != 0) {
2133 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2134 dequeueReadyTime, std::move(releaseFenceTime));
2135 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002136}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002137#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08002138
Robert Carr1f0a16a2016-10-24 16:27:39 -07002139bool Layer::isHiddenByPolicy() const {
2140 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07002141 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002142 if (parent != nullptr && parent->isHiddenByPolicy()) {
2143 return true;
2144 }
2145 return s.flags & layer_state_t::eLayerHidden;
2146}
2147
Mathias Agopianda27af92012-09-13 18:17:13 -07002148bool Layer::isVisible() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002149#ifdef USE_HWC2
Robert Carr6452f122017-03-21 10:41:29 -07002150 return !(isHiddenByPolicy()) && getAlpha() > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08002151 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002152#else
Robert Carr6452f122017-03-21 10:41:29 -07002153 return !(isHiddenByPolicy()) && getAlpha()
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002154 && (mActiveBuffer != NULL || mSidebandStream != NULL);
2155#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07002156}
2157
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002158bool Layer::allTransactionsSignaled() {
2159 auto headFrameNumber = getHeadFrameNumber();
2160 bool matchingFramesFound = false;
2161 bool allTransactionsApplied = true;
2162 Mutex::Autolock lock(mLocalSyncPointMutex);
2163
2164 for (auto& point : mLocalSyncPoints) {
2165 if (point->getFrameNumber() > headFrameNumber) {
2166 break;
2167 }
2168 matchingFramesFound = true;
2169
2170 if (!point->frameIsAvailable()) {
2171 // We haven't notified the remote layer that the frame for
2172 // this point is available yet. Notify it now, and then
2173 // abort this attempt to latch.
2174 point->setFrameAvailable();
2175 allTransactionsApplied = false;
2176 break;
2177 }
2178
2179 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
2180 }
2181 return !matchingFramesFound || allTransactionsApplied;
2182}
2183
Brian Andersond6927fb2016-07-23 23:37:30 -07002184Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002186 ATRACE_CALL();
2187
Jesse Hall399184a2014-03-03 15:42:54 -08002188 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2189 // mSidebandStreamChanged was true
2190 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002191 if (mSidebandStream != NULL) {
2192 setTransactionFlags(eTransactionNeeded);
2193 mFlinger->setTransactionFlags(eTraversalNeeded);
2194 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002195 recomputeVisibleRegions = true;
2196
2197 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002198 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002199 }
2200
Mathias Agopian4fec8732012-06-29 14:12:52 -07002201 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002202 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2203 return outDirtyRegion;
2204 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002205
Fabien Sanglard223eb912016-10-13 10:15:06 -07002206 // if we've already called updateTexImage() without going through
2207 // a composition step, we have to skip this layer at this point
2208 // because we cannot call updateTeximage() without a corresponding
2209 // compositionComplete() call.
2210 // we'll trigger an update in onPreComposition().
2211 if (mRefreshPending) {
2212 return outDirtyRegion;
2213 }
2214
2215 // If the head buffer's acquire fence hasn't signaled yet, return and
2216 // try again later
2217 if (!headFenceHasSignaled()) {
2218 mFlinger->signalLayerUpdate();
2219 return outDirtyRegion;
2220 }
2221
2222 // Capture the old state of the layer for comparisons later
2223 const State& s(getDrawingState());
2224 const bool oldOpacity = isOpaque(s);
2225 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2226
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002227 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002228 mFlinger->signalLayerUpdate();
2229 return outDirtyRegion;
2230 }
2231
2232 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2233 // of the buffer queue isn't modified when the buffer queue is returning
2234 // BufferItem's that weren't actually queued. This can happen in shared
2235 // buffer mode.
2236 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002237 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2238 getProducerStickyTransform() != 0, mName.string(),
Robert Carr7bf247e2017-05-18 14:02:49 -07002239 mOverrideScalingMode, mFreezeGeometryUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002240 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2241 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2242 mLastFrameNumberReceived);
2243 if (updateResult == BufferQueue::PRESENT_LATER) {
2244 // Producer doesn't want buffer to be displayed yet. Signal a
2245 // layer update so we check again at the next opportunity.
2246 mFlinger->signalLayerUpdate();
2247 return outDirtyRegion;
2248 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2249 // If the buffer has been rejected, remove it from the shadow queue
2250 // and return early
2251 if (queuedBuffer) {
2252 Mutex::Autolock lock(mQueueItemLock);
2253 mQueueItems.removeAt(0);
2254 android_atomic_dec(&mQueuedFrames);
2255 }
2256 return outDirtyRegion;
2257 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2258 // This can occur if something goes wrong when trying to create the
2259 // EGLImage for this buffer. If this happens, the buffer has already
2260 // been released, so we need to clean up the queue and bug out
2261 // early.
2262 if (queuedBuffer) {
2263 Mutex::Autolock lock(mQueueItemLock);
2264 mQueueItems.clear();
2265 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002266 }
2267
Fabien Sanglard223eb912016-10-13 10:15:06 -07002268 // Once we have hit this state, the shadow queue may no longer
2269 // correctly reflect the incoming BufferQueue's contents, so even if
2270 // updateTexImage starts working, the only safe course of action is
2271 // to continue to ignore updates.
2272 mUpdateTexImageFailed = true;
2273
2274 return outDirtyRegion;
2275 }
2276
2277 if (queuedBuffer) {
2278 // Autolock scope
2279 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2280
2281 Mutex::Autolock lock(mQueueItemLock);
2282
2283 // Remove any stale buffers that have been dropped during
2284 // updateTexImage
2285 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2286 mQueueItems.removeAt(0);
2287 android_atomic_dec(&mQueuedFrames);
2288 }
2289
2290 mQueueItems.removeAt(0);
2291 }
2292
2293
2294 // Decrement the queued-frames count. Signal another event if we
2295 // have more frames pending.
2296 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2297 || mAutoRefresh) {
2298 mFlinger->signalLayerUpdate();
2299 }
2300
Fabien Sanglard223eb912016-10-13 10:15:06 -07002301 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002302 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2303 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002304 if (mActiveBuffer == NULL) {
2305 // this can only happen if the very first buffer was rejected.
2306 return outDirtyRegion;
2307 }
2308
Brian Andersond6927fb2016-07-23 23:37:30 -07002309 mBufferLatched = true;
2310 mPreviousFrameNumber = mCurrentFrameNumber;
2311 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2312
2313 {
2314 Mutex::Autolock lock(mFrameEventHistoryMutex);
2315 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2316#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002317 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002318 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002319 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002320 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002321 if (mPreviousFrameNumber != 0) {
2322 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2323 latchTime, std::move(releaseFenceTime));
2324 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002325#endif
2326 }
2327
Fabien Sanglard223eb912016-10-13 10:15:06 -07002328 mRefreshPending = true;
2329 mFrameLatencyNeeded = true;
2330 if (oldActiveBuffer == NULL) {
2331 // the first time we receive a buffer, we need to trigger a
2332 // geometry invalidation.
2333 recomputeVisibleRegions = true;
2334 }
2335
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002336 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2337
Fabien Sanglard223eb912016-10-13 10:15:06 -07002338 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2339 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2340 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2341 if ((crop != mCurrentCrop) ||
2342 (transform != mCurrentTransform) ||
2343 (scalingMode != mCurrentScalingMode))
2344 {
2345 mCurrentCrop = crop;
2346 mCurrentTransform = transform;
2347 mCurrentScalingMode = scalingMode;
2348 recomputeVisibleRegions = true;
2349 }
2350
2351 if (oldActiveBuffer != NULL) {
2352 uint32_t bufWidth = mActiveBuffer->getWidth();
2353 uint32_t bufHeight = mActiveBuffer->getHeight();
2354 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2355 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002356 recomputeVisibleRegions = true;
2357 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002358 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002359
Fabien Sanglard223eb912016-10-13 10:15:06 -07002360 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2361 if (oldOpacity != isOpaque(s)) {
2362 recomputeVisibleRegions = true;
2363 }
Dan Stozacac35382016-01-27 12:21:06 -08002364
Fabien Sanglard223eb912016-10-13 10:15:06 -07002365 // Remove any sync points corresponding to the buffer which was just
2366 // latched
2367 {
2368 Mutex::Autolock lock(mLocalSyncPointMutex);
2369 auto point = mLocalSyncPoints.begin();
2370 while (point != mLocalSyncPoints.end()) {
2371 if (!(*point)->frameIsAvailable() ||
2372 !(*point)->transactionIsApplied()) {
2373 // This sync point must have been added since we started
2374 // latching. Don't drop it yet.
2375 ++point;
2376 continue;
2377 }
2378
2379 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2380 point = mLocalSyncPoints.erase(point);
2381 } else {
2382 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002383 }
2384 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002385 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002386
2387 // FIXME: postedRegion should be dirty & bounds
2388 Region dirtyRegion(Rect(s.active.w, s.active.h));
2389
2390 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002391 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002392
Mathias Agopian4fec8732012-06-29 14:12:52 -07002393 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002394}
2395
Mathias Agopiana67932f2011-04-20 14:20:59 -07002396uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002397{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002398 // TODO: should we do something special if mSecure is set?
2399 if (mProtectedByApp) {
2400 // need a hardware-protected path to external video sink
2401 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002402 }
Riley Andrews03414a12014-07-01 14:22:59 -07002403 if (mPotentialCursor) {
2404 usage |= GraphicBuffer::USAGE_CURSOR;
2405 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002406 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002407 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002408}
2409
Mathias Agopian84300952012-11-21 16:02:13 -08002410void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002411 uint32_t orientation = 0;
2412 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002413 // The transform hint is used to improve performance, but we can
2414 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002415 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002416 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002417 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002418 if (orientation & Transform::ROT_INVALID) {
2419 orientation = 0;
2420 }
2421 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002422 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002423}
2424
Mathias Agopian13127d82013-03-05 17:47:11 -08002425// ----------------------------------------------------------------------------
2426// debugging
2427// ----------------------------------------------------------------------------
2428
Kalle Raitaa099a242017-01-11 11:17:29 -08002429LayerDebugInfo Layer::getLayerDebugInfo() const {
2430 LayerDebugInfo info;
2431 const Layer::State& ds = getDrawingState();
2432 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07002433 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08002434 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
2435 info.mType = String8(getTypeId());
2436 info.mTransparentRegion = ds.activeTransparentRegion;
2437 info.mVisibleRegion = visibleRegion;
2438 info.mSurfaceDamageRegion = surfaceDamageRegion;
2439 info.mLayerStack = getLayerStack();
2440 info.mX = ds.active.transform.tx();
2441 info.mY = ds.active.transform.ty();
2442 info.mZ = ds.z;
2443 info.mWidth = ds.active.w;
2444 info.mHeight = ds.active.h;
2445 info.mCrop = ds.crop;
2446 info.mFinalCrop = ds.finalCrop;
2447 info.mAlpha = ds.alpha;
2448 info.mFlags = ds.flags;
2449 info.mPixelFormat = getPixelFormat();
2450 info.mDataSpace = getDataSpace();
2451 info.mMatrix[0][0] = ds.active.transform[0][0];
2452 info.mMatrix[0][1] = ds.active.transform[0][1];
2453 info.mMatrix[1][0] = ds.active.transform[1][0];
2454 info.mMatrix[1][1] = ds.active.transform[1][1];
2455 {
2456 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
2457 if (activeBuffer != 0) {
2458 info.mActiveBufferWidth = activeBuffer->getWidth();
2459 info.mActiveBufferHeight = activeBuffer->getHeight();
2460 info.mActiveBufferStride = activeBuffer->getStride();
2461 info.mActiveBufferFormat = activeBuffer->format;
2462 } else {
2463 info.mActiveBufferWidth = 0;
2464 info.mActiveBufferHeight = 0;
2465 info.mActiveBufferStride = 0;
2466 info.mActiveBufferFormat = 0;
2467 }
Mathias Agopian13127d82013-03-05 17:47:11 -08002468 }
Kalle Raitaa099a242017-01-11 11:17:29 -08002469 info.mNumQueuedFrames = getQueuedFrameCount();
2470 info.mRefreshPending = isBufferLatched();
2471 info.mIsOpaque = isOpaque(ds);
2472 info.mContentDirty = contentDirty;
2473 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08002474}
2475
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002476#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002477void Layer::miniDumpHeader(String8& result) {
2478 result.append("----------------------------------------");
2479 result.append("---------------------------------------\n");
2480 result.append(" Layer name\n");
2481 result.append(" Z | ");
2482 result.append(" Comp Type | ");
2483 result.append(" Disp Frame (LTRB) | ");
2484 result.append(" Source Crop (LTRB)\n");
2485 result.append("----------------------------------------");
2486 result.append("---------------------------------------\n");
2487}
2488
2489void Layer::miniDump(String8& result, int32_t hwcId) const {
2490 if (mHwcLayers.count(hwcId) == 0) {
2491 return;
2492 }
2493
2494 String8 name;
2495 if (mName.length() > 77) {
2496 std::string shortened;
2497 shortened.append(mName.string(), 36);
2498 shortened.append("[...]");
2499 shortened.append(mName.string() + (mName.length() - 36), 36);
2500 name = shortened.c_str();
2501 } else {
2502 name = mName;
2503 }
2504
2505 result.appendFormat(" %s\n", name.string());
2506
2507 const Layer::State& layerState(getDrawingState());
2508 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07002509 result.appendFormat(" %10d | ", layerState.z);
Dan Stozae22aec72016-08-01 13:20:59 -07002510 result.appendFormat("%10s | ",
2511 to_string(getCompositionType(hwcId)).c_str());
2512 const Rect& frame = hwcInfo.displayFrame;
2513 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2514 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002515 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002516 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2517 crop.right, crop.bottom);
2518
2519 result.append("- - - - - - - - - - - - - - - - - - - - ");
2520 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2521}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002522#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002523
Svetoslavd85084b2014-03-20 10:28:31 -07002524void Layer::dumpFrameStats(String8& result) const {
2525 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002526}
2527
Svetoslavd85084b2014-03-20 10:28:31 -07002528void Layer::clearFrameStats() {
2529 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002530}
2531
Jamie Gennis6547ff42013-07-16 20:12:42 -07002532void Layer::logFrameStats() {
2533 mFrameTracker.logAndResetStats(mName);
2534}
2535
Svetoslavd85084b2014-03-20 10:28:31 -07002536void Layer::getFrameStats(FrameStats* outStats) const {
2537 mFrameTracker.getStats(outStats);
2538}
2539
Brian Andersond6927fb2016-07-23 23:37:30 -07002540void Layer::dumpFrameEvents(String8& result) {
2541 result.appendFormat("- Layer %s (%s, %p)\n",
2542 getName().string(), getTypeId(), this);
2543 Mutex::Autolock lock(mFrameEventHistoryMutex);
2544 mFrameEventHistory.checkFencesForCompletion();
2545 mFrameEventHistory.dump(result);
2546}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002547
Brian Anderson5ea5e592016-12-01 16:54:33 -08002548void Layer::onDisconnect() {
2549 Mutex::Autolock lock(mFrameEventHistoryMutex);
2550 mFrameEventHistory.onDisconnect();
2551}
2552
Brian Anderson3890c392016-07-25 12:48:08 -07002553void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2554 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002555 Mutex::Autolock lock(mFrameEventHistoryMutex);
2556 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002557 // If there are any unsignaled fences in the aquire timeline at this
2558 // point, the previously queued frame hasn't been latched yet. Go ahead
2559 // and try to get the signal time here so the syscall is taken out of
2560 // the main thread's critical path.
2561 mAcquireTimeline.updateSignalTimes();
2562 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002563 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002564 mFrameEventHistory.addQueue(*newTimestamps);
2565 }
2566
Brian Anderson3890c392016-07-25 12:48:08 -07002567 if (outDelta) {
2568 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002569 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002570}
Dan Stozae77c7662016-05-13 11:37:28 -07002571
2572std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2573 bool forceFlush) {
2574 std::vector<OccupancyTracker::Segment> history;
2575 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2576 &history);
2577 if (result != NO_ERROR) {
2578 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2579 result);
2580 return {};
2581 }
2582 return history;
2583}
2584
Robert Carr367c5682016-06-20 11:55:28 -07002585bool Layer::getTransformToDisplayInverse() const {
2586 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2587}
2588
Chia-I Wu98f1c102017-05-30 14:54:08 -07002589size_t Layer::getChildrenCount() const {
2590 size_t count = 0;
2591 for (const sp<Layer>& child : mCurrentChildren) {
2592 count += 1 + child->getChildrenCount();
2593 }
2594 return count;
2595}
2596
Robert Carr1f0a16a2016-10-24 16:27:39 -07002597void Layer::addChild(const sp<Layer>& layer) {
2598 mCurrentChildren.add(layer);
2599 layer->setParent(this);
2600}
2601
2602ssize_t Layer::removeChild(const sp<Layer>& layer) {
2603 layer->setParent(nullptr);
2604 return mCurrentChildren.remove(layer);
2605}
2606
Robert Carr1db73f62016-12-21 12:58:51 -08002607bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2608 sp<Handle> handle = nullptr;
2609 sp<Layer> newParent = nullptr;
2610 if (newParentHandle == nullptr) {
2611 return false;
2612 }
2613 handle = static_cast<Handle*>(newParentHandle.get());
2614 newParent = handle->owner.promote();
2615 if (newParent == nullptr) {
2616 ALOGE("Unable to promote Layer handle");
2617 return false;
2618 }
2619
2620 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002621 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08002622
2623 sp<Client> client(child->mClientRef.promote());
2624 if (client != nullptr) {
2625 client->setParentLayer(newParent);
2626 }
2627 }
2628 mCurrentChildren.clear();
2629
2630 return true;
2631}
2632
chaviw06178942017-07-27 10:25:59 -07002633bool Layer::reparentChild(const sp<IBinder>& newParentHandle, const sp<IBinder>& childHandle) {
2634 if (newParentHandle == nullptr || childHandle == nullptr) {
2635 return false;
2636 }
2637
2638 auto handle = static_cast<Handle*>(newParentHandle.get());
2639 sp<Layer> newParent = handle->owner.promote();
2640 if (newParent == nullptr) {
2641 ALOGE("Unable to promote Layer handle");
2642 return false;
2643 }
2644
2645 handle = static_cast<Handle*>(childHandle.get());
2646 sp<Layer> child = handle->owner.promote();
2647 if (child == nullptr) {
2648 ALOGE("Unable to promote child Layer handle");
2649 return false;
2650 }
2651
2652 if (mCurrentChildren.indexOf(child) < 0) {
2653 ALOGE("Child layer is not child of current layer");
2654 return false;
2655 }
2656
2657 sp<Client> parentClient(mClientRef.promote());
2658 sp<Client> childClient(child->mClientRef.promote());
2659 sp<Client> newParentClient(newParent->mClientRef.promote());
2660
2661 if (parentClient != childClient || childClient != newParentClient) {
2662 ALOGE("Current layer, child layer, and new parent layer must have the same client");
2663 return false;
2664 }
2665
2666 newParent->addChild(child);
2667 mCurrentChildren.remove(child);
2668 return true;
2669}
2670
Robert Carr9524cb32017-02-13 11:32:32 -08002671bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07002672 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08002673 if (child == this) {
2674 return;
2675 }
2676
chaviw161410b02017-07-27 10:46:08 -07002677 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08002678 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07002679 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08002680 client->detachLayer(child);
2681 }
2682 });
2683
2684 return true;
2685}
2686
Robert Carr1f0a16a2016-10-24 16:27:39 -07002687void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002688 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002689}
2690
2691void Layer::clearSyncPoints() {
2692 for (const auto& child : mCurrentChildren) {
2693 child->clearSyncPoints();
2694 }
2695
2696 Mutex::Autolock lock(mLocalSyncPointMutex);
2697 for (auto& point : mLocalSyncPoints) {
2698 point->setFrameAvailable();
2699 }
2700 mLocalSyncPoints.clear();
2701}
2702
2703int32_t Layer::getZ() const {
2704 return mDrawingState.z;
2705}
2706
Dan Stoza412903f2017-04-27 13:42:17 -07002707LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) {
2708 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
2709 "makeTraversalList received invalid stateSet");
2710 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2711 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2712 const State& state = useDrawing ? mDrawingState : mCurrentState;
2713
2714 if (state.zOrderRelatives.size() == 0) {
2715 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07002716 }
2717 LayerVector traverse;
2718
Dan Stoza412903f2017-04-27 13:42:17 -07002719 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07002720 sp<Layer> strongRelative = weakRelative.promote();
2721 if (strongRelative != nullptr) {
2722 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07002723 }
2724 }
2725
Dan Stoza412903f2017-04-27 13:42:17 -07002726 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07002727 traverse.add(child);
2728 }
2729
2730 return traverse;
2731}
2732
Robert Carr1f0a16a2016-10-24 16:27:39 -07002733/**
Robert Carrdb66e622017-04-10 16:55:57 -07002734 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002735 */
Dan Stoza412903f2017-04-27 13:42:17 -07002736void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
2737 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002738
Robert Carr1f0a16a2016-10-24 16:27:39 -07002739 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002740 for (; i < list.size(); i++) {
2741 const auto& relative = list[i];
2742 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002743 break;
Robert Carrdb66e622017-04-10 16:55:57 -07002744 }
Dan Stoza412903f2017-04-27 13:42:17 -07002745 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002746 }
Dan Stoza412903f2017-04-27 13:42:17 -07002747 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07002748 for (; i < list.size(); i++) {
2749 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002750 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002751 }
2752}
2753
2754/**
Robert Carrdb66e622017-04-10 16:55:57 -07002755 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002756 */
Dan Stoza412903f2017-04-27 13:42:17 -07002757void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
2758 const LayerVector::Visitor& visitor) {
2759 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002760
Robert Carr1f0a16a2016-10-24 16:27:39 -07002761 int32_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002762 for (i = list.size()-1; i>=0; i--) {
2763 const auto& relative = list[i];
2764 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002765 break;
2766 }
Dan Stoza412903f2017-04-27 13:42:17 -07002767 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002768 }
Dan Stoza412903f2017-04-27 13:42:17 -07002769 visitor(this);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002770 for (; i>=0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002771 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002772 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002773 }
2774}
2775
2776Transform Layer::getTransform() const {
2777 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002778 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002779 if (p != nullptr) {
2780 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002781
2782 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2783 // it isFixedSize) then there may be additional scaling not accounted
2784 // for in the transform. We need to mirror this scaling in child surfaces
2785 // or we will break the contract where WM can treat child surfaces as
2786 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002787 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002788 int bufferWidth;
2789 int bufferHeight;
2790 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2791 bufferWidth = p->mActiveBuffer->getWidth();
2792 bufferHeight = p->mActiveBuffer->getHeight();
2793 } else {
2794 bufferHeight = p->mActiveBuffer->getWidth();
2795 bufferWidth = p->mActiveBuffer->getHeight();
2796 }
Robert Carr9b429f42017-04-17 14:56:57 -07002797 float sx = p->getDrawingState().active.w /
Robert Carr1725eee2017-04-26 18:32:15 -07002798 static_cast<float>(bufferWidth);
Robert Carr9b429f42017-04-17 14:56:57 -07002799 float sy = p->getDrawingState().active.h /
Robert Carr1725eee2017-04-26 18:32:15 -07002800 static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002801 Transform extraParentScaling;
2802 extraParentScaling.set(sx, 0, 0, sy);
2803 t = t * extraParentScaling;
2804 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002805 }
2806 return t * getDrawingState().active.transform;
2807}
2808
Robert Carr6452f122017-03-21 10:41:29 -07002809#ifdef USE_HWC2
2810float Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002811 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002812
2813 float parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0;
2814 return parentAlpha * getDrawingState().alpha;
2815}
2816#else
2817uint8_t Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002818 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002819
2820 float parentAlpha = (p != nullptr) ? (p->getAlpha() / 255.0f) : 1.0;
2821 float drawingAlpha = getDrawingState().alpha / 255.0f;
2822 drawingAlpha = drawingAlpha * parentAlpha;
2823 return static_cast<uint8_t>(std::round(drawingAlpha * 255));
2824}
2825#endif
2826
Robert Carr1f0a16a2016-10-24 16:27:39 -07002827void Layer::commitChildList() {
2828 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2829 const auto& child = mCurrentChildren[i];
2830 child->commitChildList();
2831 }
2832 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002833 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002834}
2835
Mathias Agopian13127d82013-03-05 17:47:11 -08002836// ---------------------------------------------------------------------------
2837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002838}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002839
2840#if defined(__gl_h_)
2841#error "don't include gl/gl.h in this file"
2842#endif
2843
2844#if defined(__gl2_h_)
2845#error "don't include gl2/gl2.h in this file"
2846#endif