blob: e78da968a84efe0ec3927ef0c0f97e10b8c45cb0 [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;
Dan Stozab9b08832014-03-13 11:55:57 -0700158 BufferQueue::createBufferQueue(&producer, &consumer);
159 mProducer = new MonitoredProducer(producer, mFlinger);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800160 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName,
161 this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800162 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800163 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700164 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800165
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700166#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
167#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700168#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700169 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800170#endif
Andy McFadden69052052012-09-14 16:10:11 -0700171
Mathias Agopian84300952012-11-21 16:02:13 -0800172 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
173 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700174}
175
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700176Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800177 sp<Client> c(mClientRef.promote());
178 if (c != 0) {
179 c->detachLayer(this);
180 }
181
Dan Stozacac35382016-01-27 12:21:06 -0800182 for (auto& point : mRemoteSyncPoints) {
183 point->setTransactionApplied();
184 }
Dan Stozac8145172016-04-28 16:29:06 -0700185 for (auto& point : mLocalSyncPoints) {
186 point->setFrameAvailable();
187 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700188 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700189 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700190}
191
Mathias Agopian13127d82013-03-05 17:47:11 -0800192// ---------------------------------------------------------------------------
193// callbacks
194// ---------------------------------------------------------------------------
195
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196#ifdef USE_HWC2
197void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
198 if (mHwcLayers.empty()) {
199 return;
200 }
201 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
202}
203#else
Dan Stozac7014012014-02-14 15:03:43 -0800204void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800205 HWComposer::HWCLayerInterface* layer) {
206 if (layer) {
207 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700208 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800209 }
210}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800212
Dan Stoza6b9454d2014-11-07 16:00:59 -0800213void Layer::onFrameAvailable(const BufferItem& item) {
214 // Add this buffer from our internal queue tracker
215 { // Autolock scope
216 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700217
218 // Reset the frame number tracker when we receive the first buffer after
219 // a frame number reset
220 if (item.mFrameNumber == 1) {
221 mLastFrameNumberReceived = 0;
222 }
223
224 // Ensure that callbacks are handled in order
225 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
226 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
227 ms2ns(500));
228 if (result != NO_ERROR) {
229 ALOGE("[%s] Timed out waiting on callback", mName.string());
230 }
231 }
232
Dan Stoza6b9454d2014-11-07 16:00:59 -0800233 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700234 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700235
236 // Wake up any pending callbacks
237 mLastFrameNumberReceived = item.mFrameNumber;
238 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800239 }
240
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800241 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700242}
243
Dan Stoza6b9454d2014-11-07 16:00:59 -0800244void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700245 { // Autolock scope
246 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700247
Dan Stoza7dde5992015-05-22 09:51:44 -0700248 // Ensure that callbacks are handled in order
249 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
250 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
251 ms2ns(500));
252 if (result != NO_ERROR) {
253 ALOGE("[%s] Timed out waiting on callback", mName.string());
254 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700255 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700256
257 if (mQueueItems.empty()) {
258 ALOGE("Can't replace a frame on an empty queue");
259 return;
260 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700261 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700262
263 // Wake up any pending callbacks
264 mLastFrameNumberReceived = item.mFrameNumber;
265 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700266 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800267}
268
Jesse Hall399184a2014-03-03 15:42:54 -0800269void Layer::onSidebandStreamChanged() {
270 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
271 // mSidebandStreamChanged was false
272 mFlinger->signalLayerUpdate();
273 }
274}
275
Mathias Agopian67106042013-03-14 19:18:13 -0700276// called with SurfaceFlinger::mStateLock from the drawing thread after
277// the layer has been remove from the current state list (and just before
278// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800279void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800280 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700281}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700282
Mathias Agopian13127d82013-03-05 17:47:11 -0800283// ---------------------------------------------------------------------------
284// set-up
285// ---------------------------------------------------------------------------
286
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700287const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800288 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289}
290
Mathias Agopianf9d93272009-06-19 17:00:27 -0700291status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 PixelFormat format, uint32_t flags)
293{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700294 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700296
297 // never allow a surface larger than what our underlying GL implementation
298 // can handle.
299 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800300 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700301 return BAD_VALUE;
302 }
303
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700304 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700305
Riley Andrews03414a12014-07-01 14:22:59 -0700306 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700307 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308 mCurrentOpacity = getOpacityForFormat(format);
309
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800310 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
311 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
312 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 return NO_ERROR;
315}
316
Dan Stoza7dde5992015-05-22 09:51:44 -0700317/*
318 * The layer handle is just a BBinder object passed to the client
319 * (remote process) -- we don't keep any reference on our side such that
320 * the dtor is called when the remote side let go of its reference.
321 *
322 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
323 * this layer when the handle is destroyed.
324 */
325class Layer::Handle : public BBinder, public LayerCleaner {
326 public:
327 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
328 : LayerCleaner(flinger, layer), owner(layer) {}
329
330 wp<Layer> owner;
331};
332
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700333sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800334 Mutex::Autolock _l(mLock);
335
336 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700337 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800338
339 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700340
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700341 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800342}
343
Dan Stozab9b08832014-03-13 11:55:57 -0700344sp<IGraphicBufferProducer> Layer::getProducer() const {
345 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700346}
347
Mathias Agopian13127d82013-03-05 17:47:11 -0800348// ---------------------------------------------------------------------------
349// h/w composer set-up
350// ---------------------------------------------------------------------------
351
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800352Rect Layer::getContentCrop() const {
353 // this is the crop rectangle that applies to the buffer
354 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700355 Rect crop;
356 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800357 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700358 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800359 } else if (mActiveBuffer != NULL) {
360 // otherwise we use the whole buffer
361 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800363 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700364 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700365 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700366 return crop;
367}
368
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700369static Rect reduce(const Rect& win, const Region& exclude) {
370 if (CC_LIKELY(exclude.isEmpty())) {
371 return win;
372 }
373 if (exclude.isRect()) {
374 return win.reduce(exclude.getBounds());
375 }
376 return Region(win).subtract(exclude).getBounds();
377}
378
Mathias Agopian13127d82013-03-05 17:47:11 -0800379Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700380 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700381 return computeBounds(s.activeTransparentRegion);
382}
383
384Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
385 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700387
388 if (!s.crop.isEmpty()) {
389 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800390 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700391 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700392 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800393}
394
Mathias Agopian6b442672013-07-09 21:24:52 -0700395FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800396 // the content crop is the area of the content that gets scaled to the
397 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700398 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800399
Robert Carrb5d3d262016-03-25 15:08:13 -0700400 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800401 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700402 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800403
404 // apply the projection's clipping to the window crop in
405 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700406 // if there are no window scaling involved, this operation will map to full
407 // pixels in the buffer.
408 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
409 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700410
411 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700412 if (!s.crop.isEmpty()) {
413 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700414 }
415
Robert Carr3dcabfa2016-03-01 18:36:58 -0800416 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000417 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
418 activeCrop.clear();
419 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700420 if (!s.finalCrop.isEmpty()) {
421 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 activeCrop.clear();
423 }
424 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800425 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800426
Michael Lentine28ea2172014-11-19 18:32:37 -0800427 // This needs to be here as transform.transform(Rect) computes the
428 // transformed rect and then takes the bounding box of the result before
429 // returning. This means
430 // transform.inverse().transform(transform.transform(Rect)) != Rect
431 // in which case we need to make sure the final rect is clipped to the
432 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
434 activeCrop.clear();
435 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800436
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700437 // subtract the transparent region and snap to the bounds
438 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
439
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 // Transform the window crop to match the buffer coordinate system,
441 // which means using the inverse of the current transform set on the
442 // SurfaceFlingerConsumer.
443 uint32_t invTransform = mCurrentTransform;
444 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
445 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700446 * the code below applies the primary display's inverse transform to the
447 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700449 uint32_t invTransformOrient =
450 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 // calculate the inverse transform
452 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
453 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
454 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700457 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
458 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800459 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460
461 int winWidth = s.active.w;
462 int winHeight = s.active.h;
463 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
464 // If the activeCrop has been rotate the ends are rotated but not
465 // the space itself so when transforming ends back we can't rely on
466 // a modification of the axes of rotation. To account for this we
467 // need to reorient the inverse rotation in terms of the current
468 // axes of rotation.
469 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
470 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
471 if (is_h_flipped == is_v_flipped) {
472 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
473 NATIVE_WINDOW_TRANSFORM_FLIP_H;
474 }
475 winWidth = s.active.h;
476 winHeight = s.active.w;
477 }
478 const Rect winCrop = activeCrop.transform(
479 invTransform, s.active.w, s.active.h);
480
481 // below, crop is intersected with winCrop expressed in crop's coordinate space
482 float xScale = crop.getWidth() / float(winWidth);
483 float yScale = crop.getHeight() / float(winHeight);
484
485 float insetL = winCrop.left * xScale;
486 float insetT = winCrop.top * yScale;
487 float insetR = (winWidth - winCrop.right ) * xScale;
488 float insetB = (winHeight - winCrop.bottom) * yScale;
489
490 crop.left += insetL;
491 crop.top += insetT;
492 crop.right -= insetR;
493 crop.bottom -= insetB;
494
Mathias Agopian13127d82013-03-05 17:47:11 -0800495 return crop;
496}
497
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498#ifdef USE_HWC2
499void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
500#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700501void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700502 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700503 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700505{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506#ifdef USE_HWC2
507 const auto hwcId = displayDevice->getHwcDisplayId();
508 auto& hwcInfo = mHwcLayers[hwcId];
509#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800510 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700512
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700513 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514#ifdef USE_HWC2
515 hwcInfo.forceClientComposition = false;
516
517 if (isSecure() && !displayDevice->isSecure()) {
518 hwcInfo.forceClientComposition = true;
519 }
520
521 auto& hwcLayer = hwcInfo.layer;
522#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700523 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700524
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700525 if (isSecure() && !hw->isSecure()) {
526 layer.setSkip(true);
527 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800528#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700529
Mathias Agopian13127d82013-03-05 17:47:11 -0800530 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700531 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800532#ifdef USE_HWC2
533 if (!isOpaque(s) || s.alpha != 1.0f) {
534 auto blendMode = mPremultipliedAlpha ?
535 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
536 auto error = hwcLayer->setBlendMode(blendMode);
537 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
538 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
539 to_string(error).c_str(), static_cast<int32_t>(error));
540 }
541#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800542 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800543 layer.setBlending(mPremultipliedAlpha ?
544 HWC_BLENDING_PREMULT :
545 HWC_BLENDING_COVERAGE);
546 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800548
549 // apply the layer's transform, followed by the display's global transform
550 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700551 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700552 if (!s.crop.isEmpty()) {
553 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800554 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000558 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000560 activeCrop.clear();
561 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800562 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800563 // This needs to be here as transform.transform(Rect) computes the
564 // transformed rect and then takes the bounding box of the result before
565 // returning. This means
566 // transform.inverse().transform(transform.transform(Rect)) != Rect
567 // in which case we need to make sure the final rect is clipped to the
568 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000569 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
570 activeCrop.clear();
571 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700572 // mark regions outside the crop as transparent
573 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
574 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
575 s.active.w, s.active.h));
576 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
577 activeCrop.left, activeCrop.bottom));
578 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
579 s.active.w, activeCrop.bottom));
580 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800581 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700582 if (!s.finalCrop.isEmpty()) {
583 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000584 frame.clear();
585 }
586 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000588 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
589 frame.clear();
590 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 const Transform& tr(displayDevice->getTransform());
592 Rect transformedFrame = tr.transform(frame);
593 auto error = hwcLayer->setDisplayFrame(transformedFrame);
594 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
595 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
596 transformedFrame.top, transformedFrame.right,
597 transformedFrame.bottom, to_string(error).c_str(),
598 static_cast<int32_t>(error));
599
600 FloatRect sourceCrop = computeCrop(displayDevice);
601 error = hwcLayer->setSourceCrop(sourceCrop);
602 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
603 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
604 sourceCrop.left, sourceCrop.top, sourceCrop.right,
605 sourceCrop.bottom, to_string(error).c_str(),
606 static_cast<int32_t>(error));
607
608 error = hwcLayer->setPlaneAlpha(s.alpha);
609 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
610 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
611 static_cast<int32_t>(error));
612
613 error = hwcLayer->setZOrder(s.z);
614 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
615 mName.string(), s.z, to_string(error).c_str(),
616 static_cast<int32_t>(error));
617#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000618 if (!frame.intersect(hw->getViewport(), &frame)) {
619 frame.clear();
620 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800621 const Transform& tr(hw->getTransform());
622 layer.setFrame(tr.transform(frame));
623 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800624 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800626
Mathias Agopian29a367b2011-07-12 14:51:45 -0700627 /*
628 * Transformations are applied in this order:
629 * 1) buffer orientation/flip/mirror
630 * 2) state transformation (window manager)
631 * 3) layer orientation (screen orientation)
632 * (NOTE: the matrices are multiplied in reverse order)
633 */
634
635 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800636 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700637
638 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
639 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700640 * the code below applies the primary display's inverse transform to the
641 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700642 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700643 uint32_t invTransform =
644 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 // calculate the inverse transform
646 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
647 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
648 NATIVE_WINDOW_TRANSFORM_FLIP_H;
649 }
650 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700651 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700652 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700653
654 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800655 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656#ifdef USE_HWC2
657 if (orientation & Transform::ROT_INVALID) {
658 // we can only handle simple transformation
659 hwcInfo.forceClientComposition = true;
660 } else {
661 auto transform = static_cast<HWC2::Transform>(orientation);
662 auto error = hwcLayer->setTransform(transform);
663 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
664 "%s (%d)", mName.string(), to_string(transform).c_str(),
665 to_string(error).c_str(), static_cast<int32_t>(error));
666 }
667#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800668 if (orientation & Transform::ROT_INVALID) {
669 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700670 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700671 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800672 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700673 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700675}
676
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677#ifdef USE_HWC2
678void Layer::forceClientComposition(int32_t hwcId) {
679 if (mHwcLayers.count(hwcId) == 0) {
680 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
681 return;
682 }
683
684 mHwcLayers[hwcId].forceClientComposition = true;
685}
686#endif
687
688#ifdef USE_HWC2
689void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
690 // Apply this display's projection's viewport to the visible region
691 // before giving it to the HWC HAL.
692 const Transform& tr = displayDevice->getTransform();
693 const auto& viewport = displayDevice->getViewport();
694 Region visible = tr.transform(visibleRegion.intersect(viewport));
695 auto hwcId = displayDevice->getHwcDisplayId();
696 auto& hwcLayer = mHwcLayers[hwcId].layer;
697 auto error = hwcLayer->setVisibleRegion(visible);
698 if (error != HWC2::Error::None) {
699 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
700 to_string(error).c_str(), static_cast<int32_t>(error));
701 visible.dump(LOG_TAG);
702 }
703
704 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
705 if (error != HWC2::Error::None) {
706 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
707 to_string(error).c_str(), static_cast<int32_t>(error));
708 surfaceDamageRegion.dump(LOG_TAG);
709 }
710
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700711 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700713 setCompositionType(hwcId, HWC2::Composition::Sideband);
714 ALOGV("[%s] Requesting Sideband composition", mName.string());
715 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 if (error != HWC2::Error::None) {
717 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
718 mName.string(), mSidebandStream->handle(),
719 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700721 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722 }
723
Dan Stoza0a21df72016-07-20 12:52:38 -0700724 // Client layers
725 if (mHwcLayers[hwcId].forceClientComposition ||
726 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700727 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700729 return;
730 }
731
Dan Stoza0a21df72016-07-20 12:52:38 -0700732 // SolidColor layers
733 if (mActiveBuffer == nullptr) {
734 setCompositionType(hwcId, HWC2::Composition::SolidColor);
735 error = hwcLayer->setColor({0, 0, 0, 255});
736 if (error != HWC2::Error::None) {
737 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
738 to_string(error).c_str(), static_cast<int32_t>(error));
739 }
740 return;
741 }
742
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700743 // Device or Cursor layers
744 if (mPotentialCursor) {
745 ALOGV("[%s] Requesting Cursor composition", mName.string());
746 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747 } else {
748 ALOGV("[%s] Requesting Device composition", mName.string());
749 setCompositionType(hwcId, HWC2::Composition::Device);
750 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700751
752 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
753 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
754 if (error != HWC2::Error::None) {
755 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
756 mActiveBuffer->handle, to_string(error).c_str(),
757 static_cast<int32_t>(error));
758 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759}
760#else
Mathias Agopian42977342012-08-05 00:40:46 -0700761void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700762 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800763 // we have to set the visible region on every frame because
764 // we currently free it during onLayerDisplayed(), which is called
765 // after HWComposer::commit() -- every frame.
766 // Apply this display's projection's viewport to the visible region
767 // before giving it to the HWC HAL.
768 const Transform& tr = hw->getTransform();
769 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
770 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700771 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800772 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700773
Jesse Hall399184a2014-03-03 15:42:54 -0800774 if (mSidebandStream.get()) {
775 layer.setSidebandStream(mSidebandStream);
776 } else {
777 // NOTE: buffer can be NULL if the client never drew into this
778 // layer yet, or if we ran out of memory
779 layer.setBuffer(mActiveBuffer);
780 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700781}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700783
Dan Stoza9e56aa02015-11-02 13:00:03 -0800784#ifdef USE_HWC2
785void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
786 auto hwcId = displayDevice->getHwcDisplayId();
787 if (mHwcLayers.count(hwcId) == 0 ||
788 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
789 return;
790 }
791
792 // This gives us only the "orientation" component of the transform
793 const State& s(getCurrentState());
794
795 // Apply the layer's transform, followed by the display's global transform
796 // Here we're guaranteed that the layer's transform preserves rects
797 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700798 if (!s.crop.isEmpty()) {
799 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800800 }
801 // Subtract the transparent region and snap to the bounds
802 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800803 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800804 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700805 if (!s.finalCrop.isEmpty()) {
806 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000807 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800808 auto& displayTransform(displayDevice->getTransform());
809 auto position = displayTransform.transform(frame);
810
811 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
812 position.top);
813 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
814 "to (%d, %d): %s (%d)", mName.string(), position.left,
815 position.top, to_string(error).c_str(),
816 static_cast<int32_t>(error));
817}
818#else
Dan Stozac7014012014-02-14 15:03:43 -0800819void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700820 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700821 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700822
823 // TODO: there is a possible optimization here: we only need to set the
824 // acquire fence the first time a new buffer is acquired on EACH display.
825
Riley Andrews03414a12014-07-01 14:22:59 -0700826 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800827 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800828 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700829 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700830 if (fenceFd == -1) {
831 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
832 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700833 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700834 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700835 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700836}
837
Riley Andrews03414a12014-07-01 14:22:59 -0700838Rect Layer::getPosition(
839 const sp<const DisplayDevice>& hw)
840{
841 // this gives us only the "orientation" component of the transform
842 const State& s(getCurrentState());
843
844 // apply the layer's transform, followed by the display's global transform
845 // here we're guaranteed that the layer's transform preserves rects
846 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700847 if (!s.crop.isEmpty()) {
848 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700849 }
850 // subtract the transparent region and snap to the bounds
851 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800852 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700853 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700854 if (!s.finalCrop.isEmpty()) {
855 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000856 }
Riley Andrews03414a12014-07-01 14:22:59 -0700857 const Transform& tr(hw->getTransform());
858 return Rect(tr.transform(frame));
859}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800860#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700861
Mathias Agopian13127d82013-03-05 17:47:11 -0800862// ---------------------------------------------------------------------------
863// drawing...
864// ---------------------------------------------------------------------------
865
866void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800867 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800868}
869
Dan Stozac7014012014-02-14 15:03:43 -0800870void Layer::draw(const sp<const DisplayDevice>& hw,
871 bool useIdentityTransform) const {
872 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800873}
874
Dan Stozac7014012014-02-14 15:03:43 -0800875void Layer::draw(const sp<const DisplayDevice>& hw) const {
876 onDraw(hw, Region(hw->bounds()), false);
877}
878
879void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
880 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800882 ATRACE_CALL();
883
Mathias Agopiana67932f2011-04-20 14:20:59 -0700884 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700886 // in fact never been drawn into. This happens frequently with
887 // SurfaceView because the WindowManager can't know when the client
888 // has drawn the first time.
889
890 // If there is nothing under us, we paint the screen in black, otherwise
891 // we just skip this update.
892
893 // figure out if there is something below us
894 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700895 const SurfaceFlinger::LayerVector& drawingLayers(
896 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700897 const size_t count = drawingLayers.size();
898 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 const sp<Layer>& layer(drawingLayers[i]);
900 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700901 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700902 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700903 }
904 // if not everything below us is covered, we plug the holes!
905 Region holes(clip.subtract(under));
906 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700907 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700908 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 return;
910 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700911
Andy McFadden97eba892012-12-11 15:21:45 -0800912 // Bind the current buffer to the GL texture, and wait for it to be
913 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800914 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
915 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800916 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700917 // Go ahead and draw the buffer anyway; no matter what we do the screen
918 // is probably going to have something visibly wrong.
919 }
920
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700921 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
922
Mathias Agopian875d8e12013-06-07 15:35:48 -0700923 RenderEngine& engine(mFlinger->getRenderEngine());
924
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700925 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700926 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700927 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700928
929 // Query the texture matrix given our current filtering mode.
930 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800931 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
932 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700933
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700934 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
935
936 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700937 * the code below applies the primary display's inverse transform to
938 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700939 */
940
941 // create a 4x4 transform matrix from the display transform flags
942 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
943 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
944 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
945
946 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700947 uint32_t transform =
948 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700949 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
950 tr = tr * rot90;
951 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
952 tr = tr * flipH;
953 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
954 tr = tr * flipV;
955
956 // calculate the inverse
957 tr = inverse(tr);
958
959 // and finally apply it to the original texture matrix
960 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
961 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
962 }
963
Jamie Genniscbb1a952012-05-08 17:05:52 -0700964 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700965 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
966 mTexture.setFiltering(useFiltering);
967 mTexture.setMatrix(textureMatrix);
968
969 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700970 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700971 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700972 }
Dan Stozac7014012014-02-14 15:03:43 -0800973 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700974 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975}
976
Mathias Agopian13127d82013-03-05 17:47:11 -0800977
Dan Stozac7014012014-02-14 15:03:43 -0800978void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
979 const Region& /* clip */, float red, float green, float blue,
980 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800981{
Mathias Agopian19733a32013-08-28 18:13:56 -0700982 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800983 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700984 engine.setupFillWithColor(red, green, blue, alpha);
985 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800986}
987
988void Layer::clearWithOpenGL(
989 const sp<const DisplayDevice>& hw, const Region& clip) const {
990 clearWithOpenGL(hw, clip, 0,0,0,0);
991}
992
Dan Stozac7014012014-02-14 15:03:43 -0800993void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
994 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700995 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800996
Dan Stozac7014012014-02-14 15:03:43 -0800997 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800998
Mathias Agopian13127d82013-03-05 17:47:11 -0800999 /*
1000 * NOTE: the way we compute the texture coordinates here produces
1001 * different results than when we take the HWC path -- in the later case
1002 * the "source crop" is rounded to texel boundaries.
1003 * This can produce significantly different results when the texture
1004 * is scaled by a large amount.
1005 *
1006 * The GL code below is more logical (imho), and the difference with
1007 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001008 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001009 * GL composition when a buffer scaling is applied (maybe with some
1010 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1011 * like more of a hack.
1012 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001013 Rect win(computeBounds());
1014
Robert Carrb5d3d262016-03-25 15:08:13 -07001015 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001016 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001017 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001018 win.clear();
1019 }
1020 win = s.active.transform.inverse().transform(win);
1021 if (!win.intersect(computeBounds(), &win)) {
1022 win.clear();
1023 }
1024 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001025
Mathias Agopian3f844832013-08-07 21:24:32 -07001026 float left = float(win.left) / float(s.active.w);
1027 float top = float(win.top) / float(s.active.h);
1028 float right = float(win.right) / float(s.active.w);
1029 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001030
Mathias Agopian875d8e12013-06-07 15:35:48 -07001031 // TODO: we probably want to generate the texture coords with the mesh
1032 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001033 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1034 texCoords[0] = vec2(left, 1.0f - top);
1035 texCoords[1] = vec2(left, 1.0f - bottom);
1036 texCoords[2] = vec2(right, 1.0f - bottom);
1037 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001038
Mathias Agopian875d8e12013-06-07 15:35:48 -07001039 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001040 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001041 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001042 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001043}
1044
Dan Stoza9e56aa02015-11-02 13:00:03 -08001045#ifdef USE_HWC2
1046void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1047 bool callIntoHwc) {
1048 if (mHwcLayers.count(hwcId) == 0) {
1049 ALOGE("setCompositionType called without a valid HWC layer");
1050 return;
1051 }
1052 auto& hwcInfo = mHwcLayers[hwcId];
1053 auto& hwcLayer = hwcInfo.layer;
1054 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1055 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1056 if (hwcInfo.compositionType != type) {
1057 ALOGV(" actually setting");
1058 hwcInfo.compositionType = type;
1059 if (callIntoHwc) {
1060 auto error = hwcLayer->setCompositionType(type);
1061 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1062 "composition type %s: %s (%d)", mName.string(),
1063 to_string(type).c_str(), to_string(error).c_str(),
1064 static_cast<int32_t>(error));
1065 }
1066 }
1067}
1068
1069HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001070 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1071 // If we're querying the composition type for a display that does not
1072 // have a HWC counterpart, then it will always be Client
1073 return HWC2::Composition::Client;
1074 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001075 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001076 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001077 return HWC2::Composition::Invalid;
1078 }
1079 return mHwcLayers.at(hwcId).compositionType;
1080}
1081
1082void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1083 if (mHwcLayers.count(hwcId) == 0) {
1084 ALOGE("setClearClientTarget called without a valid HWC layer");
1085 return;
1086 }
1087 mHwcLayers[hwcId].clearClientTarget = clear;
1088}
1089
1090bool Layer::getClearClientTarget(int32_t hwcId) const {
1091 if (mHwcLayers.count(hwcId) == 0) {
1092 ALOGE("getClearClientTarget called without a valid HWC layer");
1093 return false;
1094 }
1095 return mHwcLayers.at(hwcId).clearClientTarget;
1096}
1097#endif
1098
Ruben Brunk1681d952014-06-27 15:51:55 -07001099uint32_t Layer::getProducerStickyTransform() const {
1100 int producerStickyTransform = 0;
1101 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1102 if (ret != OK) {
1103 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1104 strerror(-ret), ret);
1105 return 0;
1106 }
1107 return static_cast<uint32_t>(producerStickyTransform);
1108}
1109
Dan Stozac5da2712016-07-20 15:38:12 -07001110bool Layer::latchUnsignaledBuffers() {
1111 static bool propertyLoaded = false;
1112 static bool latch = false;
1113 static std::mutex mutex;
1114 std::lock_guard<std::mutex> lock(mutex);
1115 if (!propertyLoaded) {
1116 char value[PROPERTY_VALUE_MAX] = {};
1117 property_get("debug.sf.latch_unsignaled", value, "0");
1118 latch = atoi(value);
1119 propertyLoaded = true;
1120 }
1121 return latch;
1122}
1123
Dan Stozacac35382016-01-27 12:21:06 -08001124uint64_t Layer::getHeadFrameNumber() const {
1125 Mutex::Autolock lock(mQueueItemLock);
1126 if (!mQueueItems.empty()) {
1127 return mQueueItems[0].mFrameNumber;
1128 } else {
1129 return mCurrentFrameNumber;
1130 }
1131}
1132
Dan Stoza1ce65812016-06-15 16:26:27 -07001133bool Layer::headFenceHasSignaled() const {
1134#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001135 if (latchUnsignaledBuffers()) {
1136 return true;
1137 }
1138
Dan Stoza1ce65812016-06-15 16:26:27 -07001139 Mutex::Autolock lock(mQueueItemLock);
1140 if (mQueueItems.empty()) {
1141 return true;
1142 }
1143 if (mQueueItems[0].mIsDroppable) {
1144 // Even though this buffer's fence may not have signaled yet, it could
1145 // be replaced by another buffer before it has a chance to, which means
1146 // that it's possible to get into a situation where a buffer is never
1147 // able to be latched. To avoid this, grab this buffer anyway.
1148 return true;
1149 }
1150 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1151#else
1152 return true;
1153#endif
1154}
1155
Dan Stozacac35382016-01-27 12:21:06 -08001156bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1157 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1158 // Don't bother with a SyncPoint, since we've already latched the
1159 // relevant frame
1160 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001161 }
1162
Dan Stozacac35382016-01-27 12:21:06 -08001163 Mutex::Autolock lock(mLocalSyncPointMutex);
1164 mLocalSyncPoints.push_back(point);
1165 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001166}
1167
Mathias Agopian13127d82013-03-05 17:47:11 -08001168void Layer::setFiltering(bool filtering) {
1169 mFiltering = filtering;
1170}
1171
1172bool Layer::getFiltering() const {
1173 return mFiltering;
1174}
1175
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001176// As documented in libhardware header, formats in the range
1177// 0x100 - 0x1FF are specific to the HAL implementation, and
1178// are known to have no alpha channel
1179// TODO: move definition for device-specific range into
1180// hardware.h, instead of using hard-coded values here.
1181#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1182
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001183bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001184 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1185 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001186 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001187 switch (format) {
1188 case HAL_PIXEL_FORMAT_RGBA_8888:
1189 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001190 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001191 }
1192 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001193 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001194}
1195
Mathias Agopian13127d82013-03-05 17:47:11 -08001196// ----------------------------------------------------------------------------
1197// local state
1198// ----------------------------------------------------------------------------
1199
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001200static void boundPoint(vec2* point, const Rect& crop) {
1201 if (point->x < crop.left) {
1202 point->x = crop.left;
1203 }
1204 if (point->x > crop.right) {
1205 point->x = crop.right;
1206 }
1207 if (point->y < crop.top) {
1208 point->y = crop.top;
1209 }
1210 if (point->y > crop.bottom) {
1211 point->y = crop.bottom;
1212 }
1213}
1214
Dan Stozac7014012014-02-14 15:03:43 -08001215void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1216 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001217{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001218 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001219 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 const uint32_t hw_h = hw->getHeight();
1221 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001222 if (!s.crop.isEmpty()) {
1223 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001225 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001226 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001227
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001228 vec2 lt = vec2(win.left, win.top);
1229 vec2 lb = vec2(win.left, win.bottom);
1230 vec2 rb = vec2(win.right, win.bottom);
1231 vec2 rt = vec2(win.right, win.top);
1232
1233 if (!useIdentityTransform) {
1234 lt = s.active.transform.transform(lt);
1235 lb = s.active.transform.transform(lb);
1236 rb = s.active.transform.transform(rb);
1237 rt = s.active.transform.transform(rt);
1238 }
1239
Robert Carrb5d3d262016-03-25 15:08:13 -07001240 if (!s.finalCrop.isEmpty()) {
1241 boundPoint(&lt, s.finalCrop);
1242 boundPoint(&lb, s.finalCrop);
1243 boundPoint(&rb, s.finalCrop);
1244 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001245 }
1246
Mathias Agopianff2ed702013-09-01 21:36:12 -07001247 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001248 position[0] = tr.transform(lt);
1249 position[1] = tr.transform(lb);
1250 position[2] = tr.transform(rb);
1251 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001252 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001253 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 }
1255}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001256
Andy McFadden4125a4f2014-01-29 17:17:11 -08001257bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001258{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001259 // if we don't have a buffer yet, we're translucent regardless of the
1260 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001261 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001262 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001263 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001264
1265 // if the layer has the opaque flag, then we're always opaque,
1266 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001267 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001268}
1269
Dan Stoza23116082015-06-18 14:58:39 -07001270bool Layer::isSecure() const
1271{
1272 const Layer::State& s(mDrawingState);
1273 return (s.flags & layer_state_t::eLayerSecure);
1274}
1275
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001276bool Layer::isProtected() const
1277{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001278 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001279 return (activeBuffer != 0) &&
1280 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1281}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001282
Mathias Agopian13127d82013-03-05 17:47:11 -08001283bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001284 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001285}
1286
1287bool Layer::isCropped() const {
1288 return !mCurrentCrop.isEmpty();
1289}
1290
1291bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1292 return mNeedsFiltering || hw->needsFiltering();
1293}
1294
1295void Layer::setVisibleRegion(const Region& visibleRegion) {
1296 // always called from main thread
1297 this->visibleRegion = visibleRegion;
1298}
1299
1300void Layer::setCoveredRegion(const Region& coveredRegion) {
1301 // always called from main thread
1302 this->coveredRegion = coveredRegion;
1303}
1304
1305void Layer::setVisibleNonTransparentRegion(const Region&
1306 setVisibleNonTransparentRegion) {
1307 // always called from main thread
1308 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1309}
1310
1311// ----------------------------------------------------------------------------
1312// transaction
1313// ----------------------------------------------------------------------------
1314
Dan Stoza7dde5992015-05-22 09:51:44 -07001315void Layer::pushPendingState() {
1316 if (!mCurrentState.modified) {
1317 return;
1318 }
1319
Dan Stoza7dde5992015-05-22 09:51:44 -07001320 // If this transaction is waiting on the receipt of a frame, generate a sync
1321 // point and send it to the remote layer.
1322 if (mCurrentState.handle != nullptr) {
1323 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1324 sp<Layer> handleLayer = handle->owner.promote();
1325 if (handleLayer == nullptr) {
1326 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1327 // If we can't promote the layer we are intended to wait on,
1328 // then it is expired or otherwise invalid. Allow this transaction
1329 // to be applied as per normal (no synchronization).
1330 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001331 } else {
1332 auto syncPoint = std::make_shared<SyncPoint>(
1333 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001334 if (handleLayer->addSyncPoint(syncPoint)) {
1335 mRemoteSyncPoints.push_back(std::move(syncPoint));
1336 } else {
1337 // We already missed the frame we're supposed to synchronize
1338 // on, so go ahead and apply the state update
1339 mCurrentState.handle = nullptr;
1340 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001341 }
1342
Dan Stoza7dde5992015-05-22 09:51:44 -07001343 // Wake us up to check if the frame has been received
1344 setTransactionFlags(eTransactionNeeded);
1345 }
1346 mPendingStates.push_back(mCurrentState);
1347}
1348
Pablo Ceballos05289c22016-04-14 15:49:55 -07001349void Layer::popPendingState(State* stateToCommit) {
1350 auto oldFlags = stateToCommit->flags;
1351 *stateToCommit = mPendingStates[0];
1352 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1353 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001354
1355 mPendingStates.removeAt(0);
1356}
1357
Pablo Ceballos05289c22016-04-14 15:49:55 -07001358bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 bool stateUpdateAvailable = false;
1360 while (!mPendingStates.empty()) {
1361 if (mPendingStates[0].handle != nullptr) {
1362 if (mRemoteSyncPoints.empty()) {
1363 // If we don't have a sync point for this, apply it anyway. It
1364 // will be visually wrong, but it should keep us from getting
1365 // into too much trouble.
1366 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001367 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 stateUpdateAvailable = true;
1369 continue;
1370 }
1371
Dan Stozacac35382016-01-27 12:21:06 -08001372 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1373 mPendingStates[0].frameNumber) {
1374 ALOGE("[%s] Unexpected sync point frame number found",
1375 mName.string());
1376
1377 // Signal our end of the sync point and then dispose of it
1378 mRemoteSyncPoints.front()->setTransactionApplied();
1379 mRemoteSyncPoints.pop_front();
1380 continue;
1381 }
1382
Dan Stoza7dde5992015-05-22 09:51:44 -07001383 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1384 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001385 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001386 stateUpdateAvailable = true;
1387
1388 // Signal our end of the sync point and then dispose of it
1389 mRemoteSyncPoints.front()->setTransactionApplied();
1390 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001391 } else {
1392 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001393 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001394 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001395 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001396 stateUpdateAvailable = true;
1397 }
1398 }
1399
1400 // If we still have pending updates, wake SurfaceFlinger back up and point
1401 // it at this layer so we can process them
1402 if (!mPendingStates.empty()) {
1403 setTransactionFlags(eTransactionNeeded);
1404 mFlinger->setTransactionFlags(eTraversalNeeded);
1405 }
1406
1407 mCurrentState.modified = false;
1408 return stateUpdateAvailable;
1409}
1410
1411void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001412 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001413 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001414 Mutex::Autolock lock(mLocalSyncPointMutex);
1415 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001416 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001417 point->setFrameAvailable();
1418 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001419 }
1420}
1421
Mathias Agopian13127d82013-03-05 17:47:11 -08001422uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001423 ATRACE_CALL();
1424
Dan Stoza7dde5992015-05-22 09:51:44 -07001425 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001426 Layer::State c = getCurrentState();
1427 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001428 return 0;
1429 }
1430
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001431 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001433 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1434 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001435
1436 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001437 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001438 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001439 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001440 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001441 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001442 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001443 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001444 this, getName().string(), mCurrentTransform,
1445 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001446 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001447 c.crop.left,
1448 c.crop.top,
1449 c.crop.right,
1450 c.crop.bottom,
1451 c.crop.getWidth(),
1452 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001453 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001454 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001455 s.crop.left,
1456 s.crop.top,
1457 s.crop.right,
1458 s.crop.bottom,
1459 s.crop.getWidth(),
1460 s.crop.getHeight(),
1461 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001463 // record the new size, form this point on, when the client request
1464 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001465 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001466 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001468
Robert Carr82364e32016-05-15 11:27:47 -07001469 const bool resizePending = (c.requested.w != c.active.w) ||
1470 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001471 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001472 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001473 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001474 // if we have a pending resize, unless we are in fixed-size mode.
1475 // the drawing state will be updated only once we receive a buffer
1476 // with the correct size.
1477 //
1478 // in particular, we want to make sure the clip (which is part
1479 // of the geometry state) is latched together with the size but is
1480 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001481 //
1482 // If a sideband stream is attached, however, we want to skip this
1483 // optimization so that transactions aren't missed when a buffer
1484 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001485
1486 flags |= eDontUpdateGeometryState;
1487 }
1488 }
1489
Mathias Agopian13127d82013-03-05 17:47:11 -08001490 // always set active to requested, unless we're asked not to
1491 // this is used by Layer, which special cases resizes.
1492 if (flags & eDontUpdateGeometryState) {
1493 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001494 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001495 if (mFreezePositionUpdates) {
1496 float tx = c.active.transform.tx();
1497 float ty = c.active.transform.ty();
1498 c.active = c.requested;
1499 c.active.transform.set(tx, ty);
1500 editCurrentState.active = c.active;
1501 } else {
1502 editCurrentState.active = editCurrentState.requested;
1503 c.active = c.requested;
1504 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001505 }
1506
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001507 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001508 // invalidate and recompute the visible regions if needed
1509 flags |= Layer::eVisibleRegion;
1510 }
1511
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001512 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001513 // invalidate and recompute the visible regions if needed
1514 flags |= eVisibleRegion;
1515 this->contentDirty = true;
1516
1517 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001518 const uint8_t type = c.active.transform.getType();
1519 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001520 (type >= Transform::SCALE));
1521 }
1522
Dan Stozac8145172016-04-28 16:29:06 -07001523 // If the layer is hidden, signal and clear out all local sync points so
1524 // that transactions for layers depending on this layer's frames becoming
1525 // visible are not blocked
1526 if (c.flags & layer_state_t::eLayerHidden) {
1527 Mutex::Autolock lock(mLocalSyncPointMutex);
1528 for (auto& point : mLocalSyncPoints) {
1529 point->setFrameAvailable();
1530 }
1531 mLocalSyncPoints.clear();
1532 }
1533
Mathias Agopian13127d82013-03-05 17:47:11 -08001534 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001535 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001536 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537}
1538
Pablo Ceballos05289c22016-04-14 15:49:55 -07001539void Layer::commitTransaction(const State& stateToCommit) {
1540 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001541}
1542
Mathias Agopian13127d82013-03-05 17:47:11 -08001543uint32_t Layer::getTransactionFlags(uint32_t flags) {
1544 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1545}
1546
1547uint32_t Layer::setTransactionFlags(uint32_t flags) {
1548 return android_atomic_or(flags, &mTransactionFlags);
1549}
1550
Robert Carr82364e32016-05-15 11:27:47 -07001551bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001552 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001553 return false;
1554 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001555
1556 // We update the requested and active position simultaneously because
1557 // we want to apply the position portion of the transform matrix immediately,
1558 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001559 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001560 if (immediate && !mFreezePositionUpdates) {
1561 mCurrentState.active.transform.set(x, y);
1562 }
1563 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001564
Dan Stoza7dde5992015-05-22 09:51:44 -07001565 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001566 setTransactionFlags(eTransactionNeeded);
1567 return true;
1568}
Robert Carr82364e32016-05-15 11:27:47 -07001569
Mathias Agopian13127d82013-03-05 17:47:11 -08001570bool Layer::setLayer(uint32_t z) {
1571 if (mCurrentState.z == z)
1572 return false;
1573 mCurrentState.sequence++;
1574 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001575 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001576 setTransactionFlags(eTransactionNeeded);
1577 return true;
1578}
1579bool Layer::setSize(uint32_t w, uint32_t h) {
1580 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1581 return false;
1582 mCurrentState.requested.w = w;
1583 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001584 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001585 setTransactionFlags(eTransactionNeeded);
1586 return true;
1587}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001588#ifdef USE_HWC2
1589bool Layer::setAlpha(float alpha) {
1590#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001591bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001592#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001593 if (mCurrentState.alpha == alpha)
1594 return false;
1595 mCurrentState.sequence++;
1596 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001597 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001598 setTransactionFlags(eTransactionNeeded);
1599 return true;
1600}
1601bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1602 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001603 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001604 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001605 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001606 setTransactionFlags(eTransactionNeeded);
1607 return true;
1608}
1609bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001610 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001611 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001612 setTransactionFlags(eTransactionNeeded);
1613 return true;
1614}
1615bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1616 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1617 if (mCurrentState.flags == newFlags)
1618 return false;
1619 mCurrentState.sequence++;
1620 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001621 mCurrentState.mask = mask;
1622 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001623 setTransactionFlags(eTransactionNeeded);
1624 return true;
1625}
Robert Carr99e27f02016-06-16 15:18:02 -07001626
1627bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001628 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001629 return false;
1630 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001631 mCurrentState.requestedCrop = crop;
1632 if (immediate) {
1633 mCurrentState.crop = crop;
1634 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001635 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 setTransactionFlags(eTransactionNeeded);
1637 return true;
1638}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001639bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001640 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001641 return false;
1642 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001643 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001644 mCurrentState.modified = true;
1645 setTransactionFlags(eTransactionNeeded);
1646 return true;
1647}
Mathias Agopian13127d82013-03-05 17:47:11 -08001648
Robert Carrc3574f72016-03-24 12:19:32 -07001649bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1650 if (scalingMode == mOverrideScalingMode)
1651 return false;
1652 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001653 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001654 return true;
1655}
1656
1657uint32_t Layer::getEffectiveScalingMode() const {
1658 if (mOverrideScalingMode >= 0) {
1659 return mOverrideScalingMode;
1660 }
1661 return mCurrentScalingMode;
1662}
1663
Mathias Agopian13127d82013-03-05 17:47:11 -08001664bool Layer::setLayerStack(uint32_t layerStack) {
1665 if (mCurrentState.layerStack == layerStack)
1666 return false;
1667 mCurrentState.sequence++;
1668 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001669 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001670 setTransactionFlags(eTransactionNeeded);
1671 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001672}
1673
Dan Stoza7dde5992015-05-22 09:51:44 -07001674void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1675 uint64_t frameNumber) {
1676 mCurrentState.handle = handle;
1677 mCurrentState.frameNumber = frameNumber;
1678 // We don't set eTransactionNeeded, because just receiving a deferral
1679 // request without any other state updates shouldn't actually induce a delay
1680 mCurrentState.modified = true;
1681 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001682 mCurrentState.handle = nullptr;
1683 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001684 mCurrentState.modified = false;
1685}
1686
Dan Stozaee44edd2015-03-23 15:50:23 -07001687void Layer::useSurfaceDamage() {
1688 if (mFlinger->mForceFullDamage) {
1689 surfaceDamageRegion = Region::INVALID_REGION;
1690 } else {
1691 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1692 }
1693}
1694
1695void Layer::useEmptyDamage() {
1696 surfaceDamageRegion.clear();
1697}
1698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699// ----------------------------------------------------------------------------
1700// pageflip handling...
1701// ----------------------------------------------------------------------------
1702
Dan Stoza6b9454d2014-11-07 16:00:59 -08001703bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001704 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001705 return true;
1706 }
1707
Dan Stoza6b9454d2014-11-07 16:00:59 -08001708 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001709 if (mQueueItems.empty()) {
1710 return false;
1711 }
1712 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001713 nsecs_t expectedPresent =
1714 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001715
1716 // Ignore timestamps more than a second in the future
1717 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1718 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1719 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1720 expectedPresent);
1721
1722 bool isDue = timestamp < expectedPresent;
1723 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001724}
1725
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001726bool Layer::onPreComposition() {
1727 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001728 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001729}
1730
Dan Stozae77c7662016-05-13 11:37:28 -07001731bool Layer::onPostComposition() {
1732 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001733 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001734 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001735 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1736
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001737 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001738 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001739 mFrameTracker.setFrameReadyFence(frameReadyFence);
1740 } else {
1741 // There was no fence for this frame, so assume that it was ready
1742 // to be presented at the desired present time.
1743 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1744 }
1745
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001746 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001747#ifdef USE_HWC2
1748 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1749#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001750 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001751#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001752 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001753 mFrameTracker.setActualPresentFence(presentFence);
1754 } else {
1755 // The HWC doesn't support present fences, so use the refresh
1756 // timestamp instead.
1757 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1758 mFrameTracker.setActualPresentTime(presentTime);
1759 }
1760
1761 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001762 mFrameLatencyNeeded = false;
1763 }
Dan Stozae77c7662016-05-13 11:37:28 -07001764 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001765}
1766
Dan Stoza9e56aa02015-11-02 13:00:03 -08001767#ifdef USE_HWC2
1768void Layer::releasePendingBuffer() {
1769 mSurfaceFlingerConsumer->releasePendingBuffer();
1770}
1771#endif
1772
Mathias Agopianda27af92012-09-13 18:17:13 -07001773bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001774 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001775#ifdef USE_HWC2
1776 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1777 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1778#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001779 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001780 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001781#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001782}
1783
Mathias Agopian4fec8732012-06-29 14:12:52 -07001784Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001786 ATRACE_CALL();
1787
Jesse Hall399184a2014-03-03 15:42:54 -08001788 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1789 // mSidebandStreamChanged was true
1790 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001791 if (mSidebandStream != NULL) {
1792 setTransactionFlags(eTransactionNeeded);
1793 mFlinger->setTransactionFlags(eTraversalNeeded);
1794 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001795 recomputeVisibleRegions = true;
1796
1797 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001798 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001799 }
1800
Mathias Agopian4fec8732012-06-29 14:12:52 -07001801 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001802 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001803
1804 // if we've already called updateTexImage() without going through
1805 // a composition step, we have to skip this layer at this point
1806 // because we cannot call updateTeximage() without a corresponding
1807 // compositionComplete() call.
1808 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001809 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001810 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001811 }
1812
Dan Stoza1ce65812016-06-15 16:26:27 -07001813 // If the head buffer's acquire fence hasn't signaled yet, return and
1814 // try again later
1815 if (!headFenceHasSignaled()) {
1816 mFlinger->signalLayerUpdate();
1817 return outDirtyRegion;
1818 }
1819
Jamie Gennis351a5132011-09-14 18:23:37 -07001820 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001821 const State& s(getDrawingState());
1822 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001823 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001824
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001825 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001826 Layer::State& front;
1827 Layer::State& current;
1828 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001829 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001830 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001831 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001832 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001833
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001834 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001835 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001836 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001837 int32_t overrideScalingMode,
1838 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001839 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001840 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001841 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001842 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001843 overrideScalingMode(overrideScalingMode),
1844 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001845 }
1846
1847 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001848 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001849 if (buf == NULL) {
1850 return false;
1851 }
1852
1853 uint32_t bufWidth = buf->getWidth();
1854 uint32_t bufHeight = buf->getHeight();
1855
1856 // check that we received a buffer of the right size
1857 // (Take the buffer's orientation into account)
1858 if (item.mTransform & Transform::ROT_90) {
1859 swap(bufWidth, bufHeight);
1860 }
1861
Robert Carrc3574f72016-03-24 12:19:32 -07001862 int actualScalingMode = overrideScalingMode >= 0 ?
1863 overrideScalingMode : item.mScalingMode;
1864 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001865 if (front.active != front.requested) {
1866
1867 if (isFixedSize ||
1868 (bufWidth == front.requested.w &&
1869 bufHeight == front.requested.h))
1870 {
1871 // Here we pretend the transaction happened by updating the
1872 // current and drawing states. Drawing state is only accessed
1873 // in this thread, no need to have it locked
1874 front.active = front.requested;
1875
1876 // We also need to update the current state so that
1877 // we don't end-up overwriting the drawing state with
1878 // this stale current state during the next transaction
1879 //
1880 // NOTE: We don't need to hold the transaction lock here
1881 // because State::active is only accessed from this thread.
1882 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001883 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001884
1885 // recompute visible region
1886 recomputeVisibleRegions = true;
1887 }
1888
1889 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001890 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001891 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001892 " requested={ wh={%4u,%4u} }}\n",
1893 name,
Andy McFadden69052052012-09-14 16:10:11 -07001894 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001895 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001896 front.crop.left,
1897 front.crop.top,
1898 front.crop.right,
1899 front.crop.bottom,
1900 front.crop.getWidth(),
1901 front.crop.getHeight(),
1902 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001903 }
1904
Ruben Brunk1681d952014-06-27 15:51:55 -07001905 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001906 if (front.active.w != bufWidth ||
1907 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001908 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001909 ALOGE("[%s] rejecting buffer: "
1910 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1911 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001912 return true;
1913 }
1914 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001915
1916 // if the transparent region has changed (this test is
1917 // conservative, but that's fine, worst case we're doing
1918 // a bit of extra work), we latch the new one and we
1919 // trigger a visible-region recompute.
1920 if (!front.activeTransparentRegion.isTriviallyEqual(
1921 front.requestedTransparentRegion)) {
1922 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001923
1924 // We also need to update the current state so that
1925 // we don't end-up overwriting the drawing state with
1926 // this stale current state during the next transaction
1927 //
1928 // NOTE: We don't need to hold the transaction lock here
1929 // because State::active is only accessed from this thread.
1930 current.activeTransparentRegion = front.activeTransparentRegion;
1931
1932 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001933 recomputeVisibleRegions = true;
1934 }
1935
Robert Carr99e27f02016-06-16 15:18:02 -07001936 if (front.crop != front.requestedCrop) {
1937 front.crop = front.requestedCrop;
1938 current.crop = front.requestedCrop;
1939 recomputeVisibleRegions = true;
1940 }
Robert Carra3920732016-06-20 21:49:49 -07001941 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001942
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001943 return false;
1944 }
1945 };
1946
Ruben Brunk1681d952014-06-27 15:51:55 -07001947 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001948 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001949 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001950
Dan Stozacac35382016-01-27 12:21:06 -08001951
1952 // Check all of our local sync points to ensure that all transactions
1953 // which need to have been applied prior to the frame which is about to
1954 // be latched have signaled
1955
1956 auto headFrameNumber = getHeadFrameNumber();
1957 bool matchingFramesFound = false;
1958 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001959 {
Dan Stozacac35382016-01-27 12:21:06 -08001960 Mutex::Autolock lock(mLocalSyncPointMutex);
1961 for (auto& point : mLocalSyncPoints) {
1962 if (point->getFrameNumber() > headFrameNumber) {
1963 break;
1964 }
1965
1966 matchingFramesFound = true;
1967
1968 if (!point->frameIsAvailable()) {
1969 // We haven't notified the remote layer that the frame for
1970 // this point is available yet. Notify it now, and then
1971 // abort this attempt to latch.
1972 point->setFrameAvailable();
1973 allTransactionsApplied = false;
1974 break;
1975 }
1976
1977 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001978 }
1979 }
1980
Dan Stozacac35382016-01-27 12:21:06 -08001981 if (matchingFramesFound && !allTransactionsApplied) {
1982 mFlinger->signalLayerUpdate();
1983 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001984 }
1985
Pablo Ceballos06312182015-10-07 16:32:12 -07001986 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1987 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001988 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001989 // buffer mode.
1990 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001991 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001992 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001993 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001994 if (updateResult == BufferQueue::PRESENT_LATER) {
1995 // Producer doesn't want buffer to be displayed yet. Signal a
1996 // layer update so we check again at the next opportunity.
1997 mFlinger->signalLayerUpdate();
1998 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001999 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2000 // If the buffer has been rejected, remove it from the shadow queue
2001 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07002002 if (queuedBuffer) {
2003 Mutex::Autolock lock(mQueueItemLock);
2004 mQueueItems.removeAt(0);
2005 android_atomic_dec(&mQueuedFrames);
2006 }
Dan Stozaecc50402015-04-28 14:42:06 -07002007 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07002008 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2009 // This can occur if something goes wrong when trying to create the
2010 // EGLImage for this buffer. If this happens, the buffer has already
2011 // been released, so we need to clean up the queue and bug out
2012 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07002013 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07002014 Mutex::Autolock lock(mQueueItemLock);
2015 mQueueItems.clear();
2016 android_atomic_and(0, &mQueuedFrames);
2017 }
2018
2019 // Once we have hit this state, the shadow queue may no longer
2020 // correctly reflect the incoming BufferQueue's contents, so even if
2021 // updateTexImage starts working, the only safe course of action is
2022 // to continue to ignore updates.
2023 mUpdateTexImageFailed = true;
2024
2025 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07002026 }
2027
Pablo Ceballos06312182015-10-07 16:32:12 -07002028 if (queuedBuffer) {
2029 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07002030 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2031
Dan Stoza6b9454d2014-11-07 16:00:59 -08002032 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07002033
2034 // Remove any stale buffers that have been dropped during
2035 // updateTexImage
2036 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2037 mQueueItems.removeAt(0);
2038 android_atomic_dec(&mQueuedFrames);
2039 }
2040
Dan Stoza6b9454d2014-11-07 16:00:59 -08002041 mQueueItems.removeAt(0);
2042 }
2043
Dan Stozaecc50402015-04-28 14:42:06 -07002044
Andy McFadden1585c4d2013-06-28 13:52:40 -07002045 // Decrement the queued-frames count. Signal another event if we
2046 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002047 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002048 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002049 mFlinger->signalLayerUpdate();
2050 }
2051
2052 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002053 // something happened!
2054 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002055 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002056 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002057
Jamie Gennis351a5132011-09-14 18:23:37 -07002058 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002059 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002060 if (mActiveBuffer == NULL) {
2061 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002062 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002063 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002064
Mathias Agopian4824d402012-06-04 18:16:30 -07002065 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002066 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002067 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002068 // the first time we receive a buffer, we need to trigger a
2069 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002070 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002071 }
2072
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002073 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2074 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2075 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002076 if ((crop != mCurrentCrop) ||
2077 (transform != mCurrentTransform) ||
2078 (scalingMode != mCurrentScalingMode))
2079 {
2080 mCurrentCrop = crop;
2081 mCurrentTransform = transform;
2082 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002083 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002084 }
2085
2086 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002087 uint32_t bufWidth = mActiveBuffer->getWidth();
2088 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002089 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2090 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002091 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002092 }
2093 }
2094
2095 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002096 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002097 recomputeVisibleRegions = true;
2098 }
2099
Dan Stozacac35382016-01-27 12:21:06 -08002100 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2101
2102 // Remove any sync points corresponding to the buffer which was just
2103 // latched
2104 {
2105 Mutex::Autolock lock(mLocalSyncPointMutex);
2106 auto point = mLocalSyncPoints.begin();
2107 while (point != mLocalSyncPoints.end()) {
2108 if (!(*point)->frameIsAvailable() ||
2109 !(*point)->transactionIsApplied()) {
2110 // This sync point must have been added since we started
2111 // latching. Don't drop it yet.
2112 ++point;
2113 continue;
2114 }
2115
2116 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2117 point = mLocalSyncPoints.erase(point);
2118 } else {
2119 ++point;
2120 }
2121 }
2122 }
2123
Mathias Agopian4fec8732012-06-29 14:12:52 -07002124 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002125 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002126
2127 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002128 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002129 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002130 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002131}
2132
Mathias Agopiana67932f2011-04-20 14:20:59 -07002133uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002134{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002135 // TODO: should we do something special if mSecure is set?
2136 if (mProtectedByApp) {
2137 // need a hardware-protected path to external video sink
2138 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002139 }
Riley Andrews03414a12014-07-01 14:22:59 -07002140 if (mPotentialCursor) {
2141 usage |= GraphicBuffer::USAGE_CURSOR;
2142 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002143 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002144 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002145}
2146
Mathias Agopian84300952012-11-21 16:02:13 -08002147void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002148 uint32_t orientation = 0;
2149 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002150 // The transform hint is used to improve performance, but we can
2151 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002152 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002153 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002154 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002155 if (orientation & Transform::ROT_INVALID) {
2156 orientation = 0;
2157 }
2158 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002159 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002160}
2161
Mathias Agopian13127d82013-03-05 17:47:11 -08002162// ----------------------------------------------------------------------------
2163// debugging
2164// ----------------------------------------------------------------------------
2165
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002166void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002167{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002168 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002169
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002170 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002171 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002172 "+ %s %p (%s)\n",
2173 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002174 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002175
Mathias Agopian2ca79392013-04-02 18:30:32 -07002176 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002177 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002178 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002179 sp<Client> client(mClientRef.promote());
2180
Mathias Agopian74d211a2013-04-22 16:55:35 +02002181 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002182 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2183 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002184 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002185#ifdef USE_HWC2
2186 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2187#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002188 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002189#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002190 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002191 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 -07002192 s.crop.left, s.crop.top,
2193 s.crop.right, s.crop.bottom,
2194 s.finalCrop.left, s.finalCrop.top,
2195 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002196 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002197 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002198 s.active.transform[0][0], s.active.transform[0][1],
2199 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002200 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002201
2202 sp<const GraphicBuffer> buf0(mActiveBuffer);
2203 uint32_t w0=0, h0=0, s0=0, f0=0;
2204 if (buf0 != 0) {
2205 w0 = buf0->getWidth();
2206 h0 = buf0->getHeight();
2207 s0 = buf0->getStride();
2208 f0 = buf0->format;
2209 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002210 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002211 " "
2212 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2213 " queued-frames=%d, mRefreshPending=%d\n",
2214 mFormat, w0, h0, s0,f0,
2215 mQueuedFrames, mRefreshPending);
2216
Mathias Agopian13127d82013-03-05 17:47:11 -08002217 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002218 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002219 }
2220}
2221
Svetoslavd85084b2014-03-20 10:28:31 -07002222void Layer::dumpFrameStats(String8& result) const {
2223 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002224}
2225
Svetoslavd85084b2014-03-20 10:28:31 -07002226void Layer::clearFrameStats() {
2227 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002228}
2229
Jamie Gennis6547ff42013-07-16 20:12:42 -07002230void Layer::logFrameStats() {
2231 mFrameTracker.logAndResetStats(mName);
2232}
2233
Svetoslavd85084b2014-03-20 10:28:31 -07002234void Layer::getFrameStats(FrameStats* outStats) const {
2235 mFrameTracker.getStats(outStats);
2236}
2237
Pablo Ceballos40845df2016-01-25 17:41:15 -08002238void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2239 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2240 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2241 *outName = mName;
2242 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2243
2244#ifdef USE_HWC2
2245 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2246 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2247 HWC2::Composition::Client : true;
2248#else
2249 *outIsGlesComposition = mIsGlesComposition;
2250#endif
2251 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2252 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2253 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2254}
Dan Stozae77c7662016-05-13 11:37:28 -07002255
2256std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2257 bool forceFlush) {
2258 std::vector<OccupancyTracker::Segment> history;
2259 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2260 &history);
2261 if (result != NO_ERROR) {
2262 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2263 result);
2264 return {};
2265 }
2266 return history;
2267}
2268
Robert Carr367c5682016-06-20 11:55:28 -07002269bool Layer::getTransformToDisplayInverse() const {
2270 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2271}
2272
Mathias Agopian13127d82013-03-05 17:47:11 -08002273// ---------------------------------------------------------------------------
2274
2275Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2276 const sp<Layer>& layer)
2277 : mFlinger(flinger), mLayer(layer) {
2278}
2279
2280Layer::LayerCleaner::~LayerCleaner() {
2281 // destroy client resources
2282 mFlinger->onLayerDestroyed(mLayer);
2283}
2284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002285// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002287
2288#if defined(__gl_h_)
2289#error "don't include gl/gl.h in this file"
2290#endif
2291
2292#if defined(__gl2_h_)
2293#error "don't include gl2/gl2.h in this file"
2294#endif