blob: 0eef708afdde497850d3dbfd857ce64ab5ce59e2 [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
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Dan Stoza6b9454d2014-11-07 16:00:59 -080040#include <gui/BufferItem.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020044#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070047#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070048#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian1b031492012-06-20 17:51:20 -070051#include "DisplayHardware/HWComposer.h"
52
Mathias Agopian875d8e12013-06-07 15:35:48 -070053#include "RenderEngine/RenderEngine.h"
54
Dan Stozac5da2712016-07-20 15:38:12 -070055#include <mutex>
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#define DEBUG_RESIZE 0
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059namespace android {
60
61// ---------------------------------------------------------------------------
62
Mathias Agopian13127d82013-03-05 17:47:11 -080063int32_t Layer::sSequence = 1;
64
Mathias Agopian4d9b8222013-03-12 17:11:48 -070065Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
66 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080067 : contentDirty(false),
68 sequence(uint32_t(android_atomic_inc(&sSequence))),
69 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070070 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mPremultipliedAlpha(true),
72 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080078 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070080 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070081 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mCurrentOpacity(true),
Brian Andersond6927fb2016-07-23 23:37:30 -070083 mBufferLatched(false),
Dan Stozacac35382016-01-27 12:21:06 -080084 mCurrentFrameNumber(0),
Brian Andersond6927fb2016-07-23 23:37:30 -070085 mPreviousFrameNumber(-1U),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080086 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080087 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mFiltering(false),
89 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070090 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000091#ifndef USE_HWC2
92 mIsGlesComposition(false),
93#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080094 mProtectedByApp(false),
95 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070096 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070097 mPotentialCursor(false),
98 mQueueItemLock(),
99 mQueueItemCondition(),
100 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700101 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -0800102 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700103 mAutoRefresh(false),
104 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000106#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800107 ALOGV("Creating Layer %s", name.string());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000108#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800109
Mathias Agopiana67932f2011-04-20 14:20:59 -0700110 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700111 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700112 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 uint32_t layerFlags = 0;
115 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800116 layerFlags |= layer_state_t::eLayerHidden;
117 if (flags & ISurfaceComposerClient::eOpaque)
118 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700119 if (flags & ISurfaceComposerClient::eSecure)
120 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121
122 if (flags & ISurfaceComposerClient::eNonPremultiplied)
123 mPremultipliedAlpha = false;
124
125 mName = name;
126
127 mCurrentState.active.w = w;
128 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800129 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700130 mCurrentState.crop.makeInvalid();
131 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132 mCurrentState.z = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000133#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800134 mCurrentState.alpha = 1.0f;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000135#else
136 mCurrentState.alpha = 0xFF;
137#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138 mCurrentState.layerStack = 0;
139 mCurrentState.flags = layerFlags;
140 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700141 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700142 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500143 mCurrentState.appId = 0;
144 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145
146 // drawing state & current state are identical
147 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000149#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150 const auto& hwc = flinger->getHwComposer();
151 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
152 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000153#else
154 nsecs_t displayPeriod =
155 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
156#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700157 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800158
159 CompositorTiming compositorTiming;
160 flinger->getCompositorTiming(&compositorTiming);
161 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800162}
163
Mathias Agopian3f844832013-08-07 21:24:32 -0700164void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800165 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700166 sp<IGraphicBufferProducer> producer;
167 sp<IGraphicBufferConsumer> consumer;
Irvel468051e2016-06-13 16:44:44 -0700168 BufferQueue::createBufferQueue(&producer, &consumer, nullptr, true);
Robert Carr1db73f62016-12-21 12:58:51 -0800169 mProducer = new MonitoredProducer(producer, mFlinger, this);
Irvel468051e2016-06-13 16:44:44 -0700170 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800171 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800172 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700173 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800174
Fabien Sanglard63a5fcd2016-12-29 15:13:07 -0800175 if (mFlinger->isLayerTripleBufferingDisabled()) {
176 mProducer->setMaxDequeuedBufferCount(2);
177 }
Andy McFadden69052052012-09-14 16:10:11 -0700178
Mathias Agopian84300952012-11-21 16:02:13 -0800179 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
180 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700181}
182
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700183Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800184 sp<Client> c(mClientRef.promote());
185 if (c != 0) {
186 c->detachLayer(this);
187 }
188
Dan Stozacac35382016-01-27 12:21:06 -0800189 for (auto& point : mRemoteSyncPoints) {
190 point->setTransactionApplied();
191 }
Dan Stozac8145172016-04-28 16:29:06 -0700192 for (auto& point : mLocalSyncPoints) {
193 point->setFrameAvailable();
194 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700195 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700196 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700197}
198
Mathias Agopian13127d82013-03-05 17:47:11 -0800199// ---------------------------------------------------------------------------
200// callbacks
201// ---------------------------------------------------------------------------
202
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000203#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
205 if (mHwcLayers.empty()) {
206 return;
207 }
208 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
209}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000210#else
211void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
212 HWComposer::HWCLayerInterface* layer) {
213 if (layer) {
214 layer->onDisplayed();
215 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
216 }
217}
218#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800219
Dan Stoza6b9454d2014-11-07 16:00:59 -0800220void Layer::onFrameAvailable(const BufferItem& item) {
221 // Add this buffer from our internal queue tracker
222 { // Autolock scope
223 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700224 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
225 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700226 // Reset the frame number tracker when we receive the first buffer after
227 // a frame number reset
228 if (item.mFrameNumber == 1) {
229 mLastFrameNumberReceived = 0;
230 }
231
232 // Ensure that callbacks are handled in order
233 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
234 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
235 ms2ns(500));
236 if (result != NO_ERROR) {
237 ALOGE("[%s] Timed out waiting on callback", mName.string());
238 }
239 }
240
Dan Stoza6b9454d2014-11-07 16:00:59 -0800241 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700242 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700243
244 // Wake up any pending callbacks
245 mLastFrameNumberReceived = item.mFrameNumber;
246 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800247 }
248
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800249 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700250}
251
Dan Stoza6b9454d2014-11-07 16:00:59 -0800252void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700253 { // Autolock scope
254 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700255
Dan Stoza7dde5992015-05-22 09:51:44 -0700256 // Ensure that callbacks are handled in order
257 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
258 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
259 ms2ns(500));
260 if (result != NO_ERROR) {
261 ALOGE("[%s] Timed out waiting on callback", mName.string());
262 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700263 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700264
265 if (mQueueItems.empty()) {
266 ALOGE("Can't replace a frame on an empty queue");
267 return;
268 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700269 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700270
271 // Wake up any pending callbacks
272 mLastFrameNumberReceived = item.mFrameNumber;
273 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700274 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800275}
276
Chia-I Wu06d63de2017-01-04 14:58:51 +0800277void Layer::onBuffersReleased() {
278#ifdef USE_HWC2
279 Mutex::Autolock lock(mHwcBufferCacheMutex);
280
281 for (auto info : mHwcBufferCaches) {
282 info.second.clear();
283 }
284#endif
285}
286
Jesse Hall399184a2014-03-03 15:42:54 -0800287void Layer::onSidebandStreamChanged() {
288 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
289 // mSidebandStreamChanged was false
290 mFlinger->signalLayerUpdate();
291 }
292}
293
Mathias Agopian67106042013-03-14 19:18:13 -0700294// called with SurfaceFlinger::mStateLock from the drawing thread after
295// the layer has been remove from the current state list (and just before
296// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800297void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800298 mSurfaceFlingerConsumer->abandon();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299 for (const auto& child : mCurrentChildren) {
300 child->onRemoved();
301 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700302}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700303
Mathias Agopian13127d82013-03-05 17:47:11 -0800304// ---------------------------------------------------------------------------
305// set-up
306// ---------------------------------------------------------------------------
307
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700308const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800309 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310}
311
Mathias Agopianf9d93272009-06-19 17:00:27 -0700312status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 PixelFormat format, uint32_t flags)
314{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700315 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700316 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700317
318 // never allow a surface larger than what our underlying GL implementation
319 // can handle.
320 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800321 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700322 return BAD_VALUE;
323 }
324
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700326
Riley Andrews03414a12014-07-01 14:22:59 -0700327 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700328 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700329 mCurrentOpacity = getOpacityForFormat(format);
330
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800331 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
332 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
333 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 return NO_ERROR;
336}
337
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700338sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800339 Mutex::Autolock _l(mLock);
340
341 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700342 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800343
344 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700345
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700346 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800347}
348
Dan Stozab9b08832014-03-13 11:55:57 -0700349sp<IGraphicBufferProducer> Layer::getProducer() const {
350 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700351}
352
Mathias Agopian13127d82013-03-05 17:47:11 -0800353// ---------------------------------------------------------------------------
354// h/w composer set-up
355// ---------------------------------------------------------------------------
356
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800357Rect Layer::getContentCrop() const {
358 // this is the crop rectangle that applies to the buffer
359 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700360 Rect crop;
361 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800362 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700363 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800364 } else if (mActiveBuffer != NULL) {
365 // otherwise we use the whole buffer
366 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700367 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800368 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700369 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700370 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700371 return crop;
372}
373
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700374static Rect reduce(const Rect& win, const Region& exclude) {
375 if (CC_LIKELY(exclude.isEmpty())) {
376 return win;
377 }
378 if (exclude.isRect()) {
379 return win.reduce(exclude.getBounds());
380 }
381 return Region(win).subtract(exclude).getBounds();
382}
383
Robert Carr1f0a16a2016-10-24 16:27:39 -0700384Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
385 const Layer::State& s(getDrawingState());
386 Rect win(s.active.w, s.active.h);
387
388 if (!s.crop.isEmpty()) {
389 win.intersect(s.crop, &win);
390 }
391
392 Transform t = getTransform();
393 win = t.transform(win);
394
395 const sp<Layer>& p = getParent();
396 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
397 // When calculating the parent bounds for purposes of clipping,
398 // we don't need to constrain the parent to its transparent region.
399 // The transparent region is an optimization based on the
400 // buffer contents of the layer, but does not affect the space allocated to
401 // it by policy, and thus children should be allowed to extend into the
402 // parent's transparent region. In fact one of the main uses, is to reduce
403 // buffer allocation size in cases where a child window sits behind a main window
404 // (by marking the hole in the parent window as a transparent region)
405 if (p != nullptr) {
406 Rect bounds = p->computeScreenBounds(false);
407 bounds.intersect(win, &win);
408 }
409
410 if (reduceTransparentRegion) {
411 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
412 win = reduce(win, screenTransparentRegion);
413 }
414
415 return win;
416}
417
Mathias Agopian13127d82013-03-05 17:47:11 -0800418Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700419 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700420 return computeBounds(s.activeTransparentRegion);
421}
422
423Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
424 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800425 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700426
427 if (!s.crop.isEmpty()) {
428 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800429 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700430
431 Rect bounds = win;
432 const auto& p = getParent();
433 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800434 // Look in computeScreenBounds recursive call for explanation of
435 // why we pass false here.
436 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700437 }
438
439 Transform t = getTransform();
440 if (p != nullptr) {
441 win = t.transform(win);
442 win.intersect(bounds, &win);
443 win = t.inverse().transform(win);
444 }
445
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700446 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700447 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800448}
449
Robert Carr1f0a16a2016-10-24 16:27:39 -0700450Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700451 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800452 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700453 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800454
455 // apply the projection's clipping to the window crop in
456 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700457 // if there are no window scaling involved, this operation will map to full
458 // pixels in the buffer.
459 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
460 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700461
462 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700463 if (!s.crop.isEmpty()) {
464 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700465 }
466
Robert Carr1f0a16a2016-10-24 16:27:39 -0700467 Transform t = getTransform();
468 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
470 activeCrop.clear();
471 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700472 if (!s.finalCrop.isEmpty()) {
473 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000474 activeCrop.clear();
475 }
476 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700477 return activeCrop;
478}
479
480gfx::FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
481 // the content crop is the area of the content that gets scaled to the
482 // layer's size. This is in buffer space.
483 gfx::FloatRect crop = getContentCrop().toFloatRect();
484
485 // In addition there is a WM-specified crop we pull from our drawing state.
486 const State& s(getDrawingState());
487
488 // Screen space to make reduction to parent crop clearer.
489 Rect activeCrop = computeInitialCrop(hw);
490 const auto& p = getParent();
491 if (p != nullptr) {
492 auto parentCrop = p->computeInitialCrop(hw);
493 activeCrop.intersect(parentCrop, &activeCrop);
494 }
495 Transform t = getTransform();
496 // Back to layer space to work with the content crop.
497 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800498
Michael Lentine28ea2172014-11-19 18:32:37 -0800499 // This needs to be here as transform.transform(Rect) computes the
500 // transformed rect and then takes the bounding box of the result before
501 // returning. This means
502 // transform.inverse().transform(transform.transform(Rect)) != Rect
503 // in which case we need to make sure the final rect is clipped to the
504 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000505 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
506 activeCrop.clear();
507 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800508
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700509 // subtract the transparent region and snap to the bounds
510 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
511
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000512 // Transform the window crop to match the buffer coordinate system,
513 // which means using the inverse of the current transform set on the
514 // SurfaceFlingerConsumer.
515 uint32_t invTransform = mCurrentTransform;
516 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
517 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700518 * the code below applies the primary display's inverse transform to the
519 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000520 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700521 uint32_t invTransformOrient =
522 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000523 // calculate the inverse transform
524 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
525 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
526 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800527 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700529 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
530 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800531 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000532
533 int winWidth = s.active.w;
534 int winHeight = s.active.h;
535 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
536 // If the activeCrop has been rotate the ends are rotated but not
537 // the space itself so when transforming ends back we can't rely on
538 // a modification of the axes of rotation. To account for this we
539 // need to reorient the inverse rotation in terms of the current
540 // axes of rotation.
541 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
542 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
543 if (is_h_flipped == is_v_flipped) {
544 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
545 NATIVE_WINDOW_TRANSFORM_FLIP_H;
546 }
547 winWidth = s.active.h;
548 winHeight = s.active.w;
549 }
550 const Rect winCrop = activeCrop.transform(
551 invTransform, s.active.w, s.active.h);
552
553 // below, crop is intersected with winCrop expressed in crop's coordinate space
554 float xScale = crop.getWidth() / float(winWidth);
555 float yScale = crop.getHeight() / float(winHeight);
556
557 float insetL = winCrop.left * xScale;
558 float insetT = winCrop.top * yScale;
559 float insetR = (winWidth - winCrop.right ) * xScale;
560 float insetB = (winHeight - winCrop.bottom) * yScale;
561
562 crop.left += insetL;
563 crop.top += insetT;
564 crop.right -= insetR;
565 crop.bottom -= insetB;
566
Mathias Agopian13127d82013-03-05 17:47:11 -0800567 return crop;
568}
569
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000570#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800571void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000572#else
573void Layer::setGeometry(
574 const sp<const DisplayDevice>& hw,
575 HWComposer::HWCLayerInterface& layer)
576#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700577{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000578#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 const auto hwcId = displayDevice->getHwcDisplayId();
580 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000581#else
582 layer.setDefaultState();
583#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700584
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700585 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000586#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 hwcInfo.forceClientComposition = false;
588
589 if (isSecure() && !displayDevice->isSecure()) {
590 hwcInfo.forceClientComposition = true;
591 }
592
593 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000594#else
595 layer.setSkip(false);
596
597 if (isSecure() && !hw->isSecure()) {
598 layer.setSkip(true);
599 }
600#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700601
Mathias Agopian13127d82013-03-05 17:47:11 -0800602 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700603 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000604#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 if (!isOpaque(s) || s.alpha != 1.0f) {
606 auto blendMode = mPremultipliedAlpha ?
607 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
608 auto error = hwcLayer->setBlendMode(blendMode);
609 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
610 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
611 to_string(error).c_str(), static_cast<int32_t>(error));
612 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000613#else
614 if (!isOpaque(s) || s.alpha != 0xFF) {
615 layer.setBlending(mPremultipliedAlpha ?
616 HWC_BLENDING_PREMULT :
617 HWC_BLENDING_COVERAGE);
618 }
619#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800620
621 // apply the layer's transform, followed by the display's global transform
622 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700623 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700624 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700625 if (!s.crop.isEmpty()) {
626 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700627 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000628#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000629 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000630#else
631 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
632#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000633 activeCrop.clear();
634 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700635 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800636 // This needs to be here as transform.transform(Rect) computes the
637 // transformed rect and then takes the bounding box of the result before
638 // returning. This means
639 // transform.inverse().transform(transform.transform(Rect)) != Rect
640 // in which case we need to make sure the final rect is clipped to the
641 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000642 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
643 activeCrop.clear();
644 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700645 // mark regions outside the crop as transparent
646 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
647 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
648 s.active.w, s.active.h));
649 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
650 activeCrop.left, activeCrop.bottom));
651 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
652 s.active.w, activeCrop.bottom));
653 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700654
655 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700656 if (!s.finalCrop.isEmpty()) {
657 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000658 frame.clear();
659 }
660 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000661#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000662 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
663 frame.clear();
664 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 const Transform& tr(displayDevice->getTransform());
666 Rect transformedFrame = tr.transform(frame);
667 auto error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700668 if (error != HWC2::Error::None) {
669 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
670 mName.string(), transformedFrame.left, transformedFrame.top,
671 transformedFrame.right, transformedFrame.bottom,
672 to_string(error).c_str(), static_cast<int32_t>(error));
673 } else {
674 hwcInfo.displayFrame = transformedFrame;
675 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676
Dan Stoza71bded52016-10-19 11:10:33 -0700677 gfx::FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700679 if (error != HWC2::Error::None) {
680 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
681 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
682 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
683 static_cast<int32_t>(error));
684 } else {
685 hwcInfo.sourceCrop = sourceCrop;
686 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687
688 error = hwcLayer->setPlaneAlpha(s.alpha);
689 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
690 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
691 static_cast<int32_t>(error));
692
Robert Carrae060832016-11-28 10:51:00 -0800693 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800695 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800696 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500697
698 error = hwcLayer->setInfo(s.type, s.appId);
699 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
700 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000701#else
702 if (!frame.intersect(hw->getViewport(), &frame)) {
703 frame.clear();
704 }
705 const Transform& tr(hw->getTransform());
706 layer.setFrame(tr.transform(frame));
707 layer.setCrop(computeCrop(hw));
708 layer.setPlaneAlpha(s.alpha);
709#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800710
Mathias Agopian29a367b2011-07-12 14:51:45 -0700711 /*
712 * Transformations are applied in this order:
713 * 1) buffer orientation/flip/mirror
714 * 2) state transformation (window manager)
715 * 3) layer orientation (screen orientation)
716 * (NOTE: the matrices are multiplied in reverse order)
717 */
718
719 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700720 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700721
722 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
723 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700724 * the code below applies the primary display's inverse transform to the
725 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700726 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700727 uint32_t invTransform =
728 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700729 // calculate the inverse transform
730 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
731 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
732 NATIVE_WINDOW_TRANSFORM_FLIP_H;
733 }
734 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700735 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700736 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700737
738 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800739 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000740#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 if (orientation & Transform::ROT_INVALID) {
742 // we can only handle simple transformation
743 hwcInfo.forceClientComposition = true;
744 } else {
745 auto transform = static_cast<HWC2::Transform>(orientation);
746 auto error = hwcLayer->setTransform(transform);
747 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
748 "%s (%d)", mName.string(), to_string(transform).c_str(),
749 to_string(error).c_str(), static_cast<int32_t>(error));
750 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000751#else
752 if (orientation & Transform::ROT_INVALID) {
753 // we can only handle simple transformation
754 layer.setSkip(true);
755 } else {
756 layer.setTransform(orientation);
757 }
758#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700759}
760
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000761#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762void Layer::forceClientComposition(int32_t hwcId) {
763 if (mHwcLayers.count(hwcId) == 0) {
764 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
765 return;
766 }
767
768 mHwcLayers[hwcId].forceClientComposition = true;
769}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000770#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000772#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
774 // Apply this display's projection's viewport to the visible region
775 // before giving it to the HWC HAL.
776 const Transform& tr = displayDevice->getTransform();
777 const auto& viewport = displayDevice->getViewport();
778 Region visible = tr.transform(visibleRegion.intersect(viewport));
779 auto hwcId = displayDevice->getHwcDisplayId();
780 auto& hwcLayer = mHwcLayers[hwcId].layer;
781 auto error = hwcLayer->setVisibleRegion(visible);
782 if (error != HWC2::Error::None) {
783 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
784 to_string(error).c_str(), static_cast<int32_t>(error));
785 visible.dump(LOG_TAG);
786 }
787
788 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
789 if (error != HWC2::Error::None) {
790 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
791 to_string(error).c_str(), static_cast<int32_t>(error));
792 surfaceDamageRegion.dump(LOG_TAG);
793 }
794
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700795 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700797 setCompositionType(hwcId, HWC2::Composition::Sideband);
798 ALOGV("[%s] Requesting Sideband composition", mName.string());
799 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800800 if (error != HWC2::Error::None) {
801 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
802 mName.string(), mSidebandStream->handle(),
803 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800804 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700805 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806 }
807
Dan Stoza0a21df72016-07-20 12:52:38 -0700808 // Client layers
809 if (mHwcLayers[hwcId].forceClientComposition ||
810 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700811 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800812 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700813 return;
814 }
815
Dan Stoza0a21df72016-07-20 12:52:38 -0700816 // SolidColor layers
817 if (mActiveBuffer == nullptr) {
818 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700819
820 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700821 error = hwcLayer->setColor({0, 0, 0, 255});
822 if (error != HWC2::Error::None) {
823 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
824 to_string(error).c_str(), static_cast<int32_t>(error));
825 }
Dan Stozac6c89542016-07-27 10:16:52 -0700826
827 // Clear out the transform, because it doesn't make sense absent a
828 // source buffer
829 error = hwcLayer->setTransform(HWC2::Transform::None);
830 if (error != HWC2::Error::None) {
831 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
832 to_string(error).c_str(), static_cast<int32_t>(error));
833 }
834
Dan Stoza0a21df72016-07-20 12:52:38 -0700835 return;
836 }
837
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700838 // Device or Cursor layers
839 if (mPotentialCursor) {
840 ALOGV("[%s] Requesting Cursor composition", mName.string());
841 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800842 } else {
843 ALOGV("[%s] Requesting Device composition", mName.string());
844 setCompositionType(hwcId, HWC2::Composition::Device);
845 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700846
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700847 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
848 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
849 if (error != HWC2::Error::None) {
850 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
851 mCurrentState.dataSpace, to_string(error).c_str(),
852 static_cast<int32_t>(error));
853 }
854
Chia-I Wu06d63de2017-01-04 14:58:51 +0800855 uint32_t hwcSlot = 0;
856 buffer_handle_t hwcHandle = nullptr;
857 {
858 Mutex::Autolock lock(mHwcBufferCacheMutex);
859
860 auto& hwcBufferCache = mHwcBufferCaches[hwcId];
861 sp<GraphicBuffer> hwcBuffer;
862 hwcBufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
863 &hwcSlot, &hwcBuffer);
864 if (hwcBuffer != nullptr) {
865 hwcHandle = hwcBuffer->handle;
866 }
867 }
868
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700869 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Chia-I Wu06d63de2017-01-04 14:58:51 +0800870 error = hwcLayer->setBuffer(hwcSlot, hwcHandle, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700871 if (error != HWC2::Error::None) {
872 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
873 mActiveBuffer->handle, to_string(error).c_str(),
874 static_cast<int32_t>(error));
875 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800876}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000877#else
878void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
879 HWComposer::HWCLayerInterface& layer) {
880 // we have to set the visible region on every frame because
881 // we currently free it during onLayerDisplayed(), which is called
882 // after HWComposer::commit() -- every frame.
883 // Apply this display's projection's viewport to the visible region
884 // before giving it to the HWC HAL.
885 const Transform& tr = hw->getTransform();
886 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
887 layer.setVisibleRegionScreen(visible);
888 layer.setSurfaceDamage(surfaceDamageRegion);
889 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700890
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000891 if (mSidebandStream.get()) {
892 layer.setSidebandStream(mSidebandStream);
893 } else {
894 // NOTE: buffer can be NULL if the client never drew into this
895 // layer yet, or if we ran out of memory
896 layer.setBuffer(mActiveBuffer);
897 }
898}
899#endif
900
901#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800902void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
903 auto hwcId = displayDevice->getHwcDisplayId();
904 if (mHwcLayers.count(hwcId) == 0 ||
905 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
906 return;
907 }
908
909 // This gives us only the "orientation" component of the transform
910 const State& s(getCurrentState());
911
912 // Apply the layer's transform, followed by the display's global transform
913 // Here we're guaranteed that the layer's transform preserves rects
914 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700915 if (!s.crop.isEmpty()) {
916 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800917 }
918 // Subtract the transparent region and snap to the bounds
919 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700920 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800921 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700922 if (!s.finalCrop.isEmpty()) {
923 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000924 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800925 auto& displayTransform(displayDevice->getTransform());
926 auto position = displayTransform.transform(frame);
927
928 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
929 position.top);
930 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
931 "to (%d, %d): %s (%d)", mName.string(), position.left,
932 position.top, to_string(error).c_str(),
933 static_cast<int32_t>(error));
934}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000935#else
936void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
937 HWComposer::HWCLayerInterface& layer) {
938 int fenceFd = -1;
939
940 // TODO: there is a possible optimization here: we only need to set the
941 // acquire fence the first time a new buffer is acquired on EACH display.
942
943 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
944 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
945 if (fence->isValid()) {
946 fenceFd = fence->dup();
947 if (fenceFd == -1) {
948 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
949 }
950 }
951 }
952 layer.setAcquireFenceFd(fenceFd);
953}
954
955Rect Layer::getPosition(
956 const sp<const DisplayDevice>& hw)
957{
958 // this gives us only the "orientation" component of the transform
959 const State& s(getCurrentState());
960
961 // apply the layer's transform, followed by the display's global transform
962 // here we're guaranteed that the layer's transform preserves rects
963 Rect win(s.active.w, s.active.h);
964 if (!s.crop.isEmpty()) {
965 win.intersect(s.crop, &win);
966 }
967 // subtract the transparent region and snap to the bounds
968 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700969 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000970 frame.intersect(hw->getViewport(), &frame);
971 if (!s.finalCrop.isEmpty()) {
972 frame.intersect(s.finalCrop, &frame);
973 }
974 const Transform& tr(hw->getTransform());
975 return Rect(tr.transform(frame));
976}
977#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700978
Mathias Agopian13127d82013-03-05 17:47:11 -0800979// ---------------------------------------------------------------------------
980// drawing...
981// ---------------------------------------------------------------------------
982
983void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800984 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800985}
986
Dan Stozac7014012014-02-14 15:03:43 -0800987void Layer::draw(const sp<const DisplayDevice>& hw,
988 bool useIdentityTransform) const {
989 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800990}
991
Dan Stozac7014012014-02-14 15:03:43 -0800992void Layer::draw(const sp<const DisplayDevice>& hw) const {
993 onDraw(hw, Region(hw->bounds()), false);
994}
995
996void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
997 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800999 ATRACE_CALL();
1000
Mathias Agopiana67932f2011-04-20 14:20:59 -07001001 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001003 // in fact never been drawn into. This happens frequently with
1004 // SurfaceView because the WindowManager can't know when the client
1005 // has drawn the first time.
1006
1007 // If there is nothing under us, we paint the screen in black, otherwise
1008 // we just skip this update.
1009
1010 // figure out if there is something below us
1011 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001012 bool finished = false;
1013 mFlinger->mDrawingState.layersSortedByZ.traverseInZOrder([&](Layer* layer) {
1014 if (finished || layer == static_cast<Layer const*>(this)) {
1015 finished = true;
1016 return;
1017 }
Mathias Agopian42977342012-08-05 00:40:46 -07001018 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001019 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001020 // if not everything below us is covered, we plug the holes!
1021 Region holes(clip.subtract(under));
1022 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001023 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001024 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 return;
1026 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001027
Andy McFadden97eba892012-12-11 15:21:45 -08001028 // Bind the current buffer to the GL texture, and wait for it to be
1029 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001030 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1031 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001032 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001033 // Go ahead and draw the buffer anyway; no matter what we do the screen
1034 // is probably going to have something visibly wrong.
1035 }
1036
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001037 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1038
Mathias Agopian875d8e12013-06-07 15:35:48 -07001039 RenderEngine& engine(mFlinger->getRenderEngine());
1040
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001041 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001042 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001043 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001044
1045 // Query the texture matrix given our current filtering mode.
1046 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001047 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1048 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001049
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001050 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
1051
1052 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001053 * the code below applies the primary display's inverse transform to
1054 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001055 */
1056
1057 // create a 4x4 transform matrix from the display transform flags
1058 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1059 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1060 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1061
1062 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -07001063 uint32_t transform =
1064 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001065 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
1066 tr = tr * rot90;
1067 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
1068 tr = tr * flipH;
1069 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
1070 tr = tr * flipV;
1071
1072 // calculate the inverse
1073 tr = inverse(tr);
1074
1075 // and finally apply it to the original texture matrix
1076 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1077 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1078 }
1079
Jamie Genniscbb1a952012-05-08 17:05:52 -07001080 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001081 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1082 mTexture.setFiltering(useFiltering);
1083 mTexture.setMatrix(textureMatrix);
1084
1085 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001086 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001087 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001088 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001089 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001090 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091}
1092
Mathias Agopian13127d82013-03-05 17:47:11 -08001093
Dan Stozac7014012014-02-14 15:03:43 -08001094void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001095 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001096 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001097{
Mathias Agopian19733a32013-08-28 18:13:56 -07001098 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001099 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001100 engine.setupFillWithColor(red, green, blue, alpha);
1101 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001102}
1103
1104void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001105 const sp<const DisplayDevice>& hw) const {
1106 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001107}
1108
Dan Stozac7014012014-02-14 15:03:43 -08001109void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001110 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001111 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001112
Dan Stozac7014012014-02-14 15:03:43 -08001113 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001114
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 /*
1116 * NOTE: the way we compute the texture coordinates here produces
1117 * different results than when we take the HWC path -- in the later case
1118 * the "source crop" is rounded to texel boundaries.
1119 * This can produce significantly different results when the texture
1120 * is scaled by a large amount.
1121 *
1122 * The GL code below is more logical (imho), and the difference with
1123 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001124 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 * GL composition when a buffer scaling is applied (maybe with some
1126 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1127 * like more of a hack.
1128 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001129 Rect win(computeBounds());
1130
Robert Carr1f0a16a2016-10-24 16:27:39 -07001131 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001132 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001134 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001135 win.clear();
1136 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001137 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001138 if (!win.intersect(computeBounds(), &win)) {
1139 win.clear();
1140 }
1141 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001142
Mathias Agopian3f844832013-08-07 21:24:32 -07001143 float left = float(win.left) / float(s.active.w);
1144 float top = float(win.top) / float(s.active.h);
1145 float right = float(win.right) / float(s.active.w);
1146 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001147
Mathias Agopian875d8e12013-06-07 15:35:48 -07001148 // TODO: we probably want to generate the texture coords with the mesh
1149 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001150 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1151 texCoords[0] = vec2(left, 1.0f - top);
1152 texCoords[1] = vec2(left, 1.0f - bottom);
1153 texCoords[2] = vec2(right, 1.0f - bottom);
1154 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001155
Mathias Agopian875d8e12013-06-07 15:35:48 -07001156 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001157 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001158 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001159 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001160}
1161
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001162#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001163void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1164 bool callIntoHwc) {
1165 if (mHwcLayers.count(hwcId) == 0) {
1166 ALOGE("setCompositionType called without a valid HWC layer");
1167 return;
1168 }
1169 auto& hwcInfo = mHwcLayers[hwcId];
1170 auto& hwcLayer = hwcInfo.layer;
1171 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1172 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1173 if (hwcInfo.compositionType != type) {
1174 ALOGV(" actually setting");
1175 hwcInfo.compositionType = type;
1176 if (callIntoHwc) {
1177 auto error = hwcLayer->setCompositionType(type);
1178 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1179 "composition type %s: %s (%d)", mName.string(),
1180 to_string(type).c_str(), to_string(error).c_str(),
1181 static_cast<int32_t>(error));
1182 }
1183 }
1184}
1185
1186HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001187 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1188 // If we're querying the composition type for a display that does not
1189 // have a HWC counterpart, then it will always be Client
1190 return HWC2::Composition::Client;
1191 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001192 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001193 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001194 return HWC2::Composition::Invalid;
1195 }
1196 return mHwcLayers.at(hwcId).compositionType;
1197}
1198
1199void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1200 if (mHwcLayers.count(hwcId) == 0) {
1201 ALOGE("setClearClientTarget called without a valid HWC layer");
1202 return;
1203 }
1204 mHwcLayers[hwcId].clearClientTarget = clear;
1205}
1206
1207bool Layer::getClearClientTarget(int32_t hwcId) const {
1208 if (mHwcLayers.count(hwcId) == 0) {
1209 ALOGE("getClearClientTarget called without a valid HWC layer");
1210 return false;
1211 }
1212 return mHwcLayers.at(hwcId).clearClientTarget;
1213}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001214#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001215
Ruben Brunk1681d952014-06-27 15:51:55 -07001216uint32_t Layer::getProducerStickyTransform() const {
1217 int producerStickyTransform = 0;
1218 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1219 if (ret != OK) {
1220 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1221 strerror(-ret), ret);
1222 return 0;
1223 }
1224 return static_cast<uint32_t>(producerStickyTransform);
1225}
1226
Dan Stozac5da2712016-07-20 15:38:12 -07001227bool Layer::latchUnsignaledBuffers() {
1228 static bool propertyLoaded = false;
1229 static bool latch = false;
1230 static std::mutex mutex;
1231 std::lock_guard<std::mutex> lock(mutex);
1232 if (!propertyLoaded) {
1233 char value[PROPERTY_VALUE_MAX] = {};
1234 property_get("debug.sf.latch_unsignaled", value, "0");
1235 latch = atoi(value);
1236 propertyLoaded = true;
1237 }
1238 return latch;
1239}
1240
Dan Stozacac35382016-01-27 12:21:06 -08001241uint64_t Layer::getHeadFrameNumber() const {
1242 Mutex::Autolock lock(mQueueItemLock);
1243 if (!mQueueItems.empty()) {
1244 return mQueueItems[0].mFrameNumber;
1245 } else {
1246 return mCurrentFrameNumber;
1247 }
1248}
1249
Dan Stoza1ce65812016-06-15 16:26:27 -07001250bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001251#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001252 if (latchUnsignaledBuffers()) {
1253 return true;
1254 }
1255
Dan Stoza1ce65812016-06-15 16:26:27 -07001256 Mutex::Autolock lock(mQueueItemLock);
1257 if (mQueueItems.empty()) {
1258 return true;
1259 }
1260 if (mQueueItems[0].mIsDroppable) {
1261 // Even though this buffer's fence may not have signaled yet, it could
1262 // be replaced by another buffer before it has a chance to, which means
1263 // that it's possible to get into a situation where a buffer is never
1264 // able to be latched. To avoid this, grab this buffer anyway.
1265 return true;
1266 }
1267 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001268#else
1269 return true;
1270#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001271}
1272
Dan Stozacac35382016-01-27 12:21:06 -08001273bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1274 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1275 // Don't bother with a SyncPoint, since we've already latched the
1276 // relevant frame
1277 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001278 }
1279
Dan Stozacac35382016-01-27 12:21:06 -08001280 Mutex::Autolock lock(mLocalSyncPointMutex);
1281 mLocalSyncPoints.push_back(point);
1282 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283}
1284
Mathias Agopian13127d82013-03-05 17:47:11 -08001285void Layer::setFiltering(bool filtering) {
1286 mFiltering = filtering;
1287}
1288
1289bool Layer::getFiltering() const {
1290 return mFiltering;
1291}
1292
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001293// As documented in libhardware header, formats in the range
1294// 0x100 - 0x1FF are specific to the HAL implementation, and
1295// are known to have no alpha channel
1296// TODO: move definition for device-specific range into
1297// hardware.h, instead of using hard-coded values here.
1298#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1299
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001300bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001301 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1302 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001303 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001304 switch (format) {
1305 case HAL_PIXEL_FORMAT_RGBA_8888:
1306 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001307 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001308 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001309 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001310 }
1311 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001312 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001313}
1314
Mathias Agopian13127d82013-03-05 17:47:11 -08001315// ----------------------------------------------------------------------------
1316// local state
1317// ----------------------------------------------------------------------------
1318
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001319static void boundPoint(vec2* point, const Rect& crop) {
1320 if (point->x < crop.left) {
1321 point->x = crop.left;
1322 }
1323 if (point->x > crop.right) {
1324 point->x = crop.right;
1325 }
1326 if (point->y < crop.top) {
1327 point->y = crop.top;
1328 }
1329 if (point->y > crop.bottom) {
1330 point->y = crop.bottom;
1331 }
1332}
1333
Dan Stozac7014012014-02-14 15:03:43 -08001334void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1335 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001336{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001337 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001338 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001340 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001341
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001342 vec2 lt = vec2(win.left, win.top);
1343 vec2 lb = vec2(win.left, win.bottom);
1344 vec2 rb = vec2(win.right, win.bottom);
1345 vec2 rt = vec2(win.right, win.top);
1346
Robert Carr1f0a16a2016-10-24 16:27:39 -07001347 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001348 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001349 lt = layerTransform.transform(lt);
1350 lb = layerTransform.transform(lb);
1351 rb = layerTransform.transform(rb);
1352 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001353 }
1354
Robert Carrb5d3d262016-03-25 15:08:13 -07001355 if (!s.finalCrop.isEmpty()) {
1356 boundPoint(&lt, s.finalCrop);
1357 boundPoint(&lb, s.finalCrop);
1358 boundPoint(&rb, s.finalCrop);
1359 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001360 }
1361
Mathias Agopianff2ed702013-09-01 21:36:12 -07001362 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001363 position[0] = hwTransform.transform(lt);
1364 position[1] = hwTransform.transform(lb);
1365 position[2] = hwTransform.transform(rb);
1366 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001367 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001368 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001369 }
1370}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001371
Andy McFadden4125a4f2014-01-29 17:17:11 -08001372bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001373{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374 // if we don't have a buffer yet, we're translucent regardless of the
1375 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001376 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001377 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001378 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001379
1380 // if the layer has the opaque flag, then we're always opaque,
1381 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001382 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001383}
1384
Dan Stoza23116082015-06-18 14:58:39 -07001385bool Layer::isSecure() const
1386{
1387 const Layer::State& s(mDrawingState);
1388 return (s.flags & layer_state_t::eLayerSecure);
1389}
1390
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001391bool Layer::isProtected() const
1392{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001393 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001394 return (activeBuffer != 0) &&
1395 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1396}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001397
Mathias Agopian13127d82013-03-05 17:47:11 -08001398bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001399 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001400}
1401
1402bool Layer::isCropped() const {
1403 return !mCurrentCrop.isEmpty();
1404}
1405
1406bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1407 return mNeedsFiltering || hw->needsFiltering();
1408}
1409
1410void Layer::setVisibleRegion(const Region& visibleRegion) {
1411 // always called from main thread
1412 this->visibleRegion = visibleRegion;
1413}
1414
1415void Layer::setCoveredRegion(const Region& coveredRegion) {
1416 // always called from main thread
1417 this->coveredRegion = coveredRegion;
1418}
1419
1420void Layer::setVisibleNonTransparentRegion(const Region&
1421 setVisibleNonTransparentRegion) {
1422 // always called from main thread
1423 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1424}
1425
1426// ----------------------------------------------------------------------------
1427// transaction
1428// ----------------------------------------------------------------------------
1429
Dan Stoza7dde5992015-05-22 09:51:44 -07001430void Layer::pushPendingState() {
1431 if (!mCurrentState.modified) {
1432 return;
1433 }
1434
Dan Stoza7dde5992015-05-22 09:51:44 -07001435 // If this transaction is waiting on the receipt of a frame, generate a sync
1436 // point and send it to the remote layer.
1437 if (mCurrentState.handle != nullptr) {
Dan Stoza22851c32016-08-09 13:21:03 -07001438 sp<IBinder> strongBinder = mCurrentState.handle.promote();
1439 sp<Handle> handle = nullptr;
1440 sp<Layer> handleLayer = nullptr;
1441 if (strongBinder != nullptr) {
1442 handle = static_cast<Handle*>(strongBinder.get());
1443 handleLayer = handle->owner.promote();
1444 }
1445 if (strongBinder == nullptr || handleLayer == nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001446 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1447 // If we can't promote the layer we are intended to wait on,
1448 // then it is expired or otherwise invalid. Allow this transaction
1449 // to be applied as per normal (no synchronization).
1450 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001451 } else {
1452 auto syncPoint = std::make_shared<SyncPoint>(
1453 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001454 if (handleLayer->addSyncPoint(syncPoint)) {
1455 mRemoteSyncPoints.push_back(std::move(syncPoint));
1456 } else {
1457 // We already missed the frame we're supposed to synchronize
1458 // on, so go ahead and apply the state update
1459 mCurrentState.handle = nullptr;
1460 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001461 }
1462
Dan Stoza7dde5992015-05-22 09:51:44 -07001463 // Wake us up to check if the frame has been received
1464 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001465 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001466 }
1467 mPendingStates.push_back(mCurrentState);
1468}
1469
Pablo Ceballos05289c22016-04-14 15:49:55 -07001470void Layer::popPendingState(State* stateToCommit) {
1471 auto oldFlags = stateToCommit->flags;
1472 *stateToCommit = mPendingStates[0];
1473 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1474 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001475
1476 mPendingStates.removeAt(0);
1477}
1478
Pablo Ceballos05289c22016-04-14 15:49:55 -07001479bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001480 bool stateUpdateAvailable = false;
1481 while (!mPendingStates.empty()) {
1482 if (mPendingStates[0].handle != nullptr) {
1483 if (mRemoteSyncPoints.empty()) {
1484 // If we don't have a sync point for this, apply it anyway. It
1485 // will be visually wrong, but it should keep us from getting
1486 // into too much trouble.
1487 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001488 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001489 stateUpdateAvailable = true;
1490 continue;
1491 }
1492
Dan Stozacac35382016-01-27 12:21:06 -08001493 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1494 mPendingStates[0].frameNumber) {
1495 ALOGE("[%s] Unexpected sync point frame number found",
1496 mName.string());
1497
1498 // Signal our end of the sync point and then dispose of it
1499 mRemoteSyncPoints.front()->setTransactionApplied();
1500 mRemoteSyncPoints.pop_front();
1501 continue;
1502 }
1503
Dan Stoza7dde5992015-05-22 09:51:44 -07001504 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1505 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001506 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001507 stateUpdateAvailable = true;
1508
1509 // Signal our end of the sync point and then dispose of it
1510 mRemoteSyncPoints.front()->setTransactionApplied();
1511 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001512 } else {
1513 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001514 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001515 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001516 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001517 stateUpdateAvailable = true;
1518 }
1519 }
1520
1521 // If we still have pending updates, wake SurfaceFlinger back up and point
1522 // it at this layer so we can process them
1523 if (!mPendingStates.empty()) {
1524 setTransactionFlags(eTransactionNeeded);
1525 mFlinger->setTransactionFlags(eTraversalNeeded);
1526 }
1527
1528 mCurrentState.modified = false;
1529 return stateUpdateAvailable;
1530}
1531
1532void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001533 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001534 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001535 Mutex::Autolock lock(mLocalSyncPointMutex);
1536 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001537 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001538 point->setFrameAvailable();
1539 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001540 }
1541}
1542
Mathias Agopian13127d82013-03-05 17:47:11 -08001543uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001544 ATRACE_CALL();
1545
Dan Stoza7dde5992015-05-22 09:51:44 -07001546 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001547 Layer::State c = getCurrentState();
1548 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001549 return 0;
1550 }
1551
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001552 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001554 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1555 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001556
1557 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001558 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001559 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001560 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001561 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001562 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001563 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001564 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001565 this, getName().string(), mCurrentTransform,
1566 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001567 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001568 c.crop.left,
1569 c.crop.top,
1570 c.crop.right,
1571 c.crop.bottom,
1572 c.crop.getWidth(),
1573 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001574 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001575 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001576 s.crop.left,
1577 s.crop.top,
1578 s.crop.right,
1579 s.crop.bottom,
1580 s.crop.getWidth(),
1581 s.crop.getHeight(),
1582 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001584 // record the new size, form this point on, when the client request
1585 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001586 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001587 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001589
Robert Carr82364e32016-05-15 11:27:47 -07001590 const bool resizePending = (c.requested.w != c.active.w) ||
1591 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001592 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001593 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001594 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001595 // if we have a pending resize, unless we are in fixed-size mode.
1596 // the drawing state will be updated only once we receive a buffer
1597 // with the correct size.
1598 //
1599 // in particular, we want to make sure the clip (which is part
1600 // of the geometry state) is latched together with the size but is
1601 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001602 //
1603 // If a sideband stream is attached, however, we want to skip this
1604 // optimization so that transactions aren't missed when a buffer
1605 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001606
1607 flags |= eDontUpdateGeometryState;
1608 }
1609 }
1610
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 // always set active to requested, unless we're asked not to
1612 // this is used by Layer, which special cases resizes.
1613 if (flags & eDontUpdateGeometryState) {
1614 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001615 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001616 if (mFreezePositionUpdates) {
1617 float tx = c.active.transform.tx();
1618 float ty = c.active.transform.ty();
1619 c.active = c.requested;
1620 c.active.transform.set(tx, ty);
1621 editCurrentState.active = c.active;
1622 } else {
1623 editCurrentState.active = editCurrentState.requested;
1624 c.active = c.requested;
1625 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001626 }
1627
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001628 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001629 // invalidate and recompute the visible regions if needed
1630 flags |= Layer::eVisibleRegion;
1631 }
1632
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001633 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001634 // invalidate and recompute the visible regions if needed
1635 flags |= eVisibleRegion;
1636 this->contentDirty = true;
1637
1638 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001639 const uint8_t type = c.active.transform.getType();
1640 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001641 (type >= Transform::SCALE));
1642 }
1643
Dan Stozac8145172016-04-28 16:29:06 -07001644 // If the layer is hidden, signal and clear out all local sync points so
1645 // that transactions for layers depending on this layer's frames becoming
1646 // visible are not blocked
1647 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001649 }
1650
Mathias Agopian13127d82013-03-05 17:47:11 -08001651 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001652 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001653 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654}
1655
Pablo Ceballos05289c22016-04-14 15:49:55 -07001656void Layer::commitTransaction(const State& stateToCommit) {
1657 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001658}
1659
Mathias Agopian13127d82013-03-05 17:47:11 -08001660uint32_t Layer::getTransactionFlags(uint32_t flags) {
1661 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1662}
1663
1664uint32_t Layer::setTransactionFlags(uint32_t flags) {
1665 return android_atomic_or(flags, &mTransactionFlags);
1666}
1667
Robert Carr82364e32016-05-15 11:27:47 -07001668bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001669 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001670 return false;
1671 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001672
1673 // We update the requested and active position simultaneously because
1674 // we want to apply the position portion of the transform matrix immediately,
1675 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001676 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001677 if (immediate && !mFreezePositionUpdates) {
1678 mCurrentState.active.transform.set(x, y);
1679 }
1680 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001681
Dan Stoza7dde5992015-05-22 09:51:44 -07001682 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001683 setTransactionFlags(eTransactionNeeded);
1684 return true;
1685}
Robert Carr82364e32016-05-15 11:27:47 -07001686
Robert Carr1f0a16a2016-10-24 16:27:39 -07001687bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1688 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1689 if (idx < 0) {
1690 return false;
1691 }
1692 if (childLayer->setLayer(z)) {
1693 mCurrentChildren.removeAt(idx);
1694 mCurrentChildren.add(childLayer);
1695 }
1696 return true;
1697}
1698
Robert Carrae060832016-11-28 10:51:00 -08001699bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001700 if (mCurrentState.z == z)
1701 return false;
1702 mCurrentState.sequence++;
1703 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001704 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001705 setTransactionFlags(eTransactionNeeded);
1706 return true;
1707}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708
Mathias Agopian13127d82013-03-05 17:47:11 -08001709bool Layer::setSize(uint32_t w, uint32_t h) {
1710 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1711 return false;
1712 mCurrentState.requested.w = w;
1713 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001714 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001715 setTransactionFlags(eTransactionNeeded);
1716 return true;
1717}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001718#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001719bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001720#else
1721bool Layer::setAlpha(uint8_t alpha) {
1722#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001723 if (mCurrentState.alpha == alpha)
1724 return false;
1725 mCurrentState.sequence++;
1726 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001727 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001728 setTransactionFlags(eTransactionNeeded);
1729 return true;
1730}
1731bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1732 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001733 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001734 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001735 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001736 setTransactionFlags(eTransactionNeeded);
1737 return true;
1738}
1739bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001740 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001741 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001742 setTransactionFlags(eTransactionNeeded);
1743 return true;
1744}
1745bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1746 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1747 if (mCurrentState.flags == newFlags)
1748 return false;
1749 mCurrentState.sequence++;
1750 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001751 mCurrentState.mask = mask;
1752 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001753 setTransactionFlags(eTransactionNeeded);
1754 return true;
1755}
Robert Carr99e27f02016-06-16 15:18:02 -07001756
1757bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001758 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001759 return false;
1760 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001761 mCurrentState.requestedCrop = crop;
1762 if (immediate) {
1763 mCurrentState.crop = crop;
1764 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001765 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001766 setTransactionFlags(eTransactionNeeded);
1767 return true;
1768}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001769bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001770 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001771 return false;
1772 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001773 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001774 mCurrentState.modified = true;
1775 setTransactionFlags(eTransactionNeeded);
1776 return true;
1777}
Mathias Agopian13127d82013-03-05 17:47:11 -08001778
Robert Carrc3574f72016-03-24 12:19:32 -07001779bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1780 if (scalingMode == mOverrideScalingMode)
1781 return false;
1782 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001783 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001784 return true;
1785}
1786
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001787void Layer::setInfo(uint32_t type, uint32_t appId) {
1788 mCurrentState.appId = appId;
1789 mCurrentState.type = type;
1790 mCurrentState.modified = true;
1791 setTransactionFlags(eTransactionNeeded);
1792}
1793
Robert Carrc3574f72016-03-24 12:19:32 -07001794uint32_t Layer::getEffectiveScalingMode() const {
1795 if (mOverrideScalingMode >= 0) {
1796 return mOverrideScalingMode;
1797 }
1798 return mCurrentScalingMode;
1799}
1800
Mathias Agopian13127d82013-03-05 17:47:11 -08001801bool Layer::setLayerStack(uint32_t layerStack) {
1802 if (mCurrentState.layerStack == layerStack)
1803 return false;
1804 mCurrentState.sequence++;
1805 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001806 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001807 setTransactionFlags(eTransactionNeeded);
1808 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001809}
1810
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001811bool Layer::setDataSpace(android_dataspace dataSpace) {
1812 if (mCurrentState.dataSpace == dataSpace)
1813 return false;
1814 mCurrentState.sequence++;
1815 mCurrentState.dataSpace = dataSpace;
1816 mCurrentState.modified = true;
1817 setTransactionFlags(eTransactionNeeded);
1818 return true;
1819}
1820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821uint32_t Layer::getLayerStack() const {
1822 auto p = getParent();
1823 if (p == nullptr) {
1824 return getDrawingState().layerStack;
1825 }
1826 return p->getLayerStack();
1827}
1828
Dan Stoza7dde5992015-05-22 09:51:44 -07001829void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1830 uint64_t frameNumber) {
1831 mCurrentState.handle = handle;
1832 mCurrentState.frameNumber = frameNumber;
1833 // We don't set eTransactionNeeded, because just receiving a deferral
1834 // request without any other state updates shouldn't actually induce a delay
1835 mCurrentState.modified = true;
1836 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001837 mCurrentState.handle = nullptr;
1838 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001839 mCurrentState.modified = false;
1840}
1841
Dan Stozaee44edd2015-03-23 15:50:23 -07001842void Layer::useSurfaceDamage() {
1843 if (mFlinger->mForceFullDamage) {
1844 surfaceDamageRegion = Region::INVALID_REGION;
1845 } else {
1846 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1847 }
1848}
1849
1850void Layer::useEmptyDamage() {
1851 surfaceDamageRegion.clear();
1852}
1853
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854// ----------------------------------------------------------------------------
1855// pageflip handling...
1856// ----------------------------------------------------------------------------
1857
Dan Stoza6b9454d2014-11-07 16:00:59 -08001858bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001859 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001860 return true;
1861 }
1862
Dan Stoza6b9454d2014-11-07 16:00:59 -08001863 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001864 if (mQueueItems.empty()) {
1865 return false;
1866 }
1867 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001868 nsecs_t expectedPresent =
1869 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001870
1871 // Ignore timestamps more than a second in the future
1872 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1873 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1874 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1875 expectedPresent);
1876
1877 bool isDue = timestamp < expectedPresent;
1878 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001879}
1880
Brian Andersond6927fb2016-07-23 23:37:30 -07001881bool Layer::onPreComposition(nsecs_t refreshStartTime) {
1882 if (mBufferLatched) {
1883 Mutex::Autolock lock(mFrameEventHistoryMutex);
1884 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
1885 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001886 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001887 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001888}
1889
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001890bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001891 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001892 const std::shared_ptr<FenceTime>& retireFence,
1893 const CompositorTiming& compositorTiming) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001894 mAcquireTimeline.updateSignalTimes();
1895 mReleaseTimeline.updateSignalTimes();
1896
Brian Andersond6927fb2016-07-23 23:37:30 -07001897 // mFrameLatencyNeeded is true when a new frame was latched for the
1898 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08001899 if (!mFrameLatencyNeeded)
1900 return false;
1901
Fabien Sanglard28e98082016-12-05 10:19:46 -08001902 // Update mFrameEventHistory.
1903 {
1904 Mutex::Autolock lock(mFrameEventHistoryMutex);
1905 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001906 glDoneFence, presentFence, compositorTiming);
Fabien Sanglard28e98082016-12-05 10:19:46 -08001907 mFrameEventHistory.addRetire(mPreviousFrameNumber,
1908 retireFence);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001909 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08001910
1911 // Update mFrameTracker.
1912 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1913 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1914
Brian Anderson3d4039d2016-09-23 16:31:30 -07001915 std::shared_ptr<FenceTime> frameReadyFence =
1916 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08001917 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001918 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001919 } else {
1920 // There was no fence for this frame, so assume that it was ready
1921 // to be presented at the desired present time.
1922 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1923 }
1924
Brian Anderson3d4039d2016-09-23 16:31:30 -07001925 if (presentFence->isValid()) {
1926 mFrameTracker.setActualPresentFence(
1927 std::shared_ptr<FenceTime>(presentFence));
1928 } else if (retireFence->isValid()) {
1929 mFrameTracker.setActualPresentFence(
1930 std::shared_ptr<FenceTime>(retireFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001931 } else {
1932 // The HWC doesn't support present fences, so use the refresh
1933 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001934 mFrameTracker.setActualPresentTime(
1935 mFlinger->getHwComposer().getRefreshTimestamp(
1936 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001937 }
1938
1939 mFrameTracker.advanceFrame();
1940 mFrameLatencyNeeded = false;
1941 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001942}
1943
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001944#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07001945void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001946 mSurfaceFlingerConsumer->releasePendingBuffer();
Brian Anderson3d4039d2016-09-23 16:31:30 -07001947 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07001948 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07001949 mReleaseTimeline.push(releaseFenceTime);
1950
1951 Mutex::Autolock lock(mFrameEventHistoryMutex);
1952 mFrameEventHistory.addRelease(
Brian Andersonf6386862016-10-31 16:34:13 -07001953 mPreviousFrameNumber, dequeueReadyTime, std::move(releaseFenceTime));
Dan Stoza9e56aa02015-11-02 13:00:03 -08001954}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001955#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001956
Robert Carr1f0a16a2016-10-24 16:27:39 -07001957bool Layer::isHiddenByPolicy() const {
1958 const Layer::State& s(mDrawingState);
1959 const auto& parent = getParent();
1960 if (parent != nullptr && parent->isHiddenByPolicy()) {
1961 return true;
1962 }
1963 return s.flags & layer_state_t::eLayerHidden;
1964}
1965
Mathias Agopianda27af92012-09-13 18:17:13 -07001966bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001967 const Layer::State& s(mDrawingState);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001968#ifdef USE_HWC2
Robert Carr1f0a16a2016-10-24 16:27:39 -07001969 return !(isHiddenByPolicy()) && s.alpha > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08001970 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001971#else
Robert Carr1f0a16a2016-10-24 16:27:39 -07001972 return !(isHiddenByPolicy()) && s.alpha
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001973 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1974#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001975}
1976
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07001977bool Layer::allTransactionsSignaled() {
1978 auto headFrameNumber = getHeadFrameNumber();
1979 bool matchingFramesFound = false;
1980 bool allTransactionsApplied = true;
1981 Mutex::Autolock lock(mLocalSyncPointMutex);
1982
1983 for (auto& point : mLocalSyncPoints) {
1984 if (point->getFrameNumber() > headFrameNumber) {
1985 break;
1986 }
1987 matchingFramesFound = true;
1988
1989 if (!point->frameIsAvailable()) {
1990 // We haven't notified the remote layer that the frame for
1991 // this point is available yet. Notify it now, and then
1992 // abort this attempt to latch.
1993 point->setFrameAvailable();
1994 allTransactionsApplied = false;
1995 break;
1996 }
1997
1998 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
1999 }
2000 return !matchingFramesFound || allTransactionsApplied;
2001}
2002
Brian Andersond6927fb2016-07-23 23:37:30 -07002003Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002004{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002005 ATRACE_CALL();
2006
Jesse Hall399184a2014-03-03 15:42:54 -08002007 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2008 // mSidebandStreamChanged was true
2009 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002010 if (mSidebandStream != NULL) {
2011 setTransactionFlags(eTransactionNeeded);
2012 mFlinger->setTransactionFlags(eTraversalNeeded);
2013 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002014 recomputeVisibleRegions = true;
2015
2016 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002017 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002018 }
2019
Mathias Agopian4fec8732012-06-29 14:12:52 -07002020 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002021 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2022 return outDirtyRegion;
2023 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002024
Fabien Sanglard223eb912016-10-13 10:15:06 -07002025 // if we've already called updateTexImage() without going through
2026 // a composition step, we have to skip this layer at this point
2027 // because we cannot call updateTeximage() without a corresponding
2028 // compositionComplete() call.
2029 // we'll trigger an update in onPreComposition().
2030 if (mRefreshPending) {
2031 return outDirtyRegion;
2032 }
2033
2034 // If the head buffer's acquire fence hasn't signaled yet, return and
2035 // try again later
2036 if (!headFenceHasSignaled()) {
2037 mFlinger->signalLayerUpdate();
2038 return outDirtyRegion;
2039 }
2040
2041 // Capture the old state of the layer for comparisons later
2042 const State& s(getDrawingState());
2043 const bool oldOpacity = isOpaque(s);
2044 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2045
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002046 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002047 mFlinger->signalLayerUpdate();
2048 return outDirtyRegion;
2049 }
2050
2051 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2052 // of the buffer queue isn't modified when the buffer queue is returning
2053 // BufferItem's that weren't actually queued. This can happen in shared
2054 // buffer mode.
2055 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002056 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2057 getProducerStickyTransform() != 0, mName.string(),
2058 mOverrideScalingMode, mFreezePositionUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002059 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2060 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2061 mLastFrameNumberReceived);
2062 if (updateResult == BufferQueue::PRESENT_LATER) {
2063 // Producer doesn't want buffer to be displayed yet. Signal a
2064 // layer update so we check again at the next opportunity.
2065 mFlinger->signalLayerUpdate();
2066 return outDirtyRegion;
2067 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2068 // If the buffer has been rejected, remove it from the shadow queue
2069 // and return early
2070 if (queuedBuffer) {
2071 Mutex::Autolock lock(mQueueItemLock);
2072 mQueueItems.removeAt(0);
2073 android_atomic_dec(&mQueuedFrames);
2074 }
2075 return outDirtyRegion;
2076 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2077 // This can occur if something goes wrong when trying to create the
2078 // EGLImage for this buffer. If this happens, the buffer has already
2079 // been released, so we need to clean up the queue and bug out
2080 // early.
2081 if (queuedBuffer) {
2082 Mutex::Autolock lock(mQueueItemLock);
2083 mQueueItems.clear();
2084 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002085 }
2086
Fabien Sanglard223eb912016-10-13 10:15:06 -07002087 // Once we have hit this state, the shadow queue may no longer
2088 // correctly reflect the incoming BufferQueue's contents, so even if
2089 // updateTexImage starts working, the only safe course of action is
2090 // to continue to ignore updates.
2091 mUpdateTexImageFailed = true;
2092
2093 return outDirtyRegion;
2094 }
2095
2096 if (queuedBuffer) {
2097 // Autolock scope
2098 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2099
2100 Mutex::Autolock lock(mQueueItemLock);
2101
2102 // Remove any stale buffers that have been dropped during
2103 // updateTexImage
2104 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2105 mQueueItems.removeAt(0);
2106 android_atomic_dec(&mQueuedFrames);
2107 }
2108
2109 mQueueItems.removeAt(0);
2110 }
2111
2112
2113 // Decrement the queued-frames count. Signal another event if we
2114 // have more frames pending.
2115 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2116 || mAutoRefresh) {
2117 mFlinger->signalLayerUpdate();
2118 }
2119
Fabien Sanglard223eb912016-10-13 10:15:06 -07002120 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002121 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2122 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002123 if (mActiveBuffer == NULL) {
2124 // this can only happen if the very first buffer was rejected.
2125 return outDirtyRegion;
2126 }
2127
Brian Andersond6927fb2016-07-23 23:37:30 -07002128 mBufferLatched = true;
2129 mPreviousFrameNumber = mCurrentFrameNumber;
2130 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2131
2132 {
2133 Mutex::Autolock lock(mFrameEventHistoryMutex);
2134 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2135#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002136 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002137 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07002138 mReleaseTimeline.push(releaseFenceTime);
2139 mFrameEventHistory.addRelease(
Brian Andersonf6386862016-10-31 16:34:13 -07002140 mPreviousFrameNumber, latchTime, std::move(releaseFenceTime));
Brian Andersond6927fb2016-07-23 23:37:30 -07002141#endif
2142 }
2143
Fabien Sanglard223eb912016-10-13 10:15:06 -07002144 mRefreshPending = true;
2145 mFrameLatencyNeeded = true;
2146 if (oldActiveBuffer == NULL) {
2147 // the first time we receive a buffer, we need to trigger a
2148 // geometry invalidation.
2149 recomputeVisibleRegions = true;
2150 }
2151
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002152 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2153
Fabien Sanglard223eb912016-10-13 10:15:06 -07002154 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2155 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2156 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2157 if ((crop != mCurrentCrop) ||
2158 (transform != mCurrentTransform) ||
2159 (scalingMode != mCurrentScalingMode))
2160 {
2161 mCurrentCrop = crop;
2162 mCurrentTransform = transform;
2163 mCurrentScalingMode = scalingMode;
2164 recomputeVisibleRegions = true;
2165 }
2166
2167 if (oldActiveBuffer != NULL) {
2168 uint32_t bufWidth = mActiveBuffer->getWidth();
2169 uint32_t bufHeight = mActiveBuffer->getHeight();
2170 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2171 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002172 recomputeVisibleRegions = true;
2173 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002174 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002175
Fabien Sanglard223eb912016-10-13 10:15:06 -07002176 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2177 if (oldOpacity != isOpaque(s)) {
2178 recomputeVisibleRegions = true;
2179 }
Dan Stozacac35382016-01-27 12:21:06 -08002180
Fabien Sanglard223eb912016-10-13 10:15:06 -07002181 // Remove any sync points corresponding to the buffer which was just
2182 // latched
2183 {
2184 Mutex::Autolock lock(mLocalSyncPointMutex);
2185 auto point = mLocalSyncPoints.begin();
2186 while (point != mLocalSyncPoints.end()) {
2187 if (!(*point)->frameIsAvailable() ||
2188 !(*point)->transactionIsApplied()) {
2189 // This sync point must have been added since we started
2190 // latching. Don't drop it yet.
2191 ++point;
2192 continue;
2193 }
2194
2195 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2196 point = mLocalSyncPoints.erase(point);
2197 } else {
2198 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002199 }
2200 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002201 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002202
2203 // FIXME: postedRegion should be dirty & bounds
2204 Region dirtyRegion(Rect(s.active.w, s.active.h));
2205
2206 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002207 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002208
Mathias Agopian4fec8732012-06-29 14:12:52 -07002209 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002210}
2211
Mathias Agopiana67932f2011-04-20 14:20:59 -07002212uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002213{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002214 // TODO: should we do something special if mSecure is set?
2215 if (mProtectedByApp) {
2216 // need a hardware-protected path to external video sink
2217 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002218 }
Riley Andrews03414a12014-07-01 14:22:59 -07002219 if (mPotentialCursor) {
2220 usage |= GraphicBuffer::USAGE_CURSOR;
2221 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002222 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002223 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002224}
2225
Mathias Agopian84300952012-11-21 16:02:13 -08002226void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002227 uint32_t orientation = 0;
2228 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002229 // The transform hint is used to improve performance, but we can
2230 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002231 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002232 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002233 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002234 if (orientation & Transform::ROT_INVALID) {
2235 orientation = 0;
2236 }
2237 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002238 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002239}
2240
Mathias Agopian13127d82013-03-05 17:47:11 -08002241// ----------------------------------------------------------------------------
2242// debugging
2243// ----------------------------------------------------------------------------
2244
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002245void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002246{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002247 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002248
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002249 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002250 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002251 "+ %s %p (%s)\n",
2252 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002253 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002254
Mathias Agopian2ca79392013-04-02 18:30:32 -07002255 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002256 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002257 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002258 sp<Client> client(mClientRef.promote());
2259
Mathias Agopian74d211a2013-04-22 16:55:35 +02002260 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002261 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2262 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002263 "isOpaque=%1d, invalidate=%1d, "
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002264#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08002265 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002266#else
2267 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2268#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002269 " client=%p\n",
Robert Carr1f0a16a2016-10-24 16:27:39 -07002270 getLayerStack(), s.z,
2271 s.active.transform.tx(), s.active.transform.ty(),
2272 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07002273 s.crop.left, s.crop.top,
2274 s.crop.right, s.crop.bottom,
2275 s.finalCrop.left, s.finalCrop.top,
2276 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002277 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002278 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002279 s.active.transform[0][0], s.active.transform[0][1],
2280 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002281 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002282
2283 sp<const GraphicBuffer> buf0(mActiveBuffer);
2284 uint32_t w0=0, h0=0, s0=0, f0=0;
2285 if (buf0 != 0) {
2286 w0 = buf0->getWidth();
2287 h0 = buf0->getHeight();
2288 s0 = buf0->getStride();
2289 f0 = buf0->format;
2290 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002291 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002292 " "
2293 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2294 " queued-frames=%d, mRefreshPending=%d\n",
2295 mFormat, w0, h0, s0,f0,
2296 mQueuedFrames, mRefreshPending);
2297
Mathias Agopian13127d82013-03-05 17:47:11 -08002298 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002299 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002300 }
2301}
2302
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002303#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002304void Layer::miniDumpHeader(String8& result) {
2305 result.append("----------------------------------------");
2306 result.append("---------------------------------------\n");
2307 result.append(" Layer name\n");
2308 result.append(" Z | ");
2309 result.append(" Comp Type | ");
2310 result.append(" Disp Frame (LTRB) | ");
2311 result.append(" Source Crop (LTRB)\n");
2312 result.append("----------------------------------------");
2313 result.append("---------------------------------------\n");
2314}
2315
2316void Layer::miniDump(String8& result, int32_t hwcId) const {
2317 if (mHwcLayers.count(hwcId) == 0) {
2318 return;
2319 }
2320
2321 String8 name;
2322 if (mName.length() > 77) {
2323 std::string shortened;
2324 shortened.append(mName.string(), 36);
2325 shortened.append("[...]");
2326 shortened.append(mName.string() + (mName.length() - 36), 36);
2327 name = shortened.c_str();
2328 } else {
2329 name = mName;
2330 }
2331
2332 result.appendFormat(" %s\n", name.string());
2333
2334 const Layer::State& layerState(getDrawingState());
2335 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2336 result.appendFormat(" %10u | ", layerState.z);
2337 result.appendFormat("%10s | ",
2338 to_string(getCompositionType(hwcId)).c_str());
2339 const Rect& frame = hwcInfo.displayFrame;
2340 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2341 frame.right, frame.bottom);
Dan Stoza71bded52016-10-19 11:10:33 -07002342 const gfx::FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002343 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2344 crop.right, crop.bottom);
2345
2346 result.append("- - - - - - - - - - - - - - - - - - - - ");
2347 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2348}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002349#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002350
Svetoslavd85084b2014-03-20 10:28:31 -07002351void Layer::dumpFrameStats(String8& result) const {
2352 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002353}
2354
Svetoslavd85084b2014-03-20 10:28:31 -07002355void Layer::clearFrameStats() {
2356 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002357}
2358
Jamie Gennis6547ff42013-07-16 20:12:42 -07002359void Layer::logFrameStats() {
2360 mFrameTracker.logAndResetStats(mName);
2361}
2362
Svetoslavd85084b2014-03-20 10:28:31 -07002363void Layer::getFrameStats(FrameStats* outStats) const {
2364 mFrameTracker.getStats(outStats);
2365}
2366
Brian Andersond6927fb2016-07-23 23:37:30 -07002367void Layer::dumpFrameEvents(String8& result) {
2368 result.appendFormat("- Layer %s (%s, %p)\n",
2369 getName().string(), getTypeId(), this);
2370 Mutex::Autolock lock(mFrameEventHistoryMutex);
2371 mFrameEventHistory.checkFencesForCompletion();
2372 mFrameEventHistory.dump(result);
2373}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002374
Brian Anderson3890c392016-07-25 12:48:08 -07002375void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2376 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002377 Mutex::Autolock lock(mFrameEventHistoryMutex);
2378 if (newTimestamps) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002379 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002380 mFrameEventHistory.addQueue(*newTimestamps);
2381 }
2382
Brian Anderson3890c392016-07-25 12:48:08 -07002383 if (outDelta) {
2384 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002385 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002386}
Dan Stozae77c7662016-05-13 11:37:28 -07002387
2388std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2389 bool forceFlush) {
2390 std::vector<OccupancyTracker::Segment> history;
2391 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2392 &history);
2393 if (result != NO_ERROR) {
2394 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2395 result);
2396 return {};
2397 }
2398 return history;
2399}
2400
Robert Carr367c5682016-06-20 11:55:28 -07002401bool Layer::getTransformToDisplayInverse() const {
2402 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2403}
2404
Robert Carr1f0a16a2016-10-24 16:27:39 -07002405void Layer::addChild(const sp<Layer>& layer) {
2406 mCurrentChildren.add(layer);
2407 layer->setParent(this);
2408}
2409
2410ssize_t Layer::removeChild(const sp<Layer>& layer) {
2411 layer->setParent(nullptr);
2412 return mCurrentChildren.remove(layer);
2413}
2414
Robert Carr1db73f62016-12-21 12:58:51 -08002415bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2416 sp<Handle> handle = nullptr;
2417 sp<Layer> newParent = nullptr;
2418 if (newParentHandle == nullptr) {
2419 return false;
2420 }
2421 handle = static_cast<Handle*>(newParentHandle.get());
2422 newParent = handle->owner.promote();
2423 if (newParent == nullptr) {
2424 ALOGE("Unable to promote Layer handle");
2425 return false;
2426 }
2427
2428 for (const sp<Layer>& child : mCurrentChildren) {
2429 newParent->addChild(child);
2430
2431 sp<Client> client(child->mClientRef.promote());
2432 if (client != nullptr) {
2433 client->setParentLayer(newParent);
2434 }
2435 }
2436 mCurrentChildren.clear();
2437
2438 return true;
2439}
2440
Robert Carr1f0a16a2016-10-24 16:27:39 -07002441void Layer::setParent(const sp<Layer>& layer) {
2442 mParent = layer;
2443}
2444
2445void Layer::clearSyncPoints() {
2446 for (const auto& child : mCurrentChildren) {
2447 child->clearSyncPoints();
2448 }
2449
2450 Mutex::Autolock lock(mLocalSyncPointMutex);
2451 for (auto& point : mLocalSyncPoints) {
2452 point->setFrameAvailable();
2453 }
2454 mLocalSyncPoints.clear();
2455}
2456
2457int32_t Layer::getZ() const {
2458 return mDrawingState.z;
2459}
2460
2461/**
2462 * Negatively signed children are before 'this' in Z-order.
2463 */
2464void Layer::traverseInZOrder(const std::function<void(Layer*)>& exec) {
2465 size_t i = 0;
2466 for (; i < mDrawingChildren.size(); i++) {
2467 const auto& child = mDrawingChildren[i];
2468 if (child->getZ() >= 0)
2469 break;
2470 child->traverseInZOrder(exec);
2471 }
2472 exec(this);
2473 for (; i < mDrawingChildren.size(); i++) {
2474 const auto& child = mDrawingChildren[i];
2475 child->traverseInZOrder(exec);
2476 }
2477}
2478
2479/**
2480 * Positively signed children are before 'this' in reverse Z-order.
2481 */
2482void Layer::traverseInReverseZOrder(const std::function<void(Layer*)>& exec) {
2483 int32_t i = 0;
2484 for (i = mDrawingChildren.size()-1; i>=0; i--) {
2485 const auto& child = mDrawingChildren[i];
2486 if (child->getZ() < 0) {
2487 break;
2488 }
2489 child->traverseInReverseZOrder(exec);
2490 }
2491 exec(this);
2492 for (; i>=0; i--) {
2493 const auto& child = mDrawingChildren[i];
2494 child->traverseInReverseZOrder(exec);
2495 }
2496}
2497
2498Transform Layer::getTransform() const {
2499 Transform t;
2500 const auto& p = getParent();
2501 if (p != nullptr) {
2502 t = p->getTransform();
2503 }
2504 return t * getDrawingState().active.transform;
2505}
2506
2507void Layer::commitChildList() {
2508 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2509 const auto& child = mCurrentChildren[i];
2510 child->commitChildList();
2511 }
2512 mDrawingChildren = mCurrentChildren;
2513}
2514
Mathias Agopian13127d82013-03-05 17:47:11 -08002515// ---------------------------------------------------------------------------
2516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002518
2519#if defined(__gl_h_)
2520#error "don't include gl/gl.h in this file"
2521#endif
2522
2523#if defined(__gl2_h_)
2524#error "don't include gl2/gl2.h in this file"
2525#endif