blob: 97c56b3542e63d13f305b9804c1e6a041cacbcb0 [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),
Ivan Lozanoc6c2a8a2017-10-30 11:24:08 -070073 mTextureName(UINT32_MAX),
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) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
221}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000222#else
223void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
224 HWComposer::HWCLayerInterface* layer) {
225 if (layer) {
226 layer->onDisplayed();
227 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
228 }
229}
230#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800231
Dan Stoza6b9454d2014-11-07 16:00:59 -0800232void Layer::onFrameAvailable(const BufferItem& item) {
233 // Add this buffer from our internal queue tracker
234 { // Autolock scope
235 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700236 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
237 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700238 // Reset the frame number tracker when we receive the first buffer after
239 // a frame number reset
240 if (item.mFrameNumber == 1) {
241 mLastFrameNumberReceived = 0;
242 }
243
244 // Ensure that callbacks are handled in order
245 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
246 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
247 ms2ns(500));
248 if (result != NO_ERROR) {
249 ALOGE("[%s] Timed out waiting on callback", mName.string());
250 }
251 }
252
Dan Stoza6b9454d2014-11-07 16:00:59 -0800253 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700254 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700255
256 // Wake up any pending callbacks
257 mLastFrameNumberReceived = item.mFrameNumber;
258 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800259 }
260
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800261 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700262}
263
Dan Stoza6b9454d2014-11-07 16:00:59 -0800264void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700265 { // Autolock scope
266 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700267
Dan Stoza7dde5992015-05-22 09:51:44 -0700268 // Ensure that callbacks are handled in order
269 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
270 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
271 ms2ns(500));
272 if (result != NO_ERROR) {
273 ALOGE("[%s] Timed out waiting on callback", mName.string());
274 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700275 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700276
277 if (mQueueItems.empty()) {
278 ALOGE("Can't replace a frame on an empty queue");
279 return;
280 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700281 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700282
283 // Wake up any pending callbacks
284 mLastFrameNumberReceived = item.mFrameNumber;
285 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700286 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800287}
288
Jesse Hall399184a2014-03-03 15:42:54 -0800289void Layer::onSidebandStreamChanged() {
290 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
291 // mSidebandStreamChanged was false
292 mFlinger->signalLayerUpdate();
293 }
294}
295
Chia-I Wuc6657022017-08-15 11:18:17 -0700296void Layer::onRemovedFromCurrentState() {
297 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
298
Robert Carr5edb1ad2017-04-25 10:54:24 -0700299 if (mCurrentState.zOrderRelativeOf != nullptr) {
300 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
301 if (strongRelative != nullptr) {
302 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700303 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700304 }
305 mCurrentState.zOrderRelativeOf = nullptr;
306 }
307
Chia-I Wuc6657022017-08-15 11:18:17 -0700308 for (const auto& child : mCurrentChildren) {
309 child->onRemovedFromCurrentState();
310 }
311}
Chia-I Wu38512252017-05-17 14:36:16 -0700312
Chia-I Wuc6657022017-08-15 11:18:17 -0700313void Layer::onRemoved() {
314 // the layer is removed from SF mLayersPendingRemoval
315
316 mSurfaceFlingerConsumer->abandon();
Chia-I Wu38512252017-05-17 14:36:16 -0700317#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700318 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700319#endif
320
Robert Carr1f0a16a2016-10-24 16:27:39 -0700321 for (const auto& child : mCurrentChildren) {
322 child->onRemoved();
323 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700324}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325
Mathias Agopian13127d82013-03-05 17:47:11 -0800326// ---------------------------------------------------------------------------
327// set-up
328// ---------------------------------------------------------------------------
329
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700330const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800331 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332}
333
Mathias Agopianf9d93272009-06-19 17:00:27 -0700334status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 PixelFormat format, uint32_t flags)
336{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700337 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700338 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700339
340 // never allow a surface larger than what our underlying GL implementation
341 // can handle.
342 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800343 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700344 return BAD_VALUE;
345 }
346
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700347 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700348
Riley Andrews03414a12014-07-01 14:22:59 -0700349 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700350 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700351 mCurrentOpacity = getOpacityForFormat(format);
352
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800353 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
354 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
355 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 return NO_ERROR;
358}
359
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700360sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800361 Mutex::Autolock _l(mLock);
362
363 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700364 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800365
366 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700367
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700368 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800369}
370
Dan Stozab9b08832014-03-13 11:55:57 -0700371sp<IGraphicBufferProducer> Layer::getProducer() const {
372 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700373}
374
Mathias Agopian13127d82013-03-05 17:47:11 -0800375// ---------------------------------------------------------------------------
376// h/w composer set-up
377// ---------------------------------------------------------------------------
378
Steven Thomasb02664d2017-07-26 18:48:28 -0700379#ifdef USE_HWC2
380bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
381 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
382 "Already have a layer for hwcId %d", hwcId);
383 HWC2::Layer* layer = hwc->createLayer(hwcId);
384 if (!layer) {
385 return false;
386 }
387 HWCInfo& hwcInfo = mHwcLayers[hwcId];
388 hwcInfo.hwc = hwc;
389 hwcInfo.layer = layer;
390 layer->setLayerDestroyedListener(
391 [this, hwcId] (HWC2::Layer* /*layer*/){mHwcLayers.erase(hwcId);});
392 return true;
393}
394
Chia-I Wu92257612017-11-20 10:43:41 -0800395bool Layer::destroyHwcLayer(int32_t hwcId) {
Steven Thomasb02664d2017-07-26 18:48:28 -0700396 if (mHwcLayers.count(hwcId) == 0) {
Chia-I Wu92257612017-11-20 10:43:41 -0800397 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700398 }
399 auto& hwcInfo = mHwcLayers[hwcId];
400 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr,
401 "Attempt to destroy null layer");
402 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
403 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
404 // The layer destroyed listener should have cleared the entry from
405 // mHwcLayers. Verify that.
406 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
407 "Stale layer entry in mHwcLayers");
Chia-I Wu92257612017-11-20 10:43:41 -0800408
409 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700410}
411
412void Layer::destroyAllHwcLayers() {
413 size_t numLayers = mHwcLayers.size();
414 for (size_t i = 0; i < numLayers; ++i) {
415 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
416 destroyHwcLayer(mHwcLayers.begin()->first);
417 }
418 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
419 "All hardware composer layers should have been destroyed");
420}
421#endif
422
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800423Rect Layer::getContentCrop() const {
424 // this is the crop rectangle that applies to the buffer
425 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700426 Rect crop;
427 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800428 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700429 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800430 } else if (mActiveBuffer != NULL) {
431 // otherwise we use the whole buffer
432 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700433 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800434 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700435 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700436 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437 return crop;
438}
439
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700440static Rect reduce(const Rect& win, const Region& exclude) {
441 if (CC_LIKELY(exclude.isEmpty())) {
442 return win;
443 }
444 if (exclude.isRect()) {
445 return win.reduce(exclude.getBounds());
446 }
447 return Region(win).subtract(exclude).getBounds();
448}
449
Dan Stoza74a5e172017-12-20 15:57:52 -0800450static FloatRect reduce(const FloatRect& win, const Region& exclude) {
451 if (CC_LIKELY(exclude.isEmpty())) {
452 return win;
453 }
454 // Convert through Rect (by rounding) for lack of FloatRegion
455 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
456}
457
Robert Carr1f0a16a2016-10-24 16:27:39 -0700458Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
459 const Layer::State& s(getDrawingState());
460 Rect win(s.active.w, s.active.h);
461
462 if (!s.crop.isEmpty()) {
463 win.intersect(s.crop, &win);
464 }
465
466 Transform t = getTransform();
467 win = t.transform(win);
468
Robert Carr41b08b52017-06-01 16:11:34 -0700469 if (!s.finalCrop.isEmpty()) {
470 win.intersect(s.finalCrop, &win);
471 }
472
Chia-I Wue41dbe62017-06-13 14:10:56 -0700473 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700474 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
475 // When calculating the parent bounds for purposes of clipping,
476 // we don't need to constrain the parent to its transparent region.
477 // The transparent region is an optimization based on the
478 // buffer contents of the layer, but does not affect the space allocated to
479 // it by policy, and thus children should be allowed to extend into the
480 // parent's transparent region. In fact one of the main uses, is to reduce
481 // buffer allocation size in cases where a child window sits behind a main window
482 // (by marking the hole in the parent window as a transparent region)
483 if (p != nullptr) {
484 Rect bounds = p->computeScreenBounds(false);
485 bounds.intersect(win, &win);
486 }
487
488 if (reduceTransparentRegion) {
489 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
490 win = reduce(win, screenTransparentRegion);
491 }
492
493 return win;
494}
495
Dan Stoza74a5e172017-12-20 15:57:52 -0800496FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700497 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700498 return computeBounds(s.activeTransparentRegion);
499}
500
Dan Stoza74a5e172017-12-20 15:57:52 -0800501FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700502 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800503 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700504
505 if (!s.crop.isEmpty()) {
506 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800507 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508
509 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700510 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700511 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800512 // Look in computeScreenBounds recursive call for explanation of
513 // why we pass false here.
514 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700515 }
516
517 Transform t = getTransform();
Dan Stoza74a5e172017-12-20 15:57:52 -0800518
519 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520 if (p != nullptr) {
Dan Stoza74a5e172017-12-20 15:57:52 -0800521 floatWin = t.transform(floatWin);
522 floatWin = floatWin.intersect(bounds.toFloatRect());
523 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700524 }
525
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700526 // subtract the transparent region and snap to the bounds
Dan Stoza74a5e172017-12-20 15:57:52 -0800527 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800528}
529
Robert Carr1f0a16a2016-10-24 16:27:39 -0700530Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700531 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800532 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700533 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800534
535 // apply the projection's clipping to the window crop in
536 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700537 // if there are no window scaling involved, this operation will map to full
538 // pixels in the buffer.
539 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
540 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700541
542 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700543 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700544 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700545 }
546
Robert Carr1f0a16a2016-10-24 16:27:39 -0700547 Transform t = getTransform();
548 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
550 activeCrop.clear();
551 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700552 if (!s.finalCrop.isEmpty()) {
553 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000554 activeCrop.clear();
555 }
556 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700557 return activeCrop;
558}
559
Dan Stoza5a423ea2017-02-16 14:10:39 -0800560FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700561 // the content crop is the area of the content that gets scaled to the
562 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800563 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700564
565 // In addition there is a WM-specified crop we pull from our drawing state.
566 const State& s(getDrawingState());
567
568 // Screen space to make reduction to parent crop clearer.
569 Rect activeCrop = computeInitialCrop(hw);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700570 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700571 if (p != nullptr) {
572 auto parentCrop = p->computeInitialCrop(hw);
573 activeCrop.intersect(parentCrop, &activeCrop);
574 }
575 Transform t = getTransform();
576 // Back to layer space to work with the content crop.
577 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800578
Michael Lentine28ea2172014-11-19 18:32:37 -0800579 // This needs to be here as transform.transform(Rect) computes the
580 // transformed rect and then takes the bounding box of the result before
581 // returning. This means
582 // transform.inverse().transform(transform.transform(Rect)) != Rect
583 // in which case we need to make sure the final rect is clipped to the
584 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000585 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
586 activeCrop.clear();
587 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800588
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700589 // subtract the transparent region and snap to the bounds
590 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
591
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000592 // Transform the window crop to match the buffer coordinate system,
593 // which means using the inverse of the current transform set on the
594 // SurfaceFlingerConsumer.
595 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700596 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000597 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700598 * the code below applies the primary display's inverse transform to the
599 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000600 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700601 uint32_t invTransformOrient =
602 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000603 // calculate the inverse transform
604 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
605 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
606 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800607 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000608 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700609 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
610 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800611 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000612
613 int winWidth = s.active.w;
614 int winHeight = s.active.h;
615 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
616 // If the activeCrop has been rotate the ends are rotated but not
617 // the space itself so when transforming ends back we can't rely on
618 // a modification of the axes of rotation. To account for this we
619 // need to reorient the inverse rotation in terms of the current
620 // axes of rotation.
621 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
622 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
623 if (is_h_flipped == is_v_flipped) {
624 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
625 NATIVE_WINDOW_TRANSFORM_FLIP_H;
626 }
627 winWidth = s.active.h;
628 winHeight = s.active.w;
629 }
630 const Rect winCrop = activeCrop.transform(
631 invTransform, s.active.w, s.active.h);
632
633 // below, crop is intersected with winCrop expressed in crop's coordinate space
634 float xScale = crop.getWidth() / float(winWidth);
635 float yScale = crop.getHeight() / float(winHeight);
636
637 float insetL = winCrop.left * xScale;
638 float insetT = winCrop.top * yScale;
639 float insetR = (winWidth - winCrop.right ) * xScale;
640 float insetB = (winHeight - winCrop.bottom) * yScale;
641
642 crop.left += insetL;
643 crop.top += insetT;
644 crop.right -= insetR;
645 crop.bottom -= insetB;
646
Mathias Agopian13127d82013-03-05 17:47:11 -0800647 return crop;
648}
649
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000650#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800651void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000652#else
653void Layer::setGeometry(
654 const sp<const DisplayDevice>& hw,
655 HWComposer::HWCLayerInterface& layer)
656#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700657{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000658#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 const auto hwcId = displayDevice->getHwcDisplayId();
660 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000661#else
662 layer.setDefaultState();
663#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700664
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700665 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000666#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 hwcInfo.forceClientComposition = false;
668
669 if (isSecure() && !displayDevice->isSecure()) {
670 hwcInfo.forceClientComposition = true;
671 }
672
673 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000674#else
675 layer.setSkip(false);
676
677 if (isSecure() && !hw->isSecure()) {
678 layer.setSkip(true);
679 }
680#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700681
Mathias Agopian13127d82013-03-05 17:47:11 -0800682 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700683 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000684#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500685 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700686 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500687 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 }
David Revemanecf0fa52017-03-03 11:32:44 -0500690 auto error = hwcLayer->setBlendMode(blendMode);
691 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
692 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
693 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000694#else
Robert Carr6452f122017-03-21 10:41:29 -0700695 if (!isOpaque(s) || getAlpha() != 0xFF) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000696 layer.setBlending(mPremultipliedAlpha ?
697 HWC_BLENDING_PREMULT :
698 HWC_BLENDING_COVERAGE);
699 }
700#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800701
702 // apply the layer's transform, followed by the display's global transform
703 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700704 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700705 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700706 if (!s.crop.isEmpty()) {
707 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700708 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000709#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000710 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000711#else
712 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
713#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000714 activeCrop.clear();
715 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700716 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800717 // This needs to be here as transform.transform(Rect) computes the
718 // transformed rect and then takes the bounding box of the result before
719 // returning. This means
720 // transform.inverse().transform(transform.transform(Rect)) != Rect
721 // in which case we need to make sure the final rect is clipped to the
722 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000723 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
724 activeCrop.clear();
725 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700726 // mark regions outside the crop as transparent
727 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
728 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
729 s.active.w, s.active.h));
730 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
731 activeCrop.left, activeCrop.bottom));
732 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
733 s.active.w, activeCrop.bottom));
734 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700735
Dan Stoza74a5e172017-12-20 15:57:52 -0800736 // computeBounds returns a FloatRect to provide more accuracy during the
737 // transformation. We then round upon constructing 'frame'.
738 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700739 if (!s.finalCrop.isEmpty()) {
740 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000741 frame.clear();
742 }
743 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000744#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000745 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
746 frame.clear();
747 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748 const Transform& tr(displayDevice->getTransform());
749 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500750 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700751 if (error != HWC2::Error::None) {
752 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
753 mName.string(), transformedFrame.left, transformedFrame.top,
754 transformedFrame.right, transformedFrame.bottom,
755 to_string(error).c_str(), static_cast<int32_t>(error));
756 } else {
757 hwcInfo.displayFrame = transformedFrame;
758 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759
Dan Stoza5a423ea2017-02-16 14:10:39 -0800760 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700762 if (error != HWC2::Error::None) {
763 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
764 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
765 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
766 static_cast<int32_t>(error));
767 } else {
768 hwcInfo.sourceCrop = sourceCrop;
769 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770
Robert Carr6452f122017-03-21 10:41:29 -0700771 float alpha = getAlpha();
772 error = hwcLayer->setPlaneAlpha(alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
Robert Carr6452f122017-03-21 10:41:29 -0700774 "%s (%d)", mName.string(), alpha, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775 static_cast<int32_t>(error));
776
Robert Carrae060832016-11-28 10:51:00 -0800777 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800779 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500781
Albert Chaulk2a589632017-05-04 16:59:44 -0400782 int type = s.type;
783 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700784 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400785 if (parent.get()) {
786 auto& parentState = parent->getDrawingState();
787 type = parentState.type;
788 appId = parentState.appId;
789 }
790
791 error = hwcLayer->setInfo(type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500792 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
793 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000794#else
795 if (!frame.intersect(hw->getViewport(), &frame)) {
796 frame.clear();
797 }
798 const Transform& tr(hw->getTransform());
799 layer.setFrame(tr.transform(frame));
800 layer.setCrop(computeCrop(hw));
Robert Carr6452f122017-03-21 10:41:29 -0700801 layer.setPlaneAlpha(getAlpha());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000802#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800803
Mathias Agopian29a367b2011-07-12 14:51:45 -0700804 /*
805 * Transformations are applied in this order:
806 * 1) buffer orientation/flip/mirror
807 * 2) state transformation (window manager)
808 * 3) layer orientation (screen orientation)
809 * (NOTE: the matrices are multiplied in reverse order)
810 */
811
812 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700813 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700814
Robert Carrcae605c2017-03-29 12:10:31 -0700815 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700816 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700817 * the code below applies the primary display's inverse transform to the
818 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700819 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700820 uint32_t invTransform =
821 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700822 // calculate the inverse transform
823 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
824 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
825 NATIVE_WINDOW_TRANSFORM_FLIP_H;
826 }
Robert Carrcae605c2017-03-29 12:10:31 -0700827
828 /*
829 * Here we cancel out the orientation component of the WM transform.
830 * The scaling and translate components are already included in our bounds
831 * computation so it's enough to just omit it in the composition.
832 * See comment in onDraw with ref to b/36727915 for why.
833 */
834 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700835 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700836
837 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800838 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000839#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800840 if (orientation & Transform::ROT_INVALID) {
841 // we can only handle simple transformation
842 hwcInfo.forceClientComposition = true;
843 } else {
844 auto transform = static_cast<HWC2::Transform>(orientation);
845 auto error = hwcLayer->setTransform(transform);
846 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
847 "%s (%d)", mName.string(), to_string(transform).c_str(),
848 to_string(error).c_str(), static_cast<int32_t>(error));
849 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000850#else
851 if (orientation & Transform::ROT_INVALID) {
852 // we can only handle simple transformation
853 layer.setSkip(true);
854 } else {
855 layer.setTransform(orientation);
856 }
857#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700858}
859
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000860#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800861void Layer::forceClientComposition(int32_t hwcId) {
862 if (mHwcLayers.count(hwcId) == 0) {
863 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
864 return;
865 }
866
867 mHwcLayers[hwcId].forceClientComposition = true;
868}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800869
Dan Stoza9e56aa02015-11-02 13:00:03 -0800870void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
871 // Apply this display's projection's viewport to the visible region
872 // before giving it to the HWC HAL.
873 const Transform& tr = displayDevice->getTransform();
874 const auto& viewport = displayDevice->getViewport();
875 Region visible = tr.transform(visibleRegion.intersect(viewport));
876 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800877 auto& hwcInfo = mHwcLayers[hwcId];
878 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800879 auto error = hwcLayer->setVisibleRegion(visible);
880 if (error != HWC2::Error::None) {
881 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
882 to_string(error).c_str(), static_cast<int32_t>(error));
883 visible.dump(LOG_TAG);
884 }
885
886 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
887 if (error != HWC2::Error::None) {
888 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
889 to_string(error).c_str(), static_cast<int32_t>(error));
890 surfaceDamageRegion.dump(LOG_TAG);
891 }
892
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700893 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800894 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700895 setCompositionType(hwcId, HWC2::Composition::Sideband);
896 ALOGV("[%s] Requesting Sideband composition", mName.string());
897 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800898 if (error != HWC2::Error::None) {
899 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
900 mName.string(), mSidebandStream->handle(),
901 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800902 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700903 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800904 }
905
Dan Stoza0a21df72016-07-20 12:52:38 -0700906 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800907 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700908 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700909 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800910 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700911 return;
912 }
913
Dan Stoza0a21df72016-07-20 12:52:38 -0700914 // SolidColor layers
915 if (mActiveBuffer == nullptr) {
916 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700917
918 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700919 error = hwcLayer->setColor({0, 0, 0, 255});
920 if (error != HWC2::Error::None) {
921 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
922 to_string(error).c_str(), static_cast<int32_t>(error));
923 }
Dan Stozac6c89542016-07-27 10:16:52 -0700924
925 // Clear out the transform, because it doesn't make sense absent a
926 // source buffer
927 error = hwcLayer->setTransform(HWC2::Transform::None);
928 if (error != HWC2::Error::None) {
929 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
930 to_string(error).c_str(), static_cast<int32_t>(error));
931 }
932
Dan Stoza0a21df72016-07-20 12:52:38 -0700933 return;
934 }
935
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700936 // Device or Cursor layers
937 if (mPotentialCursor) {
938 ALOGV("[%s] Requesting Cursor composition", mName.string());
939 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800940 } else {
941 ALOGV("[%s] Requesting Device composition", mName.string());
942 setCompositionType(hwcId, HWC2::Composition::Device);
943 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700944
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700945 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
946 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
947 if (error != HWC2::Error::None) {
948 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
949 mCurrentState.dataSpace, to_string(error).c_str(),
950 static_cast<int32_t>(error));
951 }
952
Chia-I Wu06d63de2017-01-04 14:58:51 +0800953 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400954 sp<GraphicBuffer> hwcBuffer;
955 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
956 &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800957
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700958 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400959 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700960 if (error != HWC2::Error::None) {
961 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
962 mActiveBuffer->handle, to_string(error).c_str(),
963 static_cast<int32_t>(error));
964 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800965}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600966
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000967#else
968void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
969 HWComposer::HWCLayerInterface& layer) {
970 // we have to set the visible region on every frame because
971 // we currently free it during onLayerDisplayed(), which is called
972 // after HWComposer::commit() -- every frame.
973 // Apply this display's projection's viewport to the visible region
974 // before giving it to the HWC HAL.
975 const Transform& tr = hw->getTransform();
976 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
977 layer.setVisibleRegionScreen(visible);
978 layer.setSurfaceDamage(surfaceDamageRegion);
979 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700980
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000981 if (mSidebandStream.get()) {
982 layer.setSidebandStream(mSidebandStream);
983 } else {
984 // NOTE: buffer can be NULL if the client never drew into this
985 // layer yet, or if we ran out of memory
986 layer.setBuffer(mActiveBuffer);
987 }
988}
989#endif
990
991#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800992void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
993 auto hwcId = displayDevice->getHwcDisplayId();
994 if (mHwcLayers.count(hwcId) == 0 ||
995 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
996 return;
997 }
998
999 // This gives us only the "orientation" component of the transform
1000 const State& s(getCurrentState());
1001
1002 // Apply the layer's transform, followed by the display's global transform
1003 // Here we're guaranteed that the layer's transform preserves rects
1004 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001005 if (!s.crop.isEmpty()) {
1006 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001007 }
1008 // Subtract the transparent region and snap to the bounds
1009 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001010 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -08001011 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -07001012 if (!s.finalCrop.isEmpty()) {
1013 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001014 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001015 auto& displayTransform(displayDevice->getTransform());
1016 auto position = displayTransform.transform(frame);
1017
1018 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
1019 position.top);
1020 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
1021 "to (%d, %d): %s (%d)", mName.string(), position.left,
1022 position.top, to_string(error).c_str(),
1023 static_cast<int32_t>(error));
1024}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001025#else
1026void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
1027 HWComposer::HWCLayerInterface& layer) {
1028 int fenceFd = -1;
1029
1030 // TODO: there is a possible optimization here: we only need to set the
1031 // acquire fence the first time a new buffer is acquired on EACH display.
1032
1033 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
1034 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
1035 if (fence->isValid()) {
1036 fenceFd = fence->dup();
1037 if (fenceFd == -1) {
1038 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
1039 }
1040 }
1041 }
1042 layer.setAcquireFenceFd(fenceFd);
1043}
1044
1045Rect Layer::getPosition(
1046 const sp<const DisplayDevice>& hw)
1047{
1048 // this gives us only the "orientation" component of the transform
1049 const State& s(getCurrentState());
1050
1051 // apply the layer's transform, followed by the display's global transform
1052 // here we're guaranteed that the layer's transform preserves rects
1053 Rect win(s.active.w, s.active.h);
1054 if (!s.crop.isEmpty()) {
1055 win.intersect(s.crop, &win);
1056 }
1057 // subtract the transparent region and snap to the bounds
1058 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001059 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001060 frame.intersect(hw->getViewport(), &frame);
1061 if (!s.finalCrop.isEmpty()) {
1062 frame.intersect(s.finalCrop, &frame);
1063 }
1064 const Transform& tr(hw->getTransform());
1065 return Rect(tr.transform(frame));
1066}
1067#endif
Riley Andrews03414a12014-07-01 14:22:59 -07001068
Mathias Agopian13127d82013-03-05 17:47:11 -08001069// ---------------------------------------------------------------------------
1070// drawing...
1071// ---------------------------------------------------------------------------
1072
1073void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -08001074 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -08001075}
1076
Dan Stozac7014012014-02-14 15:03:43 -08001077void Layer::draw(const sp<const DisplayDevice>& hw,
1078 bool useIdentityTransform) const {
1079 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001080}
1081
Dan Stozac7014012014-02-14 15:03:43 -08001082void Layer::draw(const sp<const DisplayDevice>& hw) const {
1083 onDraw(hw, Region(hw->bounds()), false);
1084}
1085
Robert Carrcae605c2017-03-29 12:10:31 -07001086static constexpr mat4 inverseOrientation(uint32_t transform) {
1087 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1088 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1089 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1090 mat4 tr;
1091
1092 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
1093 tr = tr * rot90;
1094 }
1095 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
1096 tr = tr * flipH;
1097 }
1098 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
1099 tr = tr * flipV;
1100 }
1101 return inverse(tr);
1102}
1103
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001104/*
1105 * onDraw will draw the current layer onto the presentable buffer
1106 */
Dan Stozac7014012014-02-14 15:03:43 -08001107void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
1108 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001110 ATRACE_CALL();
1111
Mathias Agopiana67932f2011-04-20 14:20:59 -07001112 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001114 // in fact never been drawn into. This happens frequently with
1115 // SurfaceView because the WindowManager can't know when the client
1116 // has drawn the first time.
1117
1118 // If there is nothing under us, we paint the screen in black, otherwise
1119 // we just skip this update.
1120
1121 // figure out if there is something below us
1122 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001123 bool finished = false;
Dan Stoza412903f2017-04-27 13:42:17 -07001124 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001125 if (finished || layer == static_cast<Layer const*>(this)) {
1126 finished = true;
1127 return;
1128 }
Mathias Agopian42977342012-08-05 00:40:46 -07001129 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001130 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001131 // if not everything below us is covered, we plug the holes!
1132 Region holes(clip.subtract(under));
1133 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001134 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 return;
1137 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001138
Andy McFadden97eba892012-12-11 15:21:45 -08001139 // Bind the current buffer to the GL texture, and wait for it to be
1140 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001141 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1142 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001143 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001144 // Go ahead and draw the buffer anyway; no matter what we do the screen
1145 // is probably going to have something visibly wrong.
1146 }
1147
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001148 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1149
Mathias Agopian875d8e12013-06-07 15:35:48 -07001150 RenderEngine& engine(mFlinger->getRenderEngine());
1151
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001152 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001153 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001154 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001155
1156 // Query the texture matrix given our current filtering mode.
1157 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001158 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1159 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001160
Robert Carrcae605c2017-03-29 12:10:31 -07001161 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001162
1163 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001164 * the code below applies the primary display's inverse transform to
1165 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001166 */
Pablo Ceballos021623b2016-04-15 17:31:51 -07001167 uint32_t transform =
1168 DisplayDevice::getPrimaryDisplayOrientationTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001169 mat4 tr = inverseOrientation(transform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001170
Robert Carrcae605c2017-03-29 12:10:31 -07001171 /**
1172 * TODO(b/36727915): This is basically a hack.
1173 *
1174 * Ensure that regardless of the parent transformation,
1175 * this buffer is always transformed from native display
1176 * orientation to display orientation. For example, in the case
1177 * of a camera where the buffer remains in native orientation,
1178 * we want the pixels to always be upright.
1179 */
Chia-I Wue41dbe62017-06-13 14:10:56 -07001180 sp<Layer> p = mDrawingParent.promote();
1181 if (p != nullptr) {
1182 const auto parentTransform = p->getTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001183 tr = tr * inverseOrientation(parentTransform.getOrientation());
1184 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001185
1186 // and finally apply it to the original texture matrix
1187 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1188 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1189 }
1190
Jamie Genniscbb1a952012-05-08 17:05:52 -07001191 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001192 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1193 mTexture.setFiltering(useFiltering);
1194 mTexture.setMatrix(textureMatrix);
1195
1196 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001197 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001198 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001199 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001200 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001201 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202}
1203
Mathias Agopian13127d82013-03-05 17:47:11 -08001204
Dan Stozac7014012014-02-14 15:03:43 -08001205void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001206 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001207 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001208{
Mathias Agopian19733a32013-08-28 18:13:56 -07001209 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001210 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001211 engine.setupFillWithColor(red, green, blue, alpha);
1212 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001213}
1214
1215void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001216 const sp<const DisplayDevice>& hw) const {
1217 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001218}
1219
Dan Stozac7014012014-02-14 15:03:43 -08001220void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001221 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001222 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001223
Dan Stozac7014012014-02-14 15:03:43 -08001224 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001225
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 /*
1227 * NOTE: the way we compute the texture coordinates here produces
1228 * different results than when we take the HWC path -- in the later case
1229 * the "source crop" is rounded to texel boundaries.
1230 * This can produce significantly different results when the texture
1231 * is scaled by a large amount.
1232 *
1233 * The GL code below is more logical (imho), and the difference with
1234 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001235 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 * GL composition when a buffer scaling is applied (maybe with some
1237 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1238 * like more of a hack.
1239 */
Dan Stoza74a5e172017-12-20 15:57:52 -08001240 const Rect bounds{computeBounds()}; // Rounds from FloatRect
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001241
Robert Carr1f0a16a2016-10-24 16:27:39 -07001242 Transform t = getTransform();
Dan Stoza74a5e172017-12-20 15:57:52 -08001243 Rect win = bounds;
Robert Carrb5d3d262016-03-25 15:08:13 -07001244 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001245 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001246 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001247 win.clear();
1248 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001249 win = t.inverse().transform(win);
Dan Stoza74a5e172017-12-20 15:57:52 -08001250 if (!win.intersect(bounds, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001251 win.clear();
1252 }
1253 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001254
Mathias Agopian3f844832013-08-07 21:24:32 -07001255 float left = float(win.left) / float(s.active.w);
1256 float top = float(win.top) / float(s.active.h);
1257 float right = float(win.right) / float(s.active.w);
1258 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001259
Mathias Agopian875d8e12013-06-07 15:35:48 -07001260 // TODO: we probably want to generate the texture coords with the mesh
1261 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001262 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1263 texCoords[0] = vec2(left, 1.0f - top);
1264 texCoords[1] = vec2(left, 1.0f - bottom);
1265 texCoords[2] = vec2(right, 1.0f - bottom);
1266 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001267
Mathias Agopian875d8e12013-06-07 15:35:48 -07001268 RenderEngine& engine(mFlinger->getRenderEngine());
Robert Carr6452f122017-03-21 10:41:29 -07001269 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), getAlpha());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001270#ifdef USE_HWC2
1271 engine.setSourceDataSpace(mCurrentState.dataSpace);
1272#endif
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001273 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001274 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001275}
1276
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001277#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001278void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1279 bool callIntoHwc) {
1280 if (mHwcLayers.count(hwcId) == 0) {
1281 ALOGE("setCompositionType called without a valid HWC layer");
1282 return;
1283 }
1284 auto& hwcInfo = mHwcLayers[hwcId];
1285 auto& hwcLayer = hwcInfo.layer;
1286 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1287 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1288 if (hwcInfo.compositionType != type) {
1289 ALOGV(" actually setting");
1290 hwcInfo.compositionType = type;
1291 if (callIntoHwc) {
1292 auto error = hwcLayer->setCompositionType(type);
1293 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1294 "composition type %s: %s (%d)", mName.string(),
1295 to_string(type).c_str(), to_string(error).c_str(),
1296 static_cast<int32_t>(error));
1297 }
1298 }
1299}
1300
1301HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001302 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1303 // If we're querying the composition type for a display that does not
1304 // have a HWC counterpart, then it will always be Client
1305 return HWC2::Composition::Client;
1306 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001307 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001308 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001309 return HWC2::Composition::Invalid;
1310 }
1311 return mHwcLayers.at(hwcId).compositionType;
1312}
1313
1314void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1315 if (mHwcLayers.count(hwcId) == 0) {
1316 ALOGE("setClearClientTarget called without a valid HWC layer");
1317 return;
1318 }
1319 mHwcLayers[hwcId].clearClientTarget = clear;
1320}
1321
1322bool Layer::getClearClientTarget(int32_t hwcId) const {
1323 if (mHwcLayers.count(hwcId) == 0) {
1324 ALOGE("getClearClientTarget called without a valid HWC layer");
1325 return false;
1326 }
1327 return mHwcLayers.at(hwcId).clearClientTarget;
1328}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001329#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001330
Ruben Brunk1681d952014-06-27 15:51:55 -07001331uint32_t Layer::getProducerStickyTransform() const {
1332 int producerStickyTransform = 0;
1333 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1334 if (ret != OK) {
1335 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1336 strerror(-ret), ret);
1337 return 0;
1338 }
1339 return static_cast<uint32_t>(producerStickyTransform);
1340}
1341
Dan Stozac5da2712016-07-20 15:38:12 -07001342bool Layer::latchUnsignaledBuffers() {
1343 static bool propertyLoaded = false;
1344 static bool latch = false;
1345 static std::mutex mutex;
1346 std::lock_guard<std::mutex> lock(mutex);
1347 if (!propertyLoaded) {
1348 char value[PROPERTY_VALUE_MAX] = {};
1349 property_get("debug.sf.latch_unsignaled", value, "0");
1350 latch = atoi(value);
1351 propertyLoaded = true;
1352 }
1353 return latch;
1354}
1355
Dan Stozacac35382016-01-27 12:21:06 -08001356uint64_t Layer::getHeadFrameNumber() const {
1357 Mutex::Autolock lock(mQueueItemLock);
1358 if (!mQueueItems.empty()) {
1359 return mQueueItems[0].mFrameNumber;
1360 } else {
1361 return mCurrentFrameNumber;
1362 }
1363}
1364
Dan Stoza1ce65812016-06-15 16:26:27 -07001365bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001366#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001367 if (latchUnsignaledBuffers()) {
1368 return true;
1369 }
1370
Dan Stoza1ce65812016-06-15 16:26:27 -07001371 Mutex::Autolock lock(mQueueItemLock);
1372 if (mQueueItems.empty()) {
1373 return true;
1374 }
1375 if (mQueueItems[0].mIsDroppable) {
1376 // Even though this buffer's fence may not have signaled yet, it could
1377 // be replaced by another buffer before it has a chance to, which means
1378 // that it's possible to get into a situation where a buffer is never
1379 // able to be latched. To avoid this, grab this buffer anyway.
1380 return true;
1381 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001382 return mQueueItems[0].mFenceTime->getSignalTime() !=
1383 Fence::SIGNAL_TIME_PENDING;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001384#else
1385 return true;
1386#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001387}
1388
Dan Stozacac35382016-01-27 12:21:06 -08001389bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1390 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1391 // Don't bother with a SyncPoint, since we've already latched the
1392 // relevant frame
1393 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001394 }
1395
Dan Stozacac35382016-01-27 12:21:06 -08001396 Mutex::Autolock lock(mLocalSyncPointMutex);
1397 mLocalSyncPoints.push_back(point);
1398 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001399}
1400
Mathias Agopian13127d82013-03-05 17:47:11 -08001401void Layer::setFiltering(bool filtering) {
1402 mFiltering = filtering;
1403}
1404
1405bool Layer::getFiltering() const {
1406 return mFiltering;
1407}
1408
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001409// As documented in libhardware header, formats in the range
1410// 0x100 - 0x1FF are specific to the HAL implementation, and
1411// are known to have no alpha channel
1412// TODO: move definition for device-specific range into
1413// hardware.h, instead of using hard-coded values here.
1414#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1415
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001416bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001417 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1418 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001419 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001420 switch (format) {
1421 case HAL_PIXEL_FORMAT_RGBA_8888:
1422 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001423 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001424 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001425 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001426 }
1427 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001428 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001429}
1430
Mathias Agopian13127d82013-03-05 17:47:11 -08001431// ----------------------------------------------------------------------------
1432// local state
1433// ----------------------------------------------------------------------------
1434
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001435static void boundPoint(vec2* point, const Rect& crop) {
1436 if (point->x < crop.left) {
1437 point->x = crop.left;
1438 }
1439 if (point->x > crop.right) {
1440 point->x = crop.right;
1441 }
1442 if (point->y < crop.top) {
1443 point->y = crop.top;
1444 }
1445 if (point->y > crop.bottom) {
1446 point->y = crop.bottom;
1447 }
1448}
1449
Dan Stozac7014012014-02-14 15:03:43 -08001450void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1451 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001452{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001453 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001454 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 const uint32_t hw_h = hw->getHeight();
Dan Stoza74a5e172017-12-20 15:57:52 -08001456 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001457
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001458 vec2 lt = vec2(win.left, win.top);
1459 vec2 lb = vec2(win.left, win.bottom);
1460 vec2 rb = vec2(win.right, win.bottom);
1461 vec2 rt = vec2(win.right, win.top);
1462
Robert Carr1f0a16a2016-10-24 16:27:39 -07001463 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001464 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001465 lt = layerTransform.transform(lt);
1466 lb = layerTransform.transform(lb);
1467 rb = layerTransform.transform(rb);
1468 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001469 }
1470
Robert Carrb5d3d262016-03-25 15:08:13 -07001471 if (!s.finalCrop.isEmpty()) {
1472 boundPoint(&lt, s.finalCrop);
1473 boundPoint(&lb, s.finalCrop);
1474 boundPoint(&rb, s.finalCrop);
1475 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001476 }
1477
Mathias Agopianff2ed702013-09-01 21:36:12 -07001478 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001479 position[0] = hwTransform.transform(lt);
1480 position[1] = hwTransform.transform(lb);
1481 position[2] = hwTransform.transform(rb);
1482 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001483 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001484 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001485 }
1486}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001487
Andy McFadden4125a4f2014-01-29 17:17:11 -08001488bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001489{
neo.hee6fd41d2017-02-17 14:35:33 +08001490 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
Mathias Agopiana67932f2011-04-20 14:20:59 -07001491 // layer's opaque flag.
neo.hee6fd41d2017-02-17 14:35:33 +08001492 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001493 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001494 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001495
1496 // if the layer has the opaque flag, then we're always opaque,
1497 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001498 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001499}
1500
Dan Stoza23116082015-06-18 14:58:39 -07001501bool Layer::isSecure() const
1502{
1503 const Layer::State& s(mDrawingState);
1504 return (s.flags & layer_state_t::eLayerSecure);
1505}
1506
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001507bool Layer::isProtected() const
1508{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001509 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001510 return (activeBuffer != 0) &&
1511 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1512}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001513
Mathias Agopian13127d82013-03-05 17:47:11 -08001514bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001515 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001516}
1517
1518bool Layer::isCropped() const {
1519 return !mCurrentCrop.isEmpty();
1520}
1521
1522bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1523 return mNeedsFiltering || hw->needsFiltering();
1524}
1525
1526void Layer::setVisibleRegion(const Region& visibleRegion) {
1527 // always called from main thread
1528 this->visibleRegion = visibleRegion;
1529}
1530
1531void Layer::setCoveredRegion(const Region& coveredRegion) {
1532 // always called from main thread
1533 this->coveredRegion = coveredRegion;
1534}
1535
1536void Layer::setVisibleNonTransparentRegion(const Region&
1537 setVisibleNonTransparentRegion) {
1538 // always called from main thread
1539 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1540}
1541
1542// ----------------------------------------------------------------------------
1543// transaction
1544// ----------------------------------------------------------------------------
1545
Dan Stoza7dde5992015-05-22 09:51:44 -07001546void Layer::pushPendingState() {
1547 if (!mCurrentState.modified) {
1548 return;
1549 }
1550
Dan Stoza7dde5992015-05-22 09:51:44 -07001551 // If this transaction is waiting on the receipt of a frame, generate a sync
1552 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001553 if (mCurrentState.barrierLayer != nullptr) {
1554 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1555 if (barrierLayer == nullptr) {
1556 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001557 // If we can't promote the layer we are intended to wait on,
1558 // then it is expired or otherwise invalid. Allow this transaction
1559 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001560 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001561 } else {
1562 auto syncPoint = std::make_shared<SyncPoint>(
1563 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001564 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001565 mRemoteSyncPoints.push_back(std::move(syncPoint));
1566 } else {
1567 // We already missed the frame we're supposed to synchronize
1568 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001569 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001570 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001571 }
1572
Dan Stoza7dde5992015-05-22 09:51:44 -07001573 // Wake us up to check if the frame has been received
1574 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001575 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001576 }
1577 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001578 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001579}
1580
Pablo Ceballos05289c22016-04-14 15:49:55 -07001581void Layer::popPendingState(State* stateToCommit) {
1582 auto oldFlags = stateToCommit->flags;
1583 *stateToCommit = mPendingStates[0];
1584 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1585 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001586
1587 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001588 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001589}
1590
Pablo Ceballos05289c22016-04-14 15:49:55 -07001591bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001592 bool stateUpdateAvailable = false;
1593 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001594 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001595 if (mRemoteSyncPoints.empty()) {
1596 // If we don't have a sync point for this, apply it anyway. It
1597 // will be visually wrong, but it should keep us from getting
1598 // into too much trouble.
1599 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001600 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001601 stateUpdateAvailable = true;
1602 continue;
1603 }
1604
Dan Stozacac35382016-01-27 12:21:06 -08001605 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1606 mPendingStates[0].frameNumber) {
1607 ALOGE("[%s] Unexpected sync point frame number found",
1608 mName.string());
1609
1610 // Signal our end of the sync point and then dispose of it
1611 mRemoteSyncPoints.front()->setTransactionApplied();
1612 mRemoteSyncPoints.pop_front();
1613 continue;
1614 }
1615
Dan Stoza7dde5992015-05-22 09:51:44 -07001616 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1617 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001618 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001619 stateUpdateAvailable = true;
1620
1621 // Signal our end of the sync point and then dispose of it
1622 mRemoteSyncPoints.front()->setTransactionApplied();
1623 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001624 } else {
1625 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001626 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001627 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001628 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001629 stateUpdateAvailable = true;
1630 }
1631 }
1632
1633 // If we still have pending updates, wake SurfaceFlinger back up and point
1634 // it at this layer so we can process them
1635 if (!mPendingStates.empty()) {
1636 setTransactionFlags(eTransactionNeeded);
1637 mFlinger->setTransactionFlags(eTraversalNeeded);
1638 }
1639
1640 mCurrentState.modified = false;
1641 return stateUpdateAvailable;
1642}
1643
1644void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001645 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001646 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001647 Mutex::Autolock lock(mLocalSyncPointMutex);
1648 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001649 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001650 point->setFrameAvailable();
1651 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001652 }
1653}
1654
Mathias Agopian13127d82013-03-05 17:47:11 -08001655uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001656 ATRACE_CALL();
1657
Dan Stoza7dde5992015-05-22 09:51:44 -07001658 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001659 Layer::State c = getCurrentState();
1660 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001661 return 0;
1662 }
1663
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001664 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001666 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1667 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001668
1669 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001670 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001671 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001672 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001673 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001674 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001675 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001676 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001677 this, getName().string(), mCurrentTransform,
1678 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001679 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001680 c.crop.left,
1681 c.crop.top,
1682 c.crop.right,
1683 c.crop.bottom,
1684 c.crop.getWidth(),
1685 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001686 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001687 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001688 s.crop.left,
1689 s.crop.top,
1690 s.crop.right,
1691 s.crop.bottom,
1692 s.crop.getWidth(),
1693 s.crop.getHeight(),
1694 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001696 // record the new size, form this point on, when the client request
1697 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001698 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001699 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001701
Robert Carr82364e32016-05-15 11:27:47 -07001702 const bool resizePending = (c.requested.w != c.active.w) ||
1703 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001704 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001705 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001706 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001707 // if we have a pending resize, unless we are in fixed-size mode.
1708 // the drawing state will be updated only once we receive a buffer
1709 // with the correct size.
1710 //
1711 // in particular, we want to make sure the clip (which is part
1712 // of the geometry state) is latched together with the size but is
1713 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001714 //
1715 // If a sideband stream is attached, however, we want to skip this
1716 // optimization so that transactions aren't missed when a buffer
1717 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001718
1719 flags |= eDontUpdateGeometryState;
1720 }
1721 }
1722
Robert Carr7bf247e2017-05-18 14:02:49 -07001723 // Here we apply various requested geometry states, depending on our
1724 // latching configuration. See Layer.h for a detailed discussion of
1725 // how geometry latching is controlled.
1726 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001727 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001728
1729 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1730 // mode, which causes attributes which normally latch regardless of scaling mode,
1731 // to be delayed. We copy the requested state to the active state making sure
1732 // to respect these rules (again see Layer.h for a detailed discussion).
1733 //
1734 // There is an awkward asymmetry in the handling of the crop states in the position
1735 // states, as can be seen below. Largely this arises from position and transform
1736 // being stored in the same data structure while having different latching rules.
1737 // b/38182305
1738 //
1739 // Careful that "c" and editCurrentState may not begin as equivalent due to
1740 // applyPendingStates in the presence of deferred transactions.
1741 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001742 float tx = c.active.transform.tx();
1743 float ty = c.active.transform.ty();
1744 c.active = c.requested;
1745 c.active.transform.set(tx, ty);
1746 editCurrentState.active = c.active;
1747 } else {
1748 editCurrentState.active = editCurrentState.requested;
1749 c.active = c.requested;
1750 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001751 }
1752
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001753 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001754 // invalidate and recompute the visible regions if needed
1755 flags |= Layer::eVisibleRegion;
1756 }
1757
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001758 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001759 // invalidate and recompute the visible regions if needed
1760 flags |= eVisibleRegion;
1761 this->contentDirty = true;
1762
1763 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001764 const uint8_t type = c.active.transform.getType();
1765 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001766 (type >= Transform::SCALE));
1767 }
1768
Dan Stozac8145172016-04-28 16:29:06 -07001769 // If the layer is hidden, signal and clear out all local sync points so
1770 // that transactions for layers depending on this layer's frames becoming
1771 // visible are not blocked
1772 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001773 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001774 }
1775
Mathias Agopian13127d82013-03-05 17:47:11 -08001776 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001777 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001778 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779}
1780
Pablo Ceballos05289c22016-04-14 15:49:55 -07001781void Layer::commitTransaction(const State& stateToCommit) {
1782 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001783}
1784
Mathias Agopian13127d82013-03-05 17:47:11 -08001785uint32_t Layer::getTransactionFlags(uint32_t flags) {
1786 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1787}
1788
1789uint32_t Layer::setTransactionFlags(uint32_t flags) {
1790 return android_atomic_or(flags, &mTransactionFlags);
1791}
1792
Robert Carr82364e32016-05-15 11:27:47 -07001793bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001794 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001795 return false;
1796 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001797
1798 // We update the requested and active position simultaneously because
1799 // we want to apply the position portion of the transform matrix immediately,
1800 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001801 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001802 if (immediate && !mFreezeGeometryUpdates) {
1803 // Here we directly update the active state
1804 // unlike other setters, because we store it within
1805 // the transform, but use different latching rules.
1806 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001807 mCurrentState.active.transform.set(x, y);
1808 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001809 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001810
Dan Stoza7dde5992015-05-22 09:51:44 -07001811 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001812 setTransactionFlags(eTransactionNeeded);
1813 return true;
1814}
Robert Carr82364e32016-05-15 11:27:47 -07001815
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1817 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1818 if (idx < 0) {
1819 return false;
1820 }
1821 if (childLayer->setLayer(z)) {
1822 mCurrentChildren.removeAt(idx);
1823 mCurrentChildren.add(childLayer);
1824 }
1825 return true;
1826}
1827
Robert Carrae060832016-11-28 10:51:00 -08001828bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001829 if (mCurrentState.z == z)
1830 return false;
1831 mCurrentState.sequence++;
1832 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001833 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001834
1835 // Discard all relative layering.
1836 if (mCurrentState.zOrderRelativeOf != nullptr) {
1837 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1838 if (strongRelative != nullptr) {
1839 strongRelative->removeZOrderRelative(this);
1840 }
1841 mCurrentState.zOrderRelativeOf = nullptr;
1842 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001843 setTransactionFlags(eTransactionNeeded);
1844 return true;
1845}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846
Robert Carrdb66e622017-04-10 16:55:57 -07001847void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1848 mCurrentState.zOrderRelatives.remove(relative);
1849 mCurrentState.sequence++;
1850 mCurrentState.modified = true;
1851 setTransactionFlags(eTransactionNeeded);
1852}
1853
1854void Layer::addZOrderRelative(const wp<Layer>& relative) {
1855 mCurrentState.zOrderRelatives.add(relative);
1856 mCurrentState.modified = true;
1857 mCurrentState.sequence++;
1858 setTransactionFlags(eTransactionNeeded);
1859}
1860
1861bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1862 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1863 if (handle == nullptr) {
1864 return false;
1865 }
1866 sp<Layer> relative = handle->owner.promote();
1867 if (relative == nullptr) {
1868 return false;
1869 }
1870
1871 mCurrentState.sequence++;
1872 mCurrentState.modified = true;
1873 mCurrentState.z = z;
1874
1875 mCurrentState.zOrderRelativeOf = relative;
1876 relative->addZOrderRelative(this);
1877
1878 setTransactionFlags(eTransactionNeeded);
1879
1880 return true;
1881}
1882
Mathias Agopian13127d82013-03-05 17:47:11 -08001883bool Layer::setSize(uint32_t w, uint32_t h) {
1884 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1885 return false;
1886 mCurrentState.requested.w = w;
1887 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001888 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001889 setTransactionFlags(eTransactionNeeded);
1890 return true;
1891}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001892#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001893bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001894#else
1895bool Layer::setAlpha(uint8_t alpha) {
1896#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001897 if (mCurrentState.alpha == alpha)
1898 return false;
1899 mCurrentState.sequence++;
1900 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001901 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001902 setTransactionFlags(eTransactionNeeded);
1903 return true;
1904}
1905bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1906 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001907 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001908 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001909 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001910 setTransactionFlags(eTransactionNeeded);
1911 return true;
1912}
1913bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001914 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001915 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001916 setTransactionFlags(eTransactionNeeded);
1917 return true;
1918}
1919bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1920 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1921 if (mCurrentState.flags == newFlags)
1922 return false;
1923 mCurrentState.sequence++;
1924 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001925 mCurrentState.mask = mask;
1926 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001927 setTransactionFlags(eTransactionNeeded);
1928 return true;
1929}
Robert Carr99e27f02016-06-16 15:18:02 -07001930
1931bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001932 if (mCurrentState.requestedCrop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001933 return false;
1934 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001935 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001936 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001937 mCurrentState.crop = crop;
1938 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001939 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1940
Dan Stoza7dde5992015-05-22 09:51:44 -07001941 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001942 setTransactionFlags(eTransactionNeeded);
1943 return true;
1944}
Robert Carr8d5227b2017-03-16 15:41:03 -07001945
1946bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001947 if (mCurrentState.requestedFinalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001948 return false;
1949 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001950 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001951 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001952 mCurrentState.finalCrop = crop;
1953 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001954 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1955
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001956 mCurrentState.modified = true;
1957 setTransactionFlags(eTransactionNeeded);
1958 return true;
1959}
Mathias Agopian13127d82013-03-05 17:47:11 -08001960
Robert Carrc3574f72016-03-24 12:19:32 -07001961bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1962 if (scalingMode == mOverrideScalingMode)
1963 return false;
1964 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001965 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001966 return true;
1967}
1968
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001969void Layer::setInfo(uint32_t type, uint32_t appId) {
1970 mCurrentState.appId = appId;
1971 mCurrentState.type = type;
1972 mCurrentState.modified = true;
1973 setTransactionFlags(eTransactionNeeded);
1974}
1975
Robert Carrc3574f72016-03-24 12:19:32 -07001976uint32_t Layer::getEffectiveScalingMode() const {
1977 if (mOverrideScalingMode >= 0) {
1978 return mOverrideScalingMode;
1979 }
1980 return mCurrentScalingMode;
1981}
1982
Mathias Agopian13127d82013-03-05 17:47:11 -08001983bool Layer::setLayerStack(uint32_t layerStack) {
1984 if (mCurrentState.layerStack == layerStack)
1985 return false;
1986 mCurrentState.sequence++;
1987 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001988 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001989 setTransactionFlags(eTransactionNeeded);
1990 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001991}
1992
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001993bool Layer::setDataSpace(android_dataspace dataSpace) {
1994 if (mCurrentState.dataSpace == dataSpace)
1995 return false;
1996 mCurrentState.sequence++;
1997 mCurrentState.dataSpace = dataSpace;
1998 mCurrentState.modified = true;
1999 setTransactionFlags(eTransactionNeeded);
2000 return true;
2001}
2002
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06002003android_dataspace Layer::getDataSpace() const {
2004 return mCurrentState.dataSpace;
2005}
2006
Robert Carr1f0a16a2016-10-24 16:27:39 -07002007uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002008 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002009 if (p == nullptr) {
2010 return getDrawingState().layerStack;
2011 }
2012 return p->getLayerStack();
2013}
2014
Robert Carr0d480722017-01-10 16:42:54 -08002015void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07002016 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08002017 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07002018 mCurrentState.frameNumber = frameNumber;
2019 // We don't set eTransactionNeeded, because just receiving a deferral
2020 // request without any other state updates shouldn't actually induce a delay
2021 mCurrentState.modified = true;
2022 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08002023 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08002024 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07002025 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08002026}
2027
2028void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
2029 uint64_t frameNumber) {
2030 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
2031 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07002032}
2033
Dan Stozaee44edd2015-03-23 15:50:23 -07002034void Layer::useSurfaceDamage() {
2035 if (mFlinger->mForceFullDamage) {
2036 surfaceDamageRegion = Region::INVALID_REGION;
2037 } else {
2038 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
2039 }
2040}
2041
2042void Layer::useEmptyDamage() {
2043 surfaceDamageRegion.clear();
2044}
2045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046// ----------------------------------------------------------------------------
2047// pageflip handling...
2048// ----------------------------------------------------------------------------
2049
Dan Stoza6b9454d2014-11-07 16:00:59 -08002050bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002051 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07002052 return true;
2053 }
2054
Dan Stoza6b9454d2014-11-07 16:00:59 -08002055 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002056 if (mQueueItems.empty()) {
2057 return false;
2058 }
2059 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002060 nsecs_t expectedPresent =
2061 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002062
2063 // Ignore timestamps more than a second in the future
2064 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
2065 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
2066 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
2067 expectedPresent);
2068
2069 bool isDue = timestamp < expectedPresent;
2070 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002071}
2072
Brian Andersond6927fb2016-07-23 23:37:30 -07002073bool Layer::onPreComposition(nsecs_t refreshStartTime) {
2074 if (mBufferLatched) {
2075 Mutex::Autolock lock(mFrameEventHistoryMutex);
2076 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
2077 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002078 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002079 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002080}
2081
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002082bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07002083 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002084 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002085 // mFrameLatencyNeeded is true when a new frame was latched for the
2086 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08002087 if (!mFrameLatencyNeeded)
2088 return false;
2089
Fabien Sanglard28e98082016-12-05 10:19:46 -08002090 // Update mFrameEventHistory.
2091 {
2092 Mutex::Autolock lock(mFrameEventHistoryMutex);
2093 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002094 glDoneFence, presentFence, compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002095 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08002096
2097 // Update mFrameTracker.
2098 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
2099 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
2100
Brian Anderson3d4039d2016-09-23 16:31:30 -07002101 std::shared_ptr<FenceTime> frameReadyFence =
2102 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08002103 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002104 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002105 } else {
2106 // There was no fence for this frame, so assume that it was ready
2107 // to be presented at the desired present time.
2108 mFrameTracker.setFrameReadyTime(desiredPresentTime);
2109 }
2110
Brian Anderson3d4039d2016-09-23 16:31:30 -07002111 if (presentFence->isValid()) {
2112 mFrameTracker.setActualPresentFence(
2113 std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002114 } else {
2115 // The HWC doesn't support present fences, so use the refresh
2116 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002117 mFrameTracker.setActualPresentTime(
2118 mFlinger->getHwComposer().getRefreshTimestamp(
2119 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002120 }
2121
2122 mFrameTracker.advanceFrame();
2123 mFrameLatencyNeeded = false;
2124 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002125}
2126
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002127#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07002128void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08002129 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
2130 return;
2131 }
2132
Brian Anderson3d4039d2016-09-23 16:31:30 -07002133 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002134 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002135 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002136 mReleaseTimeline.push(releaseFenceTime);
2137
2138 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002139 if (mPreviousFrameNumber != 0) {
2140 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2141 dequeueReadyTime, std::move(releaseFenceTime));
2142 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002143}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002144#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08002145
Robert Carr1f0a16a2016-10-24 16:27:39 -07002146bool Layer::isHiddenByPolicy() const {
2147 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07002148 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002149 if (parent != nullptr && parent->isHiddenByPolicy()) {
2150 return true;
2151 }
2152 return s.flags & layer_state_t::eLayerHidden;
2153}
2154
Mathias Agopianda27af92012-09-13 18:17:13 -07002155bool Layer::isVisible() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002156#ifdef USE_HWC2
Robert Carr6452f122017-03-21 10:41:29 -07002157 return !(isHiddenByPolicy()) && getAlpha() > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08002158 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002159#else
Robert Carr6452f122017-03-21 10:41:29 -07002160 return !(isHiddenByPolicy()) && getAlpha()
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002161 && (mActiveBuffer != NULL || mSidebandStream != NULL);
2162#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07002163}
2164
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002165bool Layer::allTransactionsSignaled() {
2166 auto headFrameNumber = getHeadFrameNumber();
2167 bool matchingFramesFound = false;
2168 bool allTransactionsApplied = true;
2169 Mutex::Autolock lock(mLocalSyncPointMutex);
2170
2171 for (auto& point : mLocalSyncPoints) {
2172 if (point->getFrameNumber() > headFrameNumber) {
2173 break;
2174 }
2175 matchingFramesFound = true;
2176
2177 if (!point->frameIsAvailable()) {
2178 // We haven't notified the remote layer that the frame for
2179 // this point is available yet. Notify it now, and then
2180 // abort this attempt to latch.
2181 point->setFrameAvailable();
2182 allTransactionsApplied = false;
2183 break;
2184 }
2185
2186 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
2187 }
2188 return !matchingFramesFound || allTransactionsApplied;
2189}
2190
Brian Andersond6927fb2016-07-23 23:37:30 -07002191Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002192{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002193 ATRACE_CALL();
2194
Jesse Hall399184a2014-03-03 15:42:54 -08002195 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2196 // mSidebandStreamChanged was true
2197 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002198 if (mSidebandStream != NULL) {
2199 setTransactionFlags(eTransactionNeeded);
2200 mFlinger->setTransactionFlags(eTraversalNeeded);
2201 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002202 recomputeVisibleRegions = true;
2203
2204 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002205 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002206 }
2207
Mathias Agopian4fec8732012-06-29 14:12:52 -07002208 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002209 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2210 return outDirtyRegion;
2211 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002212
Fabien Sanglard223eb912016-10-13 10:15:06 -07002213 // if we've already called updateTexImage() without going through
2214 // a composition step, we have to skip this layer at this point
2215 // because we cannot call updateTeximage() without a corresponding
2216 // compositionComplete() call.
2217 // we'll trigger an update in onPreComposition().
2218 if (mRefreshPending) {
2219 return outDirtyRegion;
2220 }
2221
2222 // If the head buffer's acquire fence hasn't signaled yet, return and
2223 // try again later
2224 if (!headFenceHasSignaled()) {
2225 mFlinger->signalLayerUpdate();
2226 return outDirtyRegion;
2227 }
2228
2229 // Capture the old state of the layer for comparisons later
2230 const State& s(getDrawingState());
2231 const bool oldOpacity = isOpaque(s);
2232 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2233
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002234 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002235 mFlinger->signalLayerUpdate();
2236 return outDirtyRegion;
2237 }
2238
2239 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2240 // of the buffer queue isn't modified when the buffer queue is returning
2241 // BufferItem's that weren't actually queued. This can happen in shared
2242 // buffer mode.
2243 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002244 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2245 getProducerStickyTransform() != 0, mName.string(),
Robert Carr7bf247e2017-05-18 14:02:49 -07002246 mOverrideScalingMode, mFreezeGeometryUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002247 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2248 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2249 mLastFrameNumberReceived);
2250 if (updateResult == BufferQueue::PRESENT_LATER) {
2251 // Producer doesn't want buffer to be displayed yet. Signal a
2252 // layer update so we check again at the next opportunity.
2253 mFlinger->signalLayerUpdate();
2254 return outDirtyRegion;
2255 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2256 // If the buffer has been rejected, remove it from the shadow queue
2257 // and return early
2258 if (queuedBuffer) {
2259 Mutex::Autolock lock(mQueueItemLock);
2260 mQueueItems.removeAt(0);
2261 android_atomic_dec(&mQueuedFrames);
2262 }
2263 return outDirtyRegion;
2264 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2265 // This can occur if something goes wrong when trying to create the
2266 // EGLImage for this buffer. If this happens, the buffer has already
2267 // been released, so we need to clean up the queue and bug out
2268 // early.
2269 if (queuedBuffer) {
2270 Mutex::Autolock lock(mQueueItemLock);
2271 mQueueItems.clear();
2272 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002273 }
2274
Fabien Sanglard223eb912016-10-13 10:15:06 -07002275 // Once we have hit this state, the shadow queue may no longer
2276 // correctly reflect the incoming BufferQueue's contents, so even if
2277 // updateTexImage starts working, the only safe course of action is
2278 // to continue to ignore updates.
2279 mUpdateTexImageFailed = true;
2280
2281 return outDirtyRegion;
2282 }
2283
2284 if (queuedBuffer) {
2285 // Autolock scope
2286 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2287
2288 Mutex::Autolock lock(mQueueItemLock);
2289
2290 // Remove any stale buffers that have been dropped during
2291 // updateTexImage
2292 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2293 mQueueItems.removeAt(0);
2294 android_atomic_dec(&mQueuedFrames);
2295 }
2296
2297 mQueueItems.removeAt(0);
2298 }
2299
2300
2301 // Decrement the queued-frames count. Signal another event if we
2302 // have more frames pending.
2303 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2304 || mAutoRefresh) {
2305 mFlinger->signalLayerUpdate();
2306 }
2307
Fabien Sanglard223eb912016-10-13 10:15:06 -07002308 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002309 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2310 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002311 if (mActiveBuffer == NULL) {
2312 // this can only happen if the very first buffer was rejected.
2313 return outDirtyRegion;
2314 }
2315
Brian Andersond6927fb2016-07-23 23:37:30 -07002316 mBufferLatched = true;
2317 mPreviousFrameNumber = mCurrentFrameNumber;
2318 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2319
2320 {
2321 Mutex::Autolock lock(mFrameEventHistoryMutex);
2322 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2323#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002324 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002325 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002326 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002327 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002328 if (mPreviousFrameNumber != 0) {
2329 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2330 latchTime, std::move(releaseFenceTime));
2331 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002332#endif
2333 }
2334
Fabien Sanglard223eb912016-10-13 10:15:06 -07002335 mRefreshPending = true;
2336 mFrameLatencyNeeded = true;
2337 if (oldActiveBuffer == NULL) {
2338 // the first time we receive a buffer, we need to trigger a
2339 // geometry invalidation.
2340 recomputeVisibleRegions = true;
2341 }
2342
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002343 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2344
Fabien Sanglard223eb912016-10-13 10:15:06 -07002345 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2346 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2347 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2348 if ((crop != mCurrentCrop) ||
2349 (transform != mCurrentTransform) ||
2350 (scalingMode != mCurrentScalingMode))
2351 {
2352 mCurrentCrop = crop;
2353 mCurrentTransform = transform;
2354 mCurrentScalingMode = scalingMode;
2355 recomputeVisibleRegions = true;
2356 }
2357
2358 if (oldActiveBuffer != NULL) {
2359 uint32_t bufWidth = mActiveBuffer->getWidth();
2360 uint32_t bufHeight = mActiveBuffer->getHeight();
2361 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2362 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002363 recomputeVisibleRegions = true;
2364 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002365 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002366
Fabien Sanglard223eb912016-10-13 10:15:06 -07002367 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2368 if (oldOpacity != isOpaque(s)) {
2369 recomputeVisibleRegions = true;
2370 }
Dan Stozacac35382016-01-27 12:21:06 -08002371
Fabien Sanglard223eb912016-10-13 10:15:06 -07002372 // Remove any sync points corresponding to the buffer which was just
2373 // latched
2374 {
2375 Mutex::Autolock lock(mLocalSyncPointMutex);
2376 auto point = mLocalSyncPoints.begin();
2377 while (point != mLocalSyncPoints.end()) {
2378 if (!(*point)->frameIsAvailable() ||
2379 !(*point)->transactionIsApplied()) {
2380 // This sync point must have been added since we started
2381 // latching. Don't drop it yet.
2382 ++point;
2383 continue;
2384 }
2385
2386 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2387 point = mLocalSyncPoints.erase(point);
2388 } else {
2389 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002390 }
2391 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002392 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002393
2394 // FIXME: postedRegion should be dirty & bounds
2395 Region dirtyRegion(Rect(s.active.w, s.active.h));
2396
2397 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002398 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002399
Mathias Agopian4fec8732012-06-29 14:12:52 -07002400 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002401}
2402
Mathias Agopiana67932f2011-04-20 14:20:59 -07002403uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002404{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002405 // TODO: should we do something special if mSecure is set?
2406 if (mProtectedByApp) {
2407 // need a hardware-protected path to external video sink
2408 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002409 }
Riley Andrews03414a12014-07-01 14:22:59 -07002410 if (mPotentialCursor) {
2411 usage |= GraphicBuffer::USAGE_CURSOR;
2412 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002413 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002414 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002415}
2416
Mathias Agopian84300952012-11-21 16:02:13 -08002417void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002418 uint32_t orientation = 0;
2419 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002420 // The transform hint is used to improve performance, but we can
2421 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002422 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002423 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002424 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002425 if (orientation & Transform::ROT_INVALID) {
2426 orientation = 0;
2427 }
2428 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002429 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002430}
2431
Mathias Agopian13127d82013-03-05 17:47:11 -08002432// ----------------------------------------------------------------------------
2433// debugging
2434// ----------------------------------------------------------------------------
2435
Kalle Raitaa099a242017-01-11 11:17:29 -08002436LayerDebugInfo Layer::getLayerDebugInfo() const {
2437 LayerDebugInfo info;
2438 const Layer::State& ds = getDrawingState();
2439 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07002440 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08002441 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
2442 info.mType = String8(getTypeId());
2443 info.mTransparentRegion = ds.activeTransparentRegion;
2444 info.mVisibleRegion = visibleRegion;
2445 info.mSurfaceDamageRegion = surfaceDamageRegion;
2446 info.mLayerStack = getLayerStack();
2447 info.mX = ds.active.transform.tx();
2448 info.mY = ds.active.transform.ty();
2449 info.mZ = ds.z;
2450 info.mWidth = ds.active.w;
2451 info.mHeight = ds.active.h;
2452 info.mCrop = ds.crop;
2453 info.mFinalCrop = ds.finalCrop;
2454 info.mAlpha = ds.alpha;
2455 info.mFlags = ds.flags;
2456 info.mPixelFormat = getPixelFormat();
2457 info.mDataSpace = getDataSpace();
2458 info.mMatrix[0][0] = ds.active.transform[0][0];
2459 info.mMatrix[0][1] = ds.active.transform[0][1];
2460 info.mMatrix[1][0] = ds.active.transform[1][0];
2461 info.mMatrix[1][1] = ds.active.transform[1][1];
2462 {
2463 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
2464 if (activeBuffer != 0) {
2465 info.mActiveBufferWidth = activeBuffer->getWidth();
2466 info.mActiveBufferHeight = activeBuffer->getHeight();
2467 info.mActiveBufferStride = activeBuffer->getStride();
2468 info.mActiveBufferFormat = activeBuffer->format;
2469 } else {
2470 info.mActiveBufferWidth = 0;
2471 info.mActiveBufferHeight = 0;
2472 info.mActiveBufferStride = 0;
2473 info.mActiveBufferFormat = 0;
2474 }
Mathias Agopian13127d82013-03-05 17:47:11 -08002475 }
Kalle Raitaa099a242017-01-11 11:17:29 -08002476 info.mNumQueuedFrames = getQueuedFrameCount();
2477 info.mRefreshPending = isBufferLatched();
2478 info.mIsOpaque = isOpaque(ds);
2479 info.mContentDirty = contentDirty;
2480 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08002481}
2482
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002483#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002484void Layer::miniDumpHeader(String8& result) {
2485 result.append("----------------------------------------");
2486 result.append("---------------------------------------\n");
2487 result.append(" Layer name\n");
2488 result.append(" Z | ");
2489 result.append(" Comp Type | ");
2490 result.append(" Disp Frame (LTRB) | ");
2491 result.append(" Source Crop (LTRB)\n");
2492 result.append("----------------------------------------");
2493 result.append("---------------------------------------\n");
2494}
2495
2496void Layer::miniDump(String8& result, int32_t hwcId) const {
2497 if (mHwcLayers.count(hwcId) == 0) {
2498 return;
2499 }
2500
2501 String8 name;
2502 if (mName.length() > 77) {
2503 std::string shortened;
2504 shortened.append(mName.string(), 36);
2505 shortened.append("[...]");
2506 shortened.append(mName.string() + (mName.length() - 36), 36);
2507 name = shortened.c_str();
2508 } else {
2509 name = mName;
2510 }
2511
2512 result.appendFormat(" %s\n", name.string());
2513
2514 const Layer::State& layerState(getDrawingState());
2515 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2516 result.appendFormat(" %10u | ", layerState.z);
2517 result.appendFormat("%10s | ",
2518 to_string(getCompositionType(hwcId)).c_str());
2519 const Rect& frame = hwcInfo.displayFrame;
2520 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2521 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002522 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002523 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2524 crop.right, crop.bottom);
2525
2526 result.append("- - - - - - - - - - - - - - - - - - - - ");
2527 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2528}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002529#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002530
Svetoslavd85084b2014-03-20 10:28:31 -07002531void Layer::dumpFrameStats(String8& result) const {
2532 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002533}
2534
Svetoslavd85084b2014-03-20 10:28:31 -07002535void Layer::clearFrameStats() {
2536 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002537}
2538
Jamie Gennis6547ff42013-07-16 20:12:42 -07002539void Layer::logFrameStats() {
2540 mFrameTracker.logAndResetStats(mName);
2541}
2542
Svetoslavd85084b2014-03-20 10:28:31 -07002543void Layer::getFrameStats(FrameStats* outStats) const {
2544 mFrameTracker.getStats(outStats);
2545}
2546
Brian Andersond6927fb2016-07-23 23:37:30 -07002547void Layer::dumpFrameEvents(String8& result) {
2548 result.appendFormat("- Layer %s (%s, %p)\n",
2549 getName().string(), getTypeId(), this);
2550 Mutex::Autolock lock(mFrameEventHistoryMutex);
2551 mFrameEventHistory.checkFencesForCompletion();
2552 mFrameEventHistory.dump(result);
2553}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002554
Brian Anderson5ea5e592016-12-01 16:54:33 -08002555void Layer::onDisconnect() {
2556 Mutex::Autolock lock(mFrameEventHistoryMutex);
2557 mFrameEventHistory.onDisconnect();
2558}
2559
Brian Anderson3890c392016-07-25 12:48:08 -07002560void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2561 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002562 Mutex::Autolock lock(mFrameEventHistoryMutex);
2563 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002564 // If there are any unsignaled fences in the aquire timeline at this
2565 // point, the previously queued frame hasn't been latched yet. Go ahead
2566 // and try to get the signal time here so the syscall is taken out of
2567 // the main thread's critical path.
2568 mAcquireTimeline.updateSignalTimes();
2569 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002570 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002571 mFrameEventHistory.addQueue(*newTimestamps);
2572 }
2573
Brian Anderson3890c392016-07-25 12:48:08 -07002574 if (outDelta) {
2575 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002576 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002577}
Dan Stozae77c7662016-05-13 11:37:28 -07002578
2579std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2580 bool forceFlush) {
2581 std::vector<OccupancyTracker::Segment> history;
2582 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2583 &history);
2584 if (result != NO_ERROR) {
2585 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2586 result);
2587 return {};
2588 }
2589 return history;
2590}
2591
Robert Carr367c5682016-06-20 11:55:28 -07002592bool Layer::getTransformToDisplayInverse() const {
2593 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2594}
2595
Chia-I Wu98f1c102017-05-30 14:54:08 -07002596size_t Layer::getChildrenCount() const {
2597 size_t count = 0;
2598 for (const sp<Layer>& child : mCurrentChildren) {
2599 count += 1 + child->getChildrenCount();
2600 }
2601 return count;
2602}
2603
Robert Carr1f0a16a2016-10-24 16:27:39 -07002604void Layer::addChild(const sp<Layer>& layer) {
2605 mCurrentChildren.add(layer);
2606 layer->setParent(this);
2607}
2608
2609ssize_t Layer::removeChild(const sp<Layer>& layer) {
2610 layer->setParent(nullptr);
2611 return mCurrentChildren.remove(layer);
2612}
2613
Robert Carr1db73f62016-12-21 12:58:51 -08002614bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2615 sp<Handle> handle = nullptr;
2616 sp<Layer> newParent = nullptr;
2617 if (newParentHandle == nullptr) {
2618 return false;
2619 }
2620 handle = static_cast<Handle*>(newParentHandle.get());
2621 newParent = handle->owner.promote();
2622 if (newParent == nullptr) {
2623 ALOGE("Unable to promote Layer handle");
2624 return false;
2625 }
2626
2627 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002628 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08002629
2630 sp<Client> client(child->mClientRef.promote());
2631 if (client != nullptr) {
2632 client->setParentLayer(newParent);
2633 }
2634 }
2635 mCurrentChildren.clear();
2636
2637 return true;
2638}
2639
Robert Carr9524cb32017-02-13 11:32:32 -08002640bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07002641 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08002642 if (child == this) {
2643 return;
2644 }
2645
2646 sp<Client> client(child->mClientRef.promote());
2647 if (client != nullptr) {
2648 client->detachLayer(child);
2649 }
2650 });
2651
2652 return true;
2653}
2654
Robert Carr1f0a16a2016-10-24 16:27:39 -07002655void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002656 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002657}
2658
2659void Layer::clearSyncPoints() {
2660 for (const auto& child : mCurrentChildren) {
2661 child->clearSyncPoints();
2662 }
2663
2664 Mutex::Autolock lock(mLocalSyncPointMutex);
2665 for (auto& point : mLocalSyncPoints) {
2666 point->setFrameAvailable();
2667 }
2668 mLocalSyncPoints.clear();
2669}
2670
2671int32_t Layer::getZ() const {
2672 return mDrawingState.z;
2673}
2674
Ivan Lozanoc6c2a8a2017-10-30 11:24:08 -07002675__attribute__((no_sanitize("unsigned-integer-overflow")))
Dan Stoza412903f2017-04-27 13:42:17 -07002676LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) {
2677 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
2678 "makeTraversalList received invalid stateSet");
2679 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2680 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2681 const State& state = useDrawing ? mDrawingState : mCurrentState;
2682
2683 if (state.zOrderRelatives.size() == 0) {
2684 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07002685 }
2686 LayerVector traverse;
2687
Dan Stoza412903f2017-04-27 13:42:17 -07002688 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07002689 sp<Layer> strongRelative = weakRelative.promote();
2690 if (strongRelative != nullptr) {
2691 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07002692 }
2693 }
2694
Dan Stoza412903f2017-04-27 13:42:17 -07002695 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07002696 traverse.add(child);
2697 }
2698
2699 return traverse;
2700}
2701
Robert Carr1f0a16a2016-10-24 16:27:39 -07002702/**
Robert Carrdb66e622017-04-10 16:55:57 -07002703 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002704 */
Dan Stoza412903f2017-04-27 13:42:17 -07002705void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
2706 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002707
Robert Carr1f0a16a2016-10-24 16:27:39 -07002708 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002709 for (; i < list.size(); i++) {
2710 const auto& relative = list[i];
2711 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002712 break;
Robert Carrdb66e622017-04-10 16:55:57 -07002713 }
Dan Stoza412903f2017-04-27 13:42:17 -07002714 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002715 }
Dan Stoza412903f2017-04-27 13:42:17 -07002716 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07002717 for (; i < list.size(); i++) {
2718 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002719 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002720 }
2721}
2722
2723/**
Robert Carrdb66e622017-04-10 16:55:57 -07002724 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002725 */
Dan Stoza412903f2017-04-27 13:42:17 -07002726void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
2727 const LayerVector::Visitor& visitor) {
2728 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002729
Robert Carr1f0a16a2016-10-24 16:27:39 -07002730 int32_t i = 0;
Joel Galenson8eb53352017-11-06 11:04:12 -08002731 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002732 const auto& relative = list[i];
2733 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002734 break;
2735 }
Dan Stoza412903f2017-04-27 13:42:17 -07002736 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002737 }
Dan Stoza412903f2017-04-27 13:42:17 -07002738 visitor(this);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002739 for (; i>=0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002740 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002741 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002742 }
2743}
2744
2745Transform Layer::getTransform() const {
2746 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002747 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002748 if (p != nullptr) {
2749 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002750
2751 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2752 // it isFixedSize) then there may be additional scaling not accounted
2753 // for in the transform. We need to mirror this scaling in child surfaces
2754 // or we will break the contract where WM can treat child surfaces as
2755 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002756 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002757 int bufferWidth;
2758 int bufferHeight;
2759 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2760 bufferWidth = p->mActiveBuffer->getWidth();
2761 bufferHeight = p->mActiveBuffer->getHeight();
2762 } else {
2763 bufferHeight = p->mActiveBuffer->getWidth();
2764 bufferWidth = p->mActiveBuffer->getHeight();
2765 }
Robert Carr9b429f42017-04-17 14:56:57 -07002766 float sx = p->getDrawingState().active.w /
Robert Carr1725eee2017-04-26 18:32:15 -07002767 static_cast<float>(bufferWidth);
Robert Carr9b429f42017-04-17 14:56:57 -07002768 float sy = p->getDrawingState().active.h /
Robert Carr1725eee2017-04-26 18:32:15 -07002769 static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002770 Transform extraParentScaling;
2771 extraParentScaling.set(sx, 0, 0, sy);
2772 t = t * extraParentScaling;
2773 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002774 }
2775 return t * getDrawingState().active.transform;
2776}
2777
Robert Carr6452f122017-03-21 10:41:29 -07002778#ifdef USE_HWC2
2779float Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002780 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002781
2782 float parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0;
2783 return parentAlpha * getDrawingState().alpha;
2784}
2785#else
2786uint8_t Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002787 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002788
2789 float parentAlpha = (p != nullptr) ? (p->getAlpha() / 255.0f) : 1.0;
2790 float drawingAlpha = getDrawingState().alpha / 255.0f;
2791 drawingAlpha = drawingAlpha * parentAlpha;
2792 return static_cast<uint8_t>(std::round(drawingAlpha * 255));
2793}
2794#endif
2795
Robert Carr1f0a16a2016-10-24 16:27:39 -07002796void Layer::commitChildList() {
2797 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2798 const auto& child = mCurrentChildren[i];
2799 child->commitChildList();
2800 }
2801 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002802 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002803}
2804
Mathias Agopian13127d82013-03-05 17:47:11 -08002805// ---------------------------------------------------------------------------
2806
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002807}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002808
2809#if defined(__gl_h_)
2810#error "don't include gl/gl.h in this file"
2811#endif
2812
2813#if defined(__gl2_h_)
2814#error "don't include gl2/gl2.h in this file"
2815#endif