blob: c3f86652ca1c62af3da288d0ba2a611185f7224b [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 Agopiana9347642017-02-13 16:42:28 -080041#include <gui/BufferQueue.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080042#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020045#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070046#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070048#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070049#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian1b031492012-06-20 17:51:20 -070052#include "DisplayHardware/HWComposer.h"
53
Mathias Agopian875d8e12013-06-07 15:35:48 -070054#include "RenderEngine/RenderEngine.h"
55
Dan Stozac5da2712016-07-20 15:38:12 -070056#include <mutex>
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#define DEBUG_RESIZE 0
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060namespace android {
61
62// ---------------------------------------------------------------------------
63
Mathias Agopian13127d82013-03-05 17:47:11 -080064int32_t Layer::sSequence = 1;
65
Mathias Agopian4d9b8222013-03-12 17:11:48 -070066Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
67 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080068 : contentDirty(false),
69 sequence(uint32_t(android_atomic_inc(&sSequence))),
70 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070071 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
73 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080079 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080080 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070082 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070083 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070084 mCurrentOpacity(true),
Brian Andersond6927fb2016-07-23 23:37:30 -070085 mBufferLatched(false),
Dan Stozacac35382016-01-27 12:21:06 -080086 mCurrentFrameNumber(0),
Brian Anderson8cc8b102016-10-21 12:43:09 -070087 mPreviousFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080088 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080089 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080090 mFiltering(false),
91 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070092 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000093#ifndef USE_HWC2
94 mIsGlesComposition(false),
95#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080096 mProtectedByApp(false),
97 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070098 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070099 mPotentialCursor(false),
100 mQueueItemLock(),
101 mQueueItemCondition(),
102 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -0700103 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -0800104 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700105 mAutoRefresh(false),
106 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000108#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800109 ALOGV("Creating Layer %s", name.string());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000110#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111
Mathias Agopiana67932f2011-04-20 14:20:59 -0700112 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700113 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700114 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 uint32_t layerFlags = 0;
117 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800118 layerFlags |= layer_state_t::eLayerHidden;
119 if (flags & ISurfaceComposerClient::eOpaque)
120 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700121 if (flags & ISurfaceComposerClient::eSecure)
122 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123
124 if (flags & ISurfaceComposerClient::eNonPremultiplied)
125 mPremultipliedAlpha = false;
126
127 mName = name;
128
129 mCurrentState.active.w = w;
130 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800131 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700132 mCurrentState.crop.makeInvalid();
133 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700134 mCurrentState.z = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000135#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800136 mCurrentState.alpha = 1.0f;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000137#else
138 mCurrentState.alpha = 0xFF;
139#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700140 mCurrentState.layerStack = 0;
141 mCurrentState.flags = layerFlags;
142 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700143 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700144 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500145 mCurrentState.appId = 0;
146 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700147
148 // drawing state & current state are identical
149 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700150
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000151#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800152 const auto& hwc = flinger->getHwComposer();
153 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
154 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000155#else
156 nsecs_t displayPeriod =
157 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
158#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700159 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800160
161 CompositorTiming compositorTiming;
162 flinger->getCompositorTiming(&compositorTiming);
163 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800164}
165
Mathias Agopian3f844832013-08-07 21:24:32 -0700166void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800167 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700168 sp<IGraphicBufferProducer> producer;
169 sp<IGraphicBufferConsumer> consumer;
Irvel468051e2016-06-13 16:44:44 -0700170 BufferQueue::createBufferQueue(&producer, &consumer, nullptr, true);
Robert Carr1db73f62016-12-21 12:58:51 -0800171 mProducer = new MonitoredProducer(producer, mFlinger, this);
Irvel468051e2016-06-13 16:44:44 -0700172 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800173 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800174 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700175 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800176
Fabien Sanglard63a5fcd2016-12-29 15:13:07 -0800177 if (mFlinger->isLayerTripleBufferingDisabled()) {
178 mProducer->setMaxDequeuedBufferCount(2);
179 }
Andy McFadden69052052012-09-14 16:10:11 -0700180
Mathias Agopian84300952012-11-21 16:02:13 -0800181 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
182 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700183}
184
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700185Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800186 sp<Client> c(mClientRef.promote());
187 if (c != 0) {
188 c->detachLayer(this);
189 }
190
Dan Stozacac35382016-01-27 12:21:06 -0800191 for (auto& point : mRemoteSyncPoints) {
192 point->setTransactionApplied();
193 }
Dan Stozac8145172016-04-28 16:29:06 -0700194 for (auto& point : mLocalSyncPoints) {
195 point->setFrameAvailable();
196 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700197 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700198 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700199}
200
Mathias Agopian13127d82013-03-05 17:47:11 -0800201// ---------------------------------------------------------------------------
202// callbacks
203// ---------------------------------------------------------------------------
204
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000205#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
207 if (mHwcLayers.empty()) {
208 return;
209 }
210 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
211}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000212#else
213void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
214 HWComposer::HWCLayerInterface* layer) {
215 if (layer) {
216 layer->onDisplayed();
217 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
218 }
219}
220#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800221
Dan Stoza6b9454d2014-11-07 16:00:59 -0800222void Layer::onFrameAvailable(const BufferItem& item) {
223 // Add this buffer from our internal queue tracker
224 { // Autolock scope
225 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700226 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
227 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700228 // Reset the frame number tracker when we receive the first buffer after
229 // a frame number reset
230 if (item.mFrameNumber == 1) {
231 mLastFrameNumberReceived = 0;
232 }
233
234 // Ensure that callbacks are handled in order
235 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
236 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
237 ms2ns(500));
238 if (result != NO_ERROR) {
239 ALOGE("[%s] Timed out waiting on callback", mName.string());
240 }
241 }
242
Dan Stoza6b9454d2014-11-07 16:00:59 -0800243 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700244 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700245
246 // Wake up any pending callbacks
247 mLastFrameNumberReceived = item.mFrameNumber;
248 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800249 }
250
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800251 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700252}
253
Dan Stoza6b9454d2014-11-07 16:00:59 -0800254void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700255 { // Autolock scope
256 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700257
Dan Stoza7dde5992015-05-22 09:51:44 -0700258 // Ensure that callbacks are handled in order
259 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
260 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
261 ms2ns(500));
262 if (result != NO_ERROR) {
263 ALOGE("[%s] Timed out waiting on callback", mName.string());
264 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700265 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700266
267 if (mQueueItems.empty()) {
268 ALOGE("Can't replace a frame on an empty queue");
269 return;
270 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700271 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700272
273 // Wake up any pending callbacks
274 mLastFrameNumberReceived = item.mFrameNumber;
275 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700276 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800277}
278
Chia-I Wu06d63de2017-01-04 14:58:51 +0800279void Layer::onBuffersReleased() {
280#ifdef USE_HWC2
281 Mutex::Autolock lock(mHwcBufferCacheMutex);
282
283 for (auto info : mHwcBufferCaches) {
284 info.second.clear();
285 }
286#endif
287}
288
Jesse Hall399184a2014-03-03 15:42:54 -0800289void Layer::onSidebandStreamChanged() {
290 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
291 // mSidebandStreamChanged was false
292 mFlinger->signalLayerUpdate();
293 }
294}
295
Mathias Agopian67106042013-03-14 19:18:13 -0700296// called with SurfaceFlinger::mStateLock from the drawing thread after
297// the layer has been remove from the current state list (and just before
298// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800299void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800300 mSurfaceFlingerConsumer->abandon();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700301 for (const auto& child : mCurrentChildren) {
302 child->onRemoved();
303 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700304}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700305
Mathias Agopian13127d82013-03-05 17:47:11 -0800306// ---------------------------------------------------------------------------
307// set-up
308// ---------------------------------------------------------------------------
309
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700310const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800311 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312}
313
Mathias Agopianf9d93272009-06-19 17:00:27 -0700314status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 PixelFormat format, uint32_t flags)
316{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700317 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700318 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700319
320 // never allow a surface larger than what our underlying GL implementation
321 // can handle.
322 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800323 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700324 return BAD_VALUE;
325 }
326
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700328
Riley Andrews03414a12014-07-01 14:22:59 -0700329 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700330 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700331 mCurrentOpacity = getOpacityForFormat(format);
332
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800333 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
334 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
335 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 return NO_ERROR;
338}
339
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700340sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 Mutex::Autolock _l(mLock);
342
343 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700344 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800345
346 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700347
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700348 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800349}
350
Dan Stozab9b08832014-03-13 11:55:57 -0700351sp<IGraphicBufferProducer> Layer::getProducer() const {
352 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700353}
354
Mathias Agopian13127d82013-03-05 17:47:11 -0800355// ---------------------------------------------------------------------------
356// h/w composer set-up
357// ---------------------------------------------------------------------------
358
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800359Rect Layer::getContentCrop() const {
360 // this is the crop rectangle that applies to the buffer
361 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 Rect crop;
363 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800364 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700365 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800366 } else if (mActiveBuffer != NULL) {
367 // otherwise we use the whole buffer
368 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700369 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800370 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700371 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700372 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700373 return crop;
374}
375
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700376static Rect reduce(const Rect& win, const Region& exclude) {
377 if (CC_LIKELY(exclude.isEmpty())) {
378 return win;
379 }
380 if (exclude.isRect()) {
381 return win.reduce(exclude.getBounds());
382 }
383 return Region(win).subtract(exclude).getBounds();
384}
385
Robert Carr1f0a16a2016-10-24 16:27:39 -0700386Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
387 const Layer::State& s(getDrawingState());
388 Rect win(s.active.w, s.active.h);
389
390 if (!s.crop.isEmpty()) {
391 win.intersect(s.crop, &win);
392 }
393
394 Transform t = getTransform();
395 win = t.transform(win);
396
397 const sp<Layer>& p = getParent();
398 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
399 // When calculating the parent bounds for purposes of clipping,
400 // we don't need to constrain the parent to its transparent region.
401 // The transparent region is an optimization based on the
402 // buffer contents of the layer, but does not affect the space allocated to
403 // it by policy, and thus children should be allowed to extend into the
404 // parent's transparent region. In fact one of the main uses, is to reduce
405 // buffer allocation size in cases where a child window sits behind a main window
406 // (by marking the hole in the parent window as a transparent region)
407 if (p != nullptr) {
408 Rect bounds = p->computeScreenBounds(false);
409 bounds.intersect(win, &win);
410 }
411
412 if (reduceTransparentRegion) {
413 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
414 win = reduce(win, screenTransparentRegion);
415 }
416
417 return win;
418}
419
Mathias Agopian13127d82013-03-05 17:47:11 -0800420Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700421 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700422 return computeBounds(s.activeTransparentRegion);
423}
424
425Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
426 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800427 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700428
429 if (!s.crop.isEmpty()) {
430 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800431 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700432
433 Rect bounds = win;
434 const auto& p = getParent();
435 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800436 // Look in computeScreenBounds recursive call for explanation of
437 // why we pass false here.
438 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700439 }
440
441 Transform t = getTransform();
442 if (p != nullptr) {
443 win = t.transform(win);
444 win.intersect(bounds, &win);
445 win = t.inverse().transform(win);
446 }
447
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700448 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700449 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800450}
451
Robert Carr1f0a16a2016-10-24 16:27:39 -0700452Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700453 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700455 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800456
457 // apply the projection's clipping to the window crop in
458 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700459 // if there are no window scaling involved, this operation will map to full
460 // pixels in the buffer.
461 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
462 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700463
464 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700465 if (!s.crop.isEmpty()) {
466 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700467 }
468
Robert Carr1f0a16a2016-10-24 16:27:39 -0700469 Transform t = getTransform();
470 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
472 activeCrop.clear();
473 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700474 if (!s.finalCrop.isEmpty()) {
475 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 activeCrop.clear();
477 }
478 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700479 return activeCrop;
480}
481
Dan Stoza5a423ea2017-02-16 14:10:39 -0800482FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700483 // the content crop is the area of the content that gets scaled to the
484 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800485 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700486
487 // In addition there is a WM-specified crop we pull from our drawing state.
488 const State& s(getDrawingState());
489
490 // Screen space to make reduction to parent crop clearer.
491 Rect activeCrop = computeInitialCrop(hw);
492 const auto& p = getParent();
493 if (p != nullptr) {
494 auto parentCrop = p->computeInitialCrop(hw);
495 activeCrop.intersect(parentCrop, &activeCrop);
496 }
497 Transform t = getTransform();
498 // Back to layer space to work with the content crop.
499 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800500
Michael Lentine28ea2172014-11-19 18:32:37 -0800501 // This needs to be here as transform.transform(Rect) computes the
502 // transformed rect and then takes the bounding box of the result before
503 // returning. This means
504 // transform.inverse().transform(transform.transform(Rect)) != Rect
505 // in which case we need to make sure the final rect is clipped to the
506 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000507 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
508 activeCrop.clear();
509 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800510
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700511 // subtract the transparent region and snap to the bounds
512 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
513
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000514 // Transform the window crop to match the buffer coordinate system,
515 // which means using the inverse of the current transform set on the
516 // SurfaceFlingerConsumer.
517 uint32_t invTransform = mCurrentTransform;
518 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
519 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700520 * the code below applies the primary display's inverse transform to the
521 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000522 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700523 uint32_t invTransformOrient =
524 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000525 // calculate the inverse transform
526 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
527 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
528 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800529 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000530 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700531 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
532 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800533 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000534
535 int winWidth = s.active.w;
536 int winHeight = s.active.h;
537 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
538 // If the activeCrop has been rotate the ends are rotated but not
539 // the space itself so when transforming ends back we can't rely on
540 // a modification of the axes of rotation. To account for this we
541 // need to reorient the inverse rotation in terms of the current
542 // axes of rotation.
543 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
544 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
545 if (is_h_flipped == is_v_flipped) {
546 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
547 NATIVE_WINDOW_TRANSFORM_FLIP_H;
548 }
549 winWidth = s.active.h;
550 winHeight = s.active.w;
551 }
552 const Rect winCrop = activeCrop.transform(
553 invTransform, s.active.w, s.active.h);
554
555 // below, crop is intersected with winCrop expressed in crop's coordinate space
556 float xScale = crop.getWidth() / float(winWidth);
557 float yScale = crop.getHeight() / float(winHeight);
558
559 float insetL = winCrop.left * xScale;
560 float insetT = winCrop.top * yScale;
561 float insetR = (winWidth - winCrop.right ) * xScale;
562 float insetB = (winHeight - winCrop.bottom) * yScale;
563
564 crop.left += insetL;
565 crop.top += insetT;
566 crop.right -= insetR;
567 crop.bottom -= insetB;
568
Mathias Agopian13127d82013-03-05 17:47:11 -0800569 return crop;
570}
571
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000572#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800573void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000574#else
575void Layer::setGeometry(
576 const sp<const DisplayDevice>& hw,
577 HWComposer::HWCLayerInterface& layer)
578#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700579{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000580#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581 const auto hwcId = displayDevice->getHwcDisplayId();
582 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000583#else
584 layer.setDefaultState();
585#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700586
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700587 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000588#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 hwcInfo.forceClientComposition = false;
590
591 if (isSecure() && !displayDevice->isSecure()) {
592 hwcInfo.forceClientComposition = true;
593 }
594
595 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000596#else
597 layer.setSkip(false);
598
599 if (isSecure() && !hw->isSecure()) {
600 layer.setSkip(true);
601 }
602#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700603
Mathias Agopian13127d82013-03-05 17:47:11 -0800604 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700605 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000606#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500607 auto blendMode = HWC2::BlendMode::None;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800608 if (!isOpaque(s) || s.alpha != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500609 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800610 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611 }
David Revemanecf0fa52017-03-03 11:32:44 -0500612 auto error = hwcLayer->setBlendMode(blendMode);
613 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
614 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
615 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000616#else
617 if (!isOpaque(s) || s.alpha != 0xFF) {
618 layer.setBlending(mPremultipliedAlpha ?
619 HWC_BLENDING_PREMULT :
620 HWC_BLENDING_COVERAGE);
621 }
622#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800623
624 // apply the layer's transform, followed by the display's global transform
625 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700626 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700627 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700628 if (!s.crop.isEmpty()) {
629 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700630 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000631#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000632 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000633#else
634 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
635#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000636 activeCrop.clear();
637 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700638 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800639 // This needs to be here as transform.transform(Rect) computes the
640 // transformed rect and then takes the bounding box of the result before
641 // returning. This means
642 // transform.inverse().transform(transform.transform(Rect)) != Rect
643 // in which case we need to make sure the final rect is clipped to the
644 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000645 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
646 activeCrop.clear();
647 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700648 // mark regions outside the crop as transparent
649 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
650 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
651 s.active.w, s.active.h));
652 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
653 activeCrop.left, activeCrop.bottom));
654 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
655 s.active.w, activeCrop.bottom));
656 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700657
658 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700659 if (!s.finalCrop.isEmpty()) {
660 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000661 frame.clear();
662 }
663 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000664#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000665 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
666 frame.clear();
667 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 const Transform& tr(displayDevice->getTransform());
669 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500670 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700671 if (error != HWC2::Error::None) {
672 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
673 mName.string(), transformedFrame.left, transformedFrame.top,
674 transformedFrame.right, transformedFrame.bottom,
675 to_string(error).c_str(), static_cast<int32_t>(error));
676 } else {
677 hwcInfo.displayFrame = transformedFrame;
678 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679
Dan Stoza5a423ea2017-02-16 14:10:39 -0800680 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700682 if (error != HWC2::Error::None) {
683 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
684 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
685 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
686 static_cast<int32_t>(error));
687 } else {
688 hwcInfo.sourceCrop = sourceCrop;
689 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800690
691 error = hwcLayer->setPlaneAlpha(s.alpha);
692 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
693 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
694 static_cast<int32_t>(error));
695
Robert Carrae060832016-11-28 10:51:00 -0800696 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800698 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500700
701 error = hwcLayer->setInfo(s.type, s.appId);
702 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
703 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000704#else
705 if (!frame.intersect(hw->getViewport(), &frame)) {
706 frame.clear();
707 }
708 const Transform& tr(hw->getTransform());
709 layer.setFrame(tr.transform(frame));
710 layer.setCrop(computeCrop(hw));
711 layer.setPlaneAlpha(s.alpha);
712#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800713
Mathias Agopian29a367b2011-07-12 14:51:45 -0700714 /*
715 * Transformations are applied in this order:
716 * 1) buffer orientation/flip/mirror
717 * 2) state transformation (window manager)
718 * 3) layer orientation (screen orientation)
719 * (NOTE: the matrices are multiplied in reverse order)
720 */
721
722 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700723 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700724
725 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
726 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700727 * the code below applies the primary display's inverse transform to the
728 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700729 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700730 uint32_t invTransform =
731 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700732 // calculate the inverse transform
733 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
734 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
735 NATIVE_WINDOW_TRANSFORM_FLIP_H;
736 }
737 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700738 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700739 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700740
741 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800742 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000743#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800744 if (orientation & Transform::ROT_INVALID) {
745 // we can only handle simple transformation
746 hwcInfo.forceClientComposition = true;
747 } else {
748 auto transform = static_cast<HWC2::Transform>(orientation);
749 auto error = hwcLayer->setTransform(transform);
750 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
751 "%s (%d)", mName.string(), to_string(transform).c_str(),
752 to_string(error).c_str(), static_cast<int32_t>(error));
753 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000754#else
755 if (orientation & Transform::ROT_INVALID) {
756 // we can only handle simple transformation
757 layer.setSkip(true);
758 } else {
759 layer.setTransform(orientation);
760 }
761#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700762}
763
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000764#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765void Layer::forceClientComposition(int32_t hwcId) {
766 if (mHwcLayers.count(hwcId) == 0) {
767 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
768 return;
769 }
770
771 mHwcLayers[hwcId].forceClientComposition = true;
772}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000773#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000775#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
777 // Apply this display's projection's viewport to the visible region
778 // before giving it to the HWC HAL.
779 const Transform& tr = displayDevice->getTransform();
780 const auto& viewport = displayDevice->getViewport();
781 Region visible = tr.transform(visibleRegion.intersect(viewport));
782 auto hwcId = displayDevice->getHwcDisplayId();
783 auto& hwcLayer = mHwcLayers[hwcId].layer;
784 auto error = hwcLayer->setVisibleRegion(visible);
785 if (error != HWC2::Error::None) {
786 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
787 to_string(error).c_str(), static_cast<int32_t>(error));
788 visible.dump(LOG_TAG);
789 }
790
791 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
792 if (error != HWC2::Error::None) {
793 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
794 to_string(error).c_str(), static_cast<int32_t>(error));
795 surfaceDamageRegion.dump(LOG_TAG);
796 }
797
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700798 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700800 setCompositionType(hwcId, HWC2::Composition::Sideband);
801 ALOGV("[%s] Requesting Sideband composition", mName.string());
802 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800803 if (error != HWC2::Error::None) {
804 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
805 mName.string(), mSidebandStream->handle(),
806 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700808 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800809 }
810
Dan Stoza0a21df72016-07-20 12:52:38 -0700811 // Client layers
812 if (mHwcLayers[hwcId].forceClientComposition ||
813 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700814 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800815 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700816 return;
817 }
818
Dan Stoza0a21df72016-07-20 12:52:38 -0700819 // SolidColor layers
820 if (mActiveBuffer == nullptr) {
821 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700822
823 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700824 error = hwcLayer->setColor({0, 0, 0, 255});
825 if (error != HWC2::Error::None) {
826 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
827 to_string(error).c_str(), static_cast<int32_t>(error));
828 }
Dan Stozac6c89542016-07-27 10:16:52 -0700829
830 // Clear out the transform, because it doesn't make sense absent a
831 // source buffer
832 error = hwcLayer->setTransform(HWC2::Transform::None);
833 if (error != HWC2::Error::None) {
834 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
835 to_string(error).c_str(), static_cast<int32_t>(error));
836 }
837
Dan Stoza0a21df72016-07-20 12:52:38 -0700838 return;
839 }
840
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700841 // Device or Cursor layers
842 if (mPotentialCursor) {
843 ALOGV("[%s] Requesting Cursor composition", mName.string());
844 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800845 } else {
846 ALOGV("[%s] Requesting Device composition", mName.string());
847 setCompositionType(hwcId, HWC2::Composition::Device);
848 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700849
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700850 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
851 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
852 if (error != HWC2::Error::None) {
853 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
854 mCurrentState.dataSpace, to_string(error).c_str(),
855 static_cast<int32_t>(error));
856 }
857
Chia-I Wu06d63de2017-01-04 14:58:51 +0800858 uint32_t hwcSlot = 0;
859 buffer_handle_t hwcHandle = nullptr;
860 {
861 Mutex::Autolock lock(mHwcBufferCacheMutex);
862
863 auto& hwcBufferCache = mHwcBufferCaches[hwcId];
864 sp<GraphicBuffer> hwcBuffer;
865 hwcBufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
866 &hwcSlot, &hwcBuffer);
867 if (hwcBuffer != nullptr) {
868 hwcHandle = hwcBuffer->handle;
869 }
870 }
871
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700872 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Chia-I Wu06d63de2017-01-04 14:58:51 +0800873 error = hwcLayer->setBuffer(hwcSlot, hwcHandle, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700874 if (error != HWC2::Error::None) {
875 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
876 mActiveBuffer->handle, to_string(error).c_str(),
877 static_cast<int32_t>(error));
878 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800879}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000880#else
881void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
882 HWComposer::HWCLayerInterface& layer) {
883 // we have to set the visible region on every frame because
884 // we currently free it during onLayerDisplayed(), which is called
885 // after HWComposer::commit() -- every frame.
886 // Apply this display's projection's viewport to the visible region
887 // before giving it to the HWC HAL.
888 const Transform& tr = hw->getTransform();
889 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
890 layer.setVisibleRegionScreen(visible);
891 layer.setSurfaceDamage(surfaceDamageRegion);
892 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700893
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000894 if (mSidebandStream.get()) {
895 layer.setSidebandStream(mSidebandStream);
896 } else {
897 // NOTE: buffer can be NULL if the client never drew into this
898 // layer yet, or if we ran out of memory
899 layer.setBuffer(mActiveBuffer);
900 }
901}
902#endif
903
904#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800905void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
906 auto hwcId = displayDevice->getHwcDisplayId();
907 if (mHwcLayers.count(hwcId) == 0 ||
908 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
909 return;
910 }
911
912 // This gives us only the "orientation" component of the transform
913 const State& s(getCurrentState());
914
915 // Apply the layer's transform, followed by the display's global transform
916 // Here we're guaranteed that the layer's transform preserves rects
917 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700918 if (!s.crop.isEmpty()) {
919 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800920 }
921 // Subtract the transparent region and snap to the bounds
922 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700923 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800924 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700925 if (!s.finalCrop.isEmpty()) {
926 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000927 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800928 auto& displayTransform(displayDevice->getTransform());
929 auto position = displayTransform.transform(frame);
930
931 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
932 position.top);
933 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
934 "to (%d, %d): %s (%d)", mName.string(), position.left,
935 position.top, to_string(error).c_str(),
936 static_cast<int32_t>(error));
937}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000938#else
939void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
940 HWComposer::HWCLayerInterface& layer) {
941 int fenceFd = -1;
942
943 // TODO: there is a possible optimization here: we only need to set the
944 // acquire fence the first time a new buffer is acquired on EACH display.
945
946 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
947 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
948 if (fence->isValid()) {
949 fenceFd = fence->dup();
950 if (fenceFd == -1) {
951 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
952 }
953 }
954 }
955 layer.setAcquireFenceFd(fenceFd);
956}
957
958Rect Layer::getPosition(
959 const sp<const DisplayDevice>& hw)
960{
961 // this gives us only the "orientation" component of the transform
962 const State& s(getCurrentState());
963
964 // apply the layer's transform, followed by the display's global transform
965 // here we're guaranteed that the layer's transform preserves rects
966 Rect win(s.active.w, s.active.h);
967 if (!s.crop.isEmpty()) {
968 win.intersect(s.crop, &win);
969 }
970 // subtract the transparent region and snap to the bounds
971 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700972 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000973 frame.intersect(hw->getViewport(), &frame);
974 if (!s.finalCrop.isEmpty()) {
975 frame.intersect(s.finalCrop, &frame);
976 }
977 const Transform& tr(hw->getTransform());
978 return Rect(tr.transform(frame));
979}
980#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700981
Mathias Agopian13127d82013-03-05 17:47:11 -0800982// ---------------------------------------------------------------------------
983// drawing...
984// ---------------------------------------------------------------------------
985
986void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800987 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800988}
989
Dan Stozac7014012014-02-14 15:03:43 -0800990void Layer::draw(const sp<const DisplayDevice>& hw,
991 bool useIdentityTransform) const {
992 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800993}
994
Dan Stozac7014012014-02-14 15:03:43 -0800995void Layer::draw(const sp<const DisplayDevice>& hw) const {
996 onDraw(hw, Region(hw->bounds()), false);
997}
998
999void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
1000 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001002 ATRACE_CALL();
1003
Mathias Agopiana67932f2011-04-20 14:20:59 -07001004 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -07001006 // in fact never been drawn into. This happens frequently with
1007 // SurfaceView because the WindowManager can't know when the client
1008 // has drawn the first time.
1009
1010 // If there is nothing under us, we paint the screen in black, otherwise
1011 // we just skip this update.
1012
1013 // figure out if there is something below us
1014 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001015 bool finished = false;
1016 mFlinger->mDrawingState.layersSortedByZ.traverseInZOrder([&](Layer* layer) {
1017 if (finished || layer == static_cast<Layer const*>(this)) {
1018 finished = true;
1019 return;
1020 }
Mathias Agopian42977342012-08-05 00:40:46 -07001021 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001022 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001023 // if not everything below us is covered, we plug the holes!
1024 Region holes(clip.subtract(under));
1025 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001026 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001027 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 return;
1029 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001030
Andy McFadden97eba892012-12-11 15:21:45 -08001031 // Bind the current buffer to the GL texture, and wait for it to be
1032 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001033 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1034 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001035 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001036 // Go ahead and draw the buffer anyway; no matter what we do the screen
1037 // is probably going to have something visibly wrong.
1038 }
1039
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001040 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1041
Mathias Agopian875d8e12013-06-07 15:35:48 -07001042 RenderEngine& engine(mFlinger->getRenderEngine());
1043
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001044 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001045 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001046 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001047
1048 // Query the texture matrix given our current filtering mode.
1049 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001050 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1051 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001052
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001053 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
1054
1055 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001056 * the code below applies the primary display's inverse transform to
1057 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001058 */
1059
1060 // create a 4x4 transform matrix from the display transform flags
1061 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1062 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1063 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1064
1065 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -07001066 uint32_t transform =
1067 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001068 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
1069 tr = tr * rot90;
1070 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
1071 tr = tr * flipH;
1072 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
1073 tr = tr * flipV;
1074
1075 // calculate the inverse
1076 tr = inverse(tr);
1077
1078 // and finally apply it to the original texture matrix
1079 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1080 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1081 }
1082
Jamie Genniscbb1a952012-05-08 17:05:52 -07001083 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001084 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1085 mTexture.setFiltering(useFiltering);
1086 mTexture.setMatrix(textureMatrix);
1087
1088 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001089 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001090 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001091 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001092 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001093 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094}
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096
Dan Stozac7014012014-02-14 15:03:43 -08001097void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001098 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001099 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001100{
Mathias Agopian19733a32013-08-28 18:13:56 -07001101 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001102 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001103 engine.setupFillWithColor(red, green, blue, alpha);
1104 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001105}
1106
1107void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001108 const sp<const DisplayDevice>& hw) const {
1109 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001110}
1111
Dan Stozac7014012014-02-14 15:03:43 -08001112void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001113 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001114 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001115
Dan Stozac7014012014-02-14 15:03:43 -08001116 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001117
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 /*
1119 * NOTE: the way we compute the texture coordinates here produces
1120 * different results than when we take the HWC path -- in the later case
1121 * the "source crop" is rounded to texel boundaries.
1122 * This can produce significantly different results when the texture
1123 * is scaled by a large amount.
1124 *
1125 * The GL code below is more logical (imho), and the difference with
1126 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001127 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001128 * GL composition when a buffer scaling is applied (maybe with some
1129 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1130 * like more of a hack.
1131 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001132 Rect win(computeBounds());
1133
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001135 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001137 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001138 win.clear();
1139 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001141 if (!win.intersect(computeBounds(), &win)) {
1142 win.clear();
1143 }
1144 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001145
Mathias Agopian3f844832013-08-07 21:24:32 -07001146 float left = float(win.left) / float(s.active.w);
1147 float top = float(win.top) / float(s.active.h);
1148 float right = float(win.right) / float(s.active.w);
1149 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001150
Mathias Agopian875d8e12013-06-07 15:35:48 -07001151 // TODO: we probably want to generate the texture coords with the mesh
1152 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001153 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1154 texCoords[0] = vec2(left, 1.0f - top);
1155 texCoords[1] = vec2(left, 1.0f - bottom);
1156 texCoords[2] = vec2(right, 1.0f - bottom);
1157 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001158
Mathias Agopian875d8e12013-06-07 15:35:48 -07001159 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001160 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001161 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001162 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001163}
1164
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001165#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001166void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1167 bool callIntoHwc) {
1168 if (mHwcLayers.count(hwcId) == 0) {
1169 ALOGE("setCompositionType called without a valid HWC layer");
1170 return;
1171 }
1172 auto& hwcInfo = mHwcLayers[hwcId];
1173 auto& hwcLayer = hwcInfo.layer;
1174 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1175 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1176 if (hwcInfo.compositionType != type) {
1177 ALOGV(" actually setting");
1178 hwcInfo.compositionType = type;
1179 if (callIntoHwc) {
1180 auto error = hwcLayer->setCompositionType(type);
1181 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1182 "composition type %s: %s (%d)", mName.string(),
1183 to_string(type).c_str(), to_string(error).c_str(),
1184 static_cast<int32_t>(error));
1185 }
1186 }
1187}
1188
1189HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001190 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1191 // If we're querying the composition type for a display that does not
1192 // have a HWC counterpart, then it will always be Client
1193 return HWC2::Composition::Client;
1194 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001195 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001196 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001197 return HWC2::Composition::Invalid;
1198 }
1199 return mHwcLayers.at(hwcId).compositionType;
1200}
1201
1202void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1203 if (mHwcLayers.count(hwcId) == 0) {
1204 ALOGE("setClearClientTarget called without a valid HWC layer");
1205 return;
1206 }
1207 mHwcLayers[hwcId].clearClientTarget = clear;
1208}
1209
1210bool Layer::getClearClientTarget(int32_t hwcId) const {
1211 if (mHwcLayers.count(hwcId) == 0) {
1212 ALOGE("getClearClientTarget called without a valid HWC layer");
1213 return false;
1214 }
1215 return mHwcLayers.at(hwcId).clearClientTarget;
1216}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001217#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001218
Ruben Brunk1681d952014-06-27 15:51:55 -07001219uint32_t Layer::getProducerStickyTransform() const {
1220 int producerStickyTransform = 0;
1221 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1222 if (ret != OK) {
1223 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1224 strerror(-ret), ret);
1225 return 0;
1226 }
1227 return static_cast<uint32_t>(producerStickyTransform);
1228}
1229
Dan Stozac5da2712016-07-20 15:38:12 -07001230bool Layer::latchUnsignaledBuffers() {
1231 static bool propertyLoaded = false;
1232 static bool latch = false;
1233 static std::mutex mutex;
1234 std::lock_guard<std::mutex> lock(mutex);
1235 if (!propertyLoaded) {
1236 char value[PROPERTY_VALUE_MAX] = {};
1237 property_get("debug.sf.latch_unsignaled", value, "0");
1238 latch = atoi(value);
1239 propertyLoaded = true;
1240 }
1241 return latch;
1242}
1243
Dan Stozacac35382016-01-27 12:21:06 -08001244uint64_t Layer::getHeadFrameNumber() const {
1245 Mutex::Autolock lock(mQueueItemLock);
1246 if (!mQueueItems.empty()) {
1247 return mQueueItems[0].mFrameNumber;
1248 } else {
1249 return mCurrentFrameNumber;
1250 }
1251}
1252
Dan Stoza1ce65812016-06-15 16:26:27 -07001253bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001254#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001255 if (latchUnsignaledBuffers()) {
1256 return true;
1257 }
1258
Dan Stoza1ce65812016-06-15 16:26:27 -07001259 Mutex::Autolock lock(mQueueItemLock);
1260 if (mQueueItems.empty()) {
1261 return true;
1262 }
1263 if (mQueueItems[0].mIsDroppable) {
1264 // Even though this buffer's fence may not have signaled yet, it could
1265 // be replaced by another buffer before it has a chance to, which means
1266 // that it's possible to get into a situation where a buffer is never
1267 // able to be latched. To avoid this, grab this buffer anyway.
1268 return true;
1269 }
1270 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001271#else
1272 return true;
1273#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001274}
1275
Dan Stozacac35382016-01-27 12:21:06 -08001276bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1277 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1278 // Don't bother with a SyncPoint, since we've already latched the
1279 // relevant frame
1280 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001281 }
1282
Dan Stozacac35382016-01-27 12:21:06 -08001283 Mutex::Autolock lock(mLocalSyncPointMutex);
1284 mLocalSyncPoints.push_back(point);
1285 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001286}
1287
Mathias Agopian13127d82013-03-05 17:47:11 -08001288void Layer::setFiltering(bool filtering) {
1289 mFiltering = filtering;
1290}
1291
1292bool Layer::getFiltering() const {
1293 return mFiltering;
1294}
1295
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001296// As documented in libhardware header, formats in the range
1297// 0x100 - 0x1FF are specific to the HAL implementation, and
1298// are known to have no alpha channel
1299// TODO: move definition for device-specific range into
1300// hardware.h, instead of using hard-coded values here.
1301#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1302
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001303bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001304 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1305 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001306 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001307 switch (format) {
1308 case HAL_PIXEL_FORMAT_RGBA_8888:
1309 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001310 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001311 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001312 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001313 }
1314 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001315 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001316}
1317
Mathias Agopian13127d82013-03-05 17:47:11 -08001318// ----------------------------------------------------------------------------
1319// local state
1320// ----------------------------------------------------------------------------
1321
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001322static void boundPoint(vec2* point, const Rect& crop) {
1323 if (point->x < crop.left) {
1324 point->x = crop.left;
1325 }
1326 if (point->x > crop.right) {
1327 point->x = crop.right;
1328 }
1329 if (point->y < crop.top) {
1330 point->y = crop.top;
1331 }
1332 if (point->y > crop.bottom) {
1333 point->y = crop.bottom;
1334 }
1335}
1336
Dan Stozac7014012014-02-14 15:03:43 -08001337void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1338 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001339{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001340 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001341 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001342 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001343 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001344
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001345 vec2 lt = vec2(win.left, win.top);
1346 vec2 lb = vec2(win.left, win.bottom);
1347 vec2 rb = vec2(win.right, win.bottom);
1348 vec2 rt = vec2(win.right, win.top);
1349
Robert Carr1f0a16a2016-10-24 16:27:39 -07001350 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001351 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001352 lt = layerTransform.transform(lt);
1353 lb = layerTransform.transform(lb);
1354 rb = layerTransform.transform(rb);
1355 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001356 }
1357
Robert Carrb5d3d262016-03-25 15:08:13 -07001358 if (!s.finalCrop.isEmpty()) {
1359 boundPoint(&lt, s.finalCrop);
1360 boundPoint(&lb, s.finalCrop);
1361 boundPoint(&rb, s.finalCrop);
1362 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001363 }
1364
Mathias Agopianff2ed702013-09-01 21:36:12 -07001365 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001366 position[0] = hwTransform.transform(lt);
1367 position[1] = hwTransform.transform(lb);
1368 position[2] = hwTransform.transform(rb);
1369 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001370 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001371 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001372 }
1373}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001374
Andy McFadden4125a4f2014-01-29 17:17:11 -08001375bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001376{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001377 // if we don't have a buffer yet, we're translucent regardless of the
1378 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001379 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001380 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001381 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001382
1383 // if the layer has the opaque flag, then we're always opaque,
1384 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001385 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001386}
1387
Dan Stoza23116082015-06-18 14:58:39 -07001388bool Layer::isSecure() const
1389{
1390 const Layer::State& s(mDrawingState);
1391 return (s.flags & layer_state_t::eLayerSecure);
1392}
1393
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001394bool Layer::isProtected() const
1395{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001396 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001397 return (activeBuffer != 0) &&
1398 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1399}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001400
Mathias Agopian13127d82013-03-05 17:47:11 -08001401bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001402 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001403}
1404
1405bool Layer::isCropped() const {
1406 return !mCurrentCrop.isEmpty();
1407}
1408
1409bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1410 return mNeedsFiltering || hw->needsFiltering();
1411}
1412
1413void Layer::setVisibleRegion(const Region& visibleRegion) {
1414 // always called from main thread
1415 this->visibleRegion = visibleRegion;
1416}
1417
1418void Layer::setCoveredRegion(const Region& coveredRegion) {
1419 // always called from main thread
1420 this->coveredRegion = coveredRegion;
1421}
1422
1423void Layer::setVisibleNonTransparentRegion(const Region&
1424 setVisibleNonTransparentRegion) {
1425 // always called from main thread
1426 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1427}
1428
1429// ----------------------------------------------------------------------------
1430// transaction
1431// ----------------------------------------------------------------------------
1432
Dan Stoza7dde5992015-05-22 09:51:44 -07001433void Layer::pushPendingState() {
1434 if (!mCurrentState.modified) {
1435 return;
1436 }
1437
Dan Stoza7dde5992015-05-22 09:51:44 -07001438 // If this transaction is waiting on the receipt of a frame, generate a sync
1439 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001440 if (mCurrentState.barrierLayer != nullptr) {
1441 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1442 if (barrierLayer == nullptr) {
1443 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001444 // If we can't promote the layer we are intended to wait on,
1445 // then it is expired or otherwise invalid. Allow this transaction
1446 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001447 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001448 } else {
1449 auto syncPoint = std::make_shared<SyncPoint>(
1450 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001451 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001452 mRemoteSyncPoints.push_back(std::move(syncPoint));
1453 } else {
1454 // We already missed the frame we're supposed to synchronize
1455 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001456 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001457 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001458 }
1459
Dan Stoza7dde5992015-05-22 09:51:44 -07001460 // Wake us up to check if the frame has been received
1461 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001462 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001463 }
1464 mPendingStates.push_back(mCurrentState);
1465}
1466
Pablo Ceballos05289c22016-04-14 15:49:55 -07001467void Layer::popPendingState(State* stateToCommit) {
1468 auto oldFlags = stateToCommit->flags;
1469 *stateToCommit = mPendingStates[0];
1470 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1471 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001472
1473 mPendingStates.removeAt(0);
1474}
1475
Pablo Ceballos05289c22016-04-14 15:49:55 -07001476bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001477 bool stateUpdateAvailable = false;
1478 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001479 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001480 if (mRemoteSyncPoints.empty()) {
1481 // If we don't have a sync point for this, apply it anyway. It
1482 // will be visually wrong, but it should keep us from getting
1483 // into too much trouble.
1484 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001485 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001486 stateUpdateAvailable = true;
1487 continue;
1488 }
1489
Dan Stozacac35382016-01-27 12:21:06 -08001490 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1491 mPendingStates[0].frameNumber) {
1492 ALOGE("[%s] Unexpected sync point frame number found",
1493 mName.string());
1494
1495 // Signal our end of the sync point and then dispose of it
1496 mRemoteSyncPoints.front()->setTransactionApplied();
1497 mRemoteSyncPoints.pop_front();
1498 continue;
1499 }
1500
Dan Stoza7dde5992015-05-22 09:51:44 -07001501 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1502 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001503 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001504 stateUpdateAvailable = true;
1505
1506 // Signal our end of the sync point and then dispose of it
1507 mRemoteSyncPoints.front()->setTransactionApplied();
1508 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001509 } else {
1510 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001511 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001512 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001513 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001514 stateUpdateAvailable = true;
1515 }
1516 }
1517
1518 // If we still have pending updates, wake SurfaceFlinger back up and point
1519 // it at this layer so we can process them
1520 if (!mPendingStates.empty()) {
1521 setTransactionFlags(eTransactionNeeded);
1522 mFlinger->setTransactionFlags(eTraversalNeeded);
1523 }
1524
1525 mCurrentState.modified = false;
1526 return stateUpdateAvailable;
1527}
1528
1529void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001530 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001531 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001532 Mutex::Autolock lock(mLocalSyncPointMutex);
1533 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001534 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001535 point->setFrameAvailable();
1536 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001537 }
1538}
1539
Mathias Agopian13127d82013-03-05 17:47:11 -08001540uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001541 ATRACE_CALL();
1542
Dan Stoza7dde5992015-05-22 09:51:44 -07001543 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001544 Layer::State c = getCurrentState();
1545 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001546 return 0;
1547 }
1548
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001549 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001551 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1552 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001553
1554 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001555 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001556 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001557 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001558 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001559 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001560 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001561 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001562 this, getName().string(), mCurrentTransform,
1563 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001564 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001565 c.crop.left,
1566 c.crop.top,
1567 c.crop.right,
1568 c.crop.bottom,
1569 c.crop.getWidth(),
1570 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001571 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001572 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001573 s.crop.left,
1574 s.crop.top,
1575 s.crop.right,
1576 s.crop.bottom,
1577 s.crop.getWidth(),
1578 s.crop.getHeight(),
1579 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001581 // record the new size, form this point on, when the client request
1582 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001583 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001584 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001586
Robert Carr82364e32016-05-15 11:27:47 -07001587 const bool resizePending = (c.requested.w != c.active.w) ||
1588 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001589 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001590 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001591 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001592 // if we have a pending resize, unless we are in fixed-size mode.
1593 // the drawing state will be updated only once we receive a buffer
1594 // with the correct size.
1595 //
1596 // in particular, we want to make sure the clip (which is part
1597 // of the geometry state) is latched together with the size but is
1598 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001599 //
1600 // If a sideband stream is attached, however, we want to skip this
1601 // optimization so that transactions aren't missed when a buffer
1602 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001603
1604 flags |= eDontUpdateGeometryState;
1605 }
1606 }
1607
Mathias Agopian13127d82013-03-05 17:47:11 -08001608 // always set active to requested, unless we're asked not to
1609 // this is used by Layer, which special cases resizes.
1610 if (flags & eDontUpdateGeometryState) {
1611 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001612 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001613 if (mFreezePositionUpdates) {
1614 float tx = c.active.transform.tx();
1615 float ty = c.active.transform.ty();
1616 c.active = c.requested;
1617 c.active.transform.set(tx, ty);
1618 editCurrentState.active = c.active;
1619 } else {
1620 editCurrentState.active = editCurrentState.requested;
1621 c.active = c.requested;
1622 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001623 }
1624
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001625 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001626 // invalidate and recompute the visible regions if needed
1627 flags |= Layer::eVisibleRegion;
1628 }
1629
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001630 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 // invalidate and recompute the visible regions if needed
1632 flags |= eVisibleRegion;
1633 this->contentDirty = true;
1634
1635 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001636 const uint8_t type = c.active.transform.getType();
1637 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001638 (type >= Transform::SCALE));
1639 }
1640
Dan Stozac8145172016-04-28 16:29:06 -07001641 // If the layer is hidden, signal and clear out all local sync points so
1642 // that transactions for layers depending on this layer's frames becoming
1643 // visible are not blocked
1644 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001646 }
1647
Mathias Agopian13127d82013-03-05 17:47:11 -08001648 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001649 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001650 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651}
1652
Pablo Ceballos05289c22016-04-14 15:49:55 -07001653void Layer::commitTransaction(const State& stateToCommit) {
1654 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001655}
1656
Mathias Agopian13127d82013-03-05 17:47:11 -08001657uint32_t Layer::getTransactionFlags(uint32_t flags) {
1658 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1659}
1660
1661uint32_t Layer::setTransactionFlags(uint32_t flags) {
1662 return android_atomic_or(flags, &mTransactionFlags);
1663}
1664
Robert Carr82364e32016-05-15 11:27:47 -07001665bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001666 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001667 return false;
1668 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001669
1670 // We update the requested and active position simultaneously because
1671 // we want to apply the position portion of the transform matrix immediately,
1672 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001673 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001674 if (immediate && !mFreezePositionUpdates) {
1675 mCurrentState.active.transform.set(x, y);
1676 }
1677 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001678
Dan Stoza7dde5992015-05-22 09:51:44 -07001679 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001680 setTransactionFlags(eTransactionNeeded);
1681 return true;
1682}
Robert Carr82364e32016-05-15 11:27:47 -07001683
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1685 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1686 if (idx < 0) {
1687 return false;
1688 }
1689 if (childLayer->setLayer(z)) {
1690 mCurrentChildren.removeAt(idx);
1691 mCurrentChildren.add(childLayer);
1692 }
1693 return true;
1694}
1695
Robert Carrae060832016-11-28 10:51:00 -08001696bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001697 if (mCurrentState.z == z)
1698 return false;
1699 mCurrentState.sequence++;
1700 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001701 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001702 setTransactionFlags(eTransactionNeeded);
1703 return true;
1704}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001705
Mathias Agopian13127d82013-03-05 17:47:11 -08001706bool Layer::setSize(uint32_t w, uint32_t h) {
1707 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1708 return false;
1709 mCurrentState.requested.w = w;
1710 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001711 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001712 setTransactionFlags(eTransactionNeeded);
1713 return true;
1714}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001715#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001716bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001717#else
1718bool Layer::setAlpha(uint8_t alpha) {
1719#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001720 if (mCurrentState.alpha == alpha)
1721 return false;
1722 mCurrentState.sequence++;
1723 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001724 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001725 setTransactionFlags(eTransactionNeeded);
1726 return true;
1727}
1728bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1729 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001730 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001731 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001732 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001733 setTransactionFlags(eTransactionNeeded);
1734 return true;
1735}
1736bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001737 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001738 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001739 setTransactionFlags(eTransactionNeeded);
1740 return true;
1741}
1742bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1743 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1744 if (mCurrentState.flags == newFlags)
1745 return false;
1746 mCurrentState.sequence++;
1747 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001748 mCurrentState.mask = mask;
1749 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001750 setTransactionFlags(eTransactionNeeded);
1751 return true;
1752}
Robert Carr99e27f02016-06-16 15:18:02 -07001753
1754bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001755 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001756 return false;
1757 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001758 mCurrentState.requestedCrop = crop;
1759 if (immediate) {
1760 mCurrentState.crop = crop;
1761 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001762 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001763 setTransactionFlags(eTransactionNeeded);
1764 return true;
1765}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001766bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001767 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001768 return false;
1769 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001770 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001771 mCurrentState.modified = true;
1772 setTransactionFlags(eTransactionNeeded);
1773 return true;
1774}
Mathias Agopian13127d82013-03-05 17:47:11 -08001775
Robert Carrc3574f72016-03-24 12:19:32 -07001776bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1777 if (scalingMode == mOverrideScalingMode)
1778 return false;
1779 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001780 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001781 return true;
1782}
1783
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001784void Layer::setInfo(uint32_t type, uint32_t appId) {
1785 mCurrentState.appId = appId;
1786 mCurrentState.type = type;
1787 mCurrentState.modified = true;
1788 setTransactionFlags(eTransactionNeeded);
1789}
1790
Robert Carrc3574f72016-03-24 12:19:32 -07001791uint32_t Layer::getEffectiveScalingMode() const {
1792 if (mOverrideScalingMode >= 0) {
1793 return mOverrideScalingMode;
1794 }
1795 return mCurrentScalingMode;
1796}
1797
Mathias Agopian13127d82013-03-05 17:47:11 -08001798bool Layer::setLayerStack(uint32_t layerStack) {
1799 if (mCurrentState.layerStack == layerStack)
1800 return false;
1801 mCurrentState.sequence++;
1802 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001803 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001804 setTransactionFlags(eTransactionNeeded);
1805 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001806}
1807
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001808bool Layer::setDataSpace(android_dataspace dataSpace) {
1809 if (mCurrentState.dataSpace == dataSpace)
1810 return false;
1811 mCurrentState.sequence++;
1812 mCurrentState.dataSpace = dataSpace;
1813 mCurrentState.modified = true;
1814 setTransactionFlags(eTransactionNeeded);
1815 return true;
1816}
1817
Robert Carr1f0a16a2016-10-24 16:27:39 -07001818uint32_t Layer::getLayerStack() const {
1819 auto p = getParent();
1820 if (p == nullptr) {
1821 return getDrawingState().layerStack;
1822 }
1823 return p->getLayerStack();
1824}
1825
Robert Carr0d480722017-01-10 16:42:54 -08001826void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07001827 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001828 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001829 mCurrentState.frameNumber = frameNumber;
1830 // We don't set eTransactionNeeded, because just receiving a deferral
1831 // request without any other state updates shouldn't actually induce a delay
1832 mCurrentState.modified = true;
1833 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001834 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001835 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001836 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001837 ALOGE("Deferred transaction");
1838}
1839
1840void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
1841 uint64_t frameNumber) {
1842 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1843 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001844}
1845
Dan Stozaee44edd2015-03-23 15:50:23 -07001846void Layer::useSurfaceDamage() {
1847 if (mFlinger->mForceFullDamage) {
1848 surfaceDamageRegion = Region::INVALID_REGION;
1849 } else {
1850 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1851 }
1852}
1853
1854void Layer::useEmptyDamage() {
1855 surfaceDamageRegion.clear();
1856}
1857
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858// ----------------------------------------------------------------------------
1859// pageflip handling...
1860// ----------------------------------------------------------------------------
1861
Dan Stoza6b9454d2014-11-07 16:00:59 -08001862bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001863 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001864 return true;
1865 }
1866
Dan Stoza6b9454d2014-11-07 16:00:59 -08001867 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001868 if (mQueueItems.empty()) {
1869 return false;
1870 }
1871 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001872 nsecs_t expectedPresent =
1873 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001874
1875 // Ignore timestamps more than a second in the future
1876 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1877 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1878 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1879 expectedPresent);
1880
1881 bool isDue = timestamp < expectedPresent;
1882 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001883}
1884
Brian Andersond6927fb2016-07-23 23:37:30 -07001885bool Layer::onPreComposition(nsecs_t refreshStartTime) {
1886 if (mBufferLatched) {
1887 Mutex::Autolock lock(mFrameEventHistoryMutex);
1888 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
1889 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001890 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001891 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001892}
1893
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001894bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001895 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001896 const std::shared_ptr<FenceTime>& retireFence,
1897 const CompositorTiming& compositorTiming) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001898 mAcquireTimeline.updateSignalTimes();
1899 mReleaseTimeline.updateSignalTimes();
1900
Brian Andersond6927fb2016-07-23 23:37:30 -07001901 // mFrameLatencyNeeded is true when a new frame was latched for the
1902 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08001903 if (!mFrameLatencyNeeded)
1904 return false;
1905
Fabien Sanglard28e98082016-12-05 10:19:46 -08001906 // Update mFrameEventHistory.
1907 {
1908 Mutex::Autolock lock(mFrameEventHistoryMutex);
1909 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001910 glDoneFence, presentFence, compositorTiming);
Brian Anderson8cc8b102016-10-21 12:43:09 -07001911 if (mPreviousFrameNumber != 0) {
1912 mFrameEventHistory.addRetire(mPreviousFrameNumber,
1913 retireFence);
1914 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001915 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08001916
1917 // Update mFrameTracker.
1918 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1919 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1920
Brian Anderson3d4039d2016-09-23 16:31:30 -07001921 std::shared_ptr<FenceTime> frameReadyFence =
1922 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08001923 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001924 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001925 } else {
1926 // There was no fence for this frame, so assume that it was ready
1927 // to be presented at the desired present time.
1928 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1929 }
1930
Brian Anderson3d4039d2016-09-23 16:31:30 -07001931 if (presentFence->isValid()) {
1932 mFrameTracker.setActualPresentFence(
1933 std::shared_ptr<FenceTime>(presentFence));
1934 } else if (retireFence->isValid()) {
1935 mFrameTracker.setActualPresentFence(
1936 std::shared_ptr<FenceTime>(retireFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001937 } else {
1938 // The HWC doesn't support present fences, so use the refresh
1939 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001940 mFrameTracker.setActualPresentTime(
1941 mFlinger->getHwComposer().getRefreshTimestamp(
1942 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001943 }
1944
1945 mFrameTracker.advanceFrame();
1946 mFrameLatencyNeeded = false;
1947 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001948}
1949
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001950#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07001951void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08001952 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
1953 return;
1954 }
1955
Brian Anderson3d4039d2016-09-23 16:31:30 -07001956 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07001957 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07001958 mReleaseTimeline.push(releaseFenceTime);
1959
1960 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07001961 if (mPreviousFrameNumber != 0) {
1962 mFrameEventHistory.addRelease(mPreviousFrameNumber,
1963 dequeueReadyTime, std::move(releaseFenceTime));
1964 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001965}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001966#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001967
Robert Carr1f0a16a2016-10-24 16:27:39 -07001968bool Layer::isHiddenByPolicy() const {
1969 const Layer::State& s(mDrawingState);
1970 const auto& parent = getParent();
1971 if (parent != nullptr && parent->isHiddenByPolicy()) {
1972 return true;
1973 }
1974 return s.flags & layer_state_t::eLayerHidden;
1975}
1976
Mathias Agopianda27af92012-09-13 18:17:13 -07001977bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001978 const Layer::State& s(mDrawingState);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001979#ifdef USE_HWC2
Robert Carr1f0a16a2016-10-24 16:27:39 -07001980 return !(isHiddenByPolicy()) && s.alpha > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08001981 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001982#else
Robert Carr1f0a16a2016-10-24 16:27:39 -07001983 return !(isHiddenByPolicy()) && s.alpha
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001984 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1985#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001986}
1987
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07001988bool Layer::allTransactionsSignaled() {
1989 auto headFrameNumber = getHeadFrameNumber();
1990 bool matchingFramesFound = false;
1991 bool allTransactionsApplied = true;
1992 Mutex::Autolock lock(mLocalSyncPointMutex);
1993
1994 for (auto& point : mLocalSyncPoints) {
1995 if (point->getFrameNumber() > headFrameNumber) {
1996 break;
1997 }
1998 matchingFramesFound = true;
1999
2000 if (!point->frameIsAvailable()) {
2001 // We haven't notified the remote layer that the frame for
2002 // this point is available yet. Notify it now, and then
2003 // abort this attempt to latch.
2004 point->setFrameAvailable();
2005 allTransactionsApplied = false;
2006 break;
2007 }
2008
2009 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
2010 }
2011 return !matchingFramesFound || allTransactionsApplied;
2012}
2013
Brian Andersond6927fb2016-07-23 23:37:30 -07002014Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002015{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002016 ATRACE_CALL();
2017
Jesse Hall399184a2014-03-03 15:42:54 -08002018 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2019 // mSidebandStreamChanged was true
2020 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002021 if (mSidebandStream != NULL) {
2022 setTransactionFlags(eTransactionNeeded);
2023 mFlinger->setTransactionFlags(eTraversalNeeded);
2024 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002025 recomputeVisibleRegions = true;
2026
2027 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002028 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002029 }
2030
Mathias Agopian4fec8732012-06-29 14:12:52 -07002031 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002032 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2033 return outDirtyRegion;
2034 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002035
Fabien Sanglard223eb912016-10-13 10:15:06 -07002036 // if we've already called updateTexImage() without going through
2037 // a composition step, we have to skip this layer at this point
2038 // because we cannot call updateTeximage() without a corresponding
2039 // compositionComplete() call.
2040 // we'll trigger an update in onPreComposition().
2041 if (mRefreshPending) {
2042 return outDirtyRegion;
2043 }
2044
2045 // If the head buffer's acquire fence hasn't signaled yet, return and
2046 // try again later
2047 if (!headFenceHasSignaled()) {
2048 mFlinger->signalLayerUpdate();
2049 return outDirtyRegion;
2050 }
2051
2052 // Capture the old state of the layer for comparisons later
2053 const State& s(getDrawingState());
2054 const bool oldOpacity = isOpaque(s);
2055 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2056
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002057 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002058 mFlinger->signalLayerUpdate();
2059 return outDirtyRegion;
2060 }
2061
2062 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2063 // of the buffer queue isn't modified when the buffer queue is returning
2064 // BufferItem's that weren't actually queued. This can happen in shared
2065 // buffer mode.
2066 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002067 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2068 getProducerStickyTransform() != 0, mName.string(),
2069 mOverrideScalingMode, mFreezePositionUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002070 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2071 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2072 mLastFrameNumberReceived);
2073 if (updateResult == BufferQueue::PRESENT_LATER) {
2074 // Producer doesn't want buffer to be displayed yet. Signal a
2075 // layer update so we check again at the next opportunity.
2076 mFlinger->signalLayerUpdate();
2077 return outDirtyRegion;
2078 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2079 // If the buffer has been rejected, remove it from the shadow queue
2080 // and return early
2081 if (queuedBuffer) {
2082 Mutex::Autolock lock(mQueueItemLock);
2083 mQueueItems.removeAt(0);
2084 android_atomic_dec(&mQueuedFrames);
2085 }
2086 return outDirtyRegion;
2087 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2088 // This can occur if something goes wrong when trying to create the
2089 // EGLImage for this buffer. If this happens, the buffer has already
2090 // been released, so we need to clean up the queue and bug out
2091 // early.
2092 if (queuedBuffer) {
2093 Mutex::Autolock lock(mQueueItemLock);
2094 mQueueItems.clear();
2095 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002096 }
2097
Fabien Sanglard223eb912016-10-13 10:15:06 -07002098 // Once we have hit this state, the shadow queue may no longer
2099 // correctly reflect the incoming BufferQueue's contents, so even if
2100 // updateTexImage starts working, the only safe course of action is
2101 // to continue to ignore updates.
2102 mUpdateTexImageFailed = true;
2103
2104 return outDirtyRegion;
2105 }
2106
2107 if (queuedBuffer) {
2108 // Autolock scope
2109 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2110
2111 Mutex::Autolock lock(mQueueItemLock);
2112
2113 // Remove any stale buffers that have been dropped during
2114 // updateTexImage
2115 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2116 mQueueItems.removeAt(0);
2117 android_atomic_dec(&mQueuedFrames);
2118 }
2119
2120 mQueueItems.removeAt(0);
2121 }
2122
2123
2124 // Decrement the queued-frames count. Signal another event if we
2125 // have more frames pending.
2126 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2127 || mAutoRefresh) {
2128 mFlinger->signalLayerUpdate();
2129 }
2130
Fabien Sanglard223eb912016-10-13 10:15:06 -07002131 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002132 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2133 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002134 if (mActiveBuffer == NULL) {
2135 // this can only happen if the very first buffer was rejected.
2136 return outDirtyRegion;
2137 }
2138
Brian Andersond6927fb2016-07-23 23:37:30 -07002139 mBufferLatched = true;
2140 mPreviousFrameNumber = mCurrentFrameNumber;
2141 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2142
2143 {
2144 Mutex::Autolock lock(mFrameEventHistoryMutex);
2145 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2146#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002147 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002148 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07002149 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002150 if (mPreviousFrameNumber != 0) {
2151 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2152 latchTime, std::move(releaseFenceTime));
2153 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002154#endif
2155 }
2156
Fabien Sanglard223eb912016-10-13 10:15:06 -07002157 mRefreshPending = true;
2158 mFrameLatencyNeeded = true;
2159 if (oldActiveBuffer == NULL) {
2160 // the first time we receive a buffer, we need to trigger a
2161 // geometry invalidation.
2162 recomputeVisibleRegions = true;
2163 }
2164
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002165 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2166
Fabien Sanglard223eb912016-10-13 10:15:06 -07002167 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2168 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2169 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2170 if ((crop != mCurrentCrop) ||
2171 (transform != mCurrentTransform) ||
2172 (scalingMode != mCurrentScalingMode))
2173 {
2174 mCurrentCrop = crop;
2175 mCurrentTransform = transform;
2176 mCurrentScalingMode = scalingMode;
2177 recomputeVisibleRegions = true;
2178 }
2179
2180 if (oldActiveBuffer != NULL) {
2181 uint32_t bufWidth = mActiveBuffer->getWidth();
2182 uint32_t bufHeight = mActiveBuffer->getHeight();
2183 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2184 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002185 recomputeVisibleRegions = true;
2186 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002187 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002188
Fabien Sanglard223eb912016-10-13 10:15:06 -07002189 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2190 if (oldOpacity != isOpaque(s)) {
2191 recomputeVisibleRegions = true;
2192 }
Dan Stozacac35382016-01-27 12:21:06 -08002193
Fabien Sanglard223eb912016-10-13 10:15:06 -07002194 // Remove any sync points corresponding to the buffer which was just
2195 // latched
2196 {
2197 Mutex::Autolock lock(mLocalSyncPointMutex);
2198 auto point = mLocalSyncPoints.begin();
2199 while (point != mLocalSyncPoints.end()) {
2200 if (!(*point)->frameIsAvailable() ||
2201 !(*point)->transactionIsApplied()) {
2202 // This sync point must have been added since we started
2203 // latching. Don't drop it yet.
2204 ++point;
2205 continue;
2206 }
2207
2208 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2209 point = mLocalSyncPoints.erase(point);
2210 } else {
2211 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002212 }
2213 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002214 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002215
2216 // FIXME: postedRegion should be dirty & bounds
2217 Region dirtyRegion(Rect(s.active.w, s.active.h));
2218
2219 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002220 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002221
Mathias Agopian4fec8732012-06-29 14:12:52 -07002222 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002223}
2224
Mathias Agopiana67932f2011-04-20 14:20:59 -07002225uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002226{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002227 // TODO: should we do something special if mSecure is set?
2228 if (mProtectedByApp) {
2229 // need a hardware-protected path to external video sink
2230 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002231 }
Riley Andrews03414a12014-07-01 14:22:59 -07002232 if (mPotentialCursor) {
2233 usage |= GraphicBuffer::USAGE_CURSOR;
2234 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002235 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002236 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002237}
2238
Mathias Agopian84300952012-11-21 16:02:13 -08002239void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002240 uint32_t orientation = 0;
2241 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002242 // The transform hint is used to improve performance, but we can
2243 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002244 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002245 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002246 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002247 if (orientation & Transform::ROT_INVALID) {
2248 orientation = 0;
2249 }
2250 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002251 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002252}
2253
Mathias Agopian13127d82013-03-05 17:47:11 -08002254// ----------------------------------------------------------------------------
2255// debugging
2256// ----------------------------------------------------------------------------
2257
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002258void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002259{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002260 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002261
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002262 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002263 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002264 "+ %s %p (%s)\n",
2265 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002266 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002267
Mathias Agopian2ca79392013-04-02 18:30:32 -07002268 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002269 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002270 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002271 sp<Client> client(mClientRef.promote());
2272
Mathias Agopian74d211a2013-04-22 16:55:35 +02002273 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002274 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2275 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002276 "isOpaque=%1d, invalidate=%1d, "
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002277#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08002278 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002279#else
2280 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2281#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002282 " client=%p\n",
Robert Carr1f0a16a2016-10-24 16:27:39 -07002283 getLayerStack(), s.z,
2284 s.active.transform.tx(), s.active.transform.ty(),
2285 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07002286 s.crop.left, s.crop.top,
2287 s.crop.right, s.crop.bottom,
2288 s.finalCrop.left, s.finalCrop.top,
2289 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002290 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002291 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002292 s.active.transform[0][0], s.active.transform[0][1],
2293 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002294 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002295
2296 sp<const GraphicBuffer> buf0(mActiveBuffer);
2297 uint32_t w0=0, h0=0, s0=0, f0=0;
2298 if (buf0 != 0) {
2299 w0 = buf0->getWidth();
2300 h0 = buf0->getHeight();
2301 s0 = buf0->getStride();
2302 f0 = buf0->format;
2303 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002304 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002305 " "
2306 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2307 " queued-frames=%d, mRefreshPending=%d\n",
2308 mFormat, w0, h0, s0,f0,
2309 mQueuedFrames, mRefreshPending);
2310
Mathias Agopian13127d82013-03-05 17:47:11 -08002311 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002312 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002313 }
2314}
2315
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002316#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002317void Layer::miniDumpHeader(String8& result) {
2318 result.append("----------------------------------------");
2319 result.append("---------------------------------------\n");
2320 result.append(" Layer name\n");
2321 result.append(" Z | ");
2322 result.append(" Comp Type | ");
2323 result.append(" Disp Frame (LTRB) | ");
2324 result.append(" Source Crop (LTRB)\n");
2325 result.append("----------------------------------------");
2326 result.append("---------------------------------------\n");
2327}
2328
2329void Layer::miniDump(String8& result, int32_t hwcId) const {
2330 if (mHwcLayers.count(hwcId) == 0) {
2331 return;
2332 }
2333
2334 String8 name;
2335 if (mName.length() > 77) {
2336 std::string shortened;
2337 shortened.append(mName.string(), 36);
2338 shortened.append("[...]");
2339 shortened.append(mName.string() + (mName.length() - 36), 36);
2340 name = shortened.c_str();
2341 } else {
2342 name = mName;
2343 }
2344
2345 result.appendFormat(" %s\n", name.string());
2346
2347 const Layer::State& layerState(getDrawingState());
2348 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2349 result.appendFormat(" %10u | ", layerState.z);
2350 result.appendFormat("%10s | ",
2351 to_string(getCompositionType(hwcId)).c_str());
2352 const Rect& frame = hwcInfo.displayFrame;
2353 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2354 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002355 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002356 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2357 crop.right, crop.bottom);
2358
2359 result.append("- - - - - - - - - - - - - - - - - - - - ");
2360 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2361}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002362#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002363
Svetoslavd85084b2014-03-20 10:28:31 -07002364void Layer::dumpFrameStats(String8& result) const {
2365 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002366}
2367
Svetoslavd85084b2014-03-20 10:28:31 -07002368void Layer::clearFrameStats() {
2369 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002370}
2371
Jamie Gennis6547ff42013-07-16 20:12:42 -07002372void Layer::logFrameStats() {
2373 mFrameTracker.logAndResetStats(mName);
2374}
2375
Svetoslavd85084b2014-03-20 10:28:31 -07002376void Layer::getFrameStats(FrameStats* outStats) const {
2377 mFrameTracker.getStats(outStats);
2378}
2379
Brian Andersond6927fb2016-07-23 23:37:30 -07002380void Layer::dumpFrameEvents(String8& result) {
2381 result.appendFormat("- Layer %s (%s, %p)\n",
2382 getName().string(), getTypeId(), this);
2383 Mutex::Autolock lock(mFrameEventHistoryMutex);
2384 mFrameEventHistory.checkFencesForCompletion();
2385 mFrameEventHistory.dump(result);
2386}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002387
Brian Anderson5ea5e592016-12-01 16:54:33 -08002388void Layer::onDisconnect() {
2389 Mutex::Autolock lock(mFrameEventHistoryMutex);
2390 mFrameEventHistory.onDisconnect();
2391}
2392
Brian Anderson3890c392016-07-25 12:48:08 -07002393void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2394 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002395 Mutex::Autolock lock(mFrameEventHistoryMutex);
2396 if (newTimestamps) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002397 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002398 mFrameEventHistory.addQueue(*newTimestamps);
2399 }
2400
Brian Anderson3890c392016-07-25 12:48:08 -07002401 if (outDelta) {
2402 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002403 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002404}
Dan Stozae77c7662016-05-13 11:37:28 -07002405
2406std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2407 bool forceFlush) {
2408 std::vector<OccupancyTracker::Segment> history;
2409 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2410 &history);
2411 if (result != NO_ERROR) {
2412 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2413 result);
2414 return {};
2415 }
2416 return history;
2417}
2418
Robert Carr367c5682016-06-20 11:55:28 -07002419bool Layer::getTransformToDisplayInverse() const {
2420 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2421}
2422
Robert Carr1f0a16a2016-10-24 16:27:39 -07002423void Layer::addChild(const sp<Layer>& layer) {
2424 mCurrentChildren.add(layer);
2425 layer->setParent(this);
2426}
2427
2428ssize_t Layer::removeChild(const sp<Layer>& layer) {
2429 layer->setParent(nullptr);
2430 return mCurrentChildren.remove(layer);
2431}
2432
Robert Carr1db73f62016-12-21 12:58:51 -08002433bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2434 sp<Handle> handle = nullptr;
2435 sp<Layer> newParent = nullptr;
2436 if (newParentHandle == nullptr) {
2437 return false;
2438 }
2439 handle = static_cast<Handle*>(newParentHandle.get());
2440 newParent = handle->owner.promote();
2441 if (newParent == nullptr) {
2442 ALOGE("Unable to promote Layer handle");
2443 return false;
2444 }
2445
2446 for (const sp<Layer>& child : mCurrentChildren) {
2447 newParent->addChild(child);
2448
2449 sp<Client> client(child->mClientRef.promote());
2450 if (client != nullptr) {
2451 client->setParentLayer(newParent);
2452 }
2453 }
2454 mCurrentChildren.clear();
2455
2456 return true;
2457}
2458
Robert Carr9524cb32017-02-13 11:32:32 -08002459bool Layer::detachChildren() {
2460 traverseInZOrder([this](Layer* child) {
2461 if (child == this) {
2462 return;
2463 }
2464
2465 sp<Client> client(child->mClientRef.promote());
2466 if (client != nullptr) {
2467 client->detachLayer(child);
2468 }
2469 });
2470
2471 return true;
2472}
2473
Robert Carr1f0a16a2016-10-24 16:27:39 -07002474void Layer::setParent(const sp<Layer>& layer) {
2475 mParent = layer;
2476}
2477
2478void Layer::clearSyncPoints() {
2479 for (const auto& child : mCurrentChildren) {
2480 child->clearSyncPoints();
2481 }
2482
2483 Mutex::Autolock lock(mLocalSyncPointMutex);
2484 for (auto& point : mLocalSyncPoints) {
2485 point->setFrameAvailable();
2486 }
2487 mLocalSyncPoints.clear();
2488}
2489
2490int32_t Layer::getZ() const {
2491 return mDrawingState.z;
2492}
2493
2494/**
2495 * Negatively signed children are before 'this' in Z-order.
2496 */
2497void Layer::traverseInZOrder(const std::function<void(Layer*)>& exec) {
2498 size_t i = 0;
2499 for (; i < mDrawingChildren.size(); i++) {
2500 const auto& child = mDrawingChildren[i];
2501 if (child->getZ() >= 0)
2502 break;
2503 child->traverseInZOrder(exec);
2504 }
2505 exec(this);
2506 for (; i < mDrawingChildren.size(); i++) {
2507 const auto& child = mDrawingChildren[i];
2508 child->traverseInZOrder(exec);
2509 }
2510}
2511
2512/**
2513 * Positively signed children are before 'this' in reverse Z-order.
2514 */
2515void Layer::traverseInReverseZOrder(const std::function<void(Layer*)>& exec) {
2516 int32_t i = 0;
2517 for (i = mDrawingChildren.size()-1; i>=0; i--) {
2518 const auto& child = mDrawingChildren[i];
2519 if (child->getZ() < 0) {
2520 break;
2521 }
2522 child->traverseInReverseZOrder(exec);
2523 }
2524 exec(this);
2525 for (; i>=0; i--) {
2526 const auto& child = mDrawingChildren[i];
2527 child->traverseInReverseZOrder(exec);
2528 }
2529}
2530
2531Transform Layer::getTransform() const {
2532 Transform t;
2533 const auto& p = getParent();
2534 if (p != nullptr) {
2535 t = p->getTransform();
2536 }
2537 return t * getDrawingState().active.transform;
2538}
2539
2540void Layer::commitChildList() {
2541 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2542 const auto& child = mCurrentChildren[i];
2543 child->commitChildList();
2544 }
2545 mDrawingChildren = mCurrentChildren;
2546}
2547
Mathias Agopian13127d82013-03-05 17:47:11 -08002548// ---------------------------------------------------------------------------
2549
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002551
2552#if defined(__gl_h_)
2553#error "don't include gl/gl.h in this file"
2554#endif
2555
2556#if defined(__gl2_h_)
2557#error "don't include gl2/gl2.h in this file"
2558#endif