blob: 956f7f6c0225f3a662c9c17e7120ea52b2344634 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <gui/BufferQueue.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#define DEBUG_RESIZE 0
61
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062namespace android {
63
64// ---------------------------------------------------------------------------
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
Mathias Agopian4d9b8222013-03-12 17:11:48 -070068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
69 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080070 : contentDirty(false),
71 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mPremultipliedAlpha(true),
75 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080076 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080077 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070078 mPendingStateMutex(),
79 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080081 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080082 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070084 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070085 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 mCurrentOpacity(true),
Brian Andersond6927fb2016-07-23 23:37:30 -070087 mBufferLatched(false),
Dan Stozacac35382016-01-27 12:21:06 -080088 mCurrentFrameNumber(0),
Brian Anderson8cc8b102016-10-21 12:43:09 -070089 mPreviousFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080090 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080091 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mFiltering(false),
93 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070094 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000095#ifndef USE_HWC2
96 mIsGlesComposition(false),
97#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080098 mProtectedByApp(false),
99 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -0700100 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -0700101 mPotentialCursor(false),
102 mQueueItemLock(),
103 mQueueItemCondition(),
104 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700105 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -0800106 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700107 mAutoRefresh(false),
Robert Carr7bf247e2017-05-18 14:02:49 -0700108 mFreezeGeometryUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000110#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111 ALOGV("Creating Layer %s", name.string());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000112#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800113
Mathias Agopiana67932f2011-04-20 14:20:59 -0700114 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700115 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700116 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117
118 uint32_t layerFlags = 0;
119 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800120 layerFlags |= layer_state_t::eLayerHidden;
121 if (flags & ISurfaceComposerClient::eOpaque)
122 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700123 if (flags & ISurfaceComposerClient::eSecure)
124 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125
126 if (flags & ISurfaceComposerClient::eNonPremultiplied)
127 mPremultipliedAlpha = false;
128
129 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700130 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131
132 mCurrentState.active.w = w;
133 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800134 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700135 mCurrentState.crop.makeInvalid();
136 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700137 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
138 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700140 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700141 mCurrentState.layerStack = 0;
142 mCurrentState.flags = layerFlags;
143 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700145 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500146 mCurrentState.appId = 0;
147 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148
149 // drawing state & current state are identical
150 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700151
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000152#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800153 const auto& hwc = flinger->getHwComposer();
154 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
155 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000156#else
157 nsecs_t displayPeriod =
158 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
159#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700160 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800161
162 CompositorTiming compositorTiming;
163 flinger->getCompositorTiming(&compositorTiming);
164 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800165}
166
Mathias Agopian3f844832013-08-07 21:24:32 -0700167void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800168 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700169 sp<IGraphicBufferProducer> producer;
170 sp<IGraphicBufferConsumer> consumer;
Mathias Agopian0556d792017-03-22 15:49:32 -0700171 BufferQueue::createBufferQueue(&producer, &consumer, true);
Robert Carr1db73f62016-12-21 12:58:51 -0800172 mProducer = new MonitoredProducer(producer, mFlinger, this);
Irvel468051e2016-06-13 16:44:44 -0700173 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800174 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800175 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700176 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800177
Fabien Sanglard63a5fcd2016-12-29 15:13:07 -0800178 if (mFlinger->isLayerTripleBufferingDisabled()) {
179 mProducer->setMaxDequeuedBufferCount(2);
180 }
Andy McFadden69052052012-09-14 16:10:11 -0700181
Mathias Agopian84300952012-11-21 16:02:13 -0800182 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
183 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700184}
185
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700186Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800187 sp<Client> c(mClientRef.promote());
188 if (c != 0) {
189 c->detachLayer(this);
190 }
191
Dan Stozacac35382016-01-27 12:21:06 -0800192 for (auto& point : mRemoteSyncPoints) {
193 point->setTransactionApplied();
194 }
Dan Stozac8145172016-04-28 16:29:06 -0700195 for (auto& point : mLocalSyncPoints) {
196 point->setFrameAvailable();
197 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700198 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700199 mFrameTracker.logAndResetStats(mName);
Steven Thomasb02664d2017-07-26 18:48:28 -0700200
201#ifdef USE_HWC2
Chia-I Wuc6657022017-08-15 11:18:17 -0700202 if (!mHwcLayers.empty()) {
203 ALOGE("Found stale hardware composer layers when destroying "
204 "surface flinger layer %s", mName.string());
205 destroyAllHwcLayers();
206 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700207#endif
Mathias Agopian96f08192010-06-02 23:28:45 -0700208}
209
Mathias Agopian13127d82013-03-05 17:47:11 -0800210// ---------------------------------------------------------------------------
211// callbacks
212// ---------------------------------------------------------------------------
213
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000214#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800215void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
216 if (mHwcLayers.empty()) {
217 return;
218 }
219 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
220}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000221#else
222void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
223 HWComposer::HWCLayerInterface* layer) {
224 if (layer) {
225 layer->onDisplayed();
226 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
227 }
228}
229#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800230
Dan Stoza6b9454d2014-11-07 16:00:59 -0800231void Layer::onFrameAvailable(const BufferItem& item) {
232 // Add this buffer from our internal queue tracker
233 { // Autolock scope
234 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700235 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
236 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700237 // Reset the frame number tracker when we receive the first buffer after
238 // a frame number reset
239 if (item.mFrameNumber == 1) {
240 mLastFrameNumberReceived = 0;
241 }
242
243 // Ensure that callbacks are handled in order
244 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
245 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
246 ms2ns(500));
247 if (result != NO_ERROR) {
248 ALOGE("[%s] Timed out waiting on callback", mName.string());
249 }
250 }
251
Dan Stoza6b9454d2014-11-07 16:00:59 -0800252 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700253 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700254
255 // Wake up any pending callbacks
256 mLastFrameNumberReceived = item.mFrameNumber;
257 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800258 }
259
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800260 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700261}
262
Dan Stoza6b9454d2014-11-07 16:00:59 -0800263void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700264 { // Autolock scope
265 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700266
Dan Stoza7dde5992015-05-22 09:51:44 -0700267 // Ensure that callbacks are handled in order
268 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
269 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
270 ms2ns(500));
271 if (result != NO_ERROR) {
272 ALOGE("[%s] Timed out waiting on callback", mName.string());
273 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700274 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700275
276 if (mQueueItems.empty()) {
277 ALOGE("Can't replace a frame on an empty queue");
278 return;
279 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700280 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700281
282 // Wake up any pending callbacks
283 mLastFrameNumberReceived = item.mFrameNumber;
284 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700285 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800286}
287
Jesse Hall399184a2014-03-03 15:42:54 -0800288void Layer::onSidebandStreamChanged() {
289 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
290 // mSidebandStreamChanged was false
291 mFlinger->signalLayerUpdate();
292 }
293}
294
Chia-I Wuc6657022017-08-15 11:18:17 -0700295void Layer::onRemovedFromCurrentState() {
296 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
297
Robert Carr5edb1ad2017-04-25 10:54:24 -0700298 if (mCurrentState.zOrderRelativeOf != nullptr) {
299 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
300 if (strongRelative != nullptr) {
301 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700302 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700303 }
304 mCurrentState.zOrderRelativeOf = nullptr;
305 }
306
Chia-I Wuc6657022017-08-15 11:18:17 -0700307 for (const auto& child : mCurrentChildren) {
308 child->onRemovedFromCurrentState();
309 }
310}
Chia-I Wu38512252017-05-17 14:36:16 -0700311
Chia-I Wuc6657022017-08-15 11:18:17 -0700312void Layer::onRemoved() {
313 // the layer is removed from SF mLayersPendingRemoval
314
315 mSurfaceFlingerConsumer->abandon();
Chia-I Wu38512252017-05-17 14:36:16 -0700316#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700317 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700318#endif
319
Robert Carr1f0a16a2016-10-24 16:27:39 -0700320 for (const auto& child : mCurrentChildren) {
321 child->onRemoved();
322 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700323}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700324
Mathias Agopian13127d82013-03-05 17:47:11 -0800325// ---------------------------------------------------------------------------
326// set-up
327// ---------------------------------------------------------------------------
328
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700329const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800330 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331}
332
chaviw13fdc492017-06-27 12:40:18 -0700333bool Layer::getPremultipledAlpha() const {
334 return mPremultipliedAlpha;
335}
336
Mathias Agopianf9d93272009-06-19 17:00:27 -0700337status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 PixelFormat format, uint32_t flags)
339{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700340 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700341 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700342
343 // never allow a surface larger than what our underlying GL implementation
344 // can handle.
345 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800346 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700347 return BAD_VALUE;
348 }
349
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700350 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700351
Riley Andrews03414a12014-07-01 14:22:59 -0700352 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700353 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354 mCurrentOpacity = getOpacityForFormat(format);
355
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800356 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
357 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
358 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 return NO_ERROR;
361}
362
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700363sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800364 Mutex::Autolock _l(mLock);
365
366 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700367 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800368
369 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700370
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700371 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800372}
373
Dan Stozab9b08832014-03-13 11:55:57 -0700374sp<IGraphicBufferProducer> Layer::getProducer() const {
375 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700376}
377
Mathias Agopian13127d82013-03-05 17:47:11 -0800378// ---------------------------------------------------------------------------
379// h/w composer set-up
380// ---------------------------------------------------------------------------
381
Steven Thomasb02664d2017-07-26 18:48:28 -0700382#ifdef USE_HWC2
383bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
384 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
385 "Already have a layer for hwcId %d", hwcId);
386 HWC2::Layer* layer = hwc->createLayer(hwcId);
387 if (!layer) {
388 return false;
389 }
390 HWCInfo& hwcInfo = mHwcLayers[hwcId];
391 hwcInfo.hwc = hwc;
392 hwcInfo.layer = layer;
393 layer->setLayerDestroyedListener(
394 [this, hwcId] (HWC2::Layer* /*layer*/){mHwcLayers.erase(hwcId);});
395 return true;
396}
397
398void Layer::destroyHwcLayer(int32_t hwcId) {
399 if (mHwcLayers.count(hwcId) == 0) {
400 return;
401 }
402 auto& hwcInfo = mHwcLayers[hwcId];
403 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr,
404 "Attempt to destroy null layer");
405 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
406 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
407 // The layer destroyed listener should have cleared the entry from
408 // mHwcLayers. Verify that.
409 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0,
410 "Stale layer entry in mHwcLayers");
411}
412
413void Layer::destroyAllHwcLayers() {
414 size_t numLayers = mHwcLayers.size();
415 for (size_t i = 0; i < numLayers; ++i) {
416 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
417 destroyHwcLayer(mHwcLayers.begin()->first);
418 }
419 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
420 "All hardware composer layers should have been destroyed");
421}
422#endif
423
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800424Rect Layer::getContentCrop() const {
425 // this is the crop rectangle that applies to the buffer
426 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700427 Rect crop;
428 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800429 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700430 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800431 } else if (mActiveBuffer != NULL) {
432 // otherwise we use the whole buffer
433 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700434 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800435 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700436 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700438 return crop;
439}
440
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700441static Rect reduce(const Rect& win, const Region& exclude) {
442 if (CC_LIKELY(exclude.isEmpty())) {
443 return win;
444 }
445 if (exclude.isRect()) {
446 return win.reduce(exclude.getBounds());
447 }
448 return Region(win).subtract(exclude).getBounds();
449}
450
Robert Carr1f0a16a2016-10-24 16:27:39 -0700451Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
452 const Layer::State& s(getDrawingState());
453 Rect win(s.active.w, s.active.h);
454
455 if (!s.crop.isEmpty()) {
456 win.intersect(s.crop, &win);
457 }
458
459 Transform t = getTransform();
460 win = t.transform(win);
461
Robert Carr41b08b52017-06-01 16:11:34 -0700462 if (!s.finalCrop.isEmpty()) {
463 win.intersect(s.finalCrop, &win);
464 }
465
Chia-I Wue41dbe62017-06-13 14:10:56 -0700466 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700467 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
468 // When calculating the parent bounds for purposes of clipping,
469 // we don't need to constrain the parent to its transparent region.
470 // The transparent region is an optimization based on the
471 // buffer contents of the layer, but does not affect the space allocated to
472 // it by policy, and thus children should be allowed to extend into the
473 // parent's transparent region. In fact one of the main uses, is to reduce
474 // buffer allocation size in cases where a child window sits behind a main window
475 // (by marking the hole in the parent window as a transparent region)
476 if (p != nullptr) {
477 Rect bounds = p->computeScreenBounds(false);
478 bounds.intersect(win, &win);
479 }
480
481 if (reduceTransparentRegion) {
482 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
483 win = reduce(win, screenTransparentRegion);
484 }
485
486 return win;
487}
488
Mathias Agopian13127d82013-03-05 17:47:11 -0800489Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700490 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700491 return computeBounds(s.activeTransparentRegion);
492}
493
494Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
495 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800496 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700497
498 if (!s.crop.isEmpty()) {
499 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800500 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700501
502 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700503 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700504 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800505 // Look in computeScreenBounds recursive call for explanation of
506 // why we pass false here.
507 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508 }
509
510 Transform t = getTransform();
511 if (p != nullptr) {
512 win = t.transform(win);
513 win.intersect(bounds, &win);
514 win = t.inverse().transform(win);
515 }
516
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700517 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700518 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800519}
520
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700522 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800523 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700524 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800525
526 // apply the projection's clipping to the window crop in
527 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700528 // if there are no window scaling involved, this operation will map to full
529 // pixels in the buffer.
530 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
531 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700532
533 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700534 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700535 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700536 }
537
Robert Carr1f0a16a2016-10-24 16:27:39 -0700538 Transform t = getTransform();
539 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000540 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
541 activeCrop.clear();
542 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700543 if (!s.finalCrop.isEmpty()) {
544 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000545 activeCrop.clear();
546 }
547 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700548 return activeCrop;
549}
550
Dan Stoza5a423ea2017-02-16 14:10:39 -0800551FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700552 // the content crop is the area of the content that gets scaled to the
553 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800554 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700555
556 // In addition there is a WM-specified crop we pull from our drawing state.
557 const State& s(getDrawingState());
558
559 // Screen space to make reduction to parent crop clearer.
560 Rect activeCrop = computeInitialCrop(hw);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700561 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700562 if (p != nullptr) {
563 auto parentCrop = p->computeInitialCrop(hw);
564 activeCrop.intersect(parentCrop, &activeCrop);
565 }
566 Transform t = getTransform();
567 // Back to layer space to work with the content crop.
568 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800569
Michael Lentine28ea2172014-11-19 18:32:37 -0800570 // This needs to be here as transform.transform(Rect) computes the
571 // transformed rect and then takes the bounding box of the result before
572 // returning. This means
573 // transform.inverse().transform(transform.transform(Rect)) != Rect
574 // in which case we need to make sure the final rect is clipped to the
575 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000576 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
577 activeCrop.clear();
578 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800579
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700580 // subtract the transparent region and snap to the bounds
581 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
582
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000583 // Transform the window crop to match the buffer coordinate system,
584 // which means using the inverse of the current transform set on the
585 // SurfaceFlingerConsumer.
586 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700587 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000588 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700589 * the code below applies the primary display's inverse transform to the
590 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000591 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700592 uint32_t invTransformOrient =
593 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000594 // calculate the inverse transform
595 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
596 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
597 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800598 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000599 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700600 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
601 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800602 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000603
604 int winWidth = s.active.w;
605 int winHeight = s.active.h;
606 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
607 // If the activeCrop has been rotate the ends are rotated but not
608 // the space itself so when transforming ends back we can't rely on
609 // a modification of the axes of rotation. To account for this we
610 // need to reorient the inverse rotation in terms of the current
611 // axes of rotation.
612 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
613 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
614 if (is_h_flipped == is_v_flipped) {
615 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
616 NATIVE_WINDOW_TRANSFORM_FLIP_H;
617 }
618 winWidth = s.active.h;
619 winHeight = s.active.w;
620 }
621 const Rect winCrop = activeCrop.transform(
622 invTransform, s.active.w, s.active.h);
623
624 // below, crop is intersected with winCrop expressed in crop's coordinate space
625 float xScale = crop.getWidth() / float(winWidth);
626 float yScale = crop.getHeight() / float(winHeight);
627
628 float insetL = winCrop.left * xScale;
629 float insetT = winCrop.top * yScale;
630 float insetR = (winWidth - winCrop.right ) * xScale;
631 float insetB = (winHeight - winCrop.bottom) * yScale;
632
633 crop.left += insetL;
634 crop.top += insetT;
635 crop.right -= insetR;
636 crop.bottom -= insetB;
637
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 return crop;
639}
640
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000641#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800642void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000643#else
644void Layer::setGeometry(
645 const sp<const DisplayDevice>& hw,
646 HWComposer::HWCLayerInterface& layer)
647#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700648{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000649#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 const auto hwcId = displayDevice->getHwcDisplayId();
651 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000652#else
653 layer.setDefaultState();
654#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700655
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700656 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000657#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 hwcInfo.forceClientComposition = false;
659
660 if (isSecure() && !displayDevice->isSecure()) {
661 hwcInfo.forceClientComposition = true;
662 }
663
664 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000665#else
666 layer.setSkip(false);
667
668 if (isSecure() && !hw->isSecure()) {
669 layer.setSkip(true);
670 }
671#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700672
Mathias Agopian13127d82013-03-05 17:47:11 -0800673 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700674 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000675#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500676 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700677 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500678 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 }
David Revemanecf0fa52017-03-03 11:32:44 -0500681 auto error = hwcLayer->setBlendMode(blendMode);
682 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
683 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
684 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000685#else
chaviw13fdc492017-06-27 12:40:18 -0700686 if (!isOpaque(s) || getAlpha() != 1.0f) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000687 layer.setBlending(mPremultipliedAlpha ?
688 HWC_BLENDING_PREMULT :
689 HWC_BLENDING_COVERAGE);
690 }
691#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800692
693 // apply the layer's transform, followed by the display's global transform
694 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700695 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700696 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700697 if (!s.crop.isEmpty()) {
698 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700699 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000700#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000701 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000702#else
703 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
704#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000705 activeCrop.clear();
706 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700707 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800708 // This needs to be here as transform.transform(Rect) computes the
709 // transformed rect and then takes the bounding box of the result before
710 // returning. This means
711 // transform.inverse().transform(transform.transform(Rect)) != Rect
712 // in which case we need to make sure the final rect is clipped to the
713 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000714 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
715 activeCrop.clear();
716 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700717 // mark regions outside the crop as transparent
718 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
719 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
720 s.active.w, s.active.h));
721 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
722 activeCrop.left, activeCrop.bottom));
723 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
724 s.active.w, activeCrop.bottom));
725 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700726
727 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700728 if (!s.finalCrop.isEmpty()) {
729 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000730 frame.clear();
731 }
732 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000733#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000734 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
735 frame.clear();
736 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 const Transform& tr(displayDevice->getTransform());
738 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500739 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700740 if (error != HWC2::Error::None) {
741 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
742 mName.string(), transformedFrame.left, transformedFrame.top,
743 transformedFrame.right, transformedFrame.bottom,
744 to_string(error).c_str(), static_cast<int32_t>(error));
745 } else {
746 hwcInfo.displayFrame = transformedFrame;
747 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748
Dan Stoza5a423ea2017-02-16 14:10:39 -0800749 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700751 if (error != HWC2::Error::None) {
752 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
753 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
754 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
755 static_cast<int32_t>(error));
756 } else {
757 hwcInfo.sourceCrop = sourceCrop;
758 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759
chaviw13fdc492017-06-27 12:40:18 -0700760 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700761 error = hwcLayer->setPlaneAlpha(alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
Robert Carr6452f122017-03-21 10:41:29 -0700763 "%s (%d)", mName.string(), alpha, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 static_cast<int32_t>(error));
765
Robert Carrae060832016-11-28 10:51:00 -0800766 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800768 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500770
Albert Chaulk2a589632017-05-04 16:59:44 -0400771 int type = s.type;
772 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700773 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400774 if (parent.get()) {
775 auto& parentState = parent->getDrawingState();
776 type = parentState.type;
777 appId = parentState.appId;
778 }
779
780 error = hwcLayer->setInfo(type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500781 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
782 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000783#else
784 if (!frame.intersect(hw->getViewport(), &frame)) {
785 frame.clear();
786 }
787 const Transform& tr(hw->getTransform());
788 layer.setFrame(tr.transform(frame));
789 layer.setCrop(computeCrop(hw));
chaviw13fdc492017-06-27 12:40:18 -0700790 layer.setPlaneAlpha(static_cast<uint8_t>(std::round(255.0f*getAlpha())));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000791#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800792
Mathias Agopian29a367b2011-07-12 14:51:45 -0700793 /*
794 * Transformations are applied in this order:
795 * 1) buffer orientation/flip/mirror
796 * 2) state transformation (window manager)
797 * 3) layer orientation (screen orientation)
798 * (NOTE: the matrices are multiplied in reverse order)
799 */
800
801 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700802 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700803
Robert Carrcae605c2017-03-29 12:10:31 -0700804 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700805 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700806 * the code below applies the primary display's inverse transform to the
807 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700808 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700809 uint32_t invTransform =
810 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700811 // calculate the inverse transform
812 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
813 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
814 NATIVE_WINDOW_TRANSFORM_FLIP_H;
815 }
Robert Carrcae605c2017-03-29 12:10:31 -0700816
817 /*
818 * Here we cancel out the orientation component of the WM transform.
819 * The scaling and translate components are already included in our bounds
820 * computation so it's enough to just omit it in the composition.
821 * See comment in onDraw with ref to b/36727915 for why.
822 */
823 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700824 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700825
826 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000828#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800829 if (orientation & Transform::ROT_INVALID) {
830 // we can only handle simple transformation
831 hwcInfo.forceClientComposition = true;
832 } else {
833 auto transform = static_cast<HWC2::Transform>(orientation);
834 auto error = hwcLayer->setTransform(transform);
835 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
836 "%s (%d)", mName.string(), to_string(transform).c_str(),
837 to_string(error).c_str(), static_cast<int32_t>(error));
838 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000839#else
840 if (orientation & Transform::ROT_INVALID) {
841 // we can only handle simple transformation
842 layer.setSkip(true);
843 } else {
844 layer.setTransform(orientation);
845 }
846#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700847}
848
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000849#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800850void Layer::forceClientComposition(int32_t hwcId) {
851 if (mHwcLayers.count(hwcId) == 0) {
852 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
853 return;
854 }
855
856 mHwcLayers[hwcId].forceClientComposition = true;
857}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800858
Dan Stoza9e56aa02015-11-02 13:00:03 -0800859void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
860 // Apply this display's projection's viewport to the visible region
861 // before giving it to the HWC HAL.
862 const Transform& tr = displayDevice->getTransform();
863 const auto& viewport = displayDevice->getViewport();
864 Region visible = tr.transform(visibleRegion.intersect(viewport));
865 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800866 auto& hwcInfo = mHwcLayers[hwcId];
867 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800868 auto error = hwcLayer->setVisibleRegion(visible);
869 if (error != HWC2::Error::None) {
870 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
871 to_string(error).c_str(), static_cast<int32_t>(error));
872 visible.dump(LOG_TAG);
873 }
874
875 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
876 if (error != HWC2::Error::None) {
877 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
878 to_string(error).c_str(), static_cast<int32_t>(error));
879 surfaceDamageRegion.dump(LOG_TAG);
880 }
881
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700882 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800883 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700884 setCompositionType(hwcId, HWC2::Composition::Sideband);
885 ALOGV("[%s] Requesting Sideband composition", mName.string());
886 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800887 if (error != HWC2::Error::None) {
888 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
889 mName.string(), mSidebandStream->handle(),
890 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800891 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700892 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800893 }
894
Dan Stoza0a21df72016-07-20 12:52:38 -0700895 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800896 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700897 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700898 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800899 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700900 return;
901 }
902
Dan Stoza0a21df72016-07-20 12:52:38 -0700903 // SolidColor layers
904 if (mActiveBuffer == nullptr) {
905 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700906
chaviw13fdc492017-06-27 12:40:18 -0700907 half4 color = getColor();
908 error = hwcLayer->setColor({static_cast<uint8_t>(std::round(255.0f*color.r)),
909 static_cast<uint8_t>(std::round(255.0f * color.g)),
910 static_cast<uint8_t>(std::round(255.0f * color.b)),
911 255});
Dan Stoza0a21df72016-07-20 12:52:38 -0700912 if (error != HWC2::Error::None) {
913 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
914 to_string(error).c_str(), static_cast<int32_t>(error));
915 }
Dan Stozac6c89542016-07-27 10:16:52 -0700916
917 // Clear out the transform, because it doesn't make sense absent a
918 // source buffer
919 error = hwcLayer->setTransform(HWC2::Transform::None);
920 if (error != HWC2::Error::None) {
921 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
922 to_string(error).c_str(), static_cast<int32_t>(error));
923 }
924
Dan Stoza0a21df72016-07-20 12:52:38 -0700925 return;
926 }
927
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700928 // Device or Cursor layers
929 if (mPotentialCursor) {
930 ALOGV("[%s] Requesting Cursor composition", mName.string());
931 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800932 } else {
933 ALOGV("[%s] Requesting Device composition", mName.string());
934 setCompositionType(hwcId, HWC2::Composition::Device);
935 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700936
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700937 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
938 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
939 if (error != HWC2::Error::None) {
940 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
941 mCurrentState.dataSpace, to_string(error).c_str(),
942 static_cast<int32_t>(error));
943 }
944
Chia-I Wu06d63de2017-01-04 14:58:51 +0800945 uint32_t hwcSlot = 0;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400946 sp<GraphicBuffer> hwcBuffer;
947 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
948 &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800949
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700950 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400951 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700952 if (error != HWC2::Error::None) {
953 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
954 mActiveBuffer->handle, to_string(error).c_str(),
955 static_cast<int32_t>(error));
956 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800957}
Courtney Goeltzenleuchter9551fd32016-10-20 17:18:15 -0600958
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000959#else
960void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
961 HWComposer::HWCLayerInterface& layer) {
962 // we have to set the visible region on every frame because
963 // we currently free it during onLayerDisplayed(), which is called
964 // after HWComposer::commit() -- every frame.
965 // Apply this display's projection's viewport to the visible region
966 // before giving it to the HWC HAL.
967 const Transform& tr = hw->getTransform();
968 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
969 layer.setVisibleRegionScreen(visible);
970 layer.setSurfaceDamage(surfaceDamageRegion);
971 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700972
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000973 if (mSidebandStream.get()) {
974 layer.setSidebandStream(mSidebandStream);
975 } else {
976 // NOTE: buffer can be NULL if the client never drew into this
977 // layer yet, or if we ran out of memory
978 layer.setBuffer(mActiveBuffer);
979 }
980}
981#endif
982
983#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800984void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
985 auto hwcId = displayDevice->getHwcDisplayId();
986 if (mHwcLayers.count(hwcId) == 0 ||
987 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
988 return;
989 }
990
991 // This gives us only the "orientation" component of the transform
992 const State& s(getCurrentState());
993
994 // Apply the layer's transform, followed by the display's global transform
995 // Here we're guaranteed that the layer's transform preserves rects
996 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700997 if (!s.crop.isEmpty()) {
998 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800999 }
1000 // Subtract the transparent region and snap to the bounds
1001 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001002 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -08001003 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -07001004 if (!s.finalCrop.isEmpty()) {
1005 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001006 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001007 auto& displayTransform(displayDevice->getTransform());
1008 auto position = displayTransform.transform(frame);
1009
1010 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
1011 position.top);
1012 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
1013 "to (%d, %d): %s (%d)", mName.string(), position.left,
1014 position.top, to_string(error).c_str(),
1015 static_cast<int32_t>(error));
1016}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001017#else
1018void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
1019 HWComposer::HWCLayerInterface& layer) {
1020 int fenceFd = -1;
1021
1022 // TODO: there is a possible optimization here: we only need to set the
1023 // acquire fence the first time a new buffer is acquired on EACH display.
1024
1025 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
1026 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
1027 if (fence->isValid()) {
1028 fenceFd = fence->dup();
1029 if (fenceFd == -1) {
1030 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
1031 }
1032 }
1033 }
1034 layer.setAcquireFenceFd(fenceFd);
1035}
1036
1037Rect Layer::getPosition(
1038 const sp<const DisplayDevice>& hw)
1039{
1040 // this gives us only the "orientation" component of the transform
1041 const State& s(getCurrentState());
1042
1043 // apply the layer's transform, followed by the display's global transform
1044 // here we're guaranteed that the layer's transform preserves rects
1045 Rect win(s.active.w, s.active.h);
1046 if (!s.crop.isEmpty()) {
1047 win.intersect(s.crop, &win);
1048 }
1049 // subtract the transparent region and snap to the bounds
1050 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001051 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001052 frame.intersect(hw->getViewport(), &frame);
1053 if (!s.finalCrop.isEmpty()) {
1054 frame.intersect(s.finalCrop, &frame);
1055 }
1056 const Transform& tr(hw->getTransform());
1057 return Rect(tr.transform(frame));
1058}
1059#endif
Riley Andrews03414a12014-07-01 14:22:59 -07001060
Mathias Agopian13127d82013-03-05 17:47:11 -08001061// ---------------------------------------------------------------------------
1062// drawing...
1063// ---------------------------------------------------------------------------
1064
1065void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -08001066 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -08001067}
1068
Dan Stozac7014012014-02-14 15:03:43 -08001069void Layer::draw(const sp<const DisplayDevice>& hw,
1070 bool useIdentityTransform) const {
1071 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001072}
1073
Dan Stozac7014012014-02-14 15:03:43 -08001074void Layer::draw(const sp<const DisplayDevice>& hw) const {
1075 onDraw(hw, Region(hw->bounds()), false);
1076}
1077
Robert Carrcae605c2017-03-29 12:10:31 -07001078static constexpr mat4 inverseOrientation(uint32_t transform) {
1079 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1080 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1081 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1082 mat4 tr;
1083
1084 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
1085 tr = tr * rot90;
1086 }
1087 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
1088 tr = tr * flipH;
1089 }
1090 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
1091 tr = tr * flipV;
1092 }
1093 return inverse(tr);
1094}
1095
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001096/*
1097 * onDraw will draw the current layer onto the presentable buffer
1098 */
Dan Stozac7014012014-02-14 15:03:43 -08001099void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
1100 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001102 ATRACE_CALL();
1103
Mathias Agopiana67932f2011-04-20 14:20:59 -07001104 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001106 // in fact never been drawn into. This happens frequently with
1107 // SurfaceView because the WindowManager can't know when the client
1108 // has drawn the first time.
1109
1110 // If there is nothing under us, we paint the screen in black, otherwise
1111 // we just skip this update.
1112
1113 // figure out if there is something below us
1114 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001115 bool finished = false;
Dan Stoza412903f2017-04-27 13:42:17 -07001116 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001117 if (finished || layer == static_cast<Layer const*>(this)) {
1118 finished = true;
1119 return;
1120 }
Mathias Agopian42977342012-08-05 00:40:46 -07001121 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001122 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001123 // if not everything below us is covered, we plug the holes!
1124 Region holes(clip.subtract(under));
1125 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001126 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001127 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 return;
1129 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001130
Andy McFadden97eba892012-12-11 15:21:45 -08001131 // Bind the current buffer to the GL texture, and wait for it to be
1132 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001133 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1134 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001135 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001136 // Go ahead and draw the buffer anyway; no matter what we do the screen
1137 // is probably going to have something visibly wrong.
1138 }
1139
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001140 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1141
Mathias Agopian875d8e12013-06-07 15:35:48 -07001142 RenderEngine& engine(mFlinger->getRenderEngine());
1143
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001144 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001145 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001146 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001147
1148 // Query the texture matrix given our current filtering mode.
1149 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001150 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1151 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001152
Robert Carrcae605c2017-03-29 12:10:31 -07001153 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001154
1155 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001156 * the code below applies the primary display's inverse transform to
1157 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001158 */
Pablo Ceballos021623b2016-04-15 17:31:51 -07001159 uint32_t transform =
1160 DisplayDevice::getPrimaryDisplayOrientationTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001161 mat4 tr = inverseOrientation(transform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001162
Robert Carrcae605c2017-03-29 12:10:31 -07001163 /**
1164 * TODO(b/36727915): This is basically a hack.
1165 *
1166 * Ensure that regardless of the parent transformation,
1167 * this buffer is always transformed from native display
1168 * orientation to display orientation. For example, in the case
1169 * of a camera where the buffer remains in native orientation,
1170 * we want the pixels to always be upright.
1171 */
Chia-I Wue41dbe62017-06-13 14:10:56 -07001172 sp<Layer> p = mDrawingParent.promote();
1173 if (p != nullptr) {
1174 const auto parentTransform = p->getTransform();
Robert Carrcae605c2017-03-29 12:10:31 -07001175 tr = tr * inverseOrientation(parentTransform.getOrientation());
1176 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001177
1178 // and finally apply it to the original texture matrix
1179 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1180 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1181 }
1182
Jamie Genniscbb1a952012-05-08 17:05:52 -07001183 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001184 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1185 mTexture.setFiltering(useFiltering);
1186 mTexture.setMatrix(textureMatrix);
1187
1188 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001189 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001190 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001191 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001192 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001193 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194}
1195
Mathias Agopian13127d82013-03-05 17:47:11 -08001196
Dan Stozac7014012014-02-14 15:03:43 -08001197void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001198 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001199 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001200{
Mathias Agopian19733a32013-08-28 18:13:56 -07001201 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001202 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001203 engine.setupFillWithColor(red, green, blue, alpha);
1204 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001205}
1206
1207void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001208 const sp<const DisplayDevice>& hw) const {
1209 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001210}
1211
Dan Stozac7014012014-02-14 15:03:43 -08001212void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001213 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001214 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001215
Dan Stozac7014012014-02-14 15:03:43 -08001216 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 /*
1219 * NOTE: the way we compute the texture coordinates here produces
1220 * different results than when we take the HWC path -- in the later case
1221 * the "source crop" is rounded to texel boundaries.
1222 * This can produce significantly different results when the texture
1223 * is scaled by a large amount.
1224 *
1225 * The GL code below is more logical (imho), and the difference with
1226 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001227 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 * GL composition when a buffer scaling is applied (maybe with some
1229 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1230 * like more of a hack.
1231 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001232 Rect win(computeBounds());
1233
Robert Carr1f0a16a2016-10-24 16:27:39 -07001234 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001235 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001236 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001237 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001238 win.clear();
1239 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001240 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001241 if (!win.intersect(computeBounds(), &win)) {
1242 win.clear();
1243 }
1244 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001245
Mathias Agopian3f844832013-08-07 21:24:32 -07001246 float left = float(win.left) / float(s.active.w);
1247 float top = float(win.top) / float(s.active.h);
1248 float right = float(win.right) / float(s.active.w);
1249 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001250
Mathias Agopian875d8e12013-06-07 15:35:48 -07001251 // TODO: we probably want to generate the texture coords with the mesh
1252 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001253 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1254 texCoords[0] = vec2(left, 1.0f - top);
1255 texCoords[1] = vec2(left, 1.0f - bottom);
1256 texCoords[2] = vec2(right, 1.0f - bottom);
1257 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001258
Mathias Agopian875d8e12013-06-07 15:35:48 -07001259 RenderEngine& engine(mFlinger->getRenderEngine());
chaviw13fdc492017-06-27 12:40:18 -07001260 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s),
1261 false /* disableTexture */, getColor());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -06001262#ifdef USE_HWC2
1263 engine.setSourceDataSpace(mCurrentState.dataSpace);
1264#endif
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001265 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001266 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001267}
1268
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001269#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001270void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1271 bool callIntoHwc) {
1272 if (mHwcLayers.count(hwcId) == 0) {
1273 ALOGE("setCompositionType called without a valid HWC layer");
1274 return;
1275 }
1276 auto& hwcInfo = mHwcLayers[hwcId];
1277 auto& hwcLayer = hwcInfo.layer;
1278 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1279 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1280 if (hwcInfo.compositionType != type) {
1281 ALOGV(" actually setting");
1282 hwcInfo.compositionType = type;
1283 if (callIntoHwc) {
1284 auto error = hwcLayer->setCompositionType(type);
1285 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1286 "composition type %s: %s (%d)", mName.string(),
1287 to_string(type).c_str(), to_string(error).c_str(),
1288 static_cast<int32_t>(error));
1289 }
1290 }
1291}
1292
1293HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001294 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1295 // If we're querying the composition type for a display that does not
1296 // have a HWC counterpart, then it will always be Client
1297 return HWC2::Composition::Client;
1298 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001299 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001300 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001301 return HWC2::Composition::Invalid;
1302 }
1303 return mHwcLayers.at(hwcId).compositionType;
1304}
1305
1306void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1307 if (mHwcLayers.count(hwcId) == 0) {
1308 ALOGE("setClearClientTarget called without a valid HWC layer");
1309 return;
1310 }
1311 mHwcLayers[hwcId].clearClientTarget = clear;
1312}
1313
1314bool Layer::getClearClientTarget(int32_t hwcId) const {
1315 if (mHwcLayers.count(hwcId) == 0) {
1316 ALOGE("getClearClientTarget called without a valid HWC layer");
1317 return false;
1318 }
1319 return mHwcLayers.at(hwcId).clearClientTarget;
1320}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001321#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001322
Ruben Brunk1681d952014-06-27 15:51:55 -07001323uint32_t Layer::getProducerStickyTransform() const {
1324 int producerStickyTransform = 0;
1325 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1326 if (ret != OK) {
1327 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1328 strerror(-ret), ret);
1329 return 0;
1330 }
1331 return static_cast<uint32_t>(producerStickyTransform);
1332}
1333
Dan Stozac5da2712016-07-20 15:38:12 -07001334bool Layer::latchUnsignaledBuffers() {
1335 static bool propertyLoaded = false;
1336 static bool latch = false;
1337 static std::mutex mutex;
1338 std::lock_guard<std::mutex> lock(mutex);
1339 if (!propertyLoaded) {
1340 char value[PROPERTY_VALUE_MAX] = {};
1341 property_get("debug.sf.latch_unsignaled", value, "0");
1342 latch = atoi(value);
1343 propertyLoaded = true;
1344 }
1345 return latch;
1346}
1347
Dan Stozacac35382016-01-27 12:21:06 -08001348uint64_t Layer::getHeadFrameNumber() const {
1349 Mutex::Autolock lock(mQueueItemLock);
1350 if (!mQueueItems.empty()) {
1351 return mQueueItems[0].mFrameNumber;
1352 } else {
1353 return mCurrentFrameNumber;
1354 }
1355}
1356
Dan Stoza1ce65812016-06-15 16:26:27 -07001357bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001358#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001359 if (latchUnsignaledBuffers()) {
1360 return true;
1361 }
1362
Dan Stoza1ce65812016-06-15 16:26:27 -07001363 Mutex::Autolock lock(mQueueItemLock);
1364 if (mQueueItems.empty()) {
1365 return true;
1366 }
1367 if (mQueueItems[0].mIsDroppable) {
1368 // Even though this buffer's fence may not have signaled yet, it could
1369 // be replaced by another buffer before it has a chance to, which means
1370 // that it's possible to get into a situation where a buffer is never
1371 // able to be latched. To avoid this, grab this buffer anyway.
1372 return true;
1373 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001374 return mQueueItems[0].mFenceTime->getSignalTime() !=
1375 Fence::SIGNAL_TIME_PENDING;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001376#else
1377 return true;
1378#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001379}
1380
Dan Stozacac35382016-01-27 12:21:06 -08001381bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1382 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1383 // Don't bother with a SyncPoint, since we've already latched the
1384 // relevant frame
1385 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001386 }
1387
Dan Stozacac35382016-01-27 12:21:06 -08001388 Mutex::Autolock lock(mLocalSyncPointMutex);
1389 mLocalSyncPoints.push_back(point);
1390 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001391}
1392
Mathias Agopian13127d82013-03-05 17:47:11 -08001393void Layer::setFiltering(bool filtering) {
1394 mFiltering = filtering;
1395}
1396
1397bool Layer::getFiltering() const {
1398 return mFiltering;
1399}
1400
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001401// As documented in libhardware header, formats in the range
1402// 0x100 - 0x1FF are specific to the HAL implementation, and
1403// are known to have no alpha channel
1404// TODO: move definition for device-specific range into
1405// hardware.h, instead of using hard-coded values here.
1406#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1407
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001408bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001409 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1410 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001411 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001412 switch (format) {
1413 case HAL_PIXEL_FORMAT_RGBA_8888:
1414 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001415 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001416 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001417 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001418 }
1419 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001420 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001421}
1422
Mathias Agopian13127d82013-03-05 17:47:11 -08001423// ----------------------------------------------------------------------------
1424// local state
1425// ----------------------------------------------------------------------------
1426
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001427static void boundPoint(vec2* point, const Rect& crop) {
1428 if (point->x < crop.left) {
1429 point->x = crop.left;
1430 }
1431 if (point->x > crop.right) {
1432 point->x = crop.right;
1433 }
1434 if (point->y < crop.top) {
1435 point->y = crop.top;
1436 }
1437 if (point->y > crop.bottom) {
1438 point->y = crop.bottom;
1439 }
1440}
1441
Dan Stozac7014012014-02-14 15:03:43 -08001442void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1443 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001444{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001445 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001446 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001447 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001448 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001449
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001450 vec2 lt = vec2(win.left, win.top);
1451 vec2 lb = vec2(win.left, win.bottom);
1452 vec2 rb = vec2(win.right, win.bottom);
1453 vec2 rt = vec2(win.right, win.top);
1454
Robert Carr1f0a16a2016-10-24 16:27:39 -07001455 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001456 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001457 lt = layerTransform.transform(lt);
1458 lb = layerTransform.transform(lb);
1459 rb = layerTransform.transform(rb);
1460 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001461 }
1462
Robert Carrb5d3d262016-03-25 15:08:13 -07001463 if (!s.finalCrop.isEmpty()) {
1464 boundPoint(&lt, s.finalCrop);
1465 boundPoint(&lb, s.finalCrop);
1466 boundPoint(&rb, s.finalCrop);
1467 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001468 }
1469
Mathias Agopianff2ed702013-09-01 21:36:12 -07001470 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001471 position[0] = hwTransform.transform(lt);
1472 position[1] = hwTransform.transform(lb);
1473 position[2] = hwTransform.transform(rb);
1474 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001475 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001476 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 }
1478}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001479
Andy McFadden4125a4f2014-01-29 17:17:11 -08001480bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001481{
neo.hee6fd41d2017-02-17 14:35:33 +08001482 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
Mathias Agopiana67932f2011-04-20 14:20:59 -07001483 // layer's opaque flag.
neo.hee6fd41d2017-02-17 14:35:33 +08001484 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001485 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001486 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001487
1488 // if the layer has the opaque flag, then we're always opaque,
1489 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001490 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001491}
1492
Dan Stoza23116082015-06-18 14:58:39 -07001493bool Layer::isSecure() const
1494{
1495 const Layer::State& s(mDrawingState);
1496 return (s.flags & layer_state_t::eLayerSecure);
1497}
1498
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001499bool Layer::isProtected() const
1500{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001501 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001502 return (activeBuffer != 0) &&
1503 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1504}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001505
Mathias Agopian13127d82013-03-05 17:47:11 -08001506bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001507 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001508}
1509
1510bool Layer::isCropped() const {
1511 return !mCurrentCrop.isEmpty();
1512}
1513
1514bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1515 return mNeedsFiltering || hw->needsFiltering();
1516}
1517
1518void Layer::setVisibleRegion(const Region& visibleRegion) {
1519 // always called from main thread
1520 this->visibleRegion = visibleRegion;
1521}
1522
1523void Layer::setCoveredRegion(const Region& coveredRegion) {
1524 // always called from main thread
1525 this->coveredRegion = coveredRegion;
1526}
1527
1528void Layer::setVisibleNonTransparentRegion(const Region&
1529 setVisibleNonTransparentRegion) {
1530 // always called from main thread
1531 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1532}
1533
1534// ----------------------------------------------------------------------------
1535// transaction
1536// ----------------------------------------------------------------------------
1537
Dan Stoza7dde5992015-05-22 09:51:44 -07001538void Layer::pushPendingState() {
1539 if (!mCurrentState.modified) {
1540 return;
1541 }
1542
Dan Stoza7dde5992015-05-22 09:51:44 -07001543 // If this transaction is waiting on the receipt of a frame, generate a sync
1544 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001545 if (mCurrentState.barrierLayer != nullptr) {
1546 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1547 if (barrierLayer == nullptr) {
1548 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001549 // If we can't promote the layer we are intended to wait on,
1550 // then it is expired or otherwise invalid. Allow this transaction
1551 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001552 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001553 } else {
1554 auto syncPoint = std::make_shared<SyncPoint>(
1555 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001556 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001557 mRemoteSyncPoints.push_back(std::move(syncPoint));
1558 } else {
1559 // We already missed the frame we're supposed to synchronize
1560 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001561 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001562 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001563 }
1564
Dan Stoza7dde5992015-05-22 09:51:44 -07001565 // Wake us up to check if the frame has been received
1566 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001567 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001568 }
1569 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001570 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001571}
1572
Pablo Ceballos05289c22016-04-14 15:49:55 -07001573void Layer::popPendingState(State* stateToCommit) {
1574 auto oldFlags = stateToCommit->flags;
1575 *stateToCommit = mPendingStates[0];
1576 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1577 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001578
1579 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -07001580 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -07001581}
1582
Pablo Ceballos05289c22016-04-14 15:49:55 -07001583bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001584 bool stateUpdateAvailable = false;
1585 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001586 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001587 if (mRemoteSyncPoints.empty()) {
1588 // If we don't have a sync point for this, apply it anyway. It
1589 // will be visually wrong, but it should keep us from getting
1590 // into too much trouble.
1591 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001592 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001593 stateUpdateAvailable = true;
1594 continue;
1595 }
1596
Dan Stozacac35382016-01-27 12:21:06 -08001597 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1598 mPendingStates[0].frameNumber) {
1599 ALOGE("[%s] Unexpected sync point frame number found",
1600 mName.string());
1601
1602 // Signal our end of the sync point and then dispose of it
1603 mRemoteSyncPoints.front()->setTransactionApplied();
1604 mRemoteSyncPoints.pop_front();
1605 continue;
1606 }
1607
Dan Stoza7dde5992015-05-22 09:51:44 -07001608 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1609 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001610 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001611 stateUpdateAvailable = true;
1612
1613 // Signal our end of the sync point and then dispose of it
1614 mRemoteSyncPoints.front()->setTransactionApplied();
1615 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001616 } else {
1617 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001618 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001619 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001620 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001621 stateUpdateAvailable = true;
1622 }
1623 }
1624
1625 // If we still have pending updates, wake SurfaceFlinger back up and point
1626 // it at this layer so we can process them
1627 if (!mPendingStates.empty()) {
1628 setTransactionFlags(eTransactionNeeded);
1629 mFlinger->setTransactionFlags(eTraversalNeeded);
1630 }
1631
1632 mCurrentState.modified = false;
1633 return stateUpdateAvailable;
1634}
1635
1636void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001637 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001638 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001639 Mutex::Autolock lock(mLocalSyncPointMutex);
1640 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001641 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001642 point->setFrameAvailable();
1643 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001644 }
1645}
1646
Mathias Agopian13127d82013-03-05 17:47:11 -08001647uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001648 ATRACE_CALL();
1649
Dan Stoza7dde5992015-05-22 09:51:44 -07001650 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001651 Layer::State c = getCurrentState();
1652 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001653 return 0;
1654 }
1655
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001656 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001658 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1659 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001660
1661 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001662 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001663 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001664 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001665 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001666 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001667 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001668 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001669 this, getName().string(), mCurrentTransform,
1670 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001671 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001672 c.crop.left,
1673 c.crop.top,
1674 c.crop.right,
1675 c.crop.bottom,
1676 c.crop.getWidth(),
1677 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001678 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001679 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001680 s.crop.left,
1681 s.crop.top,
1682 s.crop.right,
1683 s.crop.bottom,
1684 s.crop.getWidth(),
1685 s.crop.getHeight(),
1686 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001688 // record the new size, form this point on, when the client request
1689 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001690 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001691 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001693
Robert Carre392b552017-09-19 12:16:05 -07001694 // Don't let Layer::doTransaction update the drawing state
1695 // if we have a pending resize, unless we are in fixed-size mode.
1696 // the drawing state will be updated only once we receive a buffer
1697 // with the correct size.
1698 //
1699 // In particular, we want to make sure the clip (which is part
1700 // of the geometry state) is latched together with the size but is
1701 // latched immediately when no resizing is involved.
1702 //
1703 // If a sideband stream is attached, however, we want to skip this
1704 // optimization so that transactions aren't missed when a buffer
1705 // never arrives
1706 //
1707 // In the case that we don't have a buffer we ignore other factors
1708 // and avoid entering the resizePending state. At a high level the
1709 // resizePending state is to avoid applying the state of the new buffer
1710 // to the old buffer. However in the state where we don't have an old buffer
1711 // there is no such concern but we may still be being used as a parent layer.
1712 const bool resizePending = ((c.requested.w != c.active.w) ||
1713 (c.requested.h != c.active.h)) && (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001714 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001715 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001716 flags |= eDontUpdateGeometryState;
1717 }
1718 }
1719
Robert Carr7bf247e2017-05-18 14:02:49 -07001720 // Here we apply various requested geometry states, depending on our
1721 // latching configuration. See Layer.h for a detailed discussion of
1722 // how geometry latching is controlled.
1723 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001724 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001725
1726 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1727 // mode, which causes attributes which normally latch regardless of scaling mode,
1728 // to be delayed. We copy the requested state to the active state making sure
1729 // to respect these rules (again see Layer.h for a detailed discussion).
1730 //
1731 // There is an awkward asymmetry in the handling of the crop states in the position
1732 // states, as can be seen below. Largely this arises from position and transform
1733 // being stored in the same data structure while having different latching rules.
1734 // b/38182305
1735 //
1736 // Careful that "c" and editCurrentState may not begin as equivalent due to
1737 // applyPendingStates in the presence of deferred transactions.
1738 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001739 float tx = c.active.transform.tx();
1740 float ty = c.active.transform.ty();
1741 c.active = c.requested;
1742 c.active.transform.set(tx, ty);
1743 editCurrentState.active = c.active;
1744 } else {
1745 editCurrentState.active = editCurrentState.requested;
1746 c.active = c.requested;
1747 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001748 }
1749
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001750 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001751 // invalidate and recompute the visible regions if needed
1752 flags |= Layer::eVisibleRegion;
1753 }
1754
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001755 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001756 // invalidate and recompute the visible regions if needed
1757 flags |= eVisibleRegion;
1758 this->contentDirty = true;
1759
1760 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001761 const uint8_t type = c.active.transform.getType();
1762 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001763 (type >= Transform::SCALE));
1764 }
1765
Dan Stozac8145172016-04-28 16:29:06 -07001766 // If the layer is hidden, signal and clear out all local sync points so
1767 // that transactions for layers depending on this layer's frames becoming
1768 // visible are not blocked
1769 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001771 }
1772
Mathias Agopian13127d82013-03-05 17:47:11 -08001773 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001774 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001775 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776}
1777
Pablo Ceballos05289c22016-04-14 15:49:55 -07001778void Layer::commitTransaction(const State& stateToCommit) {
1779 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001780}
1781
Mathias Agopian13127d82013-03-05 17:47:11 -08001782uint32_t Layer::getTransactionFlags(uint32_t flags) {
1783 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1784}
1785
1786uint32_t Layer::setTransactionFlags(uint32_t flags) {
1787 return android_atomic_or(flags, &mTransactionFlags);
1788}
1789
Robert Carr82364e32016-05-15 11:27:47 -07001790bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001791 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001792 return false;
1793 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001794
1795 // We update the requested and active position simultaneously because
1796 // we want to apply the position portion of the transform matrix immediately,
1797 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001798 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001799 if (immediate && !mFreezeGeometryUpdates) {
1800 // Here we directly update the active state
1801 // unlike other setters, because we store it within
1802 // the transform, but use different latching rules.
1803 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001804 mCurrentState.active.transform.set(x, y);
1805 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001806 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001807
Dan Stoza7dde5992015-05-22 09:51:44 -07001808 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001809 setTransactionFlags(eTransactionNeeded);
1810 return true;
1811}
Robert Carr82364e32016-05-15 11:27:47 -07001812
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1814 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1815 if (idx < 0) {
1816 return false;
1817 }
1818 if (childLayer->setLayer(z)) {
1819 mCurrentChildren.removeAt(idx);
1820 mCurrentChildren.add(childLayer);
1821 }
1822 return true;
1823}
1824
Robert Carrae060832016-11-28 10:51:00 -08001825bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001826 if (mCurrentState.z == z)
1827 return false;
1828 mCurrentState.sequence++;
1829 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001830 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001831
1832 // Discard all relative layering.
1833 if (mCurrentState.zOrderRelativeOf != nullptr) {
1834 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1835 if (strongRelative != nullptr) {
1836 strongRelative->removeZOrderRelative(this);
1837 }
1838 mCurrentState.zOrderRelativeOf = nullptr;
1839 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001840 setTransactionFlags(eTransactionNeeded);
1841 return true;
1842}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001843
Robert Carrdb66e622017-04-10 16:55:57 -07001844void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1845 mCurrentState.zOrderRelatives.remove(relative);
1846 mCurrentState.sequence++;
1847 mCurrentState.modified = true;
1848 setTransactionFlags(eTransactionNeeded);
1849}
1850
1851void Layer::addZOrderRelative(const wp<Layer>& relative) {
1852 mCurrentState.zOrderRelatives.add(relative);
1853 mCurrentState.modified = true;
1854 mCurrentState.sequence++;
1855 setTransactionFlags(eTransactionNeeded);
1856}
1857
1858bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1859 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1860 if (handle == nullptr) {
1861 return false;
1862 }
1863 sp<Layer> relative = handle->owner.promote();
1864 if (relative == nullptr) {
1865 return false;
1866 }
1867
1868 mCurrentState.sequence++;
1869 mCurrentState.modified = true;
1870 mCurrentState.z = z;
1871
1872 mCurrentState.zOrderRelativeOf = relative;
1873 relative->addZOrderRelative(this);
1874
1875 setTransactionFlags(eTransactionNeeded);
1876
1877 return true;
1878}
1879
Mathias Agopian13127d82013-03-05 17:47:11 -08001880bool Layer::setSize(uint32_t w, uint32_t h) {
1881 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1882 return false;
1883 mCurrentState.requested.w = w;
1884 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001885 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001886 setTransactionFlags(eTransactionNeeded);
1887 return true;
1888}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001889bool Layer::setAlpha(float alpha) {
chaviw13fdc492017-06-27 12:40:18 -07001890 if (mCurrentState.color.a == alpha)
Mathias Agopian13127d82013-03-05 17:47:11 -08001891 return false;
1892 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001893 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001894 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001895 setTransactionFlags(eTransactionNeeded);
1896 return true;
1897}
chaviw13fdc492017-06-27 12:40:18 -07001898
1899bool Layer::setColor(const half3& color) {
1900 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g
1901 && color.b == mCurrentState.color.b)
1902 return false;
1903
1904 mCurrentState.sequence++;
1905 mCurrentState.color.r = color.r;
1906 mCurrentState.color.g = color.g;
1907 mCurrentState.color.b = color.b;
1908 mCurrentState.modified = true;
1909 setTransactionFlags(eTransactionNeeded);
1910 return true;
1911}
1912
Mathias Agopian13127d82013-03-05 17:47:11 -08001913bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1914 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001915 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001916 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001917 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001918 setTransactionFlags(eTransactionNeeded);
1919 return true;
1920}
1921bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001922 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001923 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001924 setTransactionFlags(eTransactionNeeded);
1925 return true;
1926}
1927bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1928 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1929 if (mCurrentState.flags == newFlags)
1930 return false;
1931 mCurrentState.sequence++;
1932 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001933 mCurrentState.mask = mask;
1934 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001935 setTransactionFlags(eTransactionNeeded);
1936 return true;
1937}
Robert Carr99e27f02016-06-16 15:18:02 -07001938
1939bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001940 if (mCurrentState.requestedCrop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001941 return false;
1942 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001943 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001944 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001945 mCurrentState.crop = crop;
1946 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001947 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1948
Dan Stoza7dde5992015-05-22 09:51:44 -07001949 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001950 setTransactionFlags(eTransactionNeeded);
1951 return true;
1952}
Robert Carr8d5227b2017-03-16 15:41:03 -07001953
1954bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
Robert Carr7bf247e2017-05-18 14:02:49 -07001955 if (mCurrentState.requestedFinalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001956 return false;
1957 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001958 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001959 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001960 mCurrentState.finalCrop = crop;
1961 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001962 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1963
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001964 mCurrentState.modified = true;
1965 setTransactionFlags(eTransactionNeeded);
1966 return true;
1967}
Mathias Agopian13127d82013-03-05 17:47:11 -08001968
Robert Carrc3574f72016-03-24 12:19:32 -07001969bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1970 if (scalingMode == mOverrideScalingMode)
1971 return false;
1972 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001973 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001974 return true;
1975}
1976
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001977void Layer::setInfo(uint32_t type, uint32_t appId) {
1978 mCurrentState.appId = appId;
1979 mCurrentState.type = type;
1980 mCurrentState.modified = true;
1981 setTransactionFlags(eTransactionNeeded);
1982}
1983
Robert Carrc3574f72016-03-24 12:19:32 -07001984uint32_t Layer::getEffectiveScalingMode() const {
1985 if (mOverrideScalingMode >= 0) {
1986 return mOverrideScalingMode;
1987 }
1988 return mCurrentScalingMode;
1989}
1990
Mathias Agopian13127d82013-03-05 17:47:11 -08001991bool Layer::setLayerStack(uint32_t layerStack) {
1992 if (mCurrentState.layerStack == layerStack)
1993 return false;
1994 mCurrentState.sequence++;
1995 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001996 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001997 setTransactionFlags(eTransactionNeeded);
1998 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001999}
2000
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002001bool Layer::setDataSpace(android_dataspace dataSpace) {
2002 if (mCurrentState.dataSpace == dataSpace)
2003 return false;
2004 mCurrentState.sequence++;
2005 mCurrentState.dataSpace = dataSpace;
2006 mCurrentState.modified = true;
2007 setTransactionFlags(eTransactionNeeded);
2008 return true;
2009}
2010
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06002011android_dataspace Layer::getDataSpace() const {
2012 return mCurrentState.dataSpace;
2013}
2014
Robert Carr1f0a16a2016-10-24 16:27:39 -07002015uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002016 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002017 if (p == nullptr) {
2018 return getDrawingState().layerStack;
2019 }
2020 return p->getLayerStack();
2021}
2022
Robert Carr0d480722017-01-10 16:42:54 -08002023void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07002024 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08002025 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07002026 mCurrentState.frameNumber = frameNumber;
2027 // We don't set eTransactionNeeded, because just receiving a deferral
2028 // request without any other state updates shouldn't actually induce a delay
2029 mCurrentState.modified = true;
2030 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08002031 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08002032 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07002033 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08002034}
2035
2036void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
2037 uint64_t frameNumber) {
2038 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
2039 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07002040}
2041
Dan Stozaee44edd2015-03-23 15:50:23 -07002042void Layer::useSurfaceDamage() {
2043 if (mFlinger->mForceFullDamage) {
2044 surfaceDamageRegion = Region::INVALID_REGION;
2045 } else {
2046 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
2047 }
2048}
2049
2050void Layer::useEmptyDamage() {
2051 surfaceDamageRegion.clear();
2052}
2053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002054// ----------------------------------------------------------------------------
2055// pageflip handling...
2056// ----------------------------------------------------------------------------
2057
Dan Stoza6b9454d2014-11-07 16:00:59 -08002058bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002059 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07002060 return true;
2061 }
2062
Dan Stoza6b9454d2014-11-07 16:00:59 -08002063 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002064 if (mQueueItems.empty()) {
2065 return false;
2066 }
2067 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002068 nsecs_t expectedPresent =
2069 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07002070
2071 // Ignore timestamps more than a second in the future
2072 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
2073 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
2074 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
2075 expectedPresent);
2076
2077 bool isDue = timestamp < expectedPresent;
2078 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002079}
2080
Brian Andersond6927fb2016-07-23 23:37:30 -07002081bool Layer::onPreComposition(nsecs_t refreshStartTime) {
2082 if (mBufferLatched) {
2083 Mutex::Autolock lock(mFrameEventHistoryMutex);
2084 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
2085 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002086 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002087 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002088}
2089
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002090bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07002091 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002092 const CompositorTiming& compositorTiming) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002093 // mFrameLatencyNeeded is true when a new frame was latched for the
2094 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08002095 if (!mFrameLatencyNeeded)
2096 return false;
2097
Fabien Sanglard28e98082016-12-05 10:19:46 -08002098 // Update mFrameEventHistory.
2099 {
2100 Mutex::Autolock lock(mFrameEventHistoryMutex);
2101 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002102 glDoneFence, presentFence, compositorTiming);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002103 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08002104
2105 // Update mFrameTracker.
2106 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
2107 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
2108
Brian Anderson3d4039d2016-09-23 16:31:30 -07002109 std::shared_ptr<FenceTime> frameReadyFence =
2110 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08002111 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002112 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002113 } else {
2114 // There was no fence for this frame, so assume that it was ready
2115 // to be presented at the desired present time.
2116 mFrameTracker.setFrameReadyTime(desiredPresentTime);
2117 }
2118
Brian Anderson3d4039d2016-09-23 16:31:30 -07002119 if (presentFence->isValid()) {
2120 mFrameTracker.setActualPresentFence(
2121 std::shared_ptr<FenceTime>(presentFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002122 } else {
2123 // The HWC doesn't support present fences, so use the refresh
2124 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002125 mFrameTracker.setActualPresentTime(
2126 mFlinger->getHwComposer().getRefreshTimestamp(
2127 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08002128 }
2129
2130 mFrameTracker.advanceFrame();
2131 mFrameLatencyNeeded = false;
2132 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002133}
2134
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002135#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07002136void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08002137 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
2138 return;
2139 }
2140
Brian Anderson3d4039d2016-09-23 16:31:30 -07002141 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002142 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002143 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002144 mReleaseTimeline.push(releaseFenceTime);
2145
2146 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002147 if (mPreviousFrameNumber != 0) {
2148 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2149 dequeueReadyTime, std::move(releaseFenceTime));
2150 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002151}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002152#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08002153
Robert Carr1f0a16a2016-10-24 16:27:39 -07002154bool Layer::isHiddenByPolicy() const {
2155 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07002156 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002157 if (parent != nullptr && parent->isHiddenByPolicy()) {
2158 return true;
2159 }
2160 return s.flags & layer_state_t::eLayerHidden;
2161}
2162
Mathias Agopianda27af92012-09-13 18:17:13 -07002163bool Layer::isVisible() const {
Robert Carr6452f122017-03-21 10:41:29 -07002164 return !(isHiddenByPolicy()) && getAlpha() > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08002165 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -07002166}
2167
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002168bool Layer::allTransactionsSignaled() {
2169 auto headFrameNumber = getHeadFrameNumber();
2170 bool matchingFramesFound = false;
2171 bool allTransactionsApplied = true;
2172 Mutex::Autolock lock(mLocalSyncPointMutex);
2173
2174 for (auto& point : mLocalSyncPoints) {
2175 if (point->getFrameNumber() > headFrameNumber) {
2176 break;
2177 }
2178 matchingFramesFound = true;
2179
2180 if (!point->frameIsAvailable()) {
2181 // We haven't notified the remote layer that the frame for
2182 // this point is available yet. Notify it now, and then
2183 // abort this attempt to latch.
2184 point->setFrameAvailable();
2185 allTransactionsApplied = false;
2186 break;
2187 }
2188
2189 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
2190 }
2191 return !matchingFramesFound || allTransactionsApplied;
2192}
2193
Brian Andersond6927fb2016-07-23 23:37:30 -07002194Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002196 ATRACE_CALL();
2197
Jesse Hall399184a2014-03-03 15:42:54 -08002198 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2199 // mSidebandStreamChanged was true
2200 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002201 if (mSidebandStream != NULL) {
2202 setTransactionFlags(eTransactionNeeded);
2203 mFlinger->setTransactionFlags(eTraversalNeeded);
2204 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002205 recomputeVisibleRegions = true;
2206
2207 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002208 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002209 }
2210
Mathias Agopian4fec8732012-06-29 14:12:52 -07002211 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002212 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2213 return outDirtyRegion;
2214 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002215
Fabien Sanglard223eb912016-10-13 10:15:06 -07002216 // if we've already called updateTexImage() without going through
2217 // a composition step, we have to skip this layer at this point
2218 // because we cannot call updateTeximage() without a corresponding
2219 // compositionComplete() call.
2220 // we'll trigger an update in onPreComposition().
2221 if (mRefreshPending) {
2222 return outDirtyRegion;
2223 }
2224
2225 // If the head buffer's acquire fence hasn't signaled yet, return and
2226 // try again later
2227 if (!headFenceHasSignaled()) {
2228 mFlinger->signalLayerUpdate();
2229 return outDirtyRegion;
2230 }
2231
2232 // Capture the old state of the layer for comparisons later
2233 const State& s(getDrawingState());
2234 const bool oldOpacity = isOpaque(s);
2235 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2236
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002237 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002238 mFlinger->signalLayerUpdate();
2239 return outDirtyRegion;
2240 }
2241
2242 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2243 // of the buffer queue isn't modified when the buffer queue is returning
2244 // BufferItem's that weren't actually queued. This can happen in shared
2245 // buffer mode.
2246 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002247 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2248 getProducerStickyTransform() != 0, mName.string(),
Robert Carr7bf247e2017-05-18 14:02:49 -07002249 mOverrideScalingMode, mFreezeGeometryUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002250 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2251 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2252 mLastFrameNumberReceived);
2253 if (updateResult == BufferQueue::PRESENT_LATER) {
2254 // Producer doesn't want buffer to be displayed yet. Signal a
2255 // layer update so we check again at the next opportunity.
2256 mFlinger->signalLayerUpdate();
2257 return outDirtyRegion;
2258 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2259 // If the buffer has been rejected, remove it from the shadow queue
2260 // and return early
2261 if (queuedBuffer) {
2262 Mutex::Autolock lock(mQueueItemLock);
2263 mQueueItems.removeAt(0);
2264 android_atomic_dec(&mQueuedFrames);
2265 }
2266 return outDirtyRegion;
2267 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2268 // This can occur if something goes wrong when trying to create the
2269 // EGLImage for this buffer. If this happens, the buffer has already
2270 // been released, so we need to clean up the queue and bug out
2271 // early.
2272 if (queuedBuffer) {
2273 Mutex::Autolock lock(mQueueItemLock);
2274 mQueueItems.clear();
2275 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002276 }
2277
Fabien Sanglard223eb912016-10-13 10:15:06 -07002278 // Once we have hit this state, the shadow queue may no longer
2279 // correctly reflect the incoming BufferQueue's contents, so even if
2280 // updateTexImage starts working, the only safe course of action is
2281 // to continue to ignore updates.
2282 mUpdateTexImageFailed = true;
2283
2284 return outDirtyRegion;
2285 }
2286
2287 if (queuedBuffer) {
2288 // Autolock scope
2289 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2290
2291 Mutex::Autolock lock(mQueueItemLock);
2292
2293 // Remove any stale buffers that have been dropped during
2294 // updateTexImage
2295 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2296 mQueueItems.removeAt(0);
2297 android_atomic_dec(&mQueuedFrames);
2298 }
2299
2300 mQueueItems.removeAt(0);
2301 }
2302
2303
2304 // Decrement the queued-frames count. Signal another event if we
2305 // have more frames pending.
2306 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2307 || mAutoRefresh) {
2308 mFlinger->signalLayerUpdate();
2309 }
2310
Fabien Sanglard223eb912016-10-13 10:15:06 -07002311 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002312 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2313 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002314 if (mActiveBuffer == NULL) {
2315 // this can only happen if the very first buffer was rejected.
2316 return outDirtyRegion;
2317 }
2318
Brian Andersond6927fb2016-07-23 23:37:30 -07002319 mBufferLatched = true;
2320 mPreviousFrameNumber = mCurrentFrameNumber;
2321 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2322
2323 {
2324 Mutex::Autolock lock(mFrameEventHistoryMutex);
2325 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2326#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002327 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002328 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002329 mReleaseTimeline.updateSignalTimes();
Brian Anderson3d4039d2016-09-23 16:31:30 -07002330 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002331 if (mPreviousFrameNumber != 0) {
2332 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2333 latchTime, std::move(releaseFenceTime));
2334 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002335#endif
2336 }
2337
Fabien Sanglard223eb912016-10-13 10:15:06 -07002338 mRefreshPending = true;
2339 mFrameLatencyNeeded = true;
2340 if (oldActiveBuffer == NULL) {
2341 // the first time we receive a buffer, we need to trigger a
2342 // geometry invalidation.
2343 recomputeVisibleRegions = true;
2344 }
2345
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002346 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2347
Fabien Sanglard223eb912016-10-13 10:15:06 -07002348 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2349 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2350 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2351 if ((crop != mCurrentCrop) ||
2352 (transform != mCurrentTransform) ||
2353 (scalingMode != mCurrentScalingMode))
2354 {
2355 mCurrentCrop = crop;
2356 mCurrentTransform = transform;
2357 mCurrentScalingMode = scalingMode;
2358 recomputeVisibleRegions = true;
2359 }
2360
2361 if (oldActiveBuffer != NULL) {
2362 uint32_t bufWidth = mActiveBuffer->getWidth();
2363 uint32_t bufHeight = mActiveBuffer->getHeight();
2364 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2365 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002366 recomputeVisibleRegions = true;
2367 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002368 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002369
Fabien Sanglard223eb912016-10-13 10:15:06 -07002370 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2371 if (oldOpacity != isOpaque(s)) {
2372 recomputeVisibleRegions = true;
2373 }
Dan Stozacac35382016-01-27 12:21:06 -08002374
Fabien Sanglard223eb912016-10-13 10:15:06 -07002375 // Remove any sync points corresponding to the buffer which was just
2376 // latched
2377 {
2378 Mutex::Autolock lock(mLocalSyncPointMutex);
2379 auto point = mLocalSyncPoints.begin();
2380 while (point != mLocalSyncPoints.end()) {
2381 if (!(*point)->frameIsAvailable() ||
2382 !(*point)->transactionIsApplied()) {
2383 // This sync point must have been added since we started
2384 // latching. Don't drop it yet.
2385 ++point;
2386 continue;
2387 }
2388
2389 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2390 point = mLocalSyncPoints.erase(point);
2391 } else {
2392 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002393 }
2394 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002395 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002396
2397 // FIXME: postedRegion should be dirty & bounds
2398 Region dirtyRegion(Rect(s.active.w, s.active.h));
2399
2400 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002401 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002402
Mathias Agopian4fec8732012-06-29 14:12:52 -07002403 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002404}
2405
Mathias Agopiana67932f2011-04-20 14:20:59 -07002406uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002407{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002408 // TODO: should we do something special if mSecure is set?
2409 if (mProtectedByApp) {
2410 // need a hardware-protected path to external video sink
2411 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002412 }
Riley Andrews03414a12014-07-01 14:22:59 -07002413 if (mPotentialCursor) {
2414 usage |= GraphicBuffer::USAGE_CURSOR;
2415 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002416 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002417 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002418}
2419
Mathias Agopian84300952012-11-21 16:02:13 -08002420void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002421 uint32_t orientation = 0;
2422 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002423 // The transform hint is used to improve performance, but we can
2424 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002425 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002426 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002427 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002428 if (orientation & Transform::ROT_INVALID) {
2429 orientation = 0;
2430 }
2431 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002432 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002433}
2434
Mathias Agopian13127d82013-03-05 17:47:11 -08002435// ----------------------------------------------------------------------------
2436// debugging
2437// ----------------------------------------------------------------------------
2438
Kalle Raitaa099a242017-01-11 11:17:29 -08002439LayerDebugInfo Layer::getLayerDebugInfo() const {
2440 LayerDebugInfo info;
2441 const Layer::State& ds = getDrawingState();
2442 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07002443 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08002444 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
2445 info.mType = String8(getTypeId());
2446 info.mTransparentRegion = ds.activeTransparentRegion;
2447 info.mVisibleRegion = visibleRegion;
2448 info.mSurfaceDamageRegion = surfaceDamageRegion;
2449 info.mLayerStack = getLayerStack();
2450 info.mX = ds.active.transform.tx();
2451 info.mY = ds.active.transform.ty();
2452 info.mZ = ds.z;
2453 info.mWidth = ds.active.w;
2454 info.mHeight = ds.active.h;
2455 info.mCrop = ds.crop;
2456 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07002457 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08002458 info.mFlags = ds.flags;
2459 info.mPixelFormat = getPixelFormat();
2460 info.mDataSpace = getDataSpace();
2461 info.mMatrix[0][0] = ds.active.transform[0][0];
2462 info.mMatrix[0][1] = ds.active.transform[0][1];
2463 info.mMatrix[1][0] = ds.active.transform[1][0];
2464 info.mMatrix[1][1] = ds.active.transform[1][1];
2465 {
2466 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
2467 if (activeBuffer != 0) {
2468 info.mActiveBufferWidth = activeBuffer->getWidth();
2469 info.mActiveBufferHeight = activeBuffer->getHeight();
2470 info.mActiveBufferStride = activeBuffer->getStride();
2471 info.mActiveBufferFormat = activeBuffer->format;
2472 } else {
2473 info.mActiveBufferWidth = 0;
2474 info.mActiveBufferHeight = 0;
2475 info.mActiveBufferStride = 0;
2476 info.mActiveBufferFormat = 0;
2477 }
Mathias Agopian13127d82013-03-05 17:47:11 -08002478 }
Kalle Raitaa099a242017-01-11 11:17:29 -08002479 info.mNumQueuedFrames = getQueuedFrameCount();
2480 info.mRefreshPending = isBufferLatched();
2481 info.mIsOpaque = isOpaque(ds);
2482 info.mContentDirty = contentDirty;
2483 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08002484}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002485#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002486void Layer::miniDumpHeader(String8& result) {
2487 result.append("----------------------------------------");
2488 result.append("---------------------------------------\n");
2489 result.append(" Layer name\n");
2490 result.append(" Z | ");
2491 result.append(" Comp Type | ");
2492 result.append(" Disp Frame (LTRB) | ");
2493 result.append(" Source Crop (LTRB)\n");
2494 result.append("----------------------------------------");
2495 result.append("---------------------------------------\n");
2496}
2497
2498void Layer::miniDump(String8& result, int32_t hwcId) const {
2499 if (mHwcLayers.count(hwcId) == 0) {
2500 return;
2501 }
2502
2503 String8 name;
2504 if (mName.length() > 77) {
2505 std::string shortened;
2506 shortened.append(mName.string(), 36);
2507 shortened.append("[...]");
2508 shortened.append(mName.string() + (mName.length() - 36), 36);
2509 name = shortened.c_str();
2510 } else {
2511 name = mName;
2512 }
2513
2514 result.appendFormat(" %s\n", name.string());
2515
2516 const Layer::State& layerState(getDrawingState());
2517 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07002518 result.appendFormat(" %10d | ", layerState.z);
Dan Stozae22aec72016-08-01 13:20:59 -07002519 result.appendFormat("%10s | ",
2520 to_string(getCompositionType(hwcId)).c_str());
2521 const Rect& frame = hwcInfo.displayFrame;
2522 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2523 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002524 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002525 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2526 crop.right, crop.bottom);
2527
2528 result.append("- - - - - - - - - - - - - - - - - - - - ");
2529 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2530}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002531#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002532
Svetoslavd85084b2014-03-20 10:28:31 -07002533void Layer::dumpFrameStats(String8& result) const {
2534 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002535}
2536
Svetoslavd85084b2014-03-20 10:28:31 -07002537void Layer::clearFrameStats() {
2538 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002539}
2540
Jamie Gennis6547ff42013-07-16 20:12:42 -07002541void Layer::logFrameStats() {
2542 mFrameTracker.logAndResetStats(mName);
2543}
2544
Svetoslavd85084b2014-03-20 10:28:31 -07002545void Layer::getFrameStats(FrameStats* outStats) const {
2546 mFrameTracker.getStats(outStats);
2547}
2548
Brian Andersond6927fb2016-07-23 23:37:30 -07002549void Layer::dumpFrameEvents(String8& result) {
2550 result.appendFormat("- Layer %s (%s, %p)\n",
2551 getName().string(), getTypeId(), this);
2552 Mutex::Autolock lock(mFrameEventHistoryMutex);
2553 mFrameEventHistory.checkFencesForCompletion();
2554 mFrameEventHistory.dump(result);
2555}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002556
Brian Anderson5ea5e592016-12-01 16:54:33 -08002557void Layer::onDisconnect() {
2558 Mutex::Autolock lock(mFrameEventHistoryMutex);
2559 mFrameEventHistory.onDisconnect();
2560}
2561
Brian Anderson3890c392016-07-25 12:48:08 -07002562void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2563 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002564 Mutex::Autolock lock(mFrameEventHistoryMutex);
2565 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07002566 // If there are any unsignaled fences in the aquire timeline at this
2567 // point, the previously queued frame hasn't been latched yet. Go ahead
2568 // and try to get the signal time here so the syscall is taken out of
2569 // the main thread's critical path.
2570 mAcquireTimeline.updateSignalTimes();
2571 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07002572 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002573 mFrameEventHistory.addQueue(*newTimestamps);
2574 }
2575
Brian Anderson3890c392016-07-25 12:48:08 -07002576 if (outDelta) {
2577 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002578 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002579}
Dan Stozae77c7662016-05-13 11:37:28 -07002580
2581std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2582 bool forceFlush) {
2583 std::vector<OccupancyTracker::Segment> history;
2584 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2585 &history);
2586 if (result != NO_ERROR) {
2587 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2588 result);
2589 return {};
2590 }
2591 return history;
2592}
2593
Robert Carr367c5682016-06-20 11:55:28 -07002594bool Layer::getTransformToDisplayInverse() const {
2595 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2596}
2597
Chia-I Wu98f1c102017-05-30 14:54:08 -07002598size_t Layer::getChildrenCount() const {
2599 size_t count = 0;
2600 for (const sp<Layer>& child : mCurrentChildren) {
2601 count += 1 + child->getChildrenCount();
2602 }
2603 return count;
2604}
2605
Robert Carr1f0a16a2016-10-24 16:27:39 -07002606void Layer::addChild(const sp<Layer>& layer) {
2607 mCurrentChildren.add(layer);
2608 layer->setParent(this);
2609}
2610
2611ssize_t Layer::removeChild(const sp<Layer>& layer) {
2612 layer->setParent(nullptr);
2613 return mCurrentChildren.remove(layer);
2614}
2615
Robert Carr1db73f62016-12-21 12:58:51 -08002616bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2617 sp<Handle> handle = nullptr;
2618 sp<Layer> newParent = nullptr;
2619 if (newParentHandle == nullptr) {
2620 return false;
2621 }
2622 handle = static_cast<Handle*>(newParentHandle.get());
2623 newParent = handle->owner.promote();
2624 if (newParent == nullptr) {
2625 ALOGE("Unable to promote Layer handle");
2626 return false;
2627 }
2628
2629 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002630 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08002631
2632 sp<Client> client(child->mClientRef.promote());
2633 if (client != nullptr) {
2634 client->setParentLayer(newParent);
2635 }
2636 }
2637 mCurrentChildren.clear();
2638
2639 return true;
2640}
2641
chaviwf1961f72017-09-18 16:41:07 -07002642bool Layer::reparent(const sp<IBinder>& newParentHandle) {
2643 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07002644 return false;
2645 }
2646
2647 auto handle = static_cast<Handle*>(newParentHandle.get());
2648 sp<Layer> newParent = handle->owner.promote();
2649 if (newParent == nullptr) {
2650 ALOGE("Unable to promote Layer handle");
2651 return false;
2652 }
2653
chaviwf1961f72017-09-18 16:41:07 -07002654 sp<Layer> parent = getParent();
2655 if (parent != nullptr) {
2656 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07002657 }
chaviwf1961f72017-09-18 16:41:07 -07002658 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07002659
chaviwf1961f72017-09-18 16:41:07 -07002660 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07002661 sp<Client> newParentClient(newParent->mClientRef.promote());
2662
chaviwf1961f72017-09-18 16:41:07 -07002663 if (client != newParentClient) {
2664 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07002665 }
2666
chaviw06178942017-07-27 10:25:59 -07002667 return true;
2668}
2669
Robert Carr9524cb32017-02-13 11:32:32 -08002670bool Layer::detachChildren() {
Dan Stoza412903f2017-04-27 13:42:17 -07002671 traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) {
Robert Carr9524cb32017-02-13 11:32:32 -08002672 if (child == this) {
2673 return;
2674 }
2675
chaviw161410b02017-07-27 10:46:08 -07002676 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08002677 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07002678 if (client != nullptr && parentClient != client) {
Robert Carr9524cb32017-02-13 11:32:32 -08002679 client->detachLayer(child);
2680 }
2681 });
2682
2683 return true;
2684}
2685
Robert Carr1f0a16a2016-10-24 16:27:39 -07002686void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002687 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002688}
2689
2690void Layer::clearSyncPoints() {
2691 for (const auto& child : mCurrentChildren) {
2692 child->clearSyncPoints();
2693 }
2694
2695 Mutex::Autolock lock(mLocalSyncPointMutex);
2696 for (auto& point : mLocalSyncPoints) {
2697 point->setFrameAvailable();
2698 }
2699 mLocalSyncPoints.clear();
2700}
2701
2702int32_t Layer::getZ() const {
2703 return mDrawingState.z;
2704}
2705
Dan Stoza412903f2017-04-27 13:42:17 -07002706LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) {
2707 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
2708 "makeTraversalList received invalid stateSet");
2709 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2710 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2711 const State& state = useDrawing ? mDrawingState : mCurrentState;
2712
2713 if (state.zOrderRelatives.size() == 0) {
2714 return children;
Robert Carrdb66e622017-04-10 16:55:57 -07002715 }
2716 LayerVector traverse;
2717
Dan Stoza412903f2017-04-27 13:42:17 -07002718 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07002719 sp<Layer> strongRelative = weakRelative.promote();
2720 if (strongRelative != nullptr) {
2721 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07002722 }
2723 }
2724
Dan Stoza412903f2017-04-27 13:42:17 -07002725 for (const sp<Layer>& child : children) {
Robert Carrdb66e622017-04-10 16:55:57 -07002726 traverse.add(child);
2727 }
2728
2729 return traverse;
2730}
2731
Robert Carr1f0a16a2016-10-24 16:27:39 -07002732/**
Robert Carrdb66e622017-04-10 16:55:57 -07002733 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002734 */
Dan Stoza412903f2017-04-27 13:42:17 -07002735void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
2736 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002737
Robert Carr1f0a16a2016-10-24 16:27:39 -07002738 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002739 for (; i < list.size(); i++) {
2740 const auto& relative = list[i];
2741 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002742 break;
Robert Carrdb66e622017-04-10 16:55:57 -07002743 }
Dan Stoza412903f2017-04-27 13:42:17 -07002744 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002745 }
Dan Stoza412903f2017-04-27 13:42:17 -07002746 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07002747 for (; i < list.size(); i++) {
2748 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002749 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002750 }
2751}
2752
2753/**
Robert Carrdb66e622017-04-10 16:55:57 -07002754 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07002755 */
Dan Stoza412903f2017-04-27 13:42:17 -07002756void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
2757 const LayerVector::Visitor& visitor) {
2758 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07002759
Robert Carr1f0a16a2016-10-24 16:27:39 -07002760 int32_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07002761 for (i = list.size()-1; i>=0; i--) {
2762 const auto& relative = list[i];
2763 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07002764 break;
2765 }
Dan Stoza412903f2017-04-27 13:42:17 -07002766 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002767 }
Dan Stoza412903f2017-04-27 13:42:17 -07002768 visitor(this);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002769 for (; i>=0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07002770 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07002771 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07002772 }
2773}
2774
2775Transform Layer::getTransform() const {
2776 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002777 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002778 if (p != nullptr) {
2779 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002780
2781 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2782 // it isFixedSize) then there may be additional scaling not accounted
2783 // for in the transform. We need to mirror this scaling in child surfaces
2784 // or we will break the contract where WM can treat child surfaces as
2785 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07002786 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002787 int bufferWidth;
2788 int bufferHeight;
2789 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
2790 bufferWidth = p->mActiveBuffer->getWidth();
2791 bufferHeight = p->mActiveBuffer->getHeight();
2792 } else {
2793 bufferHeight = p->mActiveBuffer->getWidth();
2794 bufferWidth = p->mActiveBuffer->getHeight();
2795 }
Robert Carr9b429f42017-04-17 14:56:57 -07002796 float sx = p->getDrawingState().active.w /
Robert Carr1725eee2017-04-26 18:32:15 -07002797 static_cast<float>(bufferWidth);
Robert Carr9b429f42017-04-17 14:56:57 -07002798 float sy = p->getDrawingState().active.h /
Robert Carr1725eee2017-04-26 18:32:15 -07002799 static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07002800 Transform extraParentScaling;
2801 extraParentScaling.set(sx, 0, 0, sy);
2802 t = t * extraParentScaling;
2803 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002804 }
2805 return t * getDrawingState().active.transform;
2806}
2807
chaviw13fdc492017-06-27 12:40:18 -07002808half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07002809 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07002810
chaviw13fdc492017-06-27 12:40:18 -07002811 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2812 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002813}
Robert Carr6452f122017-03-21 10:41:29 -07002814
chaviw13fdc492017-06-27 12:40:18 -07002815half4 Layer::getColor() const {
2816 const half4 color(getDrawingState().color);
2817 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002818}
Robert Carr6452f122017-03-21 10:41:29 -07002819
Robert Carr1f0a16a2016-10-24 16:27:39 -07002820void Layer::commitChildList() {
2821 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2822 const auto& child = mCurrentChildren[i];
2823 child->commitChildList();
2824 }
2825 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002826 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002827}
2828
Mathias Agopian13127d82013-03-05 17:47:11 -08002829// ---------------------------------------------------------------------------
2830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002831}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002832
2833#if defined(__gl_h_)
2834#error "don't include gl/gl.h in this file"
2835#endif
2836
2837#if defined(__gl2_h_)
2838#error "don't include gl2/gl2.h in this file"
2839#endif