blob: 295b22982d9d22ccff434c1f7e06895e1d6f8601 [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;
Romain Guyf8b4ca52017-03-16 18:39:20 +0000170 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
Jesse Hall399184a2014-03-03 15:42:54 -0800279void Layer::onSidebandStreamChanged() {
280 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
281 // mSidebandStreamChanged was false
282 mFlinger->signalLayerUpdate();
283 }
284}
285
Mathias Agopian67106042013-03-14 19:18:13 -0700286// called with SurfaceFlinger::mStateLock from the drawing thread after
287// the layer has been remove from the current state list (and just before
288// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800289void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800290 mSurfaceFlingerConsumer->abandon();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700291 for (const auto& child : mCurrentChildren) {
292 child->onRemoved();
293 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700294}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700295
Mathias Agopian13127d82013-03-05 17:47:11 -0800296// ---------------------------------------------------------------------------
297// set-up
298// ---------------------------------------------------------------------------
299
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700300const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800301 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302}
303
Mathias Agopianf9d93272009-06-19 17:00:27 -0700304status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 PixelFormat format, uint32_t flags)
306{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700307 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700308 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700309
310 // never allow a surface larger than what our underlying GL implementation
311 // can handle.
312 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800313 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700314 return BAD_VALUE;
315 }
316
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700317 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700318
Riley Andrews03414a12014-07-01 14:22:59 -0700319 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700320 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700321 mCurrentOpacity = getOpacityForFormat(format);
322
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800323 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
324 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
325 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 return NO_ERROR;
328}
329
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700330sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800331 Mutex::Autolock _l(mLock);
332
333 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700334 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800335
336 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700337
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700338 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800339}
340
Dan Stozab9b08832014-03-13 11:55:57 -0700341sp<IGraphicBufferProducer> Layer::getProducer() const {
342 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700343}
344
Mathias Agopian13127d82013-03-05 17:47:11 -0800345// ---------------------------------------------------------------------------
346// h/w composer set-up
347// ---------------------------------------------------------------------------
348
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800349Rect Layer::getContentCrop() const {
350 // this is the crop rectangle that applies to the buffer
351 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700352 Rect crop;
353 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800354 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700355 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800356 } else if (mActiveBuffer != NULL) {
357 // otherwise we use the whole buffer
358 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700359 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800360 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700361 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700363 return crop;
364}
365
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700366static Rect reduce(const Rect& win, const Region& exclude) {
367 if (CC_LIKELY(exclude.isEmpty())) {
368 return win;
369 }
370 if (exclude.isRect()) {
371 return win.reduce(exclude.getBounds());
372 }
373 return Region(win).subtract(exclude).getBounds();
374}
375
Robert Carr1f0a16a2016-10-24 16:27:39 -0700376Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
377 const Layer::State& s(getDrawingState());
378 Rect win(s.active.w, s.active.h);
379
380 if (!s.crop.isEmpty()) {
381 win.intersect(s.crop, &win);
382 }
383
384 Transform t = getTransform();
385 win = t.transform(win);
386
387 const sp<Layer>& p = getParent();
388 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
389 // When calculating the parent bounds for purposes of clipping,
390 // we don't need to constrain the parent to its transparent region.
391 // The transparent region is an optimization based on the
392 // buffer contents of the layer, but does not affect the space allocated to
393 // it by policy, and thus children should be allowed to extend into the
394 // parent's transparent region. In fact one of the main uses, is to reduce
395 // buffer allocation size in cases where a child window sits behind a main window
396 // (by marking the hole in the parent window as a transparent region)
397 if (p != nullptr) {
398 Rect bounds = p->computeScreenBounds(false);
399 bounds.intersect(win, &win);
400 }
401
402 if (reduceTransparentRegion) {
403 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
404 win = reduce(win, screenTransparentRegion);
405 }
406
407 return win;
408}
409
Mathias Agopian13127d82013-03-05 17:47:11 -0800410Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700411 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700412 return computeBounds(s.activeTransparentRegion);
413}
414
415Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
416 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800417 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700418
419 if (!s.crop.isEmpty()) {
420 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800421 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700422
423 Rect bounds = win;
424 const auto& p = getParent();
425 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800426 // Look in computeScreenBounds recursive call for explanation of
427 // why we pass false here.
428 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700429 }
430
431 Transform t = getTransform();
432 if (p != nullptr) {
433 win = t.transform(win);
434 win.intersect(bounds, &win);
435 win = t.inverse().transform(win);
436 }
437
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700438 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700439 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800440}
441
Robert Carr1f0a16a2016-10-24 16:27:39 -0700442Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700443 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800444 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700445 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800446
447 // apply the projection's clipping to the window crop in
448 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700449 // if there are no window scaling involved, this operation will map to full
450 // pixels in the buffer.
451 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
452 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700453
454 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700455 if (!s.crop.isEmpty()) {
456 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700457 }
458
Robert Carr1f0a16a2016-10-24 16:27:39 -0700459 Transform t = getTransform();
460 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
462 activeCrop.clear();
463 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700464 if (!s.finalCrop.isEmpty()) {
465 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000466 activeCrop.clear();
467 }
468 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700469 return activeCrop;
470}
471
Dan Stoza5a423ea2017-02-16 14:10:39 -0800472FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700473 // the content crop is the area of the content that gets scaled to the
474 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800475 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700476
477 // In addition there is a WM-specified crop we pull from our drawing state.
478 const State& s(getDrawingState());
479
480 // Screen space to make reduction to parent crop clearer.
481 Rect activeCrop = computeInitialCrop(hw);
482 const auto& p = getParent();
483 if (p != nullptr) {
484 auto parentCrop = p->computeInitialCrop(hw);
485 activeCrop.intersect(parentCrop, &activeCrop);
486 }
487 Transform t = getTransform();
488 // Back to layer space to work with the content crop.
489 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800490
Michael Lentine28ea2172014-11-19 18:32:37 -0800491 // This needs to be here as transform.transform(Rect) computes the
492 // transformed rect and then takes the bounding box of the result before
493 // returning. This means
494 // transform.inverse().transform(transform.transform(Rect)) != Rect
495 // in which case we need to make sure the final rect is clipped to the
496 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000497 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
498 activeCrop.clear();
499 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800500
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700501 // subtract the transparent region and snap to the bounds
502 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
503
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000504 // Transform the window crop to match the buffer coordinate system,
505 // which means using the inverse of the current transform set on the
506 // SurfaceFlingerConsumer.
507 uint32_t invTransform = mCurrentTransform;
508 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
509 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700510 * the code below applies the primary display's inverse transform to the
511 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000512 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700513 uint32_t invTransformOrient =
514 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000515 // calculate the inverse transform
516 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
517 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
518 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800519 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000520 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700521 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
522 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800523 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000524
525 int winWidth = s.active.w;
526 int winHeight = s.active.h;
527 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
528 // If the activeCrop has been rotate the ends are rotated but not
529 // the space itself so when transforming ends back we can't rely on
530 // a modification of the axes of rotation. To account for this we
531 // need to reorient the inverse rotation in terms of the current
532 // axes of rotation.
533 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
534 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
535 if (is_h_flipped == is_v_flipped) {
536 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
537 NATIVE_WINDOW_TRANSFORM_FLIP_H;
538 }
539 winWidth = s.active.h;
540 winHeight = s.active.w;
541 }
542 const Rect winCrop = activeCrop.transform(
543 invTransform, s.active.w, s.active.h);
544
545 // below, crop is intersected with winCrop expressed in crop's coordinate space
546 float xScale = crop.getWidth() / float(winWidth);
547 float yScale = crop.getHeight() / float(winHeight);
548
549 float insetL = winCrop.left * xScale;
550 float insetT = winCrop.top * yScale;
551 float insetR = (winWidth - winCrop.right ) * xScale;
552 float insetB = (winHeight - winCrop.bottom) * yScale;
553
554 crop.left += insetL;
555 crop.top += insetT;
556 crop.right -= insetR;
557 crop.bottom -= insetB;
558
Mathias Agopian13127d82013-03-05 17:47:11 -0800559 return crop;
560}
561
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000562#ifdef USE_HWC2
Robert Carrae060832016-11-28 10:51:00 -0800563void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000564#else
565void Layer::setGeometry(
566 const sp<const DisplayDevice>& hw,
567 HWComposer::HWCLayerInterface& layer)
568#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700569{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000570#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571 const auto hwcId = displayDevice->getHwcDisplayId();
572 auto& hwcInfo = mHwcLayers[hwcId];
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000573#else
574 layer.setDefaultState();
575#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700576
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700577 // enable this layer
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000578#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 hwcInfo.forceClientComposition = false;
580
581 if (isSecure() && !displayDevice->isSecure()) {
582 hwcInfo.forceClientComposition = true;
583 }
584
585 auto& hwcLayer = hwcInfo.layer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000586#else
587 layer.setSkip(false);
588
589 if (isSecure() && !hw->isSecure()) {
590 layer.setSkip(true);
591 }
592#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700593
Mathias Agopian13127d82013-03-05 17:47:11 -0800594 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700595 const State& s(getDrawingState());
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000596#ifdef USE_HWC2
David Revemanecf0fa52017-03-03 11:32:44 -0500597 auto blendMode = HWC2::BlendMode::None;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800598 if (!isOpaque(s) || s.alpha != 1.0f) {
David Revemanecf0fa52017-03-03 11:32:44 -0500599 blendMode = mPremultipliedAlpha ?
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 }
David Revemanecf0fa52017-03-03 11:32:44 -0500602 auto error = hwcLayer->setBlendMode(blendMode);
603 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
604 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
605 to_string(error).c_str(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000606#else
607 if (!isOpaque(s) || s.alpha != 0xFF) {
608 layer.setBlending(mPremultipliedAlpha ?
609 HWC_BLENDING_PREMULT :
610 HWC_BLENDING_COVERAGE);
611 }
612#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800613
614 // apply the layer's transform, followed by the display's global transform
615 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700616 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700617 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700618 if (!s.crop.isEmpty()) {
619 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700620 activeCrop = t.transform(activeCrop);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000621#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000622 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000623#else
624 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
625#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000626 activeCrop.clear();
627 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700628 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800629 // This needs to be here as transform.transform(Rect) computes the
630 // transformed rect and then takes the bounding box of the result before
631 // returning. This means
632 // transform.inverse().transform(transform.transform(Rect)) != Rect
633 // in which case we need to make sure the final rect is clipped to the
634 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000635 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
636 activeCrop.clear();
637 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700638 // mark regions outside the crop as transparent
639 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
640 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
641 s.active.w, s.active.h));
642 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
643 activeCrop.left, activeCrop.bottom));
644 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
645 s.active.w, activeCrop.bottom));
646 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700647
648 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700649 if (!s.finalCrop.isEmpty()) {
650 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000651 frame.clear();
652 }
653 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000654#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000655 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
656 frame.clear();
657 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 const Transform& tr(displayDevice->getTransform());
659 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500660 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700661 if (error != HWC2::Error::None) {
662 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
663 mName.string(), transformedFrame.left, transformedFrame.top,
664 transformedFrame.right, transformedFrame.bottom,
665 to_string(error).c_str(), static_cast<int32_t>(error));
666 } else {
667 hwcInfo.displayFrame = transformedFrame;
668 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669
Dan Stoza5a423ea2017-02-16 14:10:39 -0800670 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700672 if (error != HWC2::Error::None) {
673 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
674 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
675 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
676 static_cast<int32_t>(error));
677 } else {
678 hwcInfo.sourceCrop = sourceCrop;
679 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680
681 error = hwcLayer->setPlaneAlpha(s.alpha);
682 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
683 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
684 static_cast<int32_t>(error));
685
Robert Carrae060832016-11-28 10:51:00 -0800686 error = hwcLayer->setZOrder(z);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
Robert Carrae060832016-11-28 10:51:00 -0800688 mName.string(), z, to_string(error).c_str(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500690
691 error = hwcLayer->setInfo(s.type, s.appId);
692 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)",
693 mName.string(), static_cast<int32_t>(error));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000694#else
695 if (!frame.intersect(hw->getViewport(), &frame)) {
696 frame.clear();
697 }
698 const Transform& tr(hw->getTransform());
699 layer.setFrame(tr.transform(frame));
700 layer.setCrop(computeCrop(hw));
701 layer.setPlaneAlpha(s.alpha);
702#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800703
Mathias Agopian29a367b2011-07-12 14:51:45 -0700704 /*
705 * Transformations are applied in this order:
706 * 1) buffer orientation/flip/mirror
707 * 2) state transformation (window manager)
708 * 3) layer orientation (screen orientation)
709 * (NOTE: the matrices are multiplied in reverse order)
710 */
711
712 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700713 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700714
715 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
716 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700717 * the code below applies the primary display's inverse transform to the
718 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700719 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700720 uint32_t invTransform =
721 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700722 // calculate the inverse transform
723 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
724 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
725 NATIVE_WINDOW_TRANSFORM_FLIP_H;
726 }
727 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700728 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700729 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700730
731 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800732 const uint32_t orientation = transform.getOrientation();
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000733#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 if (orientation & Transform::ROT_INVALID) {
735 // we can only handle simple transformation
736 hwcInfo.forceClientComposition = true;
737 } else {
738 auto transform = static_cast<HWC2::Transform>(orientation);
739 auto error = hwcLayer->setTransform(transform);
740 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
741 "%s (%d)", mName.string(), to_string(transform).c_str(),
742 to_string(error).c_str(), static_cast<int32_t>(error));
743 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000744#else
745 if (orientation & Transform::ROT_INVALID) {
746 // we can only handle simple transformation
747 layer.setSkip(true);
748 } else {
749 layer.setTransform(orientation);
750 }
751#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700752}
753
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000754#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755void Layer::forceClientComposition(int32_t hwcId) {
756 if (mHwcLayers.count(hwcId) == 0) {
757 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
758 return;
759 }
760
761 mHwcLayers[hwcId].forceClientComposition = true;
762}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000763#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000765#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
767 // Apply this display's projection's viewport to the visible region
768 // before giving it to the HWC HAL.
769 const Transform& tr = displayDevice->getTransform();
770 const auto& viewport = displayDevice->getViewport();
771 Region visible = tr.transform(visibleRegion.intersect(viewport));
772 auto hwcId = displayDevice->getHwcDisplayId();
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800773 auto& hwcInfo = mHwcLayers[hwcId];
774 auto& hwcLayer = hwcInfo.layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775 auto error = hwcLayer->setVisibleRegion(visible);
776 if (error != HWC2::Error::None) {
777 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
778 to_string(error).c_str(), static_cast<int32_t>(error));
779 visible.dump(LOG_TAG);
780 }
781
782 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
783 if (error != HWC2::Error::None) {
784 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
785 to_string(error).c_str(), static_cast<int32_t>(error));
786 surfaceDamageRegion.dump(LOG_TAG);
787 }
788
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700789 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700791 setCompositionType(hwcId, HWC2::Composition::Sideband);
792 ALOGV("[%s] Requesting Sideband composition", mName.string());
793 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800794 if (error != HWC2::Error::None) {
795 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
796 mName.string(), mSidebandStream->handle(),
797 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700799 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800800 }
801
Dan Stoza0a21df72016-07-20 12:52:38 -0700802 // Client layers
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800803 if (hwcInfo.forceClientComposition ||
Dan Stoza0a21df72016-07-20 12:52:38 -0700804 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700805 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700807 return;
808 }
809
Dan Stoza0a21df72016-07-20 12:52:38 -0700810 // SolidColor layers
811 if (mActiveBuffer == nullptr) {
812 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700813
814 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700815 error = hwcLayer->setColor({0, 0, 0, 255});
816 if (error != HWC2::Error::None) {
817 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
818 to_string(error).c_str(), static_cast<int32_t>(error));
819 }
Dan Stozac6c89542016-07-27 10:16:52 -0700820
821 // Clear out the transform, because it doesn't make sense absent a
822 // source buffer
823 error = hwcLayer->setTransform(HWC2::Transform::None);
824 if (error != HWC2::Error::None) {
825 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
826 to_string(error).c_str(), static_cast<int32_t>(error));
827 }
828
Dan Stoza0a21df72016-07-20 12:52:38 -0700829 return;
830 }
831
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700832 // Device or Cursor layers
833 if (mPotentialCursor) {
834 ALOGV("[%s] Requesting Cursor composition", mName.string());
835 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800836 } else {
837 ALOGV("[%s] Requesting Device composition", mName.string());
838 setCompositionType(hwcId, HWC2::Composition::Device);
839 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700840
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700841 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
842 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
843 if (error != HWC2::Error::None) {
844 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(),
845 mCurrentState.dataSpace, to_string(error).c_str(),
846 static_cast<int32_t>(error));
847 }
848
Chia-I Wu06d63de2017-01-04 14:58:51 +0800849 uint32_t hwcSlot = 0;
850 buffer_handle_t hwcHandle = nullptr;
851 {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800852 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800853 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800854 &hwcSlot, &hwcBuffer);
855 if (hwcBuffer != nullptr) {
856 hwcHandle = hwcBuffer->handle;
857 }
858 }
859
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700860 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
Chia-I Wu06d63de2017-01-04 14:58:51 +0800861 error = hwcLayer->setBuffer(hwcSlot, hwcHandle, acquireFence);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700862 if (error != HWC2::Error::None) {
863 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
864 mActiveBuffer->handle, to_string(error).c_str(),
865 static_cast<int32_t>(error));
866 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800867}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000868#else
869void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
870 HWComposer::HWCLayerInterface& layer) {
871 // we have to set the visible region on every frame because
872 // we currently free it during onLayerDisplayed(), which is called
873 // after HWComposer::commit() -- every frame.
874 // Apply this display's projection's viewport to the visible region
875 // before giving it to the HWC HAL.
876 const Transform& tr = hw->getTransform();
877 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
878 layer.setVisibleRegionScreen(visible);
879 layer.setSurfaceDamage(surfaceDamageRegion);
880 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700881
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000882 if (mSidebandStream.get()) {
883 layer.setSidebandStream(mSidebandStream);
884 } else {
885 // NOTE: buffer can be NULL if the client never drew into this
886 // layer yet, or if we ran out of memory
887 layer.setBuffer(mActiveBuffer);
888 }
889}
890#endif
891
892#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800893void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
894 auto hwcId = displayDevice->getHwcDisplayId();
895 if (mHwcLayers.count(hwcId) == 0 ||
896 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
897 return;
898 }
899
900 // This gives us only the "orientation" component of the transform
901 const State& s(getCurrentState());
902
903 // Apply the layer's transform, followed by the display's global transform
904 // Here we're guaranteed that the layer's transform preserves rects
905 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700906 if (!s.crop.isEmpty()) {
907 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800908 }
909 // Subtract the transparent region and snap to the bounds
910 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700911 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800912 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700913 if (!s.finalCrop.isEmpty()) {
914 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000915 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800916 auto& displayTransform(displayDevice->getTransform());
917 auto position = displayTransform.transform(frame);
918
919 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
920 position.top);
921 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
922 "to (%d, %d): %s (%d)", mName.string(), position.left,
923 position.top, to_string(error).c_str(),
924 static_cast<int32_t>(error));
925}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000926#else
927void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
928 HWComposer::HWCLayerInterface& layer) {
929 int fenceFd = -1;
930
931 // TODO: there is a possible optimization here: we only need to set the
932 // acquire fence the first time a new buffer is acquired on EACH display.
933
934 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
935 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
936 if (fence->isValid()) {
937 fenceFd = fence->dup();
938 if (fenceFd == -1) {
939 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
940 }
941 }
942 }
943 layer.setAcquireFenceFd(fenceFd);
944}
945
946Rect Layer::getPosition(
947 const sp<const DisplayDevice>& hw)
948{
949 // this gives us only the "orientation" component of the transform
950 const State& s(getCurrentState());
951
952 // apply the layer's transform, followed by the display's global transform
953 // here we're guaranteed that the layer's transform preserves rects
954 Rect win(s.active.w, s.active.h);
955 if (!s.crop.isEmpty()) {
956 win.intersect(s.crop, &win);
957 }
958 // subtract the transparent region and snap to the bounds
959 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700960 Rect frame(getTransform().transform(bounds));
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000961 frame.intersect(hw->getViewport(), &frame);
962 if (!s.finalCrop.isEmpty()) {
963 frame.intersect(s.finalCrop, &frame);
964 }
965 const Transform& tr(hw->getTransform());
966 return Rect(tr.transform(frame));
967}
968#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700969
Mathias Agopian13127d82013-03-05 17:47:11 -0800970// ---------------------------------------------------------------------------
971// drawing...
972// ---------------------------------------------------------------------------
973
974void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800975 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800976}
977
Dan Stozac7014012014-02-14 15:03:43 -0800978void Layer::draw(const sp<const DisplayDevice>& hw,
979 bool useIdentityTransform) const {
980 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800981}
982
Dan Stozac7014012014-02-14 15:03:43 -0800983void Layer::draw(const sp<const DisplayDevice>& hw) const {
984 onDraw(hw, Region(hw->bounds()), false);
985}
986
987void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
988 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800990 ATRACE_CALL();
991
Mathias Agopiana67932f2011-04-20 14:20:59 -0700992 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700994 // in fact never been drawn into. This happens frequently with
995 // SurfaceView because the WindowManager can't know when the client
996 // has drawn the first time.
997
998 // If there is nothing under us, we paint the screen in black, otherwise
999 // we just skip this update.
1000
1001 // figure out if there is something below us
1002 Region under;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001003 bool finished = false;
1004 mFlinger->mDrawingState.layersSortedByZ.traverseInZOrder([&](Layer* layer) {
1005 if (finished || layer == static_cast<Layer const*>(this)) {
1006 finished = true;
1007 return;
1008 }
Mathias Agopian42977342012-08-05 00:40:46 -07001009 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Robert Carr1f0a16a2016-10-24 16:27:39 -07001010 });
Mathias Agopian179169e2010-05-06 20:21:45 -07001011 // if not everything below us is covered, we plug the holes!
1012 Region holes(clip.subtract(under));
1013 if (!holes.isEmpty()) {
Fabien Sanglard17487192016-12-07 13:03:32 -08001014 clearWithOpenGL(hw, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -07001015 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 return;
1017 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001018
Andy McFadden97eba892012-12-11 15:21:45 -08001019 // Bind the current buffer to the GL texture, and wait for it to be
1020 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001021 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
1022 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -08001023 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -07001024 // Go ahead and draw the buffer anyway; no matter what we do the screen
1025 // is probably going to have something visibly wrong.
1026 }
1027
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001028 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
1029
Mathias Agopian875d8e12013-06-07 15:35:48 -07001030 RenderEngine& engine(mFlinger->getRenderEngine());
1031
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001032 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -07001033 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -07001034 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -07001035
1036 // Query the texture matrix given our current filtering mode.
1037 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001038 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
1039 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -07001040
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001041 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
1042
1043 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -07001044 * the code below applies the primary display's inverse transform to
1045 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001046 */
1047
1048 // create a 4x4 transform matrix from the display transform flags
1049 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
1050 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
1051 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
1052
1053 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -07001054 uint32_t transform =
1055 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001056 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
1057 tr = tr * rot90;
1058 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
1059 tr = tr * flipH;
1060 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
1061 tr = tr * flipV;
1062
1063 // calculate the inverse
1064 tr = inverse(tr);
1065
1066 // and finally apply it to the original texture matrix
1067 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1068 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1069 }
1070
Jamie Genniscbb1a952012-05-08 17:05:52 -07001071 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -07001072 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
1073 mTexture.setFiltering(useFiltering);
1074 mTexture.setMatrix(textureMatrix);
1075
1076 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001077 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001078 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -07001079 }
Fabien Sanglard85789802016-12-07 13:08:24 -08001080 drawWithOpenGL(hw, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001081 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082}
1083
Mathias Agopian13127d82013-03-05 17:47:11 -08001084
Dan Stozac7014012014-02-14 15:03:43 -08001085void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard17487192016-12-07 13:03:32 -08001086 float red, float green, float blue,
Dan Stozac7014012014-02-14 15:03:43 -08001087 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001088{
Mathias Agopian19733a32013-08-28 18:13:56 -07001089 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001090 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001091 engine.setupFillWithColor(red, green, blue, alpha);
1092 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001093}
1094
1095void Layer::clearWithOpenGL(
Fabien Sanglard17487192016-12-07 13:03:32 -08001096 const sp<const DisplayDevice>& hw) const {
1097 clearWithOpenGL(hw, 0,0,0,0);
Mathias Agopian13127d82013-03-05 17:47:11 -08001098}
1099
Dan Stozac7014012014-02-14 15:03:43 -08001100void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
Fabien Sanglard85789802016-12-07 13:08:24 -08001101 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001102 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001103
Dan Stozac7014012014-02-14 15:03:43 -08001104 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001105
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 /*
1107 * NOTE: the way we compute the texture coordinates here produces
1108 * different results than when we take the HWC path -- in the later case
1109 * the "source crop" is rounded to texel boundaries.
1110 * This can produce significantly different results when the texture
1111 * is scaled by a large amount.
1112 *
1113 * The GL code below is more logical (imho), and the difference with
1114 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001115 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001116 * GL composition when a buffer scaling is applied (maybe with some
1117 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1118 * like more of a hack.
1119 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001120 Rect win(computeBounds());
1121
Robert Carr1f0a16a2016-10-24 16:27:39 -07001122 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -07001123 if (!s.finalCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001124 win = t.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001125 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001126 win.clear();
1127 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001128 win = t.inverse().transform(win);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001129 if (!win.intersect(computeBounds(), &win)) {
1130 win.clear();
1131 }
1132 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001133
Mathias Agopian3f844832013-08-07 21:24:32 -07001134 float left = float(win.left) / float(s.active.w);
1135 float top = float(win.top) / float(s.active.h);
1136 float right = float(win.right) / float(s.active.w);
1137 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001138
Mathias Agopian875d8e12013-06-07 15:35:48 -07001139 // TODO: we probably want to generate the texture coords with the mesh
1140 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001141 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1142 texCoords[0] = vec2(left, 1.0f - top);
1143 texCoords[1] = vec2(left, 1.0f - bottom);
1144 texCoords[2] = vec2(right, 1.0f - bottom);
1145 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001146
Mathias Agopian875d8e12013-06-07 15:35:48 -07001147 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001148 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001149 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001150 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001151}
1152
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001153#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001154void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1155 bool callIntoHwc) {
1156 if (mHwcLayers.count(hwcId) == 0) {
1157 ALOGE("setCompositionType called without a valid HWC layer");
1158 return;
1159 }
1160 auto& hwcInfo = mHwcLayers[hwcId];
1161 auto& hwcLayer = hwcInfo.layer;
1162 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1163 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1164 if (hwcInfo.compositionType != type) {
1165 ALOGV(" actually setting");
1166 hwcInfo.compositionType = type;
1167 if (callIntoHwc) {
1168 auto error = hwcLayer->setCompositionType(type);
1169 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1170 "composition type %s: %s (%d)", mName.string(),
1171 to_string(type).c_str(), to_string(error).c_str(),
1172 static_cast<int32_t>(error));
1173 }
1174 }
1175}
1176
1177HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001178 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1179 // If we're querying the composition type for a display that does not
1180 // have a HWC counterpart, then it will always be Client
1181 return HWC2::Composition::Client;
1182 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001183 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001184 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001185 return HWC2::Composition::Invalid;
1186 }
1187 return mHwcLayers.at(hwcId).compositionType;
1188}
1189
1190void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1191 if (mHwcLayers.count(hwcId) == 0) {
1192 ALOGE("setClearClientTarget called without a valid HWC layer");
1193 return;
1194 }
1195 mHwcLayers[hwcId].clearClientTarget = clear;
1196}
1197
1198bool Layer::getClearClientTarget(int32_t hwcId) const {
1199 if (mHwcLayers.count(hwcId) == 0) {
1200 ALOGE("getClearClientTarget called without a valid HWC layer");
1201 return false;
1202 }
1203 return mHwcLayers.at(hwcId).clearClientTarget;
1204}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001205#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001206
Ruben Brunk1681d952014-06-27 15:51:55 -07001207uint32_t Layer::getProducerStickyTransform() const {
1208 int producerStickyTransform = 0;
1209 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1210 if (ret != OK) {
1211 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1212 strerror(-ret), ret);
1213 return 0;
1214 }
1215 return static_cast<uint32_t>(producerStickyTransform);
1216}
1217
Dan Stozac5da2712016-07-20 15:38:12 -07001218bool Layer::latchUnsignaledBuffers() {
1219 static bool propertyLoaded = false;
1220 static bool latch = false;
1221 static std::mutex mutex;
1222 std::lock_guard<std::mutex> lock(mutex);
1223 if (!propertyLoaded) {
1224 char value[PROPERTY_VALUE_MAX] = {};
1225 property_get("debug.sf.latch_unsignaled", value, "0");
1226 latch = atoi(value);
1227 propertyLoaded = true;
1228 }
1229 return latch;
1230}
1231
Dan Stozacac35382016-01-27 12:21:06 -08001232uint64_t Layer::getHeadFrameNumber() const {
1233 Mutex::Autolock lock(mQueueItemLock);
1234 if (!mQueueItems.empty()) {
1235 return mQueueItems[0].mFrameNumber;
1236 } else {
1237 return mCurrentFrameNumber;
1238 }
1239}
1240
Dan Stoza1ce65812016-06-15 16:26:27 -07001241bool Layer::headFenceHasSignaled() const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001242#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001243 if (latchUnsignaledBuffers()) {
1244 return true;
1245 }
1246
Dan Stoza1ce65812016-06-15 16:26:27 -07001247 Mutex::Autolock lock(mQueueItemLock);
1248 if (mQueueItems.empty()) {
1249 return true;
1250 }
1251 if (mQueueItems[0].mIsDroppable) {
1252 // Even though this buffer's fence may not have signaled yet, it could
1253 // be replaced by another buffer before it has a chance to, which means
1254 // that it's possible to get into a situation where a buffer is never
1255 // able to be latched. To avoid this, grab this buffer anyway.
1256 return true;
1257 }
1258 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001259#else
1260 return true;
1261#endif
Dan Stoza1ce65812016-06-15 16:26:27 -07001262}
1263
Dan Stozacac35382016-01-27 12:21:06 -08001264bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1265 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1266 // Don't bother with a SyncPoint, since we've already latched the
1267 // relevant frame
1268 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001269 }
1270
Dan Stozacac35382016-01-27 12:21:06 -08001271 Mutex::Autolock lock(mLocalSyncPointMutex);
1272 mLocalSyncPoints.push_back(point);
1273 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001274}
1275
Mathias Agopian13127d82013-03-05 17:47:11 -08001276void Layer::setFiltering(bool filtering) {
1277 mFiltering = filtering;
1278}
1279
1280bool Layer::getFiltering() const {
1281 return mFiltering;
1282}
1283
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001284// As documented in libhardware header, formats in the range
1285// 0x100 - 0x1FF are specific to the HAL implementation, and
1286// are known to have no alpha channel
1287// TODO: move definition for device-specific range into
1288// hardware.h, instead of using hard-coded values here.
1289#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1290
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001291bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001292 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1293 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001294 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001295 switch (format) {
1296 case HAL_PIXEL_FORMAT_RGBA_8888:
1297 case HAL_PIXEL_FORMAT_BGRA_8888:
Romain Guyff415142016-12-13 16:51:25 -08001298 case HAL_PIXEL_FORMAT_RGBA_FP16:
Romain Guy541f2262017-02-10 18:50:17 -08001299 case HAL_PIXEL_FORMAT_RGBA_1010102:
Mathias Agopiandd533712013-07-26 15:31:39 -07001300 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001301 }
1302 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001303 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001304}
1305
Mathias Agopian13127d82013-03-05 17:47:11 -08001306// ----------------------------------------------------------------------------
1307// local state
1308// ----------------------------------------------------------------------------
1309
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001310static void boundPoint(vec2* point, const Rect& crop) {
1311 if (point->x < crop.left) {
1312 point->x = crop.left;
1313 }
1314 if (point->x > crop.right) {
1315 point->x = crop.right;
1316 }
1317 if (point->y < crop.top) {
1318 point->y = crop.top;
1319 }
1320 if (point->y > crop.bottom) {
1321 point->y = crop.bottom;
1322 }
1323}
1324
Dan Stozac7014012014-02-14 15:03:43 -08001325void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1326 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001327{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001328 const Layer::State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001329 const Transform hwTransform(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 const uint32_t hw_h = hw->getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001331 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -07001332
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001333 vec2 lt = vec2(win.left, win.top);
1334 vec2 lb = vec2(win.left, win.bottom);
1335 vec2 rb = vec2(win.right, win.bottom);
1336 vec2 rt = vec2(win.right, win.top);
1337
Robert Carr1f0a16a2016-10-24 16:27:39 -07001338 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001339 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001340 lt = layerTransform.transform(lt);
1341 lb = layerTransform.transform(lb);
1342 rb = layerTransform.transform(rb);
1343 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001344 }
1345
Robert Carrb5d3d262016-03-25 15:08:13 -07001346 if (!s.finalCrop.isEmpty()) {
1347 boundPoint(&lt, s.finalCrop);
1348 boundPoint(&lb, s.finalCrop);
1349 boundPoint(&rb, s.finalCrop);
1350 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001351 }
1352
Mathias Agopianff2ed702013-09-01 21:36:12 -07001353 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001354 position[0] = hwTransform.transform(lt);
1355 position[1] = hwTransform.transform(lb);
1356 position[2] = hwTransform.transform(rb);
1357 position[3] = hwTransform.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001358 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001359 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001360 }
1361}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001362
Andy McFadden4125a4f2014-01-29 17:17:11 -08001363bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001364{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001365 // if we don't have a buffer yet, we're translucent regardless of the
1366 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001367 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001368 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001369 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001370
1371 // if the layer has the opaque flag, then we're always opaque,
1372 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001373 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001374}
1375
Dan Stoza23116082015-06-18 14:58:39 -07001376bool Layer::isSecure() const
1377{
1378 const Layer::State& s(mDrawingState);
1379 return (s.flags & layer_state_t::eLayerSecure);
1380}
1381
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001382bool Layer::isProtected() const
1383{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001384 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001385 return (activeBuffer != 0) &&
1386 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1387}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001388
Mathias Agopian13127d82013-03-05 17:47:11 -08001389bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001390 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001391}
1392
1393bool Layer::isCropped() const {
1394 return !mCurrentCrop.isEmpty();
1395}
1396
1397bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1398 return mNeedsFiltering || hw->needsFiltering();
1399}
1400
1401void Layer::setVisibleRegion(const Region& visibleRegion) {
1402 // always called from main thread
1403 this->visibleRegion = visibleRegion;
1404}
1405
1406void Layer::setCoveredRegion(const Region& coveredRegion) {
1407 // always called from main thread
1408 this->coveredRegion = coveredRegion;
1409}
1410
1411void Layer::setVisibleNonTransparentRegion(const Region&
1412 setVisibleNonTransparentRegion) {
1413 // always called from main thread
1414 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1415}
1416
1417// ----------------------------------------------------------------------------
1418// transaction
1419// ----------------------------------------------------------------------------
1420
Dan Stoza7dde5992015-05-22 09:51:44 -07001421void Layer::pushPendingState() {
1422 if (!mCurrentState.modified) {
1423 return;
1424 }
1425
Dan Stoza7dde5992015-05-22 09:51:44 -07001426 // If this transaction is waiting on the receipt of a frame, generate a sync
1427 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -08001428 if (mCurrentState.barrierLayer != nullptr) {
1429 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
1430 if (barrierLayer == nullptr) {
1431 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -07001432 // If we can't promote the layer we are intended to wait on,
1433 // then it is expired or otherwise invalid. Allow this transaction
1434 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -08001435 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001436 } else {
1437 auto syncPoint = std::make_shared<SyncPoint>(
1438 mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -08001439 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -08001440 mRemoteSyncPoints.push_back(std::move(syncPoint));
1441 } else {
1442 // We already missed the frame we're supposed to synchronize
1443 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -08001444 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -08001445 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001446 }
1447
Dan Stoza7dde5992015-05-22 09:51:44 -07001448 // Wake us up to check if the frame has been received
1449 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -07001450 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -07001451 }
1452 mPendingStates.push_back(mCurrentState);
1453}
1454
Pablo Ceballos05289c22016-04-14 15:49:55 -07001455void Layer::popPendingState(State* stateToCommit) {
1456 auto oldFlags = stateToCommit->flags;
1457 *stateToCommit = mPendingStates[0];
1458 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1459 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001460
1461 mPendingStates.removeAt(0);
1462}
1463
Pablo Ceballos05289c22016-04-14 15:49:55 -07001464bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001465 bool stateUpdateAvailable = false;
1466 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -08001467 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001468 if (mRemoteSyncPoints.empty()) {
1469 // If we don't have a sync point for this, apply it anyway. It
1470 // will be visually wrong, but it should keep us from getting
1471 // into too much trouble.
1472 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001473 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001474 stateUpdateAvailable = true;
1475 continue;
1476 }
1477
Dan Stozacac35382016-01-27 12:21:06 -08001478 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1479 mPendingStates[0].frameNumber) {
1480 ALOGE("[%s] Unexpected sync point frame number found",
1481 mName.string());
1482
1483 // Signal our end of the sync point and then dispose of it
1484 mRemoteSyncPoints.front()->setTransactionApplied();
1485 mRemoteSyncPoints.pop_front();
1486 continue;
1487 }
1488
Dan Stoza7dde5992015-05-22 09:51:44 -07001489 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1490 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001491 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001492 stateUpdateAvailable = true;
1493
1494 // Signal our end of the sync point and then dispose of it
1495 mRemoteSyncPoints.front()->setTransactionApplied();
1496 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001497 } else {
1498 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001499 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001500 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001501 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001502 stateUpdateAvailable = true;
1503 }
1504 }
1505
1506 // If we still have pending updates, wake SurfaceFlinger back up and point
1507 // it at this layer so we can process them
1508 if (!mPendingStates.empty()) {
1509 setTransactionFlags(eTransactionNeeded);
1510 mFlinger->setTransactionFlags(eTraversalNeeded);
1511 }
1512
1513 mCurrentState.modified = false;
1514 return stateUpdateAvailable;
1515}
1516
1517void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001518 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001519 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001520 Mutex::Autolock lock(mLocalSyncPointMutex);
1521 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001522 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001523 point->setFrameAvailable();
1524 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001525 }
1526}
1527
Mathias Agopian13127d82013-03-05 17:47:11 -08001528uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001529 ATRACE_CALL();
1530
Dan Stoza7dde5992015-05-22 09:51:44 -07001531 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001532 Layer::State c = getCurrentState();
1533 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001534 return 0;
1535 }
1536
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001537 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001539 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1540 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001541
1542 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001543 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001544 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001545 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001546 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001547 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001548 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001549 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001550 this, getName().string(), mCurrentTransform,
1551 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001552 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001553 c.crop.left,
1554 c.crop.top,
1555 c.crop.right,
1556 c.crop.bottom,
1557 c.crop.getWidth(),
1558 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001559 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001560 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001561 s.crop.left,
1562 s.crop.top,
1563 s.crop.right,
1564 s.crop.bottom,
1565 s.crop.getWidth(),
1566 s.crop.getHeight(),
1567 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001569 // record the new size, form this point on, when the client request
1570 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001571 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001572 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001574
Robert Carr82364e32016-05-15 11:27:47 -07001575 const bool resizePending = (c.requested.w != c.active.w) ||
1576 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001577 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001578 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001579 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001580 // if we have a pending resize, unless we are in fixed-size mode.
1581 // the drawing state will be updated only once we receive a buffer
1582 // with the correct size.
1583 //
1584 // in particular, we want to make sure the clip (which is part
1585 // of the geometry state) is latched together with the size but is
1586 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001587 //
1588 // If a sideband stream is attached, however, we want to skip this
1589 // optimization so that transactions aren't missed when a buffer
1590 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001591
1592 flags |= eDontUpdateGeometryState;
1593 }
1594 }
1595
Mathias Agopian13127d82013-03-05 17:47:11 -08001596 // always set active to requested, unless we're asked not to
1597 // this is used by Layer, which special cases resizes.
1598 if (flags & eDontUpdateGeometryState) {
1599 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001600 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001601 if (mFreezePositionUpdates) {
1602 float tx = c.active.transform.tx();
1603 float ty = c.active.transform.ty();
1604 c.active = c.requested;
1605 c.active.transform.set(tx, ty);
1606 editCurrentState.active = c.active;
1607 } else {
1608 editCurrentState.active = editCurrentState.requested;
1609 c.active = c.requested;
1610 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 }
1612
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001613 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001614 // invalidate and recompute the visible regions if needed
1615 flags |= Layer::eVisibleRegion;
1616 }
1617
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001618 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001619 // invalidate and recompute the visible regions if needed
1620 flags |= eVisibleRegion;
1621 this->contentDirty = true;
1622
1623 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001624 const uint8_t type = c.active.transform.getType();
1625 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001626 (type >= Transform::SCALE));
1627 }
1628
Dan Stozac8145172016-04-28 16:29:06 -07001629 // If the layer is hidden, signal and clear out all local sync points so
1630 // that transactions for layers depending on this layer's frames becoming
1631 // visible are not blocked
1632 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001634 }
1635
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001637 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001638 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639}
1640
Pablo Ceballos05289c22016-04-14 15:49:55 -07001641void Layer::commitTransaction(const State& stateToCommit) {
1642 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001643}
1644
Mathias Agopian13127d82013-03-05 17:47:11 -08001645uint32_t Layer::getTransactionFlags(uint32_t flags) {
1646 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1647}
1648
1649uint32_t Layer::setTransactionFlags(uint32_t flags) {
1650 return android_atomic_or(flags, &mTransactionFlags);
1651}
1652
Robert Carr82364e32016-05-15 11:27:47 -07001653bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001654 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001655 return false;
1656 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001657
1658 // We update the requested and active position simultaneously because
1659 // we want to apply the position portion of the transform matrix immediately,
1660 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001661 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001662 if (immediate && !mFreezePositionUpdates) {
1663 mCurrentState.active.transform.set(x, y);
1664 }
1665 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001666
Dan Stoza7dde5992015-05-22 09:51:44 -07001667 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001668 setTransactionFlags(eTransactionNeeded);
1669 return true;
1670}
Robert Carr82364e32016-05-15 11:27:47 -07001671
Robert Carr1f0a16a2016-10-24 16:27:39 -07001672bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1673 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1674 if (idx < 0) {
1675 return false;
1676 }
1677 if (childLayer->setLayer(z)) {
1678 mCurrentChildren.removeAt(idx);
1679 mCurrentChildren.add(childLayer);
1680 }
1681 return true;
1682}
1683
Robert Carrae060832016-11-28 10:51:00 -08001684bool Layer::setLayer(int32_t z) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001685 if (mCurrentState.z == z)
1686 return false;
1687 mCurrentState.sequence++;
1688 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001689 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001690 setTransactionFlags(eTransactionNeeded);
1691 return true;
1692}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693
Mathias Agopian13127d82013-03-05 17:47:11 -08001694bool Layer::setSize(uint32_t w, uint32_t h) {
1695 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1696 return false;
1697 mCurrentState.requested.w = w;
1698 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001699 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001700 setTransactionFlags(eTransactionNeeded);
1701 return true;
1702}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001703#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08001704bool Layer::setAlpha(float alpha) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001705#else
1706bool Layer::setAlpha(uint8_t alpha) {
1707#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001708 if (mCurrentState.alpha == alpha)
1709 return false;
1710 mCurrentState.sequence++;
1711 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001712 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001713 setTransactionFlags(eTransactionNeeded);
1714 return true;
1715}
1716bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1717 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001718 mCurrentState.requested.transform.set(
Robert Carrcb6e1e32017-02-21 19:48:26 -08001719 matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001720 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001721 setTransactionFlags(eTransactionNeeded);
1722 return true;
1723}
1724bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001725 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001726 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001727 setTransactionFlags(eTransactionNeeded);
1728 return true;
1729}
1730bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1731 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1732 if (mCurrentState.flags == newFlags)
1733 return false;
1734 mCurrentState.sequence++;
1735 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001736 mCurrentState.mask = mask;
1737 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001738 setTransactionFlags(eTransactionNeeded);
1739 return true;
1740}
Robert Carr99e27f02016-06-16 15:18:02 -07001741
1742bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001743 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001744 return false;
1745 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001746 mCurrentState.requestedCrop = crop;
1747 if (immediate) {
1748 mCurrentState.crop = crop;
1749 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001750 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001751 setTransactionFlags(eTransactionNeeded);
1752 return true;
1753}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001754bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001755 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001756 return false;
1757 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001758 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001759 mCurrentState.modified = true;
1760 setTransactionFlags(eTransactionNeeded);
1761 return true;
1762}
Mathias Agopian13127d82013-03-05 17:47:11 -08001763
Robert Carrc3574f72016-03-24 12:19:32 -07001764bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1765 if (scalingMode == mOverrideScalingMode)
1766 return false;
1767 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001768 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001769 return true;
1770}
1771
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001772void Layer::setInfo(uint32_t type, uint32_t appId) {
1773 mCurrentState.appId = appId;
1774 mCurrentState.type = type;
1775 mCurrentState.modified = true;
1776 setTransactionFlags(eTransactionNeeded);
1777}
1778
Robert Carrc3574f72016-03-24 12:19:32 -07001779uint32_t Layer::getEffectiveScalingMode() const {
1780 if (mOverrideScalingMode >= 0) {
1781 return mOverrideScalingMode;
1782 }
1783 return mCurrentScalingMode;
1784}
1785
Mathias Agopian13127d82013-03-05 17:47:11 -08001786bool Layer::setLayerStack(uint32_t layerStack) {
1787 if (mCurrentState.layerStack == layerStack)
1788 return false;
1789 mCurrentState.sequence++;
1790 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001791 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001792 setTransactionFlags(eTransactionNeeded);
1793 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001794}
1795
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001796bool Layer::setDataSpace(android_dataspace dataSpace) {
1797 if (mCurrentState.dataSpace == dataSpace)
1798 return false;
1799 mCurrentState.sequence++;
1800 mCurrentState.dataSpace = dataSpace;
1801 mCurrentState.modified = true;
1802 setTransactionFlags(eTransactionNeeded);
1803 return true;
1804}
1805
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806uint32_t Layer::getLayerStack() const {
1807 auto p = getParent();
1808 if (p == nullptr) {
1809 return getDrawingState().layerStack;
1810 }
1811 return p->getLayerStack();
1812}
1813
Robert Carr0d480722017-01-10 16:42:54 -08001814void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer,
Dan Stoza7dde5992015-05-22 09:51:44 -07001815 uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001816 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001817 mCurrentState.frameNumber = frameNumber;
1818 // We don't set eTransactionNeeded, because just receiving a deferral
1819 // request without any other state updates shouldn't actually induce a delay
1820 mCurrentState.modified = true;
1821 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001822 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001823 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001824 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001825 ALOGE("Deferred transaction");
1826}
1827
1828void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle,
1829 uint64_t frameNumber) {
1830 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1831 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001832}
1833
Dan Stozaee44edd2015-03-23 15:50:23 -07001834void Layer::useSurfaceDamage() {
1835 if (mFlinger->mForceFullDamage) {
1836 surfaceDamageRegion = Region::INVALID_REGION;
1837 } else {
1838 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1839 }
1840}
1841
1842void Layer::useEmptyDamage() {
1843 surfaceDamageRegion.clear();
1844}
1845
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846// ----------------------------------------------------------------------------
1847// pageflip handling...
1848// ----------------------------------------------------------------------------
1849
Dan Stoza6b9454d2014-11-07 16:00:59 -08001850bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001851 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001852 return true;
1853 }
1854
Dan Stoza6b9454d2014-11-07 16:00:59 -08001855 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001856 if (mQueueItems.empty()) {
1857 return false;
1858 }
1859 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001860 nsecs_t expectedPresent =
1861 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001862
1863 // Ignore timestamps more than a second in the future
1864 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1865 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1866 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1867 expectedPresent);
1868
1869 bool isDue = timestamp < expectedPresent;
1870 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001871}
1872
Brian Andersond6927fb2016-07-23 23:37:30 -07001873bool Layer::onPreComposition(nsecs_t refreshStartTime) {
1874 if (mBufferLatched) {
1875 Mutex::Autolock lock(mFrameEventHistoryMutex);
1876 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
1877 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001878 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001879 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001880}
1881
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001882bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001883 const std::shared_ptr<FenceTime>& presentFence,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001884 const std::shared_ptr<FenceTime>& retireFence,
1885 const CompositorTiming& compositorTiming) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001886 mAcquireTimeline.updateSignalTimes();
1887 mReleaseTimeline.updateSignalTimes();
1888
Brian Andersond6927fb2016-07-23 23:37:30 -07001889 // mFrameLatencyNeeded is true when a new frame was latched for the
1890 // composition.
Fabien Sanglard28e98082016-12-05 10:19:46 -08001891 if (!mFrameLatencyNeeded)
1892 return false;
1893
Fabien Sanglard28e98082016-12-05 10:19:46 -08001894 // Update mFrameEventHistory.
1895 {
1896 Mutex::Autolock lock(mFrameEventHistoryMutex);
1897 mFrameEventHistory.addPostComposition(mCurrentFrameNumber,
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001898 glDoneFence, presentFence, compositorTiming);
Brian Anderson8cc8b102016-10-21 12:43:09 -07001899 if (mPreviousFrameNumber != 0) {
1900 mFrameEventHistory.addRetire(mPreviousFrameNumber,
1901 retireFence);
1902 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001903 }
Fabien Sanglard28e98082016-12-05 10:19:46 -08001904
1905 // Update mFrameTracker.
1906 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
1907 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1908
Brian Anderson3d4039d2016-09-23 16:31:30 -07001909 std::shared_ptr<FenceTime> frameReadyFence =
1910 mSurfaceFlingerConsumer->getCurrentFenceTime();
Fabien Sanglard28e98082016-12-05 10:19:46 -08001911 if (frameReadyFence->isValid()) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07001912 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001913 } else {
1914 // There was no fence for this frame, so assume that it was ready
1915 // to be presented at the desired present time.
1916 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1917 }
1918
Brian Anderson3d4039d2016-09-23 16:31:30 -07001919 if (presentFence->isValid()) {
1920 mFrameTracker.setActualPresentFence(
1921 std::shared_ptr<FenceTime>(presentFence));
1922 } else if (retireFence->isValid()) {
1923 mFrameTracker.setActualPresentFence(
1924 std::shared_ptr<FenceTime>(retireFence));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001925 } else {
1926 // The HWC doesn't support present fences, so use the refresh
1927 // timestamp instead.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001928 mFrameTracker.setActualPresentTime(
1929 mFlinger->getHwComposer().getRefreshTimestamp(
1930 HWC_DISPLAY_PRIMARY));
Fabien Sanglard28e98082016-12-05 10:19:46 -08001931 }
1932
1933 mFrameTracker.advanceFrame();
1934 mFrameLatencyNeeded = false;
1935 return true;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001936}
1937
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001938#ifdef USE_HWC2
Brian Andersonf6386862016-10-31 16:34:13 -07001939void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Brian Anderson5ea5e592016-12-01 16:54:33 -08001940 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
1941 return;
1942 }
1943
Brian Anderson3d4039d2016-09-23 16:31:30 -07001944 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07001945 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07001946 mReleaseTimeline.push(releaseFenceTime);
1947
1948 Mutex::Autolock lock(mFrameEventHistoryMutex);
Brian Anderson8cc8b102016-10-21 12:43:09 -07001949 if (mPreviousFrameNumber != 0) {
1950 mFrameEventHistory.addRelease(mPreviousFrameNumber,
1951 dequeueReadyTime, std::move(releaseFenceTime));
1952 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001953}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001954#endif
Dan Stoza9e56aa02015-11-02 13:00:03 -08001955
Robert Carr1f0a16a2016-10-24 16:27:39 -07001956bool Layer::isHiddenByPolicy() const {
1957 const Layer::State& s(mDrawingState);
1958 const auto& parent = getParent();
1959 if (parent != nullptr && parent->isHiddenByPolicy()) {
1960 return true;
1961 }
1962 return s.flags & layer_state_t::eLayerHidden;
1963}
1964
Mathias Agopianda27af92012-09-13 18:17:13 -07001965bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001966 const Layer::State& s(mDrawingState);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001967#ifdef USE_HWC2
Robert Carr1f0a16a2016-10-24 16:27:39 -07001968 return !(isHiddenByPolicy()) && s.alpha > 0.0f
Dan Stoza9e56aa02015-11-02 13:00:03 -08001969 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001970#else
Robert Carr1f0a16a2016-10-24 16:27:39 -07001971 return !(isHiddenByPolicy()) && s.alpha
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001972 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1973#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001974}
1975
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07001976bool Layer::allTransactionsSignaled() {
1977 auto headFrameNumber = getHeadFrameNumber();
1978 bool matchingFramesFound = false;
1979 bool allTransactionsApplied = true;
1980 Mutex::Autolock lock(mLocalSyncPointMutex);
1981
1982 for (auto& point : mLocalSyncPoints) {
1983 if (point->getFrameNumber() > headFrameNumber) {
1984 break;
1985 }
1986 matchingFramesFound = true;
1987
1988 if (!point->frameIsAvailable()) {
1989 // We haven't notified the remote layer that the frame for
1990 // this point is available yet. Notify it now, and then
1991 // abort this attempt to latch.
1992 point->setFrameAvailable();
1993 allTransactionsApplied = false;
1994 break;
1995 }
1996
1997 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
1998 }
1999 return !matchingFramesFound || allTransactionsApplied;
2000}
2001
Brian Andersond6927fb2016-07-23 23:37:30 -07002002Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002003{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08002004 ATRACE_CALL();
2005
Jesse Hall399184a2014-03-03 15:42:54 -08002006 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
2007 // mSidebandStreamChanged was true
2008 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07002009 if (mSidebandStream != NULL) {
2010 setTransactionFlags(eTransactionNeeded);
2011 mFlinger->setTransactionFlags(eTraversalNeeded);
2012 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07002013 recomputeVisibleRegions = true;
2014
2015 const State& s(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002016 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08002017 }
2018
Mathias Agopian4fec8732012-06-29 14:12:52 -07002019 Region outDirtyRegion;
Fabien Sanglard223eb912016-10-13 10:15:06 -07002020 if (mQueuedFrames <= 0 && !mAutoRefresh) {
2021 return outDirtyRegion;
2022 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002023
Fabien Sanglard223eb912016-10-13 10:15:06 -07002024 // if we've already called updateTexImage() without going through
2025 // a composition step, we have to skip this layer at this point
2026 // because we cannot call updateTeximage() without a corresponding
2027 // compositionComplete() call.
2028 // we'll trigger an update in onPreComposition().
2029 if (mRefreshPending) {
2030 return outDirtyRegion;
2031 }
2032
2033 // If the head buffer's acquire fence hasn't signaled yet, return and
2034 // try again later
2035 if (!headFenceHasSignaled()) {
2036 mFlinger->signalLayerUpdate();
2037 return outDirtyRegion;
2038 }
2039
2040 // Capture the old state of the layer for comparisons later
2041 const State& s(getDrawingState());
2042 const bool oldOpacity = isOpaque(s);
2043 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
2044
Fabien Sanglardcd6fd542016-10-13 12:47:39 -07002045 if (!allTransactionsSignaled()) {
Fabien Sanglard223eb912016-10-13 10:15:06 -07002046 mFlinger->signalLayerUpdate();
2047 return outDirtyRegion;
2048 }
2049
2050 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2051 // of the buffer queue isn't modified when the buffer queue is returning
2052 // BufferItem's that weren't actually queued. This can happen in shared
2053 // buffer mode.
2054 bool queuedBuffer = false;
Fabien Sanglard7b1563a2016-10-13 12:05:28 -07002055 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
2056 getProducerStickyTransform() != 0, mName.string(),
2057 mOverrideScalingMode, mFreezePositionUpdates);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002058 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
2059 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
2060 mLastFrameNumberReceived);
2061 if (updateResult == BufferQueue::PRESENT_LATER) {
2062 // Producer doesn't want buffer to be displayed yet. Signal a
2063 // layer update so we check again at the next opportunity.
2064 mFlinger->signalLayerUpdate();
2065 return outDirtyRegion;
2066 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2067 // If the buffer has been rejected, remove it from the shadow queue
2068 // and return early
2069 if (queuedBuffer) {
2070 Mutex::Autolock lock(mQueueItemLock);
2071 mQueueItems.removeAt(0);
2072 android_atomic_dec(&mQueuedFrames);
2073 }
2074 return outDirtyRegion;
2075 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2076 // This can occur if something goes wrong when trying to create the
2077 // EGLImage for this buffer. If this happens, the buffer has already
2078 // been released, so we need to clean up the queue and bug out
2079 // early.
2080 if (queuedBuffer) {
2081 Mutex::Autolock lock(mQueueItemLock);
2082 mQueueItems.clear();
2083 android_atomic_and(0, &mQueuedFrames);
Mathias Agopian702634a2012-05-23 17:50:31 -07002084 }
2085
Fabien Sanglard223eb912016-10-13 10:15:06 -07002086 // Once we have hit this state, the shadow queue may no longer
2087 // correctly reflect the incoming BufferQueue's contents, so even if
2088 // updateTexImage starts working, the only safe course of action is
2089 // to continue to ignore updates.
2090 mUpdateTexImageFailed = true;
2091
2092 return outDirtyRegion;
2093 }
2094
2095 if (queuedBuffer) {
2096 // Autolock scope
2097 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2098
2099 Mutex::Autolock lock(mQueueItemLock);
2100
2101 // Remove any stale buffers that have been dropped during
2102 // updateTexImage
2103 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2104 mQueueItems.removeAt(0);
2105 android_atomic_dec(&mQueuedFrames);
2106 }
2107
2108 mQueueItems.removeAt(0);
2109 }
2110
2111
2112 // Decrement the queued-frames count. Signal another event if we
2113 // have more frames pending.
2114 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
2115 || mAutoRefresh) {
2116 mFlinger->signalLayerUpdate();
2117 }
2118
Fabien Sanglard223eb912016-10-13 10:15:06 -07002119 // update the active buffer
Chia-I Wu06d63de2017-01-04 14:58:51 +08002120 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(
2121 &mActiveBufferSlot);
Fabien Sanglard223eb912016-10-13 10:15:06 -07002122 if (mActiveBuffer == NULL) {
2123 // this can only happen if the very first buffer was rejected.
2124 return outDirtyRegion;
2125 }
2126
Brian Andersond6927fb2016-07-23 23:37:30 -07002127 mBufferLatched = true;
2128 mPreviousFrameNumber = mCurrentFrameNumber;
2129 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2130
2131 {
2132 Mutex::Autolock lock(mFrameEventHistoryMutex);
2133 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
2134#ifndef USE_HWC2
Brian Anderson3d4039d2016-09-23 16:31:30 -07002135 auto releaseFenceTime = std::make_shared<FenceTime>(
Brian Andersond6927fb2016-07-23 23:37:30 -07002136 mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
Brian Anderson3d4039d2016-09-23 16:31:30 -07002137 mReleaseTimeline.push(releaseFenceTime);
Brian Anderson8cc8b102016-10-21 12:43:09 -07002138 if (mPreviousFrameNumber != 0) {
2139 mFrameEventHistory.addRelease(mPreviousFrameNumber,
2140 latchTime, std::move(releaseFenceTime));
2141 }
Brian Andersond6927fb2016-07-23 23:37:30 -07002142#endif
2143 }
2144
Fabien Sanglard223eb912016-10-13 10:15:06 -07002145 mRefreshPending = true;
2146 mFrameLatencyNeeded = true;
2147 if (oldActiveBuffer == NULL) {
2148 // the first time we receive a buffer, we need to trigger a
2149 // geometry invalidation.
2150 recomputeVisibleRegions = true;
2151 }
2152
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07002153 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
2154
Fabien Sanglard223eb912016-10-13 10:15:06 -07002155 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2156 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2157 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
2158 if ((crop != mCurrentCrop) ||
2159 (transform != mCurrentTransform) ||
2160 (scalingMode != mCurrentScalingMode))
2161 {
2162 mCurrentCrop = crop;
2163 mCurrentTransform = transform;
2164 mCurrentScalingMode = scalingMode;
2165 recomputeVisibleRegions = true;
2166 }
2167
2168 if (oldActiveBuffer != NULL) {
2169 uint32_t bufWidth = mActiveBuffer->getWidth();
2170 uint32_t bufHeight = mActiveBuffer->getHeight();
2171 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2172 bufHeight != uint32_t(oldActiveBuffer->height)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002173 recomputeVisibleRegions = true;
2174 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002175 }
Mathias Agopian702634a2012-05-23 17:50:31 -07002176
Fabien Sanglard223eb912016-10-13 10:15:06 -07002177 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
2178 if (oldOpacity != isOpaque(s)) {
2179 recomputeVisibleRegions = true;
2180 }
Dan Stozacac35382016-01-27 12:21:06 -08002181
Fabien Sanglard223eb912016-10-13 10:15:06 -07002182 // Remove any sync points corresponding to the buffer which was just
2183 // latched
2184 {
2185 Mutex::Autolock lock(mLocalSyncPointMutex);
2186 auto point = mLocalSyncPoints.begin();
2187 while (point != mLocalSyncPoints.end()) {
2188 if (!(*point)->frameIsAvailable() ||
2189 !(*point)->transactionIsApplied()) {
2190 // This sync point must have been added since we started
2191 // latching. Don't drop it yet.
2192 ++point;
2193 continue;
2194 }
2195
2196 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2197 point = mLocalSyncPoints.erase(point);
2198 } else {
2199 ++point;
Dan Stozacac35382016-01-27 12:21:06 -08002200 }
2201 }
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002202 }
Fabien Sanglard223eb912016-10-13 10:15:06 -07002203
2204 // FIXME: postedRegion should be dirty & bounds
2205 Region dirtyRegion(Rect(s.active.w, s.active.h));
2206
2207 // transform the dirty region to window-manager space
Robert Carr1f0a16a2016-10-24 16:27:39 -07002208 outDirtyRegion = (getTransform().transform(dirtyRegion));
Fabien Sanglard223eb912016-10-13 10:15:06 -07002209
Mathias Agopian4fec8732012-06-29 14:12:52 -07002210 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002211}
2212
Mathias Agopiana67932f2011-04-20 14:20:59 -07002213uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002214{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002215 // TODO: should we do something special if mSecure is set?
2216 if (mProtectedByApp) {
2217 // need a hardware-protected path to external video sink
2218 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002219 }
Riley Andrews03414a12014-07-01 14:22:59 -07002220 if (mPotentialCursor) {
2221 usage |= GraphicBuffer::USAGE_CURSOR;
2222 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002223 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002224 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002225}
2226
Mathias Agopian84300952012-11-21 16:02:13 -08002227void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002228 uint32_t orientation = 0;
2229 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002230 // The transform hint is used to improve performance, but we can
2231 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002232 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002233 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002234 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002235 if (orientation & Transform::ROT_INVALID) {
2236 orientation = 0;
2237 }
2238 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002239 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002240}
2241
Mathias Agopian13127d82013-03-05 17:47:11 -08002242// ----------------------------------------------------------------------------
2243// debugging
2244// ----------------------------------------------------------------------------
2245
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002246void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002247{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002248 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002249
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002250 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002251 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002252 "+ %s %p (%s)\n",
2253 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002254 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002255
Mathias Agopian2ca79392013-04-02 18:30:32 -07002256 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002257 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002258 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002259 sp<Client> client(mClientRef.promote());
2260
Mathias Agopian74d211a2013-04-22 16:55:35 +02002261 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002262 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2263 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002264 "isOpaque=%1d, invalidate=%1d, "
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002265#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -08002266 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002267#else
2268 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2269#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002270 " client=%p\n",
Robert Carr1f0a16a2016-10-24 16:27:39 -07002271 getLayerStack(), s.z,
2272 s.active.transform.tx(), s.active.transform.ty(),
2273 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07002274 s.crop.left, s.crop.top,
2275 s.crop.right, s.crop.bottom,
2276 s.finalCrop.left, s.finalCrop.top,
2277 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002278 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002279 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002280 s.active.transform[0][0], s.active.transform[0][1],
2281 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002282 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002283
2284 sp<const GraphicBuffer> buf0(mActiveBuffer);
2285 uint32_t w0=0, h0=0, s0=0, f0=0;
2286 if (buf0 != 0) {
2287 w0 = buf0->getWidth();
2288 h0 = buf0->getHeight();
2289 s0 = buf0->getStride();
2290 f0 = buf0->format;
2291 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002292 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002293 " "
2294 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2295 " queued-frames=%d, mRefreshPending=%d\n",
2296 mFormat, w0, h0, s0,f0,
2297 mQueuedFrames, mRefreshPending);
2298
Mathias Agopian13127d82013-03-05 17:47:11 -08002299 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002300 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002301 }
2302}
2303
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002304#ifdef USE_HWC2
Dan Stozae22aec72016-08-01 13:20:59 -07002305void Layer::miniDumpHeader(String8& result) {
2306 result.append("----------------------------------------");
2307 result.append("---------------------------------------\n");
2308 result.append(" Layer name\n");
2309 result.append(" Z | ");
2310 result.append(" Comp Type | ");
2311 result.append(" Disp Frame (LTRB) | ");
2312 result.append(" Source Crop (LTRB)\n");
2313 result.append("----------------------------------------");
2314 result.append("---------------------------------------\n");
2315}
2316
2317void Layer::miniDump(String8& result, int32_t hwcId) const {
2318 if (mHwcLayers.count(hwcId) == 0) {
2319 return;
2320 }
2321
2322 String8 name;
2323 if (mName.length() > 77) {
2324 std::string shortened;
2325 shortened.append(mName.string(), 36);
2326 shortened.append("[...]");
2327 shortened.append(mName.string() + (mName.length() - 36), 36);
2328 name = shortened.c_str();
2329 } else {
2330 name = mName;
2331 }
2332
2333 result.appendFormat(" %s\n", name.string());
2334
2335 const Layer::State& layerState(getDrawingState());
2336 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2337 result.appendFormat(" %10u | ", layerState.z);
2338 result.appendFormat("%10s | ",
2339 to_string(getCompositionType(hwcId)).c_str());
2340 const Rect& frame = hwcInfo.displayFrame;
2341 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2342 frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08002343 const FloatRect& crop = hwcInfo.sourceCrop;
Dan Stozae22aec72016-08-01 13:20:59 -07002344 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2345 crop.right, crop.bottom);
2346
2347 result.append("- - - - - - - - - - - - - - - - - - - - ");
2348 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2349}
Fabien Sanglard9d96de42016-10-11 00:15:18 +00002350#endif
Dan Stozae22aec72016-08-01 13:20:59 -07002351
Svetoslavd85084b2014-03-20 10:28:31 -07002352void Layer::dumpFrameStats(String8& result) const {
2353 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002354}
2355
Svetoslavd85084b2014-03-20 10:28:31 -07002356void Layer::clearFrameStats() {
2357 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002358}
2359
Jamie Gennis6547ff42013-07-16 20:12:42 -07002360void Layer::logFrameStats() {
2361 mFrameTracker.logAndResetStats(mName);
2362}
2363
Svetoslavd85084b2014-03-20 10:28:31 -07002364void Layer::getFrameStats(FrameStats* outStats) const {
2365 mFrameTracker.getStats(outStats);
2366}
2367
Brian Andersond6927fb2016-07-23 23:37:30 -07002368void Layer::dumpFrameEvents(String8& result) {
2369 result.appendFormat("- Layer %s (%s, %p)\n",
2370 getName().string(), getTypeId(), this);
2371 Mutex::Autolock lock(mFrameEventHistoryMutex);
2372 mFrameEventHistory.checkFencesForCompletion();
2373 mFrameEventHistory.dump(result);
2374}
Pablo Ceballos40845df2016-01-25 17:41:15 -08002375
Brian Anderson5ea5e592016-12-01 16:54:33 -08002376void Layer::onDisconnect() {
2377 Mutex::Autolock lock(mFrameEventHistoryMutex);
2378 mFrameEventHistory.onDisconnect();
2379}
2380
Brian Anderson3890c392016-07-25 12:48:08 -07002381void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2382 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07002383 Mutex::Autolock lock(mFrameEventHistoryMutex);
2384 if (newTimestamps) {
Brian Anderson3d4039d2016-09-23 16:31:30 -07002385 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07002386 mFrameEventHistory.addQueue(*newTimestamps);
2387 }
2388
Brian Anderson3890c392016-07-25 12:48:08 -07002389 if (outDelta) {
2390 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07002391 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002392}
Dan Stozae77c7662016-05-13 11:37:28 -07002393
2394std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2395 bool forceFlush) {
2396 std::vector<OccupancyTracker::Segment> history;
2397 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2398 &history);
2399 if (result != NO_ERROR) {
2400 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2401 result);
2402 return {};
2403 }
2404 return history;
2405}
2406
Robert Carr367c5682016-06-20 11:55:28 -07002407bool Layer::getTransformToDisplayInverse() const {
2408 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2409}
2410
Robert Carr1f0a16a2016-10-24 16:27:39 -07002411void Layer::addChild(const sp<Layer>& layer) {
2412 mCurrentChildren.add(layer);
2413 layer->setParent(this);
2414}
2415
2416ssize_t Layer::removeChild(const sp<Layer>& layer) {
2417 layer->setParent(nullptr);
2418 return mCurrentChildren.remove(layer);
2419}
2420
Robert Carr1db73f62016-12-21 12:58:51 -08002421bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
2422 sp<Handle> handle = nullptr;
2423 sp<Layer> newParent = nullptr;
2424 if (newParentHandle == nullptr) {
2425 return false;
2426 }
2427 handle = static_cast<Handle*>(newParentHandle.get());
2428 newParent = handle->owner.promote();
2429 if (newParent == nullptr) {
2430 ALOGE("Unable to promote Layer handle");
2431 return false;
2432 }
2433
2434 for (const sp<Layer>& child : mCurrentChildren) {
2435 newParent->addChild(child);
2436
2437 sp<Client> client(child->mClientRef.promote());
2438 if (client != nullptr) {
2439 client->setParentLayer(newParent);
2440 }
2441 }
2442 mCurrentChildren.clear();
2443
2444 return true;
2445}
2446
Robert Carr9524cb32017-02-13 11:32:32 -08002447bool Layer::detachChildren() {
2448 traverseInZOrder([this](Layer* child) {
2449 if (child == this) {
2450 return;
2451 }
2452
2453 sp<Client> client(child->mClientRef.promote());
2454 if (client != nullptr) {
2455 client->detachLayer(child);
2456 }
2457 });
2458
2459 return true;
2460}
2461
Robert Carr1f0a16a2016-10-24 16:27:39 -07002462void Layer::setParent(const sp<Layer>& layer) {
2463 mParent = layer;
2464}
2465
2466void Layer::clearSyncPoints() {
2467 for (const auto& child : mCurrentChildren) {
2468 child->clearSyncPoints();
2469 }
2470
2471 Mutex::Autolock lock(mLocalSyncPointMutex);
2472 for (auto& point : mLocalSyncPoints) {
2473 point->setFrameAvailable();
2474 }
2475 mLocalSyncPoints.clear();
2476}
2477
2478int32_t Layer::getZ() const {
2479 return mDrawingState.z;
2480}
2481
2482/**
2483 * Negatively signed children are before 'this' in Z-order.
2484 */
2485void Layer::traverseInZOrder(const std::function<void(Layer*)>& exec) {
2486 size_t i = 0;
2487 for (; i < mDrawingChildren.size(); i++) {
2488 const auto& child = mDrawingChildren[i];
2489 if (child->getZ() >= 0)
2490 break;
2491 child->traverseInZOrder(exec);
2492 }
2493 exec(this);
2494 for (; i < mDrawingChildren.size(); i++) {
2495 const auto& child = mDrawingChildren[i];
2496 child->traverseInZOrder(exec);
2497 }
2498}
2499
2500/**
2501 * Positively signed children are before 'this' in reverse Z-order.
2502 */
2503void Layer::traverseInReverseZOrder(const std::function<void(Layer*)>& exec) {
2504 int32_t i = 0;
2505 for (i = mDrawingChildren.size()-1; i>=0; i--) {
2506 const auto& child = mDrawingChildren[i];
2507 if (child->getZ() < 0) {
2508 break;
2509 }
2510 child->traverseInReverseZOrder(exec);
2511 }
2512 exec(this);
2513 for (; i>=0; i--) {
2514 const auto& child = mDrawingChildren[i];
2515 child->traverseInReverseZOrder(exec);
2516 }
2517}
2518
2519Transform Layer::getTransform() const {
2520 Transform t;
2521 const auto& p = getParent();
2522 if (p != nullptr) {
2523 t = p->getTransform();
2524 }
2525 return t * getDrawingState().active.transform;
2526}
2527
2528void Layer::commitChildList() {
2529 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2530 const auto& child = mCurrentChildren[i];
2531 child->commitChildList();
2532 }
2533 mDrawingChildren = mCurrentChildren;
2534}
2535
Mathias Agopian13127d82013-03-05 17:47:11 -08002536// ---------------------------------------------------------------------------
2537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002539
2540#if defined(__gl_h_)
2541#error "don't include gl/gl.h in this file"
2542#endif
2543
2544#if defined(__gl2_h_)
2545#error "don't include gl2/gl2.h in this file"
2546#endif