blob: 9350fa52ff57c5a7f19fec8e2577b4882d5a8d22 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Dan Stoza6b9454d2014-11-07 16:00:59 -080040#include <gui/BufferItem.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020044#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include "Layer.h"
Dan Stozab9b08832014-03-13 11:55:57 -070047#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian1b031492012-06-20 17:51:20 -070050#include "DisplayHardware/HWComposer.h"
51
Mathias Agopian875d8e12013-06-07 15:35:48 -070052#include "RenderEngine/RenderEngine.h"
53
Dan Stozac5da2712016-07-20 15:38:12 -070054#include <mutex>
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#define DEBUG_RESIZE 0
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058namespace android {
59
60// ---------------------------------------------------------------------------
61
Mathias Agopian13127d82013-03-05 17:47:11 -080062int32_t Layer::sSequence = 1;
63
Mathias Agopian4d9b8222013-03-12 17:11:48 -070064Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
65 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080066 : contentDirty(false),
67 sequence(uint32_t(android_atomic_inc(&sSequence))),
68 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070069 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mPremultipliedAlpha(true),
71 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070074 mPendingStateMutex(),
75 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080077 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070079 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070080 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080082 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080083 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080084 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080085 mFiltering(false),
86 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070087 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
89 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070090 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070091 mPotentialCursor(false),
92 mQueueItemLock(),
93 mQueueItemCondition(),
94 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070095 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080096 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -070097 mAutoRefresh(false),
98 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100 ALOGV("Creating Layer %s", name.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101
Mathias Agopiana67932f2011-04-20 14:20:59 -0700102 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700103 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700104 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
106 uint32_t layerFlags = 0;
107 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800108 layerFlags |= layer_state_t::eLayerHidden;
109 if (flags & ISurfaceComposerClient::eOpaque)
110 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700111 if (flags & ISurfaceComposerClient::eSecure)
112 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 if (flags & ISurfaceComposerClient::eNonPremultiplied)
115 mPremultipliedAlpha = false;
116
117 mName = name;
118
119 mCurrentState.active.w = w;
120 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800121 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700122 mCurrentState.crop.makeInvalid();
123 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800125 mCurrentState.alpha = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.layerStack = 0;
127 mCurrentState.flags = layerFlags;
128 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129 mCurrentState.requested = mCurrentState.active;
130
131 // drawing state & current state are identical
132 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700133
Dan Stoza9e56aa02015-11-02 13:00:03 -0800134 const auto& hwc = flinger->getHwComposer();
135 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
136 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700137 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800138}
139
Mathias Agopian3f844832013-08-07 21:24:32 -0700140void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800141 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700142 sp<IGraphicBufferProducer> producer;
143 sp<IGraphicBufferConsumer> consumer;
Irvel468051e2016-06-13 16:44:44 -0700144 BufferQueue::createBufferQueue(&producer, &consumer, nullptr, true);
Dan Stozab9b08832014-03-13 11:55:57 -0700145 mProducer = new MonitoredProducer(producer, mFlinger);
Irvel468051e2016-06-13 16:44:44 -0700146 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800147 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800148 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700149 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800150
Dan Stozac9d97202016-03-03 08:20:27 -0800151#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700152 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800153#endif
Andy McFadden69052052012-09-14 16:10:11 -0700154
Mathias Agopian84300952012-11-21 16:02:13 -0800155 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
156 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700157}
158
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700159Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800160 sp<Client> c(mClientRef.promote());
161 if (c != 0) {
162 c->detachLayer(this);
163 }
164
Dan Stozacac35382016-01-27 12:21:06 -0800165 for (auto& point : mRemoteSyncPoints) {
166 point->setTransactionApplied();
167 }
Dan Stozac8145172016-04-28 16:29:06 -0700168 for (auto& point : mLocalSyncPoints) {
169 point->setFrameAvailable();
170 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700171 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700172 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700173}
174
Mathias Agopian13127d82013-03-05 17:47:11 -0800175// ---------------------------------------------------------------------------
176// callbacks
177// ---------------------------------------------------------------------------
178
Dan Stoza9e56aa02015-11-02 13:00:03 -0800179void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
180 if (mHwcLayers.empty()) {
181 return;
182 }
183 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
184}
Mathias Agopian13127d82013-03-05 17:47:11 -0800185
Dan Stoza6b9454d2014-11-07 16:00:59 -0800186void Layer::onFrameAvailable(const BufferItem& item) {
187 // Add this buffer from our internal queue tracker
188 { // Autolock scope
189 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700190 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
191 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700192 // Reset the frame number tracker when we receive the first buffer after
193 // a frame number reset
194 if (item.mFrameNumber == 1) {
195 mLastFrameNumberReceived = 0;
196 }
197
198 // Ensure that callbacks are handled in order
199 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
200 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
201 ms2ns(500));
202 if (result != NO_ERROR) {
203 ALOGE("[%s] Timed out waiting on callback", mName.string());
204 }
205 }
206
Dan Stoza6b9454d2014-11-07 16:00:59 -0800207 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700208 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700209
210 // Wake up any pending callbacks
211 mLastFrameNumberReceived = item.mFrameNumber;
212 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800213 }
214
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800215 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700216}
217
Dan Stoza6b9454d2014-11-07 16:00:59 -0800218void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700219 { // Autolock scope
220 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700221
Dan Stoza7dde5992015-05-22 09:51:44 -0700222 // Ensure that callbacks are handled in order
223 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
224 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
225 ms2ns(500));
226 if (result != NO_ERROR) {
227 ALOGE("[%s] Timed out waiting on callback", mName.string());
228 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700229 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700230
231 if (mQueueItems.empty()) {
232 ALOGE("Can't replace a frame on an empty queue");
233 return;
234 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700235 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700236
237 // Wake up any pending callbacks
238 mLastFrameNumberReceived = item.mFrameNumber;
239 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700240 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800241}
242
Jesse Hall399184a2014-03-03 15:42:54 -0800243void Layer::onSidebandStreamChanged() {
244 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
245 // mSidebandStreamChanged was false
246 mFlinger->signalLayerUpdate();
247 }
248}
249
Mathias Agopian67106042013-03-14 19:18:13 -0700250// called with SurfaceFlinger::mStateLock from the drawing thread after
251// the layer has been remove from the current state list (and just before
252// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800253void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800254 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700255}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700256
Mathias Agopian13127d82013-03-05 17:47:11 -0800257// ---------------------------------------------------------------------------
258// set-up
259// ---------------------------------------------------------------------------
260
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700261const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800262 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263}
264
Mathias Agopianf9d93272009-06-19 17:00:27 -0700265status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 PixelFormat format, uint32_t flags)
267{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700268 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700269 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700270
271 // never allow a surface larger than what our underlying GL implementation
272 // can handle.
273 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800274 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700275 return BAD_VALUE;
276 }
277
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700278 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700279
Riley Andrews03414a12014-07-01 14:22:59 -0700280 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700281 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700282 mCurrentOpacity = getOpacityForFormat(format);
283
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800284 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
285 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
286 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 return NO_ERROR;
289}
290
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700291sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800292 Mutex::Autolock _l(mLock);
293
294 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700295 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800296
297 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700298
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700299 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800300}
301
Dan Stozab9b08832014-03-13 11:55:57 -0700302sp<IGraphicBufferProducer> Layer::getProducer() const {
303 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700304}
305
Mathias Agopian13127d82013-03-05 17:47:11 -0800306// ---------------------------------------------------------------------------
307// h/w composer set-up
308// ---------------------------------------------------------------------------
309
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800310Rect Layer::getContentCrop() const {
311 // this is the crop rectangle that applies to the buffer
312 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700313 Rect crop;
314 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800315 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700316 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800317 } else if (mActiveBuffer != NULL) {
318 // otherwise we use the whole buffer
319 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700320 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800321 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700322 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700323 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700324 return crop;
325}
326
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700327static Rect reduce(const Rect& win, const Region& exclude) {
328 if (CC_LIKELY(exclude.isEmpty())) {
329 return win;
330 }
331 if (exclude.isRect()) {
332 return win.reduce(exclude.getBounds());
333 }
334 return Region(win).subtract(exclude).getBounds();
335}
336
Mathias Agopian13127d82013-03-05 17:47:11 -0800337Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700338 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700339 return computeBounds(s.activeTransparentRegion);
340}
341
342Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
343 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800344 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700345
346 if (!s.crop.isEmpty()) {
347 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700349 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700350 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800351}
352
Mathias Agopian6b442672013-07-09 21:24:52 -0700353FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800354 // the content crop is the area of the content that gets scaled to the
355 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700356 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800357
Robert Carrb5d3d262016-03-25 15:08:13 -0700358 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800359 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700360 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800361
362 // apply the projection's clipping to the window crop in
363 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700364 // if there are no window scaling involved, this operation will map to full
365 // pixels in the buffer.
366 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
367 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700368
369 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700370 if (!s.crop.isEmpty()) {
371 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700372 }
373
Robert Carr3dcabfa2016-03-01 18:36:58 -0800374 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000375 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
376 activeCrop.clear();
377 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700378 if (!s.finalCrop.isEmpty()) {
379 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000380 activeCrop.clear();
381 }
382 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800383 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800384
Michael Lentine28ea2172014-11-19 18:32:37 -0800385 // This needs to be here as transform.transform(Rect) computes the
386 // transformed rect and then takes the bounding box of the result before
387 // returning. This means
388 // transform.inverse().transform(transform.transform(Rect)) != Rect
389 // in which case we need to make sure the final rect is clipped to the
390 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000391 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
392 activeCrop.clear();
393 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800394
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700395 // subtract the transparent region and snap to the bounds
396 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
397
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398 // Transform the window crop to match the buffer coordinate system,
399 // which means using the inverse of the current transform set on the
400 // SurfaceFlingerConsumer.
401 uint32_t invTransform = mCurrentTransform;
402 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
403 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700404 * the code below applies the primary display's inverse transform to the
405 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000406 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700407 uint32_t invTransformOrient =
408 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000409 // calculate the inverse transform
410 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
411 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
412 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800413 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700415 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
416 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800417 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418
419 int winWidth = s.active.w;
420 int winHeight = s.active.h;
421 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
422 // If the activeCrop has been rotate the ends are rotated but not
423 // the space itself so when transforming ends back we can't rely on
424 // a modification of the axes of rotation. To account for this we
425 // need to reorient the inverse rotation in terms of the current
426 // axes of rotation.
427 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
428 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
429 if (is_h_flipped == is_v_flipped) {
430 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
431 NATIVE_WINDOW_TRANSFORM_FLIP_H;
432 }
433 winWidth = s.active.h;
434 winHeight = s.active.w;
435 }
436 const Rect winCrop = activeCrop.transform(
437 invTransform, s.active.w, s.active.h);
438
439 // below, crop is intersected with winCrop expressed in crop's coordinate space
440 float xScale = crop.getWidth() / float(winWidth);
441 float yScale = crop.getHeight() / float(winHeight);
442
443 float insetL = winCrop.left * xScale;
444 float insetT = winCrop.top * yScale;
445 float insetR = (winWidth - winCrop.right ) * xScale;
446 float insetB = (winHeight - winCrop.bottom) * yScale;
447
448 crop.left += insetL;
449 crop.top += insetT;
450 crop.right -= insetR;
451 crop.bottom -= insetB;
452
Mathias Agopian13127d82013-03-05 17:47:11 -0800453 return crop;
454}
455
Dan Stoza9e56aa02015-11-02 13:00:03 -0800456void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700457{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800458 const auto hwcId = displayDevice->getHwcDisplayId();
459 auto& hwcInfo = mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700460
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700461 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800462 hwcInfo.forceClientComposition = false;
463
464 if (isSecure() && !displayDevice->isSecure()) {
465 hwcInfo.forceClientComposition = true;
466 }
467
468 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700469
Mathias Agopian13127d82013-03-05 17:47:11 -0800470 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700471 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 if (!isOpaque(s) || s.alpha != 1.0f) {
473 auto blendMode = mPremultipliedAlpha ?
474 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
475 auto error = hwcLayer->setBlendMode(blendMode);
476 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
477 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
478 to_string(error).c_str(), static_cast<int32_t>(error));
479 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800480
481 // apply the layer's transform, followed by the display's global transform
482 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700483 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700484 if (!s.crop.isEmpty()) {
485 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800486 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000487 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000488 activeCrop.clear();
489 }
Pablo Ceballos51450032016-08-03 10:20:45 -0700490 activeCrop = s.active.transform.inverse().transform(activeCrop, true);
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 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700500 // mark regions outside the crop as transparent
501 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
502 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
503 s.active.w, s.active.h));
504 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
505 activeCrop.left, activeCrop.bottom));
506 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
507 s.active.w, activeCrop.bottom));
508 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800509 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700510 if (!s.finalCrop.isEmpty()) {
511 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000512 frame.clear();
513 }
514 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000515 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
516 frame.clear();
517 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800518 const Transform& tr(displayDevice->getTransform());
519 Rect transformedFrame = tr.transform(frame);
520 auto error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700521 if (error != HWC2::Error::None) {
522 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
523 mName.string(), transformedFrame.left, transformedFrame.top,
524 transformedFrame.right, transformedFrame.bottom,
525 to_string(error).c_str(), static_cast<int32_t>(error));
526 } else {
527 hwcInfo.displayFrame = transformedFrame;
528 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529
530 FloatRect sourceCrop = computeCrop(displayDevice);
531 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700532 if (error != HWC2::Error::None) {
533 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
534 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
535 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
536 static_cast<int32_t>(error));
537 } else {
538 hwcInfo.sourceCrop = sourceCrop;
539 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800540
541 error = hwcLayer->setPlaneAlpha(s.alpha);
542 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
543 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
544 static_cast<int32_t>(error));
545
546 error = hwcLayer->setZOrder(s.z);
547 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
548 mName.string(), s.z, to_string(error).c_str(),
549 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800550
Mathias Agopian29a367b2011-07-12 14:51:45 -0700551 /*
552 * Transformations are applied in this order:
553 * 1) buffer orientation/flip/mirror
554 * 2) state transformation (window manager)
555 * 3) layer orientation (screen orientation)
556 * (NOTE: the matrices are multiplied in reverse order)
557 */
558
559 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800560 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700561
562 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
563 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700564 * the code below applies the primary display's inverse transform to the
565 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700566 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700567 uint32_t invTransform =
568 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700569 // calculate the inverse transform
570 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
571 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
572 NATIVE_WINDOW_TRANSFORM_FLIP_H;
573 }
574 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700575 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700576 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700577
578 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800579 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 if (orientation & Transform::ROT_INVALID) {
581 // we can only handle simple transformation
582 hwcInfo.forceClientComposition = true;
583 } else {
584 auto transform = static_cast<HWC2::Transform>(orientation);
585 auto error = hwcLayer->setTransform(transform);
586 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
587 "%s (%d)", mName.string(), to_string(transform).c_str(),
588 to_string(error).c_str(), static_cast<int32_t>(error));
589 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700590}
591
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592void Layer::forceClientComposition(int32_t hwcId) {
593 if (mHwcLayers.count(hwcId) == 0) {
594 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
595 return;
596 }
597
598 mHwcLayers[hwcId].forceClientComposition = true;
599}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
602 // Apply this display's projection's viewport to the visible region
603 // before giving it to the HWC HAL.
604 const Transform& tr = displayDevice->getTransform();
605 const auto& viewport = displayDevice->getViewport();
606 Region visible = tr.transform(visibleRegion.intersect(viewport));
607 auto hwcId = displayDevice->getHwcDisplayId();
608 auto& hwcLayer = mHwcLayers[hwcId].layer;
609 auto error = hwcLayer->setVisibleRegion(visible);
610 if (error != HWC2::Error::None) {
611 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
612 to_string(error).c_str(), static_cast<int32_t>(error));
613 visible.dump(LOG_TAG);
614 }
615
616 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
617 if (error != HWC2::Error::None) {
618 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
619 to_string(error).c_str(), static_cast<int32_t>(error));
620 surfaceDamageRegion.dump(LOG_TAG);
621 }
622
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700623 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700625 setCompositionType(hwcId, HWC2::Composition::Sideband);
626 ALOGV("[%s] Requesting Sideband composition", mName.string());
627 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 if (error != HWC2::Error::None) {
629 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
630 mName.string(), mSidebandStream->handle(),
631 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700633 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634 }
635
Dan Stoza0a21df72016-07-20 12:52:38 -0700636 // Client layers
637 if (mHwcLayers[hwcId].forceClientComposition ||
638 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700639 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700641 return;
642 }
643
Dan Stoza0a21df72016-07-20 12:52:38 -0700644 // SolidColor layers
645 if (mActiveBuffer == nullptr) {
646 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700647
648 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700649 error = hwcLayer->setColor({0, 0, 0, 255});
650 if (error != HWC2::Error::None) {
651 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
652 to_string(error).c_str(), static_cast<int32_t>(error));
653 }
Dan Stozac6c89542016-07-27 10:16:52 -0700654
655 // Clear out the transform, because it doesn't make sense absent a
656 // source buffer
657 error = hwcLayer->setTransform(HWC2::Transform::None);
658 if (error != HWC2::Error::None) {
659 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
660 to_string(error).c_str(), static_cast<int32_t>(error));
661 }
662
Dan Stoza0a21df72016-07-20 12:52:38 -0700663 return;
664 }
665
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700666 // Device or Cursor layers
667 if (mPotentialCursor) {
668 ALOGV("[%s] Requesting Cursor composition", mName.string());
669 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 } else {
671 ALOGV("[%s] Requesting Device composition", mName.string());
672 setCompositionType(hwcId, HWC2::Composition::Device);
673 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700674
675 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
676 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
677 if (error != HWC2::Error::None) {
678 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
679 mActiveBuffer->handle, to_string(error).c_str(),
680 static_cast<int32_t>(error));
681 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682}
Dan Stozaee44edd2015-03-23 15:50:23 -0700683
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
685 auto hwcId = displayDevice->getHwcDisplayId();
686 if (mHwcLayers.count(hwcId) == 0 ||
687 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
688 return;
689 }
690
691 // This gives us only the "orientation" component of the transform
692 const State& s(getCurrentState());
693
694 // Apply the layer's transform, followed by the display's global transform
695 // Here we're guaranteed that the layer's transform preserves rects
696 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700697 if (!s.crop.isEmpty()) {
698 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 }
700 // Subtract the transparent region and snap to the bounds
701 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800702 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700704 if (!s.finalCrop.isEmpty()) {
705 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000706 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 auto& displayTransform(displayDevice->getTransform());
708 auto position = displayTransform.transform(frame);
709
710 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
711 position.top);
712 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
713 "to (%d, %d): %s (%d)", mName.string(), position.left,
714 position.top, to_string(error).c_str(),
715 static_cast<int32_t>(error));
716}
Riley Andrews03414a12014-07-01 14:22:59 -0700717
Mathias Agopian13127d82013-03-05 17:47:11 -0800718// ---------------------------------------------------------------------------
719// drawing...
720// ---------------------------------------------------------------------------
721
722void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800723 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800724}
725
Dan Stozac7014012014-02-14 15:03:43 -0800726void Layer::draw(const sp<const DisplayDevice>& hw,
727 bool useIdentityTransform) const {
728 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
Dan Stozac7014012014-02-14 15:03:43 -0800731void Layer::draw(const sp<const DisplayDevice>& hw) const {
732 onDraw(hw, Region(hw->bounds()), false);
733}
734
735void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
736 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800738 ATRACE_CALL();
739
Mathias Agopiana67932f2011-04-20 14:20:59 -0700740 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700742 // in fact never been drawn into. This happens frequently with
743 // SurfaceView because the WindowManager can't know when the client
744 // has drawn the first time.
745
746 // If there is nothing under us, we paint the screen in black, otherwise
747 // we just skip this update.
748
749 // figure out if there is something below us
750 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700751 const SurfaceFlinger::LayerVector& drawingLayers(
752 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700753 const size_t count = drawingLayers.size();
754 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800755 const sp<Layer>& layer(drawingLayers[i]);
756 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700757 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700758 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700759 }
760 // if not everything below us is covered, we plug the holes!
761 Region holes(clip.subtract(under));
762 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700763 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700764 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 return;
766 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700767
Andy McFadden97eba892012-12-11 15:21:45 -0800768 // Bind the current buffer to the GL texture, and wait for it to be
769 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800770 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
771 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800772 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700773 // Go ahead and draw the buffer anyway; no matter what we do the screen
774 // is probably going to have something visibly wrong.
775 }
776
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700777 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
778
Mathias Agopian875d8e12013-06-07 15:35:48 -0700779 RenderEngine& engine(mFlinger->getRenderEngine());
780
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700781 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700782 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700783 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700784
785 // Query the texture matrix given our current filtering mode.
786 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800787 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
788 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700789
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700790 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
791
792 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700793 * the code below applies the primary display's inverse transform to
794 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700795 */
796
797 // create a 4x4 transform matrix from the display transform flags
798 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
799 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
800 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
801
802 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700803 uint32_t transform =
804 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700805 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
806 tr = tr * rot90;
807 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
808 tr = tr * flipH;
809 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
810 tr = tr * flipV;
811
812 // calculate the inverse
813 tr = inverse(tr);
814
815 // and finally apply it to the original texture matrix
816 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
817 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
818 }
819
Jamie Genniscbb1a952012-05-08 17:05:52 -0700820 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700821 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
822 mTexture.setFiltering(useFiltering);
823 mTexture.setMatrix(textureMatrix);
824
825 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700826 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700827 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700828 }
Dan Stozac7014012014-02-14 15:03:43 -0800829 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700830 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831}
832
Mathias Agopian13127d82013-03-05 17:47:11 -0800833
Dan Stozac7014012014-02-14 15:03:43 -0800834void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
835 const Region& /* clip */, float red, float green, float blue,
836 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800837{
Mathias Agopian19733a32013-08-28 18:13:56 -0700838 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800839 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700840 engine.setupFillWithColor(red, green, blue, alpha);
841 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800842}
843
844void Layer::clearWithOpenGL(
845 const sp<const DisplayDevice>& hw, const Region& clip) const {
846 clearWithOpenGL(hw, clip, 0,0,0,0);
847}
848
Dan Stozac7014012014-02-14 15:03:43 -0800849void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
850 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700851 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800852
Dan Stozac7014012014-02-14 15:03:43 -0800853 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800854
Mathias Agopian13127d82013-03-05 17:47:11 -0800855 /*
856 * NOTE: the way we compute the texture coordinates here produces
857 * different results than when we take the HWC path -- in the later case
858 * the "source crop" is rounded to texel boundaries.
859 * This can produce significantly different results when the texture
860 * is scaled by a large amount.
861 *
862 * The GL code below is more logical (imho), and the difference with
863 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700864 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 * GL composition when a buffer scaling is applied (maybe with some
866 * minimal value)? Or, we could make GL behave like HWC -- but this feel
867 * like more of a hack.
868 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000869 Rect win(computeBounds());
870
Robert Carrb5d3d262016-03-25 15:08:13 -0700871 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000872 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -0700873 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000874 win.clear();
875 }
876 win = s.active.transform.inverse().transform(win);
877 if (!win.intersect(computeBounds(), &win)) {
878 win.clear();
879 }
880 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800881
Mathias Agopian3f844832013-08-07 21:24:32 -0700882 float left = float(win.left) / float(s.active.w);
883 float top = float(win.top) / float(s.active.h);
884 float right = float(win.right) / float(s.active.w);
885 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800886
Mathias Agopian875d8e12013-06-07 15:35:48 -0700887 // TODO: we probably want to generate the texture coords with the mesh
888 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -0700889 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
890 texCoords[0] = vec2(left, 1.0f - top);
891 texCoords[1] = vec2(left, 1.0f - bottom);
892 texCoords[2] = vec2(right, 1.0f - bottom);
893 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -0800894
Mathias Agopian875d8e12013-06-07 15:35:48 -0700895 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -0800896 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700897 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700898 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -0800899}
900
Dan Stoza9e56aa02015-11-02 13:00:03 -0800901void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
902 bool callIntoHwc) {
903 if (mHwcLayers.count(hwcId) == 0) {
904 ALOGE("setCompositionType called without a valid HWC layer");
905 return;
906 }
907 auto& hwcInfo = mHwcLayers[hwcId];
908 auto& hwcLayer = hwcInfo.layer;
909 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
910 to_string(type).c_str(), static_cast<int>(callIntoHwc));
911 if (hwcInfo.compositionType != type) {
912 ALOGV(" actually setting");
913 hwcInfo.compositionType = type;
914 if (callIntoHwc) {
915 auto error = hwcLayer->setCompositionType(type);
916 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
917 "composition type %s: %s (%d)", mName.string(),
918 to_string(type).c_str(), to_string(error).c_str(),
919 static_cast<int32_t>(error));
920 }
921 }
922}
923
924HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700925 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
926 // If we're querying the composition type for a display that does not
927 // have a HWC counterpart, then it will always be Client
928 return HWC2::Composition::Client;
929 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800930 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700931 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800932 return HWC2::Composition::Invalid;
933 }
934 return mHwcLayers.at(hwcId).compositionType;
935}
936
937void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
938 if (mHwcLayers.count(hwcId) == 0) {
939 ALOGE("setClearClientTarget called without a valid HWC layer");
940 return;
941 }
942 mHwcLayers[hwcId].clearClientTarget = clear;
943}
944
945bool Layer::getClearClientTarget(int32_t hwcId) const {
946 if (mHwcLayers.count(hwcId) == 0) {
947 ALOGE("getClearClientTarget called without a valid HWC layer");
948 return false;
949 }
950 return mHwcLayers.at(hwcId).clearClientTarget;
951}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800952
Ruben Brunk1681d952014-06-27 15:51:55 -0700953uint32_t Layer::getProducerStickyTransform() const {
954 int producerStickyTransform = 0;
955 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
956 if (ret != OK) {
957 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
958 strerror(-ret), ret);
959 return 0;
960 }
961 return static_cast<uint32_t>(producerStickyTransform);
962}
963
Dan Stozac5da2712016-07-20 15:38:12 -0700964bool Layer::latchUnsignaledBuffers() {
965 static bool propertyLoaded = false;
966 static bool latch = false;
967 static std::mutex mutex;
968 std::lock_guard<std::mutex> lock(mutex);
969 if (!propertyLoaded) {
970 char value[PROPERTY_VALUE_MAX] = {};
971 property_get("debug.sf.latch_unsignaled", value, "0");
972 latch = atoi(value);
973 propertyLoaded = true;
974 }
975 return latch;
976}
977
Dan Stozacac35382016-01-27 12:21:06 -0800978uint64_t Layer::getHeadFrameNumber() const {
979 Mutex::Autolock lock(mQueueItemLock);
980 if (!mQueueItems.empty()) {
981 return mQueueItems[0].mFrameNumber;
982 } else {
983 return mCurrentFrameNumber;
984 }
985}
986
Dan Stoza1ce65812016-06-15 16:26:27 -0700987bool Layer::headFenceHasSignaled() const {
Dan Stozac5da2712016-07-20 15:38:12 -0700988 if (latchUnsignaledBuffers()) {
989 return true;
990 }
991
Dan Stoza1ce65812016-06-15 16:26:27 -0700992 Mutex::Autolock lock(mQueueItemLock);
993 if (mQueueItems.empty()) {
994 return true;
995 }
996 if (mQueueItems[0].mIsDroppable) {
997 // Even though this buffer's fence may not have signaled yet, it could
998 // be replaced by another buffer before it has a chance to, which means
999 // that it's possible to get into a situation where a buffer is never
1000 // able to be latched. To avoid this, grab this buffer anyway.
1001 return true;
1002 }
1003 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
Dan Stoza1ce65812016-06-15 16:26:27 -07001004}
1005
Dan Stozacac35382016-01-27 12:21:06 -08001006bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1007 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1008 // Don't bother with a SyncPoint, since we've already latched the
1009 // relevant frame
1010 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001011 }
1012
Dan Stozacac35382016-01-27 12:21:06 -08001013 Mutex::Autolock lock(mLocalSyncPointMutex);
1014 mLocalSyncPoints.push_back(point);
1015 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001016}
1017
Mathias Agopian13127d82013-03-05 17:47:11 -08001018void Layer::setFiltering(bool filtering) {
1019 mFiltering = filtering;
1020}
1021
1022bool Layer::getFiltering() const {
1023 return mFiltering;
1024}
1025
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001026// As documented in libhardware header, formats in the range
1027// 0x100 - 0x1FF are specific to the HAL implementation, and
1028// are known to have no alpha channel
1029// TODO: move definition for device-specific range into
1030// hardware.h, instead of using hard-coded values here.
1031#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1032
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001033bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001034 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1035 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001036 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001037 switch (format) {
1038 case HAL_PIXEL_FORMAT_RGBA_8888:
1039 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001040 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001041 }
1042 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001043 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001044}
1045
Mathias Agopian13127d82013-03-05 17:47:11 -08001046// ----------------------------------------------------------------------------
1047// local state
1048// ----------------------------------------------------------------------------
1049
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001050static void boundPoint(vec2* point, const Rect& crop) {
1051 if (point->x < crop.left) {
1052 point->x = crop.left;
1053 }
1054 if (point->x > crop.right) {
1055 point->x = crop.right;
1056 }
1057 if (point->y < crop.top) {
1058 point->y = crop.top;
1059 }
1060 if (point->y > crop.bottom) {
1061 point->y = crop.bottom;
1062 }
1063}
1064
Dan Stozac7014012014-02-14 15:03:43 -08001065void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1066 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001067{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001068 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001069 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 const uint32_t hw_h = hw->getHeight();
1071 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001072 if (!s.crop.isEmpty()) {
1073 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001075 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001076 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001077
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001078 vec2 lt = vec2(win.left, win.top);
1079 vec2 lb = vec2(win.left, win.bottom);
1080 vec2 rb = vec2(win.right, win.bottom);
1081 vec2 rt = vec2(win.right, win.top);
1082
1083 if (!useIdentityTransform) {
1084 lt = s.active.transform.transform(lt);
1085 lb = s.active.transform.transform(lb);
1086 rb = s.active.transform.transform(rb);
1087 rt = s.active.transform.transform(rt);
1088 }
1089
Robert Carrb5d3d262016-03-25 15:08:13 -07001090 if (!s.finalCrop.isEmpty()) {
1091 boundPoint(&lt, s.finalCrop);
1092 boundPoint(&lb, s.finalCrop);
1093 boundPoint(&rb, s.finalCrop);
1094 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001095 }
1096
Mathias Agopianff2ed702013-09-01 21:36:12 -07001097 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001098 position[0] = tr.transform(lt);
1099 position[1] = tr.transform(lb);
1100 position[2] = tr.transform(rb);
1101 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001102 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001103 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 }
1105}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001106
Andy McFadden4125a4f2014-01-29 17:17:11 -08001107bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001108{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001109 // if we don't have a buffer yet, we're translucent regardless of the
1110 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001111 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001112 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001113 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001114
1115 // if the layer has the opaque flag, then we're always opaque,
1116 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001117 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001118}
1119
Dan Stoza23116082015-06-18 14:58:39 -07001120bool Layer::isSecure() const
1121{
1122 const Layer::State& s(mDrawingState);
1123 return (s.flags & layer_state_t::eLayerSecure);
1124}
1125
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001126bool Layer::isProtected() const
1127{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001128 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001129 return (activeBuffer != 0) &&
1130 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1131}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001132
Mathias Agopian13127d82013-03-05 17:47:11 -08001133bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001134 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001135}
1136
1137bool Layer::isCropped() const {
1138 return !mCurrentCrop.isEmpty();
1139}
1140
1141bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1142 return mNeedsFiltering || hw->needsFiltering();
1143}
1144
1145void Layer::setVisibleRegion(const Region& visibleRegion) {
1146 // always called from main thread
1147 this->visibleRegion = visibleRegion;
1148}
1149
1150void Layer::setCoveredRegion(const Region& coveredRegion) {
1151 // always called from main thread
1152 this->coveredRegion = coveredRegion;
1153}
1154
1155void Layer::setVisibleNonTransparentRegion(const Region&
1156 setVisibleNonTransparentRegion) {
1157 // always called from main thread
1158 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1159}
1160
1161// ----------------------------------------------------------------------------
1162// transaction
1163// ----------------------------------------------------------------------------
1164
Dan Stoza7dde5992015-05-22 09:51:44 -07001165void Layer::pushPendingState() {
1166 if (!mCurrentState.modified) {
1167 return;
1168 }
1169
Dan Stoza7dde5992015-05-22 09:51:44 -07001170 // If this transaction is waiting on the receipt of a frame, generate a sync
1171 // point and send it to the remote layer.
1172 if (mCurrentState.handle != nullptr) {
Dan Stoza22851c32016-08-09 13:21:03 -07001173 sp<IBinder> strongBinder = mCurrentState.handle.promote();
1174 sp<Handle> handle = nullptr;
1175 sp<Layer> handleLayer = nullptr;
1176 if (strongBinder != nullptr) {
1177 handle = static_cast<Handle*>(strongBinder.get());
1178 handleLayer = handle->owner.promote();
1179 }
1180 if (strongBinder == nullptr || handleLayer == nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001181 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1182 // If we can't promote the layer we are intended to wait on,
1183 // then it is expired or otherwise invalid. Allow this transaction
1184 // to be applied as per normal (no synchronization).
1185 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001186 } else {
1187 auto syncPoint = std::make_shared<SyncPoint>(
1188 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001189 if (handleLayer->addSyncPoint(syncPoint)) {
1190 mRemoteSyncPoints.push_back(std::move(syncPoint));
1191 } else {
1192 // We already missed the frame we're supposed to synchronize
1193 // on, so go ahead and apply the state update
1194 mCurrentState.handle = nullptr;
1195 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001196 }
1197
Dan Stoza7dde5992015-05-22 09:51:44 -07001198 // Wake us up to check if the frame has been received
1199 setTransactionFlags(eTransactionNeeded);
1200 }
1201 mPendingStates.push_back(mCurrentState);
1202}
1203
Pablo Ceballos05289c22016-04-14 15:49:55 -07001204void Layer::popPendingState(State* stateToCommit) {
1205 auto oldFlags = stateToCommit->flags;
1206 *stateToCommit = mPendingStates[0];
1207 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1208 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001209
1210 mPendingStates.removeAt(0);
1211}
1212
Pablo Ceballos05289c22016-04-14 15:49:55 -07001213bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001214 bool stateUpdateAvailable = false;
1215 while (!mPendingStates.empty()) {
1216 if (mPendingStates[0].handle != nullptr) {
1217 if (mRemoteSyncPoints.empty()) {
1218 // If we don't have a sync point for this, apply it anyway. It
1219 // will be visually wrong, but it should keep us from getting
1220 // into too much trouble.
1221 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001222 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001223 stateUpdateAvailable = true;
1224 continue;
1225 }
1226
Dan Stozacac35382016-01-27 12:21:06 -08001227 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1228 mPendingStates[0].frameNumber) {
1229 ALOGE("[%s] Unexpected sync point frame number found",
1230 mName.string());
1231
1232 // Signal our end of the sync point and then dispose of it
1233 mRemoteSyncPoints.front()->setTransactionApplied();
1234 mRemoteSyncPoints.pop_front();
1235 continue;
1236 }
1237
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1239 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001240 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001241 stateUpdateAvailable = true;
1242
1243 // Signal our end of the sync point and then dispose of it
1244 mRemoteSyncPoints.front()->setTransactionApplied();
1245 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001246 } else {
1247 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001248 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001249 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001250 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001251 stateUpdateAvailable = true;
1252 }
1253 }
1254
1255 // If we still have pending updates, wake SurfaceFlinger back up and point
1256 // it at this layer so we can process them
1257 if (!mPendingStates.empty()) {
1258 setTransactionFlags(eTransactionNeeded);
1259 mFlinger->setTransactionFlags(eTraversalNeeded);
1260 }
1261
1262 mCurrentState.modified = false;
1263 return stateUpdateAvailable;
1264}
1265
1266void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001267 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001268 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001269 Mutex::Autolock lock(mLocalSyncPointMutex);
1270 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001271 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001272 point->setFrameAvailable();
1273 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001274 }
1275}
1276
Mathias Agopian13127d82013-03-05 17:47:11 -08001277uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001278 ATRACE_CALL();
1279
Dan Stoza7dde5992015-05-22 09:51:44 -07001280 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001281 Layer::State c = getCurrentState();
1282 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 return 0;
1284 }
1285
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001286 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001288 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1289 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001290
1291 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001292 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001293 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001294 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001295 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001296 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001297 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001298 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001299 this, getName().string(), mCurrentTransform,
1300 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001301 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001302 c.crop.left,
1303 c.crop.top,
1304 c.crop.right,
1305 c.crop.bottom,
1306 c.crop.getWidth(),
1307 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001308 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001309 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001310 s.crop.left,
1311 s.crop.top,
1312 s.crop.right,
1313 s.crop.bottom,
1314 s.crop.getWidth(),
1315 s.crop.getHeight(),
1316 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001318 // record the new size, form this point on, when the client request
1319 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001320 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001321 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001323
Robert Carr82364e32016-05-15 11:27:47 -07001324 const bool resizePending = (c.requested.w != c.active.w) ||
1325 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001326 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001327 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001328 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001329 // if we have a pending resize, unless we are in fixed-size mode.
1330 // the drawing state will be updated only once we receive a buffer
1331 // with the correct size.
1332 //
1333 // in particular, we want to make sure the clip (which is part
1334 // of the geometry state) is latched together with the size but is
1335 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001336 //
1337 // If a sideband stream is attached, however, we want to skip this
1338 // optimization so that transactions aren't missed when a buffer
1339 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001340
1341 flags |= eDontUpdateGeometryState;
1342 }
1343 }
1344
Mathias Agopian13127d82013-03-05 17:47:11 -08001345 // always set active to requested, unless we're asked not to
1346 // this is used by Layer, which special cases resizes.
1347 if (flags & eDontUpdateGeometryState) {
1348 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001349 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001350 if (mFreezePositionUpdates) {
1351 float tx = c.active.transform.tx();
1352 float ty = c.active.transform.ty();
1353 c.active = c.requested;
1354 c.active.transform.set(tx, ty);
1355 editCurrentState.active = c.active;
1356 } else {
1357 editCurrentState.active = editCurrentState.requested;
1358 c.active = c.requested;
1359 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001360 }
1361
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001362 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001363 // invalidate and recompute the visible regions if needed
1364 flags |= Layer::eVisibleRegion;
1365 }
1366
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001367 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001368 // invalidate and recompute the visible regions if needed
1369 flags |= eVisibleRegion;
1370 this->contentDirty = true;
1371
1372 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001373 const uint8_t type = c.active.transform.getType();
1374 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001375 (type >= Transform::SCALE));
1376 }
1377
Dan Stozac8145172016-04-28 16:29:06 -07001378 // If the layer is hidden, signal and clear out all local sync points so
1379 // that transactions for layers depending on this layer's frames becoming
1380 // visible are not blocked
1381 if (c.flags & layer_state_t::eLayerHidden) {
1382 Mutex::Autolock lock(mLocalSyncPointMutex);
1383 for (auto& point : mLocalSyncPoints) {
1384 point->setFrameAvailable();
1385 }
1386 mLocalSyncPoints.clear();
1387 }
1388
Mathias Agopian13127d82013-03-05 17:47:11 -08001389 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001390 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001391 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Pablo Ceballos05289c22016-04-14 15:49:55 -07001394void Layer::commitTransaction(const State& stateToCommit) {
1395 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001396}
1397
Mathias Agopian13127d82013-03-05 17:47:11 -08001398uint32_t Layer::getTransactionFlags(uint32_t flags) {
1399 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1400}
1401
1402uint32_t Layer::setTransactionFlags(uint32_t flags) {
1403 return android_atomic_or(flags, &mTransactionFlags);
1404}
1405
Robert Carr82364e32016-05-15 11:27:47 -07001406bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001407 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001408 return false;
1409 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001410
1411 // We update the requested and active position simultaneously because
1412 // we want to apply the position portion of the transform matrix immediately,
1413 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001414 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001415 if (immediate && !mFreezePositionUpdates) {
1416 mCurrentState.active.transform.set(x, y);
1417 }
1418 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001419
Dan Stoza7dde5992015-05-22 09:51:44 -07001420 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001421 setTransactionFlags(eTransactionNeeded);
1422 return true;
1423}
Robert Carr82364e32016-05-15 11:27:47 -07001424
Mathias Agopian13127d82013-03-05 17:47:11 -08001425bool Layer::setLayer(uint32_t z) {
1426 if (mCurrentState.z == z)
1427 return false;
1428 mCurrentState.sequence++;
1429 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001430 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001431 setTransactionFlags(eTransactionNeeded);
1432 return true;
1433}
1434bool Layer::setSize(uint32_t w, uint32_t h) {
1435 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1436 return false;
1437 mCurrentState.requested.w = w;
1438 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001439 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001440 setTransactionFlags(eTransactionNeeded);
1441 return true;
1442}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001443bool Layer::setAlpha(float alpha) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001444 if (mCurrentState.alpha == alpha)
1445 return false;
1446 mCurrentState.sequence++;
1447 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001448 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001449 setTransactionFlags(eTransactionNeeded);
1450 return true;
1451}
1452bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1453 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001454 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001456 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001457 setTransactionFlags(eTransactionNeeded);
1458 return true;
1459}
1460bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001461 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001462 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 setTransactionFlags(eTransactionNeeded);
1464 return true;
1465}
1466bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1467 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1468 if (mCurrentState.flags == newFlags)
1469 return false;
1470 mCurrentState.sequence++;
1471 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001472 mCurrentState.mask = mask;
1473 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 setTransactionFlags(eTransactionNeeded);
1475 return true;
1476}
Robert Carr99e27f02016-06-16 15:18:02 -07001477
1478bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001479 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001480 return false;
1481 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001482 mCurrentState.requestedCrop = crop;
1483 if (immediate) {
1484 mCurrentState.crop = crop;
1485 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001486 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001487 setTransactionFlags(eTransactionNeeded);
1488 return true;
1489}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001490bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001491 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001492 return false;
1493 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001494 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001495 mCurrentState.modified = true;
1496 setTransactionFlags(eTransactionNeeded);
1497 return true;
1498}
Mathias Agopian13127d82013-03-05 17:47:11 -08001499
Robert Carrc3574f72016-03-24 12:19:32 -07001500bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1501 if (scalingMode == mOverrideScalingMode)
1502 return false;
1503 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001504 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001505 return true;
1506}
1507
1508uint32_t Layer::getEffectiveScalingMode() const {
1509 if (mOverrideScalingMode >= 0) {
1510 return mOverrideScalingMode;
1511 }
1512 return mCurrentScalingMode;
1513}
1514
Mathias Agopian13127d82013-03-05 17:47:11 -08001515bool Layer::setLayerStack(uint32_t layerStack) {
1516 if (mCurrentState.layerStack == layerStack)
1517 return false;
1518 mCurrentState.sequence++;
1519 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001520 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001521 setTransactionFlags(eTransactionNeeded);
1522 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001523}
1524
Dan Stoza7dde5992015-05-22 09:51:44 -07001525void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1526 uint64_t frameNumber) {
1527 mCurrentState.handle = handle;
1528 mCurrentState.frameNumber = frameNumber;
1529 // We don't set eTransactionNeeded, because just receiving a deferral
1530 // request without any other state updates shouldn't actually induce a delay
1531 mCurrentState.modified = true;
1532 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001533 mCurrentState.handle = nullptr;
1534 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001535 mCurrentState.modified = false;
1536}
1537
Dan Stozaee44edd2015-03-23 15:50:23 -07001538void Layer::useSurfaceDamage() {
1539 if (mFlinger->mForceFullDamage) {
1540 surfaceDamageRegion = Region::INVALID_REGION;
1541 } else {
1542 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1543 }
1544}
1545
1546void Layer::useEmptyDamage() {
1547 surfaceDamageRegion.clear();
1548}
1549
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550// ----------------------------------------------------------------------------
1551// pageflip handling...
1552// ----------------------------------------------------------------------------
1553
Dan Stoza6b9454d2014-11-07 16:00:59 -08001554bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001555 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001556 return true;
1557 }
1558
Dan Stoza6b9454d2014-11-07 16:00:59 -08001559 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001560 if (mQueueItems.empty()) {
1561 return false;
1562 }
1563 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001564 nsecs_t expectedPresent =
1565 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001566
1567 // Ignore timestamps more than a second in the future
1568 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1569 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1570 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1571 expectedPresent);
1572
1573 bool isDue = timestamp < expectedPresent;
1574 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001575}
1576
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001577bool Layer::onPreComposition() {
1578 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001579 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001580}
1581
Dan Stozae77c7662016-05-13 11:37:28 -07001582bool Layer::onPostComposition() {
1583 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001584 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001585 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001586 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1587
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001588 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001589 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001590 mFrameTracker.setFrameReadyFence(frameReadyFence);
1591 } else {
1592 // There was no fence for this frame, so assume that it was ready
1593 // to be presented at the desired present time.
1594 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1595 }
1596
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001597 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001598 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
Jamie Gennis789a6c32013-02-25 13:37:54 -08001599 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001600 mFrameTracker.setActualPresentFence(presentFence);
1601 } else {
1602 // The HWC doesn't support present fences, so use the refresh
1603 // timestamp instead.
1604 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1605 mFrameTracker.setActualPresentTime(presentTime);
1606 }
1607
1608 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001609 mFrameLatencyNeeded = false;
1610 }
Dan Stozae77c7662016-05-13 11:37:28 -07001611 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001612}
1613
Dan Stoza9e56aa02015-11-02 13:00:03 -08001614void Layer::releasePendingBuffer() {
1615 mSurfaceFlingerConsumer->releasePendingBuffer();
1616}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001617
Mathias Agopianda27af92012-09-13 18:17:13 -07001618bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001619 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001620 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1621 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -07001622}
1623
Mathias Agopian4fec8732012-06-29 14:12:52 -07001624Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001626 ATRACE_CALL();
1627
Jesse Hall399184a2014-03-03 15:42:54 -08001628 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1629 // mSidebandStreamChanged was true
1630 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001631 if (mSidebandStream != NULL) {
1632 setTransactionFlags(eTransactionNeeded);
1633 mFlinger->setTransactionFlags(eTraversalNeeded);
1634 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001635 recomputeVisibleRegions = true;
1636
1637 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001638 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001639 }
1640
Mathias Agopian4fec8732012-06-29 14:12:52 -07001641 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001642 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001643
1644 // if we've already called updateTexImage() without going through
1645 // a composition step, we have to skip this layer at this point
1646 // because we cannot call updateTeximage() without a corresponding
1647 // compositionComplete() call.
1648 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001649 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001650 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001651 }
1652
Dan Stoza1ce65812016-06-15 16:26:27 -07001653 // If the head buffer's acquire fence hasn't signaled yet, return and
1654 // try again later
1655 if (!headFenceHasSignaled()) {
1656 mFlinger->signalLayerUpdate();
1657 return outDirtyRegion;
1658 }
1659
Jamie Gennis351a5132011-09-14 18:23:37 -07001660 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001661 const State& s(getDrawingState());
1662 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001663 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001664
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001665 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001666 Layer::State& front;
1667 Layer::State& current;
1668 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001669 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001670 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001671 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001672 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001673
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001674 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001675 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001676 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001677 int32_t overrideScalingMode,
1678 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001679 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001680 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001681 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001682 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001683 overrideScalingMode(overrideScalingMode),
1684 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001685 }
1686
1687 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001688 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001689 if (buf == NULL) {
1690 return false;
1691 }
1692
1693 uint32_t bufWidth = buf->getWidth();
1694 uint32_t bufHeight = buf->getHeight();
1695
1696 // check that we received a buffer of the right size
1697 // (Take the buffer's orientation into account)
1698 if (item.mTransform & Transform::ROT_90) {
1699 swap(bufWidth, bufHeight);
1700 }
1701
Robert Carrc3574f72016-03-24 12:19:32 -07001702 int actualScalingMode = overrideScalingMode >= 0 ?
1703 overrideScalingMode : item.mScalingMode;
1704 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001705 if (front.active != front.requested) {
1706
1707 if (isFixedSize ||
1708 (bufWidth == front.requested.w &&
1709 bufHeight == front.requested.h))
1710 {
1711 // Here we pretend the transaction happened by updating the
1712 // current and drawing states. Drawing state is only accessed
1713 // in this thread, no need to have it locked
1714 front.active = front.requested;
1715
1716 // We also need to update the current state so that
1717 // we don't end-up overwriting the drawing state with
1718 // this stale current state during the next transaction
1719 //
1720 // NOTE: We don't need to hold the transaction lock here
1721 // because State::active is only accessed from this thread.
1722 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001723 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001724
1725 // recompute visible region
1726 recomputeVisibleRegions = true;
1727 }
1728
1729 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001730 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001731 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001732 " requested={ wh={%4u,%4u} }}\n",
1733 name,
Andy McFadden69052052012-09-14 16:10:11 -07001734 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001735 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001736 front.crop.left,
1737 front.crop.top,
1738 front.crop.right,
1739 front.crop.bottom,
1740 front.crop.getWidth(),
1741 front.crop.getHeight(),
1742 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001743 }
1744
Ruben Brunk1681d952014-06-27 15:51:55 -07001745 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001746 if (front.active.w != bufWidth ||
1747 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001748 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001749 ALOGE("[%s] rejecting buffer: "
1750 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1751 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001752 return true;
1753 }
1754 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001755
1756 // if the transparent region has changed (this test is
1757 // conservative, but that's fine, worst case we're doing
1758 // a bit of extra work), we latch the new one and we
1759 // trigger a visible-region recompute.
1760 if (!front.activeTransparentRegion.isTriviallyEqual(
1761 front.requestedTransparentRegion)) {
1762 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001763
1764 // We also need to update the current state so that
1765 // we don't end-up overwriting the drawing state with
1766 // this stale current state during the next transaction
1767 //
1768 // NOTE: We don't need to hold the transaction lock here
1769 // because State::active is only accessed from this thread.
1770 current.activeTransparentRegion = front.activeTransparentRegion;
1771
1772 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001773 recomputeVisibleRegions = true;
1774 }
1775
Robert Carr99e27f02016-06-16 15:18:02 -07001776 if (front.crop != front.requestedCrop) {
1777 front.crop = front.requestedCrop;
1778 current.crop = front.requestedCrop;
1779 recomputeVisibleRegions = true;
1780 }
Robert Carra3920732016-06-20 21:49:49 -07001781 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001782
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001783 return false;
1784 }
1785 };
1786
Ruben Brunk1681d952014-06-27 15:51:55 -07001787 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001788 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001789 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001790
Dan Stozacac35382016-01-27 12:21:06 -08001791
1792 // Check all of our local sync points to ensure that all transactions
1793 // which need to have been applied prior to the frame which is about to
1794 // be latched have signaled
1795
1796 auto headFrameNumber = getHeadFrameNumber();
1797 bool matchingFramesFound = false;
1798 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001799 {
Dan Stozacac35382016-01-27 12:21:06 -08001800 Mutex::Autolock lock(mLocalSyncPointMutex);
1801 for (auto& point : mLocalSyncPoints) {
1802 if (point->getFrameNumber() > headFrameNumber) {
1803 break;
1804 }
1805
1806 matchingFramesFound = true;
1807
1808 if (!point->frameIsAvailable()) {
1809 // We haven't notified the remote layer that the frame for
1810 // this point is available yet. Notify it now, and then
1811 // abort this attempt to latch.
1812 point->setFrameAvailable();
1813 allTransactionsApplied = false;
1814 break;
1815 }
1816
1817 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001818 }
1819 }
1820
Dan Stozacac35382016-01-27 12:21:06 -08001821 if (matchingFramesFound && !allTransactionsApplied) {
1822 mFlinger->signalLayerUpdate();
1823 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001824 }
1825
Pablo Ceballos06312182015-10-07 16:32:12 -07001826 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1827 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos2dcb3632016-03-17 15:50:23 -07001828 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001829 // buffer mode.
1830 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001831 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001832 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001833 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001834 if (updateResult == BufferQueue::PRESENT_LATER) {
1835 // Producer doesn't want buffer to be displayed yet. Signal a
1836 // layer update so we check again at the next opportunity.
1837 mFlinger->signalLayerUpdate();
1838 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001839 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1840 // If the buffer has been rejected, remove it from the shadow queue
1841 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001842 if (queuedBuffer) {
1843 Mutex::Autolock lock(mQueueItemLock);
1844 mQueueItems.removeAt(0);
1845 android_atomic_dec(&mQueuedFrames);
1846 }
Dan Stozaecc50402015-04-28 14:42:06 -07001847 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001848 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1849 // This can occur if something goes wrong when trying to create the
1850 // EGLImage for this buffer. If this happens, the buffer has already
1851 // been released, so we need to clean up the queue and bug out
1852 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001853 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001854 Mutex::Autolock lock(mQueueItemLock);
1855 mQueueItems.clear();
1856 android_atomic_and(0, &mQueuedFrames);
1857 }
1858
1859 // Once we have hit this state, the shadow queue may no longer
1860 // correctly reflect the incoming BufferQueue's contents, so even if
1861 // updateTexImage starts working, the only safe course of action is
1862 // to continue to ignore updates.
1863 mUpdateTexImageFailed = true;
1864
1865 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001866 }
1867
Pablo Ceballos06312182015-10-07 16:32:12 -07001868 if (queuedBuffer) {
1869 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001870 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1871
Dan Stoza6b9454d2014-11-07 16:00:59 -08001872 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001873
1874 // Remove any stale buffers that have been dropped during
1875 // updateTexImage
1876 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1877 mQueueItems.removeAt(0);
1878 android_atomic_dec(&mQueuedFrames);
1879 }
1880
Dan Stoza6b9454d2014-11-07 16:00:59 -08001881 mQueueItems.removeAt(0);
1882 }
1883
Dan Stozaecc50402015-04-28 14:42:06 -07001884
Andy McFadden1585c4d2013-06-28 13:52:40 -07001885 // Decrement the queued-frames count. Signal another event if we
1886 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001887 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001888 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001889 mFlinger->signalLayerUpdate();
1890 }
1891
1892 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001893 // something happened!
1894 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001895 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001896 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001897
Jamie Gennis351a5132011-09-14 18:23:37 -07001898 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001899 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001900 if (mActiveBuffer == NULL) {
1901 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001902 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001903 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001904
Mathias Agopian4824d402012-06-04 18:16:30 -07001905 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001906 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001907 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001908 // the first time we receive a buffer, we need to trigger a
1909 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001910 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001911 }
1912
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001913 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1914 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1915 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07001916 if ((crop != mCurrentCrop) ||
1917 (transform != mCurrentTransform) ||
1918 (scalingMode != mCurrentScalingMode))
1919 {
1920 mCurrentCrop = crop;
1921 mCurrentTransform = transform;
1922 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07001923 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001924 }
1925
1926 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07001927 uint32_t bufWidth = mActiveBuffer->getWidth();
1928 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07001929 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
1930 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07001931 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001932 }
1933 }
1934
1935 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08001936 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07001937 recomputeVisibleRegions = true;
1938 }
1939
Dan Stozacac35382016-01-27 12:21:06 -08001940 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1941
1942 // Remove any sync points corresponding to the buffer which was just
1943 // latched
1944 {
1945 Mutex::Autolock lock(mLocalSyncPointMutex);
1946 auto point = mLocalSyncPoints.begin();
1947 while (point != mLocalSyncPoints.end()) {
1948 if (!(*point)->frameIsAvailable() ||
1949 !(*point)->transactionIsApplied()) {
1950 // This sync point must have been added since we started
1951 // latching. Don't drop it yet.
1952 ++point;
1953 continue;
1954 }
1955
1956 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
1957 point = mLocalSyncPoints.erase(point);
1958 } else {
1959 ++point;
1960 }
1961 }
1962 }
1963
Mathias Agopian4fec8732012-06-29 14:12:52 -07001964 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001965 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07001966
1967 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08001968 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07001969 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001970 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001971}
1972
Mathias Agopiana67932f2011-04-20 14:20:59 -07001973uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001974{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001975 // TODO: should we do something special if mSecure is set?
1976 if (mProtectedByApp) {
1977 // need a hardware-protected path to external video sink
1978 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001979 }
Riley Andrews03414a12014-07-01 14:22:59 -07001980 if (mPotentialCursor) {
1981 usage |= GraphicBuffer::USAGE_CURSOR;
1982 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001983 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001984 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001985}
1986
Mathias Agopian84300952012-11-21 16:02:13 -08001987void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001988 uint32_t orientation = 0;
1989 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001990 // The transform hint is used to improve performance, but we can
1991 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001992 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001993 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001994 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001995 if (orientation & Transform::ROT_INVALID) {
1996 orientation = 0;
1997 }
1998 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001999 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002000}
2001
Mathias Agopian13127d82013-03-05 17:47:11 -08002002// ----------------------------------------------------------------------------
2003// debugging
2004// ----------------------------------------------------------------------------
2005
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002006void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002007{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002008 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002009
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002010 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002011 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002012 "+ %s %p (%s)\n",
2013 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002014 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002015
Mathias Agopian2ca79392013-04-02 18:30:32 -07002016 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002017 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002018 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002019 sp<Client> client(mClientRef.promote());
2020
Mathias Agopian74d211a2013-04-22 16:55:35 +02002021 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002022 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2023 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002024 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002025 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Mathias Agopian13127d82013-03-05 17:47:11 -08002026 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002027 s.layerStack, s.z, s.active.transform.tx(), s.active.transform.ty(), s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07002028 s.crop.left, s.crop.top,
2029 s.crop.right, s.crop.bottom,
2030 s.finalCrop.left, s.finalCrop.top,
2031 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002032 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002033 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002034 s.active.transform[0][0], s.active.transform[0][1],
2035 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002036 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002037
2038 sp<const GraphicBuffer> buf0(mActiveBuffer);
2039 uint32_t w0=0, h0=0, s0=0, f0=0;
2040 if (buf0 != 0) {
2041 w0 = buf0->getWidth();
2042 h0 = buf0->getHeight();
2043 s0 = buf0->getStride();
2044 f0 = buf0->format;
2045 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002046 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002047 " "
2048 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2049 " queued-frames=%d, mRefreshPending=%d\n",
2050 mFormat, w0, h0, s0,f0,
2051 mQueuedFrames, mRefreshPending);
2052
Mathias Agopian13127d82013-03-05 17:47:11 -08002053 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002054 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002055 }
2056}
2057
Dan Stozae22aec72016-08-01 13:20:59 -07002058void Layer::miniDumpHeader(String8& result) {
2059 result.append("----------------------------------------");
2060 result.append("---------------------------------------\n");
2061 result.append(" Layer name\n");
2062 result.append(" Z | ");
2063 result.append(" Comp Type | ");
2064 result.append(" Disp Frame (LTRB) | ");
2065 result.append(" Source Crop (LTRB)\n");
2066 result.append("----------------------------------------");
2067 result.append("---------------------------------------\n");
2068}
2069
2070void Layer::miniDump(String8& result, int32_t hwcId) const {
2071 if (mHwcLayers.count(hwcId) == 0) {
2072 return;
2073 }
2074
2075 String8 name;
2076 if (mName.length() > 77) {
2077 std::string shortened;
2078 shortened.append(mName.string(), 36);
2079 shortened.append("[...]");
2080 shortened.append(mName.string() + (mName.length() - 36), 36);
2081 name = shortened.c_str();
2082 } else {
2083 name = mName;
2084 }
2085
2086 result.appendFormat(" %s\n", name.string());
2087
2088 const Layer::State& layerState(getDrawingState());
2089 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2090 result.appendFormat(" %10u | ", layerState.z);
2091 result.appendFormat("%10s | ",
2092 to_string(getCompositionType(hwcId)).c_str());
2093 const Rect& frame = hwcInfo.displayFrame;
2094 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2095 frame.right, frame.bottom);
2096 const FloatRect& crop = hwcInfo.sourceCrop;
2097 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2098 crop.right, crop.bottom);
2099
2100 result.append("- - - - - - - - - - - - - - - - - - - - ");
2101 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2102}
Dan Stozae22aec72016-08-01 13:20:59 -07002103
Svetoslavd85084b2014-03-20 10:28:31 -07002104void Layer::dumpFrameStats(String8& result) const {
2105 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002106}
2107
Svetoslavd85084b2014-03-20 10:28:31 -07002108void Layer::clearFrameStats() {
2109 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002110}
2111
Jamie Gennis6547ff42013-07-16 20:12:42 -07002112void Layer::logFrameStats() {
2113 mFrameTracker.logAndResetStats(mName);
2114}
2115
Svetoslavd85084b2014-03-20 10:28:31 -07002116void Layer::getFrameStats(FrameStats* outStats) const {
2117 mFrameTracker.getStats(outStats);
2118}
2119
Pablo Ceballos40845df2016-01-25 17:41:15 -08002120void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2121 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2122 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2123 *outName = mName;
2124 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2125
Pablo Ceballos40845df2016-01-25 17:41:15 -08002126 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2127 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2128 HWC2::Composition::Client : true;
Pablo Ceballos40845df2016-01-25 17:41:15 -08002129 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2130 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2131 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2132}
Dan Stozae77c7662016-05-13 11:37:28 -07002133
2134std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2135 bool forceFlush) {
2136 std::vector<OccupancyTracker::Segment> history;
2137 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2138 &history);
2139 if (result != NO_ERROR) {
2140 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2141 result);
2142 return {};
2143 }
2144 return history;
2145}
2146
Robert Carr367c5682016-06-20 11:55:28 -07002147bool Layer::getTransformToDisplayInverse() const {
2148 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2149}
2150
Mathias Agopian13127d82013-03-05 17:47:11 -08002151// ---------------------------------------------------------------------------
2152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002153}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002154
2155#if defined(__gl_h_)
2156#error "don't include gl/gl.h in this file"
2157#endif
2158
2159#if defined(__gl2_h_)
2160#error "don't include gl2/gl2.h in this file"
2161#endif