blob: 6250869d1d6b29c4a3405468522bd899e5af9121 [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),
Pablo Ceballos40845df2016-01-25 17:41:15 -080088#ifndef USE_HWC2
89 mIsGlesComposition(false),
90#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080091 mProtectedByApp(false),
92 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070093 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070094 mPotentialCursor(false),
95 mQueueItemLock(),
96 mQueueItemCondition(),
97 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070098 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080099 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700100 mAutoRefresh(false),
101 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103#ifdef USE_HWC2
104 ALOGV("Creating Layer %s", name.string());
105#endif
106
Mathias Agopiana67932f2011-04-20 14:20:59 -0700107 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700108 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700109 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
111 uint32_t layerFlags = 0;
112 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800113 layerFlags |= layer_state_t::eLayerHidden;
114 if (flags & ISurfaceComposerClient::eOpaque)
115 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700116 if (flags & ISurfaceComposerClient::eSecure)
117 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118
119 if (flags & ISurfaceComposerClient::eNonPremultiplied)
120 mPremultipliedAlpha = false;
121
122 mName = name;
123
124 mCurrentState.active.w = w;
125 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800126 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700127 mCurrentState.crop.makeInvalid();
128 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130#ifdef USE_HWC2
131 mCurrentState.alpha = 1.0f;
132#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800134#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135 mCurrentState.layerStack = 0;
136 mCurrentState.flags = layerFlags;
137 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138 mCurrentState.requested = mCurrentState.active;
139
140 // drawing state & current state are identical
141 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700142
Dan Stoza9e56aa02015-11-02 13:00:03 -0800143#ifdef USE_HWC2
144 const auto& hwc = flinger->getHwComposer();
145 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
146 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
147#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 nsecs_t displayPeriod =
149 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700151 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800152}
153
Mathias Agopian3f844832013-08-07 21:24:32 -0700154void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800155 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700156 sp<IGraphicBufferProducer> producer;
157 sp<IGraphicBufferConsumer> consumer;
Irvel468051e2016-06-13 16:44:44 -0700158 BufferQueue::createBufferQueue(&producer, &consumer, nullptr, true);
Dan Stozab9b08832014-03-13 11:55:57 -0700159 mProducer = new MonitoredProducer(producer, mFlinger);
Irvel468051e2016-06-13 16:44:44 -0700160 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800161 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800162 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700163 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800164
Dan Stozac9d97202016-03-03 08:20:27 -0800165#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700166 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800167#endif
Andy McFadden69052052012-09-14 16:10:11 -0700168
Mathias Agopian84300952012-11-21 16:02:13 -0800169 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
170 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700171}
172
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700173Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800174 sp<Client> c(mClientRef.promote());
175 if (c != 0) {
176 c->detachLayer(this);
177 }
178
Dan Stozacac35382016-01-27 12:21:06 -0800179 for (auto& point : mRemoteSyncPoints) {
180 point->setTransactionApplied();
181 }
Dan Stozac8145172016-04-28 16:29:06 -0700182 for (auto& point : mLocalSyncPoints) {
183 point->setFrameAvailable();
184 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700186 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// callbacks
191// ---------------------------------------------------------------------------
192
Dan Stoza9e56aa02015-11-02 13:00:03 -0800193#ifdef USE_HWC2
194void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
195 if (mHwcLayers.empty()) {
196 return;
197 }
198 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
199}
200#else
Dan Stozac7014012014-02-14 15:03:43 -0800201void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 HWComposer::HWCLayerInterface* layer) {
203 if (layer) {
204 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700205 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 }
207}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800209
Dan Stoza6b9454d2014-11-07 16:00:59 -0800210void Layer::onFrameAvailable(const BufferItem& item) {
211 // Add this buffer from our internal queue tracker
212 { // Autolock scope
213 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700214 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
215 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700216 // Reset the frame number tracker when we receive the first buffer after
217 // a frame number reset
218 if (item.mFrameNumber == 1) {
219 mLastFrameNumberReceived = 0;
220 }
221
222 // 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 }
229 }
230
Dan Stoza6b9454d2014-11-07 16:00:59 -0800231 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700232 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700233
234 // Wake up any pending callbacks
235 mLastFrameNumberReceived = item.mFrameNumber;
236 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800237 }
238
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800239 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700240}
241
Dan Stoza6b9454d2014-11-07 16:00:59 -0800242void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700243 { // Autolock scope
244 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700245
Dan Stoza7dde5992015-05-22 09:51:44 -0700246 // Ensure that callbacks are handled in order
247 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
248 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
249 ms2ns(500));
250 if (result != NO_ERROR) {
251 ALOGE("[%s] Timed out waiting on callback", mName.string());
252 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700253 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700254
255 if (mQueueItems.empty()) {
256 ALOGE("Can't replace a frame on an empty queue");
257 return;
258 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700259 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700260
261 // Wake up any pending callbacks
262 mLastFrameNumberReceived = item.mFrameNumber;
263 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700264 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800265}
266
Jesse Hall399184a2014-03-03 15:42:54 -0800267void Layer::onSidebandStreamChanged() {
268 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
269 // mSidebandStreamChanged was false
270 mFlinger->signalLayerUpdate();
271 }
272}
273
Mathias Agopian67106042013-03-14 19:18:13 -0700274// called with SurfaceFlinger::mStateLock from the drawing thread after
275// the layer has been remove from the current state list (and just before
276// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800277void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800278 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700279}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280
Mathias Agopian13127d82013-03-05 17:47:11 -0800281// ---------------------------------------------------------------------------
282// set-up
283// ---------------------------------------------------------------------------
284
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700285const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800286 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287}
288
Mathias Agopianf9d93272009-06-19 17:00:27 -0700289status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 PixelFormat format, uint32_t flags)
291{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700292 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700294
295 // never allow a surface larger than what our underlying GL implementation
296 // can handle.
297 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800298 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700299 return BAD_VALUE;
300 }
301
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700303
Riley Andrews03414a12014-07-01 14:22:59 -0700304 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700305 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700306 mCurrentOpacity = getOpacityForFormat(format);
307
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800308 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
309 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
310 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700315sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800316 Mutex::Autolock _l(mLock);
317
318 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700319 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800320
321 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700322
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700323 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800324}
325
Dan Stozab9b08832014-03-13 11:55:57 -0700326sp<IGraphicBufferProducer> Layer::getProducer() const {
327 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700328}
329
Mathias Agopian13127d82013-03-05 17:47:11 -0800330// ---------------------------------------------------------------------------
331// h/w composer set-up
332// ---------------------------------------------------------------------------
333
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800334Rect Layer::getContentCrop() const {
335 // this is the crop rectangle that applies to the buffer
336 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700337 Rect crop;
338 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800339 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700340 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800341 } else if (mActiveBuffer != NULL) {
342 // otherwise we use the whole buffer
343 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700344 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800345 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700346 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700347 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700348 return crop;
349}
350
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700351static Rect reduce(const Rect& win, const Region& exclude) {
352 if (CC_LIKELY(exclude.isEmpty())) {
353 return win;
354 }
355 if (exclude.isRect()) {
356 return win.reduce(exclude.getBounds());
357 }
358 return Region(win).subtract(exclude).getBounds();
359}
360
Mathias Agopian13127d82013-03-05 17:47:11 -0800361Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700362 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700363 return computeBounds(s.activeTransparentRegion);
364}
365
366Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
367 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800368 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700369
370 if (!s.crop.isEmpty()) {
371 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800372 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700373 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700374 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800375}
376
Mathias Agopian6b442672013-07-09 21:24:52 -0700377FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800378 // the content crop is the area of the content that gets scaled to the
379 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700380 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800381
Robert Carrb5d3d262016-03-25 15:08:13 -0700382 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700384 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800385
386 // apply the projection's clipping to the window crop in
387 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700388 // if there are no window scaling involved, this operation will map to full
389 // pixels in the buffer.
390 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
391 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700392
393 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700394 if (!s.crop.isEmpty()) {
395 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700396 }
397
Robert Carr3dcabfa2016-03-01 18:36:58 -0800398 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000399 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
400 activeCrop.clear();
401 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700402 if (!s.finalCrop.isEmpty()) {
403 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 activeCrop.clear();
405 }
406 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800407 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800408
Michael Lentine28ea2172014-11-19 18:32:37 -0800409 // This needs to be here as transform.transform(Rect) computes the
410 // transformed rect and then takes the bounding box of the result before
411 // returning. This means
412 // transform.inverse().transform(transform.transform(Rect)) != Rect
413 // in which case we need to make sure the final rect is clipped to the
414 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
416 activeCrop.clear();
417 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700419 // subtract the transparent region and snap to the bounds
420 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
421
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 // Transform the window crop to match the buffer coordinate system,
423 // which means using the inverse of the current transform set on the
424 // SurfaceFlingerConsumer.
425 uint32_t invTransform = mCurrentTransform;
426 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
427 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700428 * the code below applies the primary display's inverse transform to the
429 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700431 uint32_t invTransformOrient =
432 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // calculate the inverse transform
434 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
435 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
436 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800437 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700439 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
440 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800441 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442
443 int winWidth = s.active.w;
444 int winHeight = s.active.h;
445 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
446 // If the activeCrop has been rotate the ends are rotated but not
447 // the space itself so when transforming ends back we can't rely on
448 // a modification of the axes of rotation. To account for this we
449 // need to reorient the inverse rotation in terms of the current
450 // axes of rotation.
451 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
452 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
453 if (is_h_flipped == is_v_flipped) {
454 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
455 NATIVE_WINDOW_TRANSFORM_FLIP_H;
456 }
457 winWidth = s.active.h;
458 winHeight = s.active.w;
459 }
460 const Rect winCrop = activeCrop.transform(
461 invTransform, s.active.w, s.active.h);
462
463 // below, crop is intersected with winCrop expressed in crop's coordinate space
464 float xScale = crop.getWidth() / float(winWidth);
465 float yScale = crop.getHeight() / float(winHeight);
466
467 float insetL = winCrop.left * xScale;
468 float insetT = winCrop.top * yScale;
469 float insetR = (winWidth - winCrop.right ) * xScale;
470 float insetB = (winHeight - winCrop.bottom) * yScale;
471
472 crop.left += insetL;
473 crop.top += insetT;
474 crop.right -= insetR;
475 crop.bottom -= insetB;
476
Mathias Agopian13127d82013-03-05 17:47:11 -0800477 return crop;
478}
479
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480#ifdef USE_HWC2
481void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
482#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700483void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700484 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700485 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700487{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488#ifdef USE_HWC2
489 const auto hwcId = displayDevice->getHwcDisplayId();
490 auto& hwcInfo = mHwcLayers[hwcId];
491#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800492 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700494
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700495 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496#ifdef USE_HWC2
497 hwcInfo.forceClientComposition = false;
498
499 if (isSecure() && !displayDevice->isSecure()) {
500 hwcInfo.forceClientComposition = true;
501 }
502
503 auto& hwcLayer = hwcInfo.layer;
504#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700506
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700507 if (isSecure() && !hw->isSecure()) {
508 layer.setSkip(true);
509 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700511
Mathias Agopian13127d82013-03-05 17:47:11 -0800512 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700513 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514#ifdef USE_HWC2
515 if (!isOpaque(s) || s.alpha != 1.0f) {
516 auto blendMode = mPremultipliedAlpha ?
517 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
518 auto error = hwcLayer->setBlendMode(blendMode);
519 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
520 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
521 to_string(error).c_str(), static_cast<int32_t>(error));
522 }
523#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800524 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800525 layer.setBlending(mPremultipliedAlpha ?
526 HWC_BLENDING_PREMULT :
527 HWC_BLENDING_COVERAGE);
528 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800530
531 // apply the layer's transform, followed by the display's global transform
532 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700534 if (!s.crop.isEmpty()) {
535 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800536 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800537#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800539#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000540 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 activeCrop.clear();
543 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800544 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800545 // This needs to be here as transform.transform(Rect) computes the
546 // transformed rect and then takes the bounding box of the result before
547 // returning. This means
548 // transform.inverse().transform(transform.transform(Rect)) != Rect
549 // in which case we need to make sure the final rect is clipped to the
550 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000551 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
552 activeCrop.clear();
553 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700554 // mark regions outside the crop as transparent
555 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
556 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
557 s.active.w, s.active.h));
558 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
559 activeCrop.left, activeCrop.bottom));
560 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
561 s.active.w, activeCrop.bottom));
562 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800563 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700564 if (!s.finalCrop.isEmpty()) {
565 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000566 frame.clear();
567 }
568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000570 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
571 frame.clear();
572 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 const Transform& tr(displayDevice->getTransform());
574 Rect transformedFrame = tr.transform(frame);
575 auto error = hwcLayer->setDisplayFrame(transformedFrame);
576 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
577 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
578 transformedFrame.top, transformedFrame.right,
579 transformedFrame.bottom, to_string(error).c_str(),
580 static_cast<int32_t>(error));
581
582 FloatRect sourceCrop = computeCrop(displayDevice);
583 error = hwcLayer->setSourceCrop(sourceCrop);
584 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
585 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
586 sourceCrop.left, sourceCrop.top, sourceCrop.right,
587 sourceCrop.bottom, to_string(error).c_str(),
588 static_cast<int32_t>(error));
589
590 error = hwcLayer->setPlaneAlpha(s.alpha);
591 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
592 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
593 static_cast<int32_t>(error));
594
595 error = hwcLayer->setZOrder(s.z);
596 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
597 mName.string(), s.z, to_string(error).c_str(),
598 static_cast<int32_t>(error));
599#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000600 if (!frame.intersect(hw->getViewport(), &frame)) {
601 frame.clear();
602 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800603 const Transform& tr(hw->getTransform());
604 layer.setFrame(tr.transform(frame));
605 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800606 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800607#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800608
Mathias Agopian29a367b2011-07-12 14:51:45 -0700609 /*
610 * Transformations are applied in this order:
611 * 1) buffer orientation/flip/mirror
612 * 2) state transformation (window manager)
613 * 3) layer orientation (screen orientation)
614 * (NOTE: the matrices are multiplied in reverse order)
615 */
616
617 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800618 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619
620 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
621 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700622 * the code below applies the primary display's inverse transform to the
623 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700625 uint32_t invTransform =
626 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 // calculate the inverse transform
628 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
629 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
630 NATIVE_WINDOW_TRANSFORM_FLIP_H;
631 }
632 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700633 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700635
636 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800637 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638#ifdef USE_HWC2
639 if (orientation & Transform::ROT_INVALID) {
640 // we can only handle simple transformation
641 hwcInfo.forceClientComposition = true;
642 } else {
643 auto transform = static_cast<HWC2::Transform>(orientation);
644 auto error = hwcLayer->setTransform(transform);
645 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
646 "%s (%d)", mName.string(), to_string(transform).c_str(),
647 to_string(error).c_str(), static_cast<int32_t>(error));
648 }
649#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800650 if (orientation & Transform::ROT_INVALID) {
651 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700652 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700653 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800654 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700655 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700657}
658
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659#ifdef USE_HWC2
660void Layer::forceClientComposition(int32_t hwcId) {
661 if (mHwcLayers.count(hwcId) == 0) {
662 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
663 return;
664 }
665
666 mHwcLayers[hwcId].forceClientComposition = true;
667}
668#endif
669
670#ifdef USE_HWC2
671void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
672 // Apply this display's projection's viewport to the visible region
673 // before giving it to the HWC HAL.
674 const Transform& tr = displayDevice->getTransform();
675 const auto& viewport = displayDevice->getViewport();
676 Region visible = tr.transform(visibleRegion.intersect(viewport));
677 auto hwcId = displayDevice->getHwcDisplayId();
678 auto& hwcLayer = mHwcLayers[hwcId].layer;
679 auto error = hwcLayer->setVisibleRegion(visible);
680 if (error != HWC2::Error::None) {
681 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
682 to_string(error).c_str(), static_cast<int32_t>(error));
683 visible.dump(LOG_TAG);
684 }
685
686 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
687 if (error != HWC2::Error::None) {
688 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
689 to_string(error).c_str(), static_cast<int32_t>(error));
690 surfaceDamageRegion.dump(LOG_TAG);
691 }
692
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700693 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700695 setCompositionType(hwcId, HWC2::Composition::Sideband);
696 ALOGV("[%s] Requesting Sideband composition", mName.string());
697 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 if (error != HWC2::Error::None) {
699 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
700 mName.string(), mSidebandStream->handle(),
701 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700703 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 }
705
Dan Stoza0a21df72016-07-20 12:52:38 -0700706 // Client layers
707 if (mHwcLayers[hwcId].forceClientComposition ||
708 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700709 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700711 return;
712 }
713
Dan Stoza0a21df72016-07-20 12:52:38 -0700714 // SolidColor layers
715 if (mActiveBuffer == nullptr) {
716 setCompositionType(hwcId, HWC2::Composition::SolidColor);
717 error = hwcLayer->setColor({0, 0, 0, 255});
718 if (error != HWC2::Error::None) {
719 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
720 to_string(error).c_str(), static_cast<int32_t>(error));
721 }
722 return;
723 }
724
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700725 // Device or Cursor layers
726 if (mPotentialCursor) {
727 ALOGV("[%s] Requesting Cursor composition", mName.string());
728 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800729 } else {
730 ALOGV("[%s] Requesting Device composition", mName.string());
731 setCompositionType(hwcId, HWC2::Composition::Device);
732 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700733
734 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
735 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
736 if (error != HWC2::Error::None) {
737 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
738 mActiveBuffer->handle, to_string(error).c_str(),
739 static_cast<int32_t>(error));
740 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741}
742#else
Mathias Agopian42977342012-08-05 00:40:46 -0700743void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700744 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800745 // we have to set the visible region on every frame because
746 // we currently free it during onLayerDisplayed(), which is called
747 // after HWComposer::commit() -- every frame.
748 // Apply this display's projection's viewport to the visible region
749 // before giving it to the HWC HAL.
750 const Transform& tr = hw->getTransform();
751 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
752 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700753 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800754 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700755
Jesse Hall399184a2014-03-03 15:42:54 -0800756 if (mSidebandStream.get()) {
757 layer.setSidebandStream(mSidebandStream);
758 } else {
759 // NOTE: buffer can be NULL if the client never drew into this
760 // layer yet, or if we ran out of memory
761 layer.setBuffer(mActiveBuffer);
762 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700763}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700765
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766#ifdef USE_HWC2
767void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
768 auto hwcId = displayDevice->getHwcDisplayId();
769 if (mHwcLayers.count(hwcId) == 0 ||
770 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
771 return;
772 }
773
774 // This gives us only the "orientation" component of the transform
775 const State& s(getCurrentState());
776
777 // Apply the layer's transform, followed by the display's global transform
778 // Here we're guaranteed that the layer's transform preserves rects
779 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700780 if (!s.crop.isEmpty()) {
781 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782 }
783 // Subtract the transparent region and snap to the bounds
784 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800785 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700787 if (!s.finalCrop.isEmpty()) {
788 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000789 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790 auto& displayTransform(displayDevice->getTransform());
791 auto position = displayTransform.transform(frame);
792
793 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
794 position.top);
795 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
796 "to (%d, %d): %s (%d)", mName.string(), position.left,
797 position.top, to_string(error).c_str(),
798 static_cast<int32_t>(error));
799}
800#else
Dan Stozac7014012014-02-14 15:03:43 -0800801void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700802 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700803 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700804
805 // TODO: there is a possible optimization here: we only need to set the
806 // acquire fence the first time a new buffer is acquired on EACH display.
807
Riley Andrews03414a12014-07-01 14:22:59 -0700808 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800809 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800810 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700811 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700812 if (fenceFd == -1) {
813 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
814 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700815 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700816 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700817 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700818}
819
Riley Andrews03414a12014-07-01 14:22:59 -0700820Rect Layer::getPosition(
821 const sp<const DisplayDevice>& hw)
822{
823 // this gives us only the "orientation" component of the transform
824 const State& s(getCurrentState());
825
826 // apply the layer's transform, followed by the display's global transform
827 // here we're guaranteed that the layer's transform preserves rects
828 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700829 if (!s.crop.isEmpty()) {
830 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700831 }
832 // subtract the transparent region and snap to the bounds
833 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800834 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700835 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700836 if (!s.finalCrop.isEmpty()) {
837 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000838 }
Riley Andrews03414a12014-07-01 14:22:59 -0700839 const Transform& tr(hw->getTransform());
840 return Rect(tr.transform(frame));
841}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800842#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700843
Mathias Agopian13127d82013-03-05 17:47:11 -0800844// ---------------------------------------------------------------------------
845// drawing...
846// ---------------------------------------------------------------------------
847
848void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800849 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800850}
851
Dan Stozac7014012014-02-14 15:03:43 -0800852void Layer::draw(const sp<const DisplayDevice>& hw,
853 bool useIdentityTransform) const {
854 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800855}
856
Dan Stozac7014012014-02-14 15:03:43 -0800857void Layer::draw(const sp<const DisplayDevice>& hw) const {
858 onDraw(hw, Region(hw->bounds()), false);
859}
860
861void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
862 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800864 ATRACE_CALL();
865
Mathias Agopiana67932f2011-04-20 14:20:59 -0700866 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700868 // in fact never been drawn into. This happens frequently with
869 // SurfaceView because the WindowManager can't know when the client
870 // has drawn the first time.
871
872 // If there is nothing under us, we paint the screen in black, otherwise
873 // we just skip this update.
874
875 // figure out if there is something below us
876 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700877 const SurfaceFlinger::LayerVector& drawingLayers(
878 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700879 const size_t count = drawingLayers.size();
880 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 const sp<Layer>& layer(drawingLayers[i]);
882 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700883 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700884 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700885 }
886 // if not everything below us is covered, we plug the holes!
887 Region holes(clip.subtract(under));
888 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700889 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700890 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 return;
892 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700893
Andy McFadden97eba892012-12-11 15:21:45 -0800894 // Bind the current buffer to the GL texture, and wait for it to be
895 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800896 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
897 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800898 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700899 // Go ahead and draw the buffer anyway; no matter what we do the screen
900 // is probably going to have something visibly wrong.
901 }
902
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700903 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
904
Mathias Agopian875d8e12013-06-07 15:35:48 -0700905 RenderEngine& engine(mFlinger->getRenderEngine());
906
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700907 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700908 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700909 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700910
911 // Query the texture matrix given our current filtering mode.
912 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800913 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
914 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700915
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700916 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
917
918 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700919 * the code below applies the primary display's inverse transform to
920 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700921 */
922
923 // create a 4x4 transform matrix from the display transform flags
924 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
925 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
926 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
927
928 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700929 uint32_t transform =
930 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700931 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
932 tr = tr * rot90;
933 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
934 tr = tr * flipH;
935 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
936 tr = tr * flipV;
937
938 // calculate the inverse
939 tr = inverse(tr);
940
941 // and finally apply it to the original texture matrix
942 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
943 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
944 }
945
Jamie Genniscbb1a952012-05-08 17:05:52 -0700946 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700947 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
948 mTexture.setFiltering(useFiltering);
949 mTexture.setMatrix(textureMatrix);
950
951 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700952 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700953 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700954 }
Dan Stozac7014012014-02-14 15:03:43 -0800955 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700956 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957}
958
Mathias Agopian13127d82013-03-05 17:47:11 -0800959
Dan Stozac7014012014-02-14 15:03:43 -0800960void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
961 const Region& /* clip */, float red, float green, float blue,
962 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800963{
Mathias Agopian19733a32013-08-28 18:13:56 -0700964 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800965 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700966 engine.setupFillWithColor(red, green, blue, alpha);
967 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800968}
969
970void Layer::clearWithOpenGL(
971 const sp<const DisplayDevice>& hw, const Region& clip) const {
972 clearWithOpenGL(hw, clip, 0,0,0,0);
973}
974
Dan Stozac7014012014-02-14 15:03:43 -0800975void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
976 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700977 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800978
Dan Stozac7014012014-02-14 15:03:43 -0800979 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800980
Mathias Agopian13127d82013-03-05 17:47:11 -0800981 /*
982 * NOTE: the way we compute the texture coordinates here produces
983 * different results than when we take the HWC path -- in the later case
984 * the "source crop" is rounded to texel boundaries.
985 * This can produce significantly different results when the texture
986 * is scaled by a large amount.
987 *
988 * The GL code below is more logical (imho), and the difference with
989 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700990 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -0800991 * GL composition when a buffer scaling is applied (maybe with some
992 * minimal value)? Or, we could make GL behave like HWC -- but this feel
993 * like more of a hack.
994 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000995 Rect win(computeBounds());
996
Robert Carrb5d3d262016-03-25 15:08:13 -0700997 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000998 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -0700999 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001000 win.clear();
1001 }
1002 win = s.active.transform.inverse().transform(win);
1003 if (!win.intersect(computeBounds(), &win)) {
1004 win.clear();
1005 }
1006 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001007
Mathias Agopian3f844832013-08-07 21:24:32 -07001008 float left = float(win.left) / float(s.active.w);
1009 float top = float(win.top) / float(s.active.h);
1010 float right = float(win.right) / float(s.active.w);
1011 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001012
Mathias Agopian875d8e12013-06-07 15:35:48 -07001013 // TODO: we probably want to generate the texture coords with the mesh
1014 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001015 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1016 texCoords[0] = vec2(left, 1.0f - top);
1017 texCoords[1] = vec2(left, 1.0f - bottom);
1018 texCoords[2] = vec2(right, 1.0f - bottom);
1019 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001020
Mathias Agopian875d8e12013-06-07 15:35:48 -07001021 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001022 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001023 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001024 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001025}
1026
Dan Stoza9e56aa02015-11-02 13:00:03 -08001027#ifdef USE_HWC2
1028void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1029 bool callIntoHwc) {
1030 if (mHwcLayers.count(hwcId) == 0) {
1031 ALOGE("setCompositionType called without a valid HWC layer");
1032 return;
1033 }
1034 auto& hwcInfo = mHwcLayers[hwcId];
1035 auto& hwcLayer = hwcInfo.layer;
1036 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1037 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1038 if (hwcInfo.compositionType != type) {
1039 ALOGV(" actually setting");
1040 hwcInfo.compositionType = type;
1041 if (callIntoHwc) {
1042 auto error = hwcLayer->setCompositionType(type);
1043 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1044 "composition type %s: %s (%d)", mName.string(),
1045 to_string(type).c_str(), to_string(error).c_str(),
1046 static_cast<int32_t>(error));
1047 }
1048 }
1049}
1050
1051HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001052 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1053 // If we're querying the composition type for a display that does not
1054 // have a HWC counterpart, then it will always be Client
1055 return HWC2::Composition::Client;
1056 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001057 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001058 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001059 return HWC2::Composition::Invalid;
1060 }
1061 return mHwcLayers.at(hwcId).compositionType;
1062}
1063
1064void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1065 if (mHwcLayers.count(hwcId) == 0) {
1066 ALOGE("setClearClientTarget called without a valid HWC layer");
1067 return;
1068 }
1069 mHwcLayers[hwcId].clearClientTarget = clear;
1070}
1071
1072bool Layer::getClearClientTarget(int32_t hwcId) const {
1073 if (mHwcLayers.count(hwcId) == 0) {
1074 ALOGE("getClearClientTarget called without a valid HWC layer");
1075 return false;
1076 }
1077 return mHwcLayers.at(hwcId).clearClientTarget;
1078}
1079#endif
1080
Ruben Brunk1681d952014-06-27 15:51:55 -07001081uint32_t Layer::getProducerStickyTransform() const {
1082 int producerStickyTransform = 0;
1083 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1084 if (ret != OK) {
1085 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1086 strerror(-ret), ret);
1087 return 0;
1088 }
1089 return static_cast<uint32_t>(producerStickyTransform);
1090}
1091
Dan Stozac5da2712016-07-20 15:38:12 -07001092bool Layer::latchUnsignaledBuffers() {
1093 static bool propertyLoaded = false;
1094 static bool latch = false;
1095 static std::mutex mutex;
1096 std::lock_guard<std::mutex> lock(mutex);
1097 if (!propertyLoaded) {
1098 char value[PROPERTY_VALUE_MAX] = {};
1099 property_get("debug.sf.latch_unsignaled", value, "0");
1100 latch = atoi(value);
1101 propertyLoaded = true;
1102 }
1103 return latch;
1104}
1105
Dan Stozacac35382016-01-27 12:21:06 -08001106uint64_t Layer::getHeadFrameNumber() const {
1107 Mutex::Autolock lock(mQueueItemLock);
1108 if (!mQueueItems.empty()) {
1109 return mQueueItems[0].mFrameNumber;
1110 } else {
1111 return mCurrentFrameNumber;
1112 }
1113}
1114
Dan Stoza1ce65812016-06-15 16:26:27 -07001115bool Layer::headFenceHasSignaled() const {
1116#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001117 if (latchUnsignaledBuffers()) {
1118 return true;
1119 }
1120
Dan Stoza1ce65812016-06-15 16:26:27 -07001121 Mutex::Autolock lock(mQueueItemLock);
1122 if (mQueueItems.empty()) {
1123 return true;
1124 }
1125 if (mQueueItems[0].mIsDroppable) {
1126 // Even though this buffer's fence may not have signaled yet, it could
1127 // be replaced by another buffer before it has a chance to, which means
1128 // that it's possible to get into a situation where a buffer is never
1129 // able to be latched. To avoid this, grab this buffer anyway.
1130 return true;
1131 }
1132 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1133#else
1134 return true;
1135#endif
1136}
1137
Dan Stozacac35382016-01-27 12:21:06 -08001138bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1139 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1140 // Don't bother with a SyncPoint, since we've already latched the
1141 // relevant frame
1142 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001143 }
1144
Dan Stozacac35382016-01-27 12:21:06 -08001145 Mutex::Autolock lock(mLocalSyncPointMutex);
1146 mLocalSyncPoints.push_back(point);
1147 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001148}
1149
Mathias Agopian13127d82013-03-05 17:47:11 -08001150void Layer::setFiltering(bool filtering) {
1151 mFiltering = filtering;
1152}
1153
1154bool Layer::getFiltering() const {
1155 return mFiltering;
1156}
1157
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001158// As documented in libhardware header, formats in the range
1159// 0x100 - 0x1FF are specific to the HAL implementation, and
1160// are known to have no alpha channel
1161// TODO: move definition for device-specific range into
1162// hardware.h, instead of using hard-coded values here.
1163#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1164
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001165bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001166 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1167 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001168 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001169 switch (format) {
1170 case HAL_PIXEL_FORMAT_RGBA_8888:
1171 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001172 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001173 }
1174 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001175 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001176}
1177
Mathias Agopian13127d82013-03-05 17:47:11 -08001178// ----------------------------------------------------------------------------
1179// local state
1180// ----------------------------------------------------------------------------
1181
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001182static void boundPoint(vec2* point, const Rect& crop) {
1183 if (point->x < crop.left) {
1184 point->x = crop.left;
1185 }
1186 if (point->x > crop.right) {
1187 point->x = crop.right;
1188 }
1189 if (point->y < crop.top) {
1190 point->y = crop.top;
1191 }
1192 if (point->y > crop.bottom) {
1193 point->y = crop.bottom;
1194 }
1195}
1196
Dan Stozac7014012014-02-14 15:03:43 -08001197void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1198 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001199{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001200 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001201 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001202 const uint32_t hw_h = hw->getHeight();
1203 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001204 if (!s.crop.isEmpty()) {
1205 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001207 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001208 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001209
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001210 vec2 lt = vec2(win.left, win.top);
1211 vec2 lb = vec2(win.left, win.bottom);
1212 vec2 rb = vec2(win.right, win.bottom);
1213 vec2 rt = vec2(win.right, win.top);
1214
1215 if (!useIdentityTransform) {
1216 lt = s.active.transform.transform(lt);
1217 lb = s.active.transform.transform(lb);
1218 rb = s.active.transform.transform(rb);
1219 rt = s.active.transform.transform(rt);
1220 }
1221
Robert Carrb5d3d262016-03-25 15:08:13 -07001222 if (!s.finalCrop.isEmpty()) {
1223 boundPoint(&lt, s.finalCrop);
1224 boundPoint(&lb, s.finalCrop);
1225 boundPoint(&rb, s.finalCrop);
1226 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001227 }
1228
Mathias Agopianff2ed702013-09-01 21:36:12 -07001229 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001230 position[0] = tr.transform(lt);
1231 position[1] = tr.transform(lb);
1232 position[2] = tr.transform(rb);
1233 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001234 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001235 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 }
1237}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001238
Andy McFadden4125a4f2014-01-29 17:17:11 -08001239bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001240{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001241 // if we don't have a buffer yet, we're translucent regardless of the
1242 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001243 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001244 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001245 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001246
1247 // if the layer has the opaque flag, then we're always opaque,
1248 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001249 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001250}
1251
Dan Stoza23116082015-06-18 14:58:39 -07001252bool Layer::isSecure() const
1253{
1254 const Layer::State& s(mDrawingState);
1255 return (s.flags & layer_state_t::eLayerSecure);
1256}
1257
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001258bool Layer::isProtected() const
1259{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001260 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001261 return (activeBuffer != 0) &&
1262 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1263}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001264
Mathias Agopian13127d82013-03-05 17:47:11 -08001265bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001266 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001267}
1268
1269bool Layer::isCropped() const {
1270 return !mCurrentCrop.isEmpty();
1271}
1272
1273bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1274 return mNeedsFiltering || hw->needsFiltering();
1275}
1276
1277void Layer::setVisibleRegion(const Region& visibleRegion) {
1278 // always called from main thread
1279 this->visibleRegion = visibleRegion;
1280}
1281
1282void Layer::setCoveredRegion(const Region& coveredRegion) {
1283 // always called from main thread
1284 this->coveredRegion = coveredRegion;
1285}
1286
1287void Layer::setVisibleNonTransparentRegion(const Region&
1288 setVisibleNonTransparentRegion) {
1289 // always called from main thread
1290 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1291}
1292
1293// ----------------------------------------------------------------------------
1294// transaction
1295// ----------------------------------------------------------------------------
1296
Dan Stoza7dde5992015-05-22 09:51:44 -07001297void Layer::pushPendingState() {
1298 if (!mCurrentState.modified) {
1299 return;
1300 }
1301
Dan Stoza7dde5992015-05-22 09:51:44 -07001302 // If this transaction is waiting on the receipt of a frame, generate a sync
1303 // point and send it to the remote layer.
1304 if (mCurrentState.handle != nullptr) {
1305 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1306 sp<Layer> handleLayer = handle->owner.promote();
1307 if (handleLayer == nullptr) {
1308 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1309 // If we can't promote the layer we are intended to wait on,
1310 // then it is expired or otherwise invalid. Allow this transaction
1311 // to be applied as per normal (no synchronization).
1312 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001313 } else {
1314 auto syncPoint = std::make_shared<SyncPoint>(
1315 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001316 if (handleLayer->addSyncPoint(syncPoint)) {
1317 mRemoteSyncPoints.push_back(std::move(syncPoint));
1318 } else {
1319 // We already missed the frame we're supposed to synchronize
1320 // on, so go ahead and apply the state update
1321 mCurrentState.handle = nullptr;
1322 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 }
1324
Dan Stoza7dde5992015-05-22 09:51:44 -07001325 // Wake us up to check if the frame has been received
1326 setTransactionFlags(eTransactionNeeded);
1327 }
1328 mPendingStates.push_back(mCurrentState);
1329}
1330
Pablo Ceballos05289c22016-04-14 15:49:55 -07001331void Layer::popPendingState(State* stateToCommit) {
1332 auto oldFlags = stateToCommit->flags;
1333 *stateToCommit = mPendingStates[0];
1334 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1335 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001336
1337 mPendingStates.removeAt(0);
1338}
1339
Pablo Ceballos05289c22016-04-14 15:49:55 -07001340bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001341 bool stateUpdateAvailable = false;
1342 while (!mPendingStates.empty()) {
1343 if (mPendingStates[0].handle != nullptr) {
1344 if (mRemoteSyncPoints.empty()) {
1345 // If we don't have a sync point for this, apply it anyway. It
1346 // will be visually wrong, but it should keep us from getting
1347 // into too much trouble.
1348 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001349 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001350 stateUpdateAvailable = true;
1351 continue;
1352 }
1353
Dan Stozacac35382016-01-27 12:21:06 -08001354 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1355 mPendingStates[0].frameNumber) {
1356 ALOGE("[%s] Unexpected sync point frame number found",
1357 mName.string());
1358
1359 // Signal our end of the sync point and then dispose of it
1360 mRemoteSyncPoints.front()->setTransactionApplied();
1361 mRemoteSyncPoints.pop_front();
1362 continue;
1363 }
1364
Dan Stoza7dde5992015-05-22 09:51:44 -07001365 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1366 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001367 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 stateUpdateAvailable = true;
1369
1370 // Signal our end of the sync point and then dispose of it
1371 mRemoteSyncPoints.front()->setTransactionApplied();
1372 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001373 } else {
1374 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001375 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001376 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001377 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001378 stateUpdateAvailable = true;
1379 }
1380 }
1381
1382 // If we still have pending updates, wake SurfaceFlinger back up and point
1383 // it at this layer so we can process them
1384 if (!mPendingStates.empty()) {
1385 setTransactionFlags(eTransactionNeeded);
1386 mFlinger->setTransactionFlags(eTraversalNeeded);
1387 }
1388
1389 mCurrentState.modified = false;
1390 return stateUpdateAvailable;
1391}
1392
1393void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001394 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001395 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001396 Mutex::Autolock lock(mLocalSyncPointMutex);
1397 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001398 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001399 point->setFrameAvailable();
1400 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001401 }
1402}
1403
Mathias Agopian13127d82013-03-05 17:47:11 -08001404uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001405 ATRACE_CALL();
1406
Dan Stoza7dde5992015-05-22 09:51:44 -07001407 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001408 Layer::State c = getCurrentState();
1409 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001410 return 0;
1411 }
1412
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001413 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1416 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001417
1418 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001419 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001420 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001421 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001422 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001423 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001424 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001425 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001426 this, getName().string(), mCurrentTransform,
1427 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001428 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001429 c.crop.left,
1430 c.crop.top,
1431 c.crop.right,
1432 c.crop.bottom,
1433 c.crop.getWidth(),
1434 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001435 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001436 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001437 s.crop.left,
1438 s.crop.top,
1439 s.crop.right,
1440 s.crop.bottom,
1441 s.crop.getWidth(),
1442 s.crop.getHeight(),
1443 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001445 // record the new size, form this point on, when the client request
1446 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001447 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001448 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001450
Robert Carr82364e32016-05-15 11:27:47 -07001451 const bool resizePending = (c.requested.w != c.active.w) ||
1452 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001453 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001454 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001456 // if we have a pending resize, unless we are in fixed-size mode.
1457 // the drawing state will be updated only once we receive a buffer
1458 // with the correct size.
1459 //
1460 // in particular, we want to make sure the clip (which is part
1461 // of the geometry state) is latched together with the size but is
1462 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001463 //
1464 // If a sideband stream is attached, however, we want to skip this
1465 // optimization so that transactions aren't missed when a buffer
1466 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001467
1468 flags |= eDontUpdateGeometryState;
1469 }
1470 }
1471
Mathias Agopian13127d82013-03-05 17:47:11 -08001472 // always set active to requested, unless we're asked not to
1473 // this is used by Layer, which special cases resizes.
1474 if (flags & eDontUpdateGeometryState) {
1475 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001476 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001477 if (mFreezePositionUpdates) {
1478 float tx = c.active.transform.tx();
1479 float ty = c.active.transform.ty();
1480 c.active = c.requested;
1481 c.active.transform.set(tx, ty);
1482 editCurrentState.active = c.active;
1483 } else {
1484 editCurrentState.active = editCurrentState.requested;
1485 c.active = c.requested;
1486 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001487 }
1488
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001489 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001490 // invalidate and recompute the visible regions if needed
1491 flags |= Layer::eVisibleRegion;
1492 }
1493
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001494 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001495 // invalidate and recompute the visible regions if needed
1496 flags |= eVisibleRegion;
1497 this->contentDirty = true;
1498
1499 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001500 const uint8_t type = c.active.transform.getType();
1501 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001502 (type >= Transform::SCALE));
1503 }
1504
Dan Stozac8145172016-04-28 16:29:06 -07001505 // If the layer is hidden, signal and clear out all local sync points so
1506 // that transactions for layers depending on this layer's frames becoming
1507 // visible are not blocked
1508 if (c.flags & layer_state_t::eLayerHidden) {
1509 Mutex::Autolock lock(mLocalSyncPointMutex);
1510 for (auto& point : mLocalSyncPoints) {
1511 point->setFrameAvailable();
1512 }
1513 mLocalSyncPoints.clear();
1514 }
1515
Mathias Agopian13127d82013-03-05 17:47:11 -08001516 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001517 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001518 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519}
1520
Pablo Ceballos05289c22016-04-14 15:49:55 -07001521void Layer::commitTransaction(const State& stateToCommit) {
1522 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001523}
1524
Mathias Agopian13127d82013-03-05 17:47:11 -08001525uint32_t Layer::getTransactionFlags(uint32_t flags) {
1526 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1527}
1528
1529uint32_t Layer::setTransactionFlags(uint32_t flags) {
1530 return android_atomic_or(flags, &mTransactionFlags);
1531}
1532
Robert Carr82364e32016-05-15 11:27:47 -07001533bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001534 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001535 return false;
1536 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001537
1538 // We update the requested and active position simultaneously because
1539 // we want to apply the position portion of the transform matrix immediately,
1540 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001541 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001542 if (immediate && !mFreezePositionUpdates) {
1543 mCurrentState.active.transform.set(x, y);
1544 }
1545 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001546
Dan Stoza7dde5992015-05-22 09:51:44 -07001547 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001548 setTransactionFlags(eTransactionNeeded);
1549 return true;
1550}
Robert Carr82364e32016-05-15 11:27:47 -07001551
Mathias Agopian13127d82013-03-05 17:47:11 -08001552bool Layer::setLayer(uint32_t z) {
1553 if (mCurrentState.z == z)
1554 return false;
1555 mCurrentState.sequence++;
1556 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001557 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001558 setTransactionFlags(eTransactionNeeded);
1559 return true;
1560}
1561bool Layer::setSize(uint32_t w, uint32_t h) {
1562 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1563 return false;
1564 mCurrentState.requested.w = w;
1565 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001566 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001567 setTransactionFlags(eTransactionNeeded);
1568 return true;
1569}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001570#ifdef USE_HWC2
1571bool Layer::setAlpha(float alpha) {
1572#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001573bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001574#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001575 if (mCurrentState.alpha == alpha)
1576 return false;
1577 mCurrentState.sequence++;
1578 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001579 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001580 setTransactionFlags(eTransactionNeeded);
1581 return true;
1582}
1583bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1584 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001585 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001586 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001587 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001588 setTransactionFlags(eTransactionNeeded);
1589 return true;
1590}
1591bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001592 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001593 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001594 setTransactionFlags(eTransactionNeeded);
1595 return true;
1596}
1597bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1598 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1599 if (mCurrentState.flags == newFlags)
1600 return false;
1601 mCurrentState.sequence++;
1602 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001603 mCurrentState.mask = mask;
1604 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001605 setTransactionFlags(eTransactionNeeded);
1606 return true;
1607}
Robert Carr99e27f02016-06-16 15:18:02 -07001608
1609bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001610 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 return false;
1612 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001613 mCurrentState.requestedCrop = crop;
1614 if (immediate) {
1615 mCurrentState.crop = crop;
1616 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001617 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001618 setTransactionFlags(eTransactionNeeded);
1619 return true;
1620}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001621bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001622 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001623 return false;
1624 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001625 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001626 mCurrentState.modified = true;
1627 setTransactionFlags(eTransactionNeeded);
1628 return true;
1629}
Mathias Agopian13127d82013-03-05 17:47:11 -08001630
Robert Carrc3574f72016-03-24 12:19:32 -07001631bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1632 if (scalingMode == mOverrideScalingMode)
1633 return false;
1634 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001635 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001636 return true;
1637}
1638
1639uint32_t Layer::getEffectiveScalingMode() const {
1640 if (mOverrideScalingMode >= 0) {
1641 return mOverrideScalingMode;
1642 }
1643 return mCurrentScalingMode;
1644}
1645
Mathias Agopian13127d82013-03-05 17:47:11 -08001646bool Layer::setLayerStack(uint32_t layerStack) {
1647 if (mCurrentState.layerStack == layerStack)
1648 return false;
1649 mCurrentState.sequence++;
1650 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001651 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001652 setTransactionFlags(eTransactionNeeded);
1653 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001654}
1655
Dan Stoza7dde5992015-05-22 09:51:44 -07001656void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1657 uint64_t frameNumber) {
1658 mCurrentState.handle = handle;
1659 mCurrentState.frameNumber = frameNumber;
1660 // We don't set eTransactionNeeded, because just receiving a deferral
1661 // request without any other state updates shouldn't actually induce a delay
1662 mCurrentState.modified = true;
1663 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001664 mCurrentState.handle = nullptr;
1665 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001666 mCurrentState.modified = false;
1667}
1668
Dan Stozaee44edd2015-03-23 15:50:23 -07001669void Layer::useSurfaceDamage() {
1670 if (mFlinger->mForceFullDamage) {
1671 surfaceDamageRegion = Region::INVALID_REGION;
1672 } else {
1673 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1674 }
1675}
1676
1677void Layer::useEmptyDamage() {
1678 surfaceDamageRegion.clear();
1679}
1680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681// ----------------------------------------------------------------------------
1682// pageflip handling...
1683// ----------------------------------------------------------------------------
1684
Dan Stoza6b9454d2014-11-07 16:00:59 -08001685bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001686 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001687 return true;
1688 }
1689
Dan Stoza6b9454d2014-11-07 16:00:59 -08001690 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001691 if (mQueueItems.empty()) {
1692 return false;
1693 }
1694 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001695 nsecs_t expectedPresent =
1696 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001697
1698 // Ignore timestamps more than a second in the future
1699 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1700 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1701 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1702 expectedPresent);
1703
1704 bool isDue = timestamp < expectedPresent;
1705 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001706}
1707
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001708bool Layer::onPreComposition() {
1709 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001710 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001711}
1712
Dan Stozae77c7662016-05-13 11:37:28 -07001713bool Layer::onPostComposition() {
1714 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001715 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001716 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001717 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1718
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001719 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001720 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001721 mFrameTracker.setFrameReadyFence(frameReadyFence);
1722 } else {
1723 // There was no fence for this frame, so assume that it was ready
1724 // to be presented at the desired present time.
1725 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1726 }
1727
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001728 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001729#ifdef USE_HWC2
1730 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1731#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001732 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001733#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001734 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001735 mFrameTracker.setActualPresentFence(presentFence);
1736 } else {
1737 // The HWC doesn't support present fences, so use the refresh
1738 // timestamp instead.
1739 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1740 mFrameTracker.setActualPresentTime(presentTime);
1741 }
1742
1743 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001744 mFrameLatencyNeeded = false;
1745 }
Dan Stozae77c7662016-05-13 11:37:28 -07001746 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001747}
1748
Dan Stoza9e56aa02015-11-02 13:00:03 -08001749#ifdef USE_HWC2
1750void Layer::releasePendingBuffer() {
1751 mSurfaceFlingerConsumer->releasePendingBuffer();
1752}
1753#endif
1754
Mathias Agopianda27af92012-09-13 18:17:13 -07001755bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001756 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001757#ifdef USE_HWC2
1758 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1759 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1760#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001761 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001762 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001763#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001764}
1765
Mathias Agopian4fec8732012-06-29 14:12:52 -07001766Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001768 ATRACE_CALL();
1769
Jesse Hall399184a2014-03-03 15:42:54 -08001770 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1771 // mSidebandStreamChanged was true
1772 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001773 if (mSidebandStream != NULL) {
1774 setTransactionFlags(eTransactionNeeded);
1775 mFlinger->setTransactionFlags(eTraversalNeeded);
1776 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001777 recomputeVisibleRegions = true;
1778
1779 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001780 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001781 }
1782
Mathias Agopian4fec8732012-06-29 14:12:52 -07001783 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001784 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001785
1786 // if we've already called updateTexImage() without going through
1787 // a composition step, we have to skip this layer at this point
1788 // because we cannot call updateTeximage() without a corresponding
1789 // compositionComplete() call.
1790 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001791 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001792 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001793 }
1794
Dan Stoza1ce65812016-06-15 16:26:27 -07001795 // If the head buffer's acquire fence hasn't signaled yet, return and
1796 // try again later
1797 if (!headFenceHasSignaled()) {
1798 mFlinger->signalLayerUpdate();
1799 return outDirtyRegion;
1800 }
1801
Jamie Gennis351a5132011-09-14 18:23:37 -07001802 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001803 const State& s(getDrawingState());
1804 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001805 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001806
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001807 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001808 Layer::State& front;
1809 Layer::State& current;
1810 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001811 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001812 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001813 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001814 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001815
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001816 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001817 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001818 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001819 int32_t overrideScalingMode,
1820 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001821 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001822 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001823 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001824 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001825 overrideScalingMode(overrideScalingMode),
1826 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001827 }
1828
1829 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001830 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001831 if (buf == NULL) {
1832 return false;
1833 }
1834
1835 uint32_t bufWidth = buf->getWidth();
1836 uint32_t bufHeight = buf->getHeight();
1837
1838 // check that we received a buffer of the right size
1839 // (Take the buffer's orientation into account)
1840 if (item.mTransform & Transform::ROT_90) {
1841 swap(bufWidth, bufHeight);
1842 }
1843
Robert Carrc3574f72016-03-24 12:19:32 -07001844 int actualScalingMode = overrideScalingMode >= 0 ?
1845 overrideScalingMode : item.mScalingMode;
1846 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001847 if (front.active != front.requested) {
1848
1849 if (isFixedSize ||
1850 (bufWidth == front.requested.w &&
1851 bufHeight == front.requested.h))
1852 {
1853 // Here we pretend the transaction happened by updating the
1854 // current and drawing states. Drawing state is only accessed
1855 // in this thread, no need to have it locked
1856 front.active = front.requested;
1857
1858 // We also need to update the current state so that
1859 // we don't end-up overwriting the drawing state with
1860 // this stale current state during the next transaction
1861 //
1862 // NOTE: We don't need to hold the transaction lock here
1863 // because State::active is only accessed from this thread.
1864 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001865 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001866
1867 // recompute visible region
1868 recomputeVisibleRegions = true;
1869 }
1870
1871 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001872 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001873 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001874 " requested={ wh={%4u,%4u} }}\n",
1875 name,
Andy McFadden69052052012-09-14 16:10:11 -07001876 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001877 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001878 front.crop.left,
1879 front.crop.top,
1880 front.crop.right,
1881 front.crop.bottom,
1882 front.crop.getWidth(),
1883 front.crop.getHeight(),
1884 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001885 }
1886
Ruben Brunk1681d952014-06-27 15:51:55 -07001887 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001888 if (front.active.w != bufWidth ||
1889 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001890 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001891 ALOGE("[%s] rejecting buffer: "
1892 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1893 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001894 return true;
1895 }
1896 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001897
1898 // if the transparent region has changed (this test is
1899 // conservative, but that's fine, worst case we're doing
1900 // a bit of extra work), we latch the new one and we
1901 // trigger a visible-region recompute.
1902 if (!front.activeTransparentRegion.isTriviallyEqual(
1903 front.requestedTransparentRegion)) {
1904 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001905
1906 // We also need to update the current state so that
1907 // we don't end-up overwriting the drawing state with
1908 // this stale current state during the next transaction
1909 //
1910 // NOTE: We don't need to hold the transaction lock here
1911 // because State::active is only accessed from this thread.
1912 current.activeTransparentRegion = front.activeTransparentRegion;
1913
1914 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001915 recomputeVisibleRegions = true;
1916 }
1917
Robert Carr99e27f02016-06-16 15:18:02 -07001918 if (front.crop != front.requestedCrop) {
1919 front.crop = front.requestedCrop;
1920 current.crop = front.requestedCrop;
1921 recomputeVisibleRegions = true;
1922 }
Robert Carra3920732016-06-20 21:49:49 -07001923 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001924
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001925 return false;
1926 }
1927 };
1928
Ruben Brunk1681d952014-06-27 15:51:55 -07001929 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001930 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001931 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001932
Dan Stozacac35382016-01-27 12:21:06 -08001933
1934 // Check all of our local sync points to ensure that all transactions
1935 // which need to have been applied prior to the frame which is about to
1936 // be latched have signaled
1937
1938 auto headFrameNumber = getHeadFrameNumber();
1939 bool matchingFramesFound = false;
1940 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001941 {
Dan Stozacac35382016-01-27 12:21:06 -08001942 Mutex::Autolock lock(mLocalSyncPointMutex);
1943 for (auto& point : mLocalSyncPoints) {
1944 if (point->getFrameNumber() > headFrameNumber) {
1945 break;
1946 }
1947
1948 matchingFramesFound = true;
1949
1950 if (!point->frameIsAvailable()) {
1951 // We haven't notified the remote layer that the frame for
1952 // this point is available yet. Notify it now, and then
1953 // abort this attempt to latch.
1954 point->setFrameAvailable();
1955 allTransactionsApplied = false;
1956 break;
1957 }
1958
1959 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001960 }
1961 }
1962
Dan Stozacac35382016-01-27 12:21:06 -08001963 if (matchingFramesFound && !allTransactionsApplied) {
1964 mFlinger->signalLayerUpdate();
1965 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001966 }
1967
Pablo Ceballos06312182015-10-07 16:32:12 -07001968 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1969 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos2dcb3632016-03-17 15:50:23 -07001970 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001971 // buffer mode.
1972 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001973 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001974 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001975 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001976 if (updateResult == BufferQueue::PRESENT_LATER) {
1977 // Producer doesn't want buffer to be displayed yet. Signal a
1978 // layer update so we check again at the next opportunity.
1979 mFlinger->signalLayerUpdate();
1980 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001981 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1982 // If the buffer has been rejected, remove it from the shadow queue
1983 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001984 if (queuedBuffer) {
1985 Mutex::Autolock lock(mQueueItemLock);
1986 mQueueItems.removeAt(0);
1987 android_atomic_dec(&mQueuedFrames);
1988 }
Dan Stozaecc50402015-04-28 14:42:06 -07001989 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001990 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1991 // This can occur if something goes wrong when trying to create the
1992 // EGLImage for this buffer. If this happens, the buffer has already
1993 // been released, so we need to clean up the queue and bug out
1994 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001995 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001996 Mutex::Autolock lock(mQueueItemLock);
1997 mQueueItems.clear();
1998 android_atomic_and(0, &mQueuedFrames);
1999 }
2000
2001 // Once we have hit this state, the shadow queue may no longer
2002 // correctly reflect the incoming BufferQueue's contents, so even if
2003 // updateTexImage starts working, the only safe course of action is
2004 // to continue to ignore updates.
2005 mUpdateTexImageFailed = true;
2006
2007 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07002008 }
2009
Pablo Ceballos06312182015-10-07 16:32:12 -07002010 if (queuedBuffer) {
2011 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07002012 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2013
Dan Stoza6b9454d2014-11-07 16:00:59 -08002014 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07002015
2016 // Remove any stale buffers that have been dropped during
2017 // updateTexImage
2018 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2019 mQueueItems.removeAt(0);
2020 android_atomic_dec(&mQueuedFrames);
2021 }
2022
Dan Stoza6b9454d2014-11-07 16:00:59 -08002023 mQueueItems.removeAt(0);
2024 }
2025
Dan Stozaecc50402015-04-28 14:42:06 -07002026
Andy McFadden1585c4d2013-06-28 13:52:40 -07002027 // Decrement the queued-frames count. Signal another event if we
2028 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002029 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002030 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002031 mFlinger->signalLayerUpdate();
2032 }
2033
2034 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002035 // something happened!
2036 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002037 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002038 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002039
Jamie Gennis351a5132011-09-14 18:23:37 -07002040 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002041 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002042 if (mActiveBuffer == NULL) {
2043 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002044 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002045 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002046
Mathias Agopian4824d402012-06-04 18:16:30 -07002047 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002048 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002049 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002050 // the first time we receive a buffer, we need to trigger a
2051 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002052 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002053 }
2054
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002055 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2056 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2057 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002058 if ((crop != mCurrentCrop) ||
2059 (transform != mCurrentTransform) ||
2060 (scalingMode != mCurrentScalingMode))
2061 {
2062 mCurrentCrop = crop;
2063 mCurrentTransform = transform;
2064 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002065 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002066 }
2067
2068 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002069 uint32_t bufWidth = mActiveBuffer->getWidth();
2070 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002071 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2072 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002073 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002074 }
2075 }
2076
2077 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002078 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002079 recomputeVisibleRegions = true;
2080 }
2081
Dan Stozacac35382016-01-27 12:21:06 -08002082 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2083
2084 // Remove any sync points corresponding to the buffer which was just
2085 // latched
2086 {
2087 Mutex::Autolock lock(mLocalSyncPointMutex);
2088 auto point = mLocalSyncPoints.begin();
2089 while (point != mLocalSyncPoints.end()) {
2090 if (!(*point)->frameIsAvailable() ||
2091 !(*point)->transactionIsApplied()) {
2092 // This sync point must have been added since we started
2093 // latching. Don't drop it yet.
2094 ++point;
2095 continue;
2096 }
2097
2098 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2099 point = mLocalSyncPoints.erase(point);
2100 } else {
2101 ++point;
2102 }
2103 }
2104 }
2105
Mathias Agopian4fec8732012-06-29 14:12:52 -07002106 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002107 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002108
2109 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002110 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002111 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002112 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002113}
2114
Mathias Agopiana67932f2011-04-20 14:20:59 -07002115uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002116{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002117 // TODO: should we do something special if mSecure is set?
2118 if (mProtectedByApp) {
2119 // need a hardware-protected path to external video sink
2120 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002121 }
Riley Andrews03414a12014-07-01 14:22:59 -07002122 if (mPotentialCursor) {
2123 usage |= GraphicBuffer::USAGE_CURSOR;
2124 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002125 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002126 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002127}
2128
Mathias Agopian84300952012-11-21 16:02:13 -08002129void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002130 uint32_t orientation = 0;
2131 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002132 // The transform hint is used to improve performance, but we can
2133 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002134 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002135 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002136 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002137 if (orientation & Transform::ROT_INVALID) {
2138 orientation = 0;
2139 }
2140 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002141 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002142}
2143
Mathias Agopian13127d82013-03-05 17:47:11 -08002144// ----------------------------------------------------------------------------
2145// debugging
2146// ----------------------------------------------------------------------------
2147
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002148void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002149{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002150 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002151
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002152 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002153 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002154 "+ %s %p (%s)\n",
2155 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002156 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002157
Mathias Agopian2ca79392013-04-02 18:30:32 -07002158 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002159 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002160 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002161 sp<Client> client(mClientRef.promote());
2162
Mathias Agopian74d211a2013-04-22 16:55:35 +02002163 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002164 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2165 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002166 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002167#ifdef USE_HWC2
2168 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2169#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002170 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002171#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002172 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002173 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 -07002174 s.crop.left, s.crop.top,
2175 s.crop.right, s.crop.bottom,
2176 s.finalCrop.left, s.finalCrop.top,
2177 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002178 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002179 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002180 s.active.transform[0][0], s.active.transform[0][1],
2181 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002182 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002183
2184 sp<const GraphicBuffer> buf0(mActiveBuffer);
2185 uint32_t w0=0, h0=0, s0=0, f0=0;
2186 if (buf0 != 0) {
2187 w0 = buf0->getWidth();
2188 h0 = buf0->getHeight();
2189 s0 = buf0->getStride();
2190 f0 = buf0->format;
2191 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002192 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002193 " "
2194 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2195 " queued-frames=%d, mRefreshPending=%d\n",
2196 mFormat, w0, h0, s0,f0,
2197 mQueuedFrames, mRefreshPending);
2198
Mathias Agopian13127d82013-03-05 17:47:11 -08002199 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002200 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002201 }
2202}
2203
Svetoslavd85084b2014-03-20 10:28:31 -07002204void Layer::dumpFrameStats(String8& result) const {
2205 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002206}
2207
Svetoslavd85084b2014-03-20 10:28:31 -07002208void Layer::clearFrameStats() {
2209 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002210}
2211
Jamie Gennis6547ff42013-07-16 20:12:42 -07002212void Layer::logFrameStats() {
2213 mFrameTracker.logAndResetStats(mName);
2214}
2215
Svetoslavd85084b2014-03-20 10:28:31 -07002216void Layer::getFrameStats(FrameStats* outStats) const {
2217 mFrameTracker.getStats(outStats);
2218}
2219
Pablo Ceballos40845df2016-01-25 17:41:15 -08002220void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2221 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2222 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2223 *outName = mName;
2224 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2225
2226#ifdef USE_HWC2
2227 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2228 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2229 HWC2::Composition::Client : true;
2230#else
2231 *outIsGlesComposition = mIsGlesComposition;
2232#endif
2233 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2234 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2235 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2236}
Dan Stozae77c7662016-05-13 11:37:28 -07002237
2238std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2239 bool forceFlush) {
2240 std::vector<OccupancyTracker::Segment> history;
2241 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2242 &history);
2243 if (result != NO_ERROR) {
2244 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2245 result);
2246 return {};
2247 }
2248 return history;
2249}
2250
Robert Carr367c5682016-06-20 11:55:28 -07002251bool Layer::getTransformToDisplayInverse() const {
2252 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2253}
2254
Mathias Agopian13127d82013-03-05 17:47:11 -08002255// ---------------------------------------------------------------------------
2256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002257}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002258
2259#if defined(__gl_h_)
2260#error "don't include gl/gl.h in this file"
2261#endif
2262
2263#if defined(__gl2_h_)
2264#error "don't include gl2/gl2.h in this file"
2265#endif