blob: dfece935b100c248247fd8b53ae8256908ad5426 [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 }
Pablo Ceballos51450032016-08-03 10:20:45 -0700562 activeCrop = s.active.transform.inverse().transform(activeCrop, true);
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);
Dan Stozae22aec72016-08-01 13:20:59 -0700594 if (error != HWC2::Error::None) {
595 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
596 mName.string(), transformedFrame.left, transformedFrame.top,
597 transformedFrame.right, transformedFrame.bottom,
598 to_string(error).c_str(), static_cast<int32_t>(error));
599 } else {
600 hwcInfo.displayFrame = transformedFrame;
601 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602
603 FloatRect sourceCrop = computeCrop(displayDevice);
604 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700605 if (error != HWC2::Error::None) {
606 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
607 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
608 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
609 static_cast<int32_t>(error));
610 } else {
611 hwcInfo.sourceCrop = sourceCrop;
612 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613
614 error = hwcLayer->setPlaneAlpha(s.alpha);
615 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
616 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
617 static_cast<int32_t>(error));
618
619 error = hwcLayer->setZOrder(s.z);
620 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
621 mName.string(), s.z, to_string(error).c_str(),
622 static_cast<int32_t>(error));
623#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000624 if (!frame.intersect(hw->getViewport(), &frame)) {
625 frame.clear();
626 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800627 const Transform& tr(hw->getTransform());
628 layer.setFrame(tr.transform(frame));
629 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800630 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800632
Mathias Agopian29a367b2011-07-12 14:51:45 -0700633 /*
634 * Transformations are applied in this order:
635 * 1) buffer orientation/flip/mirror
636 * 2) state transformation (window manager)
637 * 3) layer orientation (screen orientation)
638 * (NOTE: the matrices are multiplied in reverse order)
639 */
640
641 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800642 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700643
644 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
645 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700646 * the code below applies the primary display's inverse transform to the
647 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700648 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700649 uint32_t invTransform =
650 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700651 // calculate the inverse transform
652 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
653 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
654 NATIVE_WINDOW_TRANSFORM_FLIP_H;
655 }
656 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700657 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700658 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700659
660 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800661 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662#ifdef USE_HWC2
663 if (orientation & Transform::ROT_INVALID) {
664 // we can only handle simple transformation
665 hwcInfo.forceClientComposition = true;
666 } else {
667 auto transform = static_cast<HWC2::Transform>(orientation);
668 auto error = hwcLayer->setTransform(transform);
669 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
670 "%s (%d)", mName.string(), to_string(transform).c_str(),
671 to_string(error).c_str(), static_cast<int32_t>(error));
672 }
673#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800674 if (orientation & Transform::ROT_INVALID) {
675 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700676 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700677 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800678 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700679 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700681}
682
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683#ifdef USE_HWC2
684void Layer::forceClientComposition(int32_t hwcId) {
685 if (mHwcLayers.count(hwcId) == 0) {
686 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
687 return;
688 }
689
690 mHwcLayers[hwcId].forceClientComposition = true;
691}
692#endif
693
694#ifdef USE_HWC2
695void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
696 // Apply this display's projection's viewport to the visible region
697 // before giving it to the HWC HAL.
698 const Transform& tr = displayDevice->getTransform();
699 const auto& viewport = displayDevice->getViewport();
700 Region visible = tr.transform(visibleRegion.intersect(viewport));
701 auto hwcId = displayDevice->getHwcDisplayId();
702 auto& hwcLayer = mHwcLayers[hwcId].layer;
703 auto error = hwcLayer->setVisibleRegion(visible);
704 if (error != HWC2::Error::None) {
705 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
706 to_string(error).c_str(), static_cast<int32_t>(error));
707 visible.dump(LOG_TAG);
708 }
709
710 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
711 if (error != HWC2::Error::None) {
712 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
713 to_string(error).c_str(), static_cast<int32_t>(error));
714 surfaceDamageRegion.dump(LOG_TAG);
715 }
716
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700717 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700719 setCompositionType(hwcId, HWC2::Composition::Sideband);
720 ALOGV("[%s] Requesting Sideband composition", mName.string());
721 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722 if (error != HWC2::Error::None) {
723 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
724 mName.string(), mSidebandStream->handle(),
725 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700727 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728 }
729
Dan Stoza0a21df72016-07-20 12:52:38 -0700730 // Client layers
731 if (mHwcLayers[hwcId].forceClientComposition ||
732 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700733 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700735 return;
736 }
737
Dan Stoza0a21df72016-07-20 12:52:38 -0700738 // SolidColor layers
739 if (mActiveBuffer == nullptr) {
740 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700741
742 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700743 error = hwcLayer->setColor({0, 0, 0, 255});
744 if (error != HWC2::Error::None) {
745 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
746 to_string(error).c_str(), static_cast<int32_t>(error));
747 }
Dan Stozac6c89542016-07-27 10:16:52 -0700748
749 // Clear out the transform, because it doesn't make sense absent a
750 // source buffer
751 error = hwcLayer->setTransform(HWC2::Transform::None);
752 if (error != HWC2::Error::None) {
753 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
754 to_string(error).c_str(), static_cast<int32_t>(error));
755 }
756
Dan Stoza0a21df72016-07-20 12:52:38 -0700757 return;
758 }
759
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700760 // Device or Cursor layers
761 if (mPotentialCursor) {
762 ALOGV("[%s] Requesting Cursor composition", mName.string());
763 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 } else {
765 ALOGV("[%s] Requesting Device composition", mName.string());
766 setCompositionType(hwcId, HWC2::Composition::Device);
767 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700768
769 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
770 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
771 if (error != HWC2::Error::None) {
772 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
773 mActiveBuffer->handle, to_string(error).c_str(),
774 static_cast<int32_t>(error));
775 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776}
777#else
Mathias Agopian42977342012-08-05 00:40:46 -0700778void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700779 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800780 // we have to set the visible region on every frame because
781 // we currently free it during onLayerDisplayed(), which is called
782 // after HWComposer::commit() -- every frame.
783 // Apply this display's projection's viewport to the visible region
784 // before giving it to the HWC HAL.
785 const Transform& tr = hw->getTransform();
786 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
787 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700788 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800789 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700790
Jesse Hall399184a2014-03-03 15:42:54 -0800791 if (mSidebandStream.get()) {
792 layer.setSidebandStream(mSidebandStream);
793 } else {
794 // NOTE: buffer can be NULL if the client never drew into this
795 // layer yet, or if we ran out of memory
796 layer.setBuffer(mActiveBuffer);
797 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700798}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700800
Dan Stoza9e56aa02015-11-02 13:00:03 -0800801#ifdef USE_HWC2
802void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
803 auto hwcId = displayDevice->getHwcDisplayId();
804 if (mHwcLayers.count(hwcId) == 0 ||
805 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
806 return;
807 }
808
809 // This gives us only the "orientation" component of the transform
810 const State& s(getCurrentState());
811
812 // Apply the layer's transform, followed by the display's global transform
813 // Here we're guaranteed that the layer's transform preserves rects
814 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700815 if (!s.crop.isEmpty()) {
816 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800817 }
818 // Subtract the transparent region and snap to the bounds
819 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800820 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800821 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700822 if (!s.finalCrop.isEmpty()) {
823 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000824 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800825 auto& displayTransform(displayDevice->getTransform());
826 auto position = displayTransform.transform(frame);
827
828 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
829 position.top);
830 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
831 "to (%d, %d): %s (%d)", mName.string(), position.left,
832 position.top, to_string(error).c_str(),
833 static_cast<int32_t>(error));
834}
835#else
Dan Stozac7014012014-02-14 15:03:43 -0800836void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700837 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700838 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700839
840 // TODO: there is a possible optimization here: we only need to set the
841 // acquire fence the first time a new buffer is acquired on EACH display.
842
Riley Andrews03414a12014-07-01 14:22:59 -0700843 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800844 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800845 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700846 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700847 if (fenceFd == -1) {
848 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
849 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700850 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700851 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700852 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700853}
854
Riley Andrews03414a12014-07-01 14:22:59 -0700855Rect Layer::getPosition(
856 const sp<const DisplayDevice>& hw)
857{
858 // this gives us only the "orientation" component of the transform
859 const State& s(getCurrentState());
860
861 // apply the layer's transform, followed by the display's global transform
862 // here we're guaranteed that the layer's transform preserves rects
863 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700864 if (!s.crop.isEmpty()) {
865 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700866 }
867 // subtract the transparent region and snap to the bounds
868 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800869 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700870 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700871 if (!s.finalCrop.isEmpty()) {
872 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000873 }
Riley Andrews03414a12014-07-01 14:22:59 -0700874 const Transform& tr(hw->getTransform());
875 return Rect(tr.transform(frame));
876}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800877#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700878
Mathias Agopian13127d82013-03-05 17:47:11 -0800879// ---------------------------------------------------------------------------
880// drawing...
881// ---------------------------------------------------------------------------
882
883void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800884 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800885}
886
Dan Stozac7014012014-02-14 15:03:43 -0800887void Layer::draw(const sp<const DisplayDevice>& hw,
888 bool useIdentityTransform) const {
889 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800890}
891
Dan Stozac7014012014-02-14 15:03:43 -0800892void Layer::draw(const sp<const DisplayDevice>& hw) const {
893 onDraw(hw, Region(hw->bounds()), false);
894}
895
896void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
897 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800899 ATRACE_CALL();
900
Mathias Agopiana67932f2011-04-20 14:20:59 -0700901 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700903 // in fact never been drawn into. This happens frequently with
904 // SurfaceView because the WindowManager can't know when the client
905 // has drawn the first time.
906
907 // If there is nothing under us, we paint the screen in black, otherwise
908 // we just skip this update.
909
910 // figure out if there is something below us
911 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700912 const SurfaceFlinger::LayerVector& drawingLayers(
913 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700914 const size_t count = drawingLayers.size();
915 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800916 const sp<Layer>& layer(drawingLayers[i]);
917 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700918 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700919 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700920 }
921 // if not everything below us is covered, we plug the holes!
922 Region holes(clip.subtract(under));
923 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700924 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700925 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 return;
927 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700928
Andy McFadden97eba892012-12-11 15:21:45 -0800929 // Bind the current buffer to the GL texture, and wait for it to be
930 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800931 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
932 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800933 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700934 // Go ahead and draw the buffer anyway; no matter what we do the screen
935 // is probably going to have something visibly wrong.
936 }
937
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700938 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
939
Mathias Agopian875d8e12013-06-07 15:35:48 -0700940 RenderEngine& engine(mFlinger->getRenderEngine());
941
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700942 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700943 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700944 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700945
946 // Query the texture matrix given our current filtering mode.
947 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800948 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
949 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700950
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700951 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
952
953 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700954 * the code below applies the primary display's inverse transform to
955 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700956 */
957
958 // create a 4x4 transform matrix from the display transform flags
959 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
960 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
961 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
962
963 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700964 uint32_t transform =
965 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700966 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
967 tr = tr * rot90;
968 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
969 tr = tr * flipH;
970 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
971 tr = tr * flipV;
972
973 // calculate the inverse
974 tr = inverse(tr);
975
976 // and finally apply it to the original texture matrix
977 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
978 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
979 }
980
Jamie Genniscbb1a952012-05-08 17:05:52 -0700981 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700982 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
983 mTexture.setFiltering(useFiltering);
984 mTexture.setMatrix(textureMatrix);
985
986 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700987 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700988 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700989 }
Dan Stozac7014012014-02-14 15:03:43 -0800990 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700991 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992}
993
Mathias Agopian13127d82013-03-05 17:47:11 -0800994
Dan Stozac7014012014-02-14 15:03:43 -0800995void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
996 const Region& /* clip */, float red, float green, float blue,
997 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800998{
Mathias Agopian19733a32013-08-28 18:13:56 -0700999 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -08001000 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -07001001 engine.setupFillWithColor(red, green, blue, alpha);
1002 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -08001003}
1004
1005void Layer::clearWithOpenGL(
1006 const sp<const DisplayDevice>& hw, const Region& clip) const {
1007 clearWithOpenGL(hw, clip, 0,0,0,0);
1008}
1009
Dan Stozac7014012014-02-14 15:03:43 -08001010void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
1011 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001012 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001013
Dan Stozac7014012014-02-14 15:03:43 -08001014 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001015
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 /*
1017 * NOTE: the way we compute the texture coordinates here produces
1018 * different results than when we take the HWC path -- in the later case
1019 * the "source crop" is rounded to texel boundaries.
1020 * This can produce significantly different results when the texture
1021 * is scaled by a large amount.
1022 *
1023 * The GL code below is more logical (imho), and the difference with
1024 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001025 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001026 * GL composition when a buffer scaling is applied (maybe with some
1027 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1028 * like more of a hack.
1029 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001030 Rect win(computeBounds());
1031
Robert Carrb5d3d262016-03-25 15:08:13 -07001032 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001033 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001034 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001035 win.clear();
1036 }
1037 win = s.active.transform.inverse().transform(win);
1038 if (!win.intersect(computeBounds(), &win)) {
1039 win.clear();
1040 }
1041 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001042
Mathias Agopian3f844832013-08-07 21:24:32 -07001043 float left = float(win.left) / float(s.active.w);
1044 float top = float(win.top) / float(s.active.h);
1045 float right = float(win.right) / float(s.active.w);
1046 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001047
Mathias Agopian875d8e12013-06-07 15:35:48 -07001048 // TODO: we probably want to generate the texture coords with the mesh
1049 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001050 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1051 texCoords[0] = vec2(left, 1.0f - top);
1052 texCoords[1] = vec2(left, 1.0f - bottom);
1053 texCoords[2] = vec2(right, 1.0f - bottom);
1054 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001055
Mathias Agopian875d8e12013-06-07 15:35:48 -07001056 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001057 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001058 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001059 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001060}
1061
Dan Stoza9e56aa02015-11-02 13:00:03 -08001062#ifdef USE_HWC2
1063void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1064 bool callIntoHwc) {
1065 if (mHwcLayers.count(hwcId) == 0) {
1066 ALOGE("setCompositionType called without a valid HWC layer");
1067 return;
1068 }
1069 auto& hwcInfo = mHwcLayers[hwcId];
1070 auto& hwcLayer = hwcInfo.layer;
1071 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1072 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1073 if (hwcInfo.compositionType != type) {
1074 ALOGV(" actually setting");
1075 hwcInfo.compositionType = type;
1076 if (callIntoHwc) {
1077 auto error = hwcLayer->setCompositionType(type);
1078 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1079 "composition type %s: %s (%d)", mName.string(),
1080 to_string(type).c_str(), to_string(error).c_str(),
1081 static_cast<int32_t>(error));
1082 }
1083 }
1084}
1085
1086HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001087 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1088 // If we're querying the composition type for a display that does not
1089 // have a HWC counterpart, then it will always be Client
1090 return HWC2::Composition::Client;
1091 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001092 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001093 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001094 return HWC2::Composition::Invalid;
1095 }
1096 return mHwcLayers.at(hwcId).compositionType;
1097}
1098
1099void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1100 if (mHwcLayers.count(hwcId) == 0) {
1101 ALOGE("setClearClientTarget called without a valid HWC layer");
1102 return;
1103 }
1104 mHwcLayers[hwcId].clearClientTarget = clear;
1105}
1106
1107bool Layer::getClearClientTarget(int32_t hwcId) const {
1108 if (mHwcLayers.count(hwcId) == 0) {
1109 ALOGE("getClearClientTarget called without a valid HWC layer");
1110 return false;
1111 }
1112 return mHwcLayers.at(hwcId).clearClientTarget;
1113}
1114#endif
1115
Ruben Brunk1681d952014-06-27 15:51:55 -07001116uint32_t Layer::getProducerStickyTransform() const {
1117 int producerStickyTransform = 0;
1118 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1119 if (ret != OK) {
1120 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1121 strerror(-ret), ret);
1122 return 0;
1123 }
1124 return static_cast<uint32_t>(producerStickyTransform);
1125}
1126
Dan Stozac5da2712016-07-20 15:38:12 -07001127bool Layer::latchUnsignaledBuffers() {
1128 static bool propertyLoaded = false;
1129 static bool latch = false;
1130 static std::mutex mutex;
1131 std::lock_guard<std::mutex> lock(mutex);
1132 if (!propertyLoaded) {
1133 char value[PROPERTY_VALUE_MAX] = {};
1134 property_get("debug.sf.latch_unsignaled", value, "0");
1135 latch = atoi(value);
1136 propertyLoaded = true;
1137 }
1138 return latch;
1139}
1140
Dan Stozacac35382016-01-27 12:21:06 -08001141uint64_t Layer::getHeadFrameNumber() const {
1142 Mutex::Autolock lock(mQueueItemLock);
1143 if (!mQueueItems.empty()) {
1144 return mQueueItems[0].mFrameNumber;
1145 } else {
1146 return mCurrentFrameNumber;
1147 }
1148}
1149
Dan Stoza1ce65812016-06-15 16:26:27 -07001150bool Layer::headFenceHasSignaled() const {
1151#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001152 if (latchUnsignaledBuffers()) {
1153 return true;
1154 }
1155
Dan Stoza1ce65812016-06-15 16:26:27 -07001156 Mutex::Autolock lock(mQueueItemLock);
1157 if (mQueueItems.empty()) {
1158 return true;
1159 }
1160 if (mQueueItems[0].mIsDroppable) {
1161 // Even though this buffer's fence may not have signaled yet, it could
1162 // be replaced by another buffer before it has a chance to, which means
1163 // that it's possible to get into a situation where a buffer is never
1164 // able to be latched. To avoid this, grab this buffer anyway.
1165 return true;
1166 }
1167 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1168#else
1169 return true;
1170#endif
1171}
1172
Dan Stozacac35382016-01-27 12:21:06 -08001173bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1174 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1175 // Don't bother with a SyncPoint, since we've already latched the
1176 // relevant frame
1177 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001178 }
1179
Dan Stozacac35382016-01-27 12:21:06 -08001180 Mutex::Autolock lock(mLocalSyncPointMutex);
1181 mLocalSyncPoints.push_back(point);
1182 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001183}
1184
Mathias Agopian13127d82013-03-05 17:47:11 -08001185void Layer::setFiltering(bool filtering) {
1186 mFiltering = filtering;
1187}
1188
1189bool Layer::getFiltering() const {
1190 return mFiltering;
1191}
1192
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001193// As documented in libhardware header, formats in the range
1194// 0x100 - 0x1FF are specific to the HAL implementation, and
1195// are known to have no alpha channel
1196// TODO: move definition for device-specific range into
1197// hardware.h, instead of using hard-coded values here.
1198#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1199
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001200bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001201 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1202 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001203 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001204 switch (format) {
1205 case HAL_PIXEL_FORMAT_RGBA_8888:
1206 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001207 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001208 }
1209 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001210 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001211}
1212
Mathias Agopian13127d82013-03-05 17:47:11 -08001213// ----------------------------------------------------------------------------
1214// local state
1215// ----------------------------------------------------------------------------
1216
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001217static void boundPoint(vec2* point, const Rect& crop) {
1218 if (point->x < crop.left) {
1219 point->x = crop.left;
1220 }
1221 if (point->x > crop.right) {
1222 point->x = crop.right;
1223 }
1224 if (point->y < crop.top) {
1225 point->y = crop.top;
1226 }
1227 if (point->y > crop.bottom) {
1228 point->y = crop.bottom;
1229 }
1230}
1231
Dan Stozac7014012014-02-14 15:03:43 -08001232void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1233 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001234{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001235 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001236 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 const uint32_t hw_h = hw->getHeight();
1238 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001239 if (!s.crop.isEmpty()) {
1240 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001242 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001243 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001244
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001245 vec2 lt = vec2(win.left, win.top);
1246 vec2 lb = vec2(win.left, win.bottom);
1247 vec2 rb = vec2(win.right, win.bottom);
1248 vec2 rt = vec2(win.right, win.top);
1249
1250 if (!useIdentityTransform) {
1251 lt = s.active.transform.transform(lt);
1252 lb = s.active.transform.transform(lb);
1253 rb = s.active.transform.transform(rb);
1254 rt = s.active.transform.transform(rt);
1255 }
1256
Robert Carrb5d3d262016-03-25 15:08:13 -07001257 if (!s.finalCrop.isEmpty()) {
1258 boundPoint(&lt, s.finalCrop);
1259 boundPoint(&lb, s.finalCrop);
1260 boundPoint(&rb, s.finalCrop);
1261 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001262 }
1263
Mathias Agopianff2ed702013-09-01 21:36:12 -07001264 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001265 position[0] = tr.transform(lt);
1266 position[1] = tr.transform(lb);
1267 position[2] = tr.transform(rb);
1268 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001269 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001270 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 }
1272}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001273
Andy McFadden4125a4f2014-01-29 17:17:11 -08001274bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001275{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001276 // if we don't have a buffer yet, we're translucent regardless of the
1277 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001278 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001279 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001280 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001281
1282 // if the layer has the opaque flag, then we're always opaque,
1283 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001284 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001285}
1286
Dan Stoza23116082015-06-18 14:58:39 -07001287bool Layer::isSecure() const
1288{
1289 const Layer::State& s(mDrawingState);
1290 return (s.flags & layer_state_t::eLayerSecure);
1291}
1292
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001293bool Layer::isProtected() const
1294{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001295 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001296 return (activeBuffer != 0) &&
1297 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1298}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001299
Mathias Agopian13127d82013-03-05 17:47:11 -08001300bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001301 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001302}
1303
1304bool Layer::isCropped() const {
1305 return !mCurrentCrop.isEmpty();
1306}
1307
1308bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1309 return mNeedsFiltering || hw->needsFiltering();
1310}
1311
1312void Layer::setVisibleRegion(const Region& visibleRegion) {
1313 // always called from main thread
1314 this->visibleRegion = visibleRegion;
1315}
1316
1317void Layer::setCoveredRegion(const Region& coveredRegion) {
1318 // always called from main thread
1319 this->coveredRegion = coveredRegion;
1320}
1321
1322void Layer::setVisibleNonTransparentRegion(const Region&
1323 setVisibleNonTransparentRegion) {
1324 // always called from main thread
1325 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1326}
1327
1328// ----------------------------------------------------------------------------
1329// transaction
1330// ----------------------------------------------------------------------------
1331
Dan Stoza7dde5992015-05-22 09:51:44 -07001332void Layer::pushPendingState() {
1333 if (!mCurrentState.modified) {
1334 return;
1335 }
1336
Dan Stoza7dde5992015-05-22 09:51:44 -07001337 // If this transaction is waiting on the receipt of a frame, generate a sync
1338 // point and send it to the remote layer.
1339 if (mCurrentState.handle != nullptr) {
Dan Stoza92cd24e2016-08-09 13:21:03 -07001340 sp<IBinder> strongBinder = mCurrentState.handle.promote();
1341 sp<Handle> handle = nullptr;
1342 sp<Layer> handleLayer = nullptr;
1343 if (strongBinder != nullptr) {
1344 handle = static_cast<Handle*>(strongBinder.get());
1345 handleLayer = handle->owner.promote();
1346 }
1347 if (strongBinder == nullptr || handleLayer == nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001348 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1349 // If we can't promote the layer we are intended to wait on,
1350 // then it is expired or otherwise invalid. Allow this transaction
1351 // to be applied as per normal (no synchronization).
1352 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001353 } else {
1354 auto syncPoint = std::make_shared<SyncPoint>(
1355 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001356 if (handleLayer->addSyncPoint(syncPoint)) {
1357 mRemoteSyncPoints.push_back(std::move(syncPoint));
1358 } else {
1359 // We already missed the frame we're supposed to synchronize
1360 // on, so go ahead and apply the state update
1361 mCurrentState.handle = nullptr;
1362 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001363 }
1364
Dan Stoza7dde5992015-05-22 09:51:44 -07001365 // Wake us up to check if the frame has been received
1366 setTransactionFlags(eTransactionNeeded);
1367 }
1368 mPendingStates.push_back(mCurrentState);
1369}
1370
Pablo Ceballos05289c22016-04-14 15:49:55 -07001371void Layer::popPendingState(State* stateToCommit) {
1372 auto oldFlags = stateToCommit->flags;
1373 *stateToCommit = mPendingStates[0];
1374 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1375 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001376
1377 mPendingStates.removeAt(0);
1378}
1379
Pablo Ceballos05289c22016-04-14 15:49:55 -07001380bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001381 bool stateUpdateAvailable = false;
1382 while (!mPendingStates.empty()) {
1383 if (mPendingStates[0].handle != nullptr) {
1384 if (mRemoteSyncPoints.empty()) {
1385 // If we don't have a sync point for this, apply it anyway. It
1386 // will be visually wrong, but it should keep us from getting
1387 // into too much trouble.
1388 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001389 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001390 stateUpdateAvailable = true;
1391 continue;
1392 }
1393
Dan Stozacac35382016-01-27 12:21:06 -08001394 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1395 mPendingStates[0].frameNumber) {
1396 ALOGE("[%s] Unexpected sync point frame number found",
1397 mName.string());
1398
1399 // Signal our end of the sync point and then dispose of it
1400 mRemoteSyncPoints.front()->setTransactionApplied();
1401 mRemoteSyncPoints.pop_front();
1402 continue;
1403 }
1404
Dan Stoza7dde5992015-05-22 09:51:44 -07001405 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1406 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001407 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001408 stateUpdateAvailable = true;
1409
1410 // Signal our end of the sync point and then dispose of it
1411 mRemoteSyncPoints.front()->setTransactionApplied();
1412 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001413 } else {
1414 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001415 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001416 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001417 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001418 stateUpdateAvailable = true;
1419 }
1420 }
1421
1422 // If we still have pending updates, wake SurfaceFlinger back up and point
1423 // it at this layer so we can process them
1424 if (!mPendingStates.empty()) {
1425 setTransactionFlags(eTransactionNeeded);
1426 mFlinger->setTransactionFlags(eTraversalNeeded);
1427 }
1428
1429 mCurrentState.modified = false;
1430 return stateUpdateAvailable;
1431}
1432
1433void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001434 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001435 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001436 Mutex::Autolock lock(mLocalSyncPointMutex);
1437 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001438 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001439 point->setFrameAvailable();
1440 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001441 }
1442}
1443
Mathias Agopian13127d82013-03-05 17:47:11 -08001444uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001445 ATRACE_CALL();
1446
Dan Stoza7dde5992015-05-22 09:51:44 -07001447 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001448 Layer::State c = getCurrentState();
1449 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001450 return 0;
1451 }
1452
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001453 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001455 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1456 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001457
1458 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001459 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001460 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001461 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001462 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001463 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001464 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001465 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001466 this, getName().string(), mCurrentTransform,
1467 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001468 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001469 c.crop.left,
1470 c.crop.top,
1471 c.crop.right,
1472 c.crop.bottom,
1473 c.crop.getWidth(),
1474 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001475 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001476 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001477 s.crop.left,
1478 s.crop.top,
1479 s.crop.right,
1480 s.crop.bottom,
1481 s.crop.getWidth(),
1482 s.crop.getHeight(),
1483 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001485 // record the new size, form this point on, when the client request
1486 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001487 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001488 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001490
Robert Carr82364e32016-05-15 11:27:47 -07001491 const bool resizePending = (c.requested.w != c.active.w) ||
1492 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001493 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001494 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001495 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001496 // if we have a pending resize, unless we are in fixed-size mode.
1497 // the drawing state will be updated only once we receive a buffer
1498 // with the correct size.
1499 //
1500 // in particular, we want to make sure the clip (which is part
1501 // of the geometry state) is latched together with the size but is
1502 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001503 //
1504 // If a sideband stream is attached, however, we want to skip this
1505 // optimization so that transactions aren't missed when a buffer
1506 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001507
1508 flags |= eDontUpdateGeometryState;
1509 }
1510 }
1511
Mathias Agopian13127d82013-03-05 17:47:11 -08001512 // always set active to requested, unless we're asked not to
1513 // this is used by Layer, which special cases resizes.
1514 if (flags & eDontUpdateGeometryState) {
1515 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001516 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001517 if (mFreezePositionUpdates) {
1518 float tx = c.active.transform.tx();
1519 float ty = c.active.transform.ty();
1520 c.active = c.requested;
1521 c.active.transform.set(tx, ty);
1522 editCurrentState.active = c.active;
1523 } else {
1524 editCurrentState.active = editCurrentState.requested;
1525 c.active = c.requested;
1526 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001527 }
1528
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001529 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001530 // invalidate and recompute the visible regions if needed
1531 flags |= Layer::eVisibleRegion;
1532 }
1533
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001534 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001535 // invalidate and recompute the visible regions if needed
1536 flags |= eVisibleRegion;
1537 this->contentDirty = true;
1538
1539 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001540 const uint8_t type = c.active.transform.getType();
1541 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001542 (type >= Transform::SCALE));
1543 }
1544
Dan Stozac8145172016-04-28 16:29:06 -07001545 // If the layer is hidden, signal and clear out all local sync points so
1546 // that transactions for layers depending on this layer's frames becoming
1547 // visible are not blocked
1548 if (c.flags & layer_state_t::eLayerHidden) {
1549 Mutex::Autolock lock(mLocalSyncPointMutex);
1550 for (auto& point : mLocalSyncPoints) {
1551 point->setFrameAvailable();
1552 }
1553 mLocalSyncPoints.clear();
1554 }
1555
Mathias Agopian13127d82013-03-05 17:47:11 -08001556 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001557 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001558 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559}
1560
Pablo Ceballos05289c22016-04-14 15:49:55 -07001561void Layer::commitTransaction(const State& stateToCommit) {
1562 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001563}
1564
Mathias Agopian13127d82013-03-05 17:47:11 -08001565uint32_t Layer::getTransactionFlags(uint32_t flags) {
1566 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1567}
1568
1569uint32_t Layer::setTransactionFlags(uint32_t flags) {
1570 return android_atomic_or(flags, &mTransactionFlags);
1571}
1572
Robert Carr82364e32016-05-15 11:27:47 -07001573bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001574 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001575 return false;
1576 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001577
1578 // We update the requested and active position simultaneously because
1579 // we want to apply the position portion of the transform matrix immediately,
1580 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001581 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001582 if (immediate && !mFreezePositionUpdates) {
1583 mCurrentState.active.transform.set(x, y);
1584 }
1585 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001586
Dan Stoza7dde5992015-05-22 09:51:44 -07001587 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001588 setTransactionFlags(eTransactionNeeded);
1589 return true;
1590}
Robert Carr82364e32016-05-15 11:27:47 -07001591
Mathias Agopian13127d82013-03-05 17:47:11 -08001592bool Layer::setLayer(uint32_t z) {
1593 if (mCurrentState.z == z)
1594 return false;
1595 mCurrentState.sequence++;
1596 mCurrentState.z = z;
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::setSize(uint32_t w, uint32_t h) {
1602 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1603 return false;
1604 mCurrentState.requested.w = w;
1605 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001606 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001607 setTransactionFlags(eTransactionNeeded);
1608 return true;
1609}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001610#ifdef USE_HWC2
1611bool Layer::setAlpha(float alpha) {
1612#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001613bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001614#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001615 if (mCurrentState.alpha == alpha)
1616 return false;
1617 mCurrentState.sequence++;
1618 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001619 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001620 setTransactionFlags(eTransactionNeeded);
1621 return true;
1622}
1623bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1624 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001625 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001626 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001627 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001628 setTransactionFlags(eTransactionNeeded);
1629 return true;
1630}
1631bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001632 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001633 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001634 setTransactionFlags(eTransactionNeeded);
1635 return true;
1636}
1637bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1638 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1639 if (mCurrentState.flags == newFlags)
1640 return false;
1641 mCurrentState.sequence++;
1642 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001643 mCurrentState.mask = mask;
1644 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001645 setTransactionFlags(eTransactionNeeded);
1646 return true;
1647}
Robert Carr99e27f02016-06-16 15:18:02 -07001648
1649bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001650 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001651 return false;
1652 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001653 mCurrentState.requestedCrop = crop;
1654 if (immediate) {
1655 mCurrentState.crop = crop;
1656 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001657 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001658 setTransactionFlags(eTransactionNeeded);
1659 return true;
1660}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001661bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001662 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001663 return false;
1664 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001665 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001666 mCurrentState.modified = true;
1667 setTransactionFlags(eTransactionNeeded);
1668 return true;
1669}
Mathias Agopian13127d82013-03-05 17:47:11 -08001670
Robert Carrc3574f72016-03-24 12:19:32 -07001671bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1672 if (scalingMode == mOverrideScalingMode)
1673 return false;
1674 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001675 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001676 return true;
1677}
1678
1679uint32_t Layer::getEffectiveScalingMode() const {
1680 if (mOverrideScalingMode >= 0) {
1681 return mOverrideScalingMode;
1682 }
1683 return mCurrentScalingMode;
1684}
1685
Mathias Agopian13127d82013-03-05 17:47:11 -08001686bool Layer::setLayerStack(uint32_t layerStack) {
1687 if (mCurrentState.layerStack == layerStack)
1688 return false;
1689 mCurrentState.sequence++;
1690 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001691 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001692 setTransactionFlags(eTransactionNeeded);
1693 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001694}
1695
Dan Stoza7dde5992015-05-22 09:51:44 -07001696void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1697 uint64_t frameNumber) {
1698 mCurrentState.handle = handle;
1699 mCurrentState.frameNumber = frameNumber;
1700 // We don't set eTransactionNeeded, because just receiving a deferral
1701 // request without any other state updates shouldn't actually induce a delay
1702 mCurrentState.modified = true;
1703 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001704 mCurrentState.handle = nullptr;
1705 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001706 mCurrentState.modified = false;
1707}
1708
Dan Stozaee44edd2015-03-23 15:50:23 -07001709void Layer::useSurfaceDamage() {
1710 if (mFlinger->mForceFullDamage) {
1711 surfaceDamageRegion = Region::INVALID_REGION;
1712 } else {
1713 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1714 }
1715}
1716
1717void Layer::useEmptyDamage() {
1718 surfaceDamageRegion.clear();
1719}
1720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721// ----------------------------------------------------------------------------
1722// pageflip handling...
1723// ----------------------------------------------------------------------------
1724
Dan Stoza6b9454d2014-11-07 16:00:59 -08001725bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001726 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001727 return true;
1728 }
1729
Dan Stoza6b9454d2014-11-07 16:00:59 -08001730 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001731 if (mQueueItems.empty()) {
1732 return false;
1733 }
1734 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001735 nsecs_t expectedPresent =
1736 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001737
1738 // Ignore timestamps more than a second in the future
1739 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1740 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1741 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1742 expectedPresent);
1743
1744 bool isDue = timestamp < expectedPresent;
1745 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001746}
1747
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001748bool Layer::onPreComposition() {
1749 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001750 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001751}
1752
Dan Stozae77c7662016-05-13 11:37:28 -07001753bool Layer::onPostComposition() {
1754 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001755 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001756 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001757 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1758
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001759 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001760 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001761 mFrameTracker.setFrameReadyFence(frameReadyFence);
1762 } else {
1763 // There was no fence for this frame, so assume that it was ready
1764 // to be presented at the desired present time.
1765 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1766 }
1767
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001768 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001769#ifdef USE_HWC2
1770 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1771#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001772 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001773#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001774 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001775 mFrameTracker.setActualPresentFence(presentFence);
1776 } else {
1777 // The HWC doesn't support present fences, so use the refresh
1778 // timestamp instead.
1779 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1780 mFrameTracker.setActualPresentTime(presentTime);
1781 }
1782
1783 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001784 mFrameLatencyNeeded = false;
1785 }
Dan Stozae77c7662016-05-13 11:37:28 -07001786 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001787}
1788
Dan Stoza9e56aa02015-11-02 13:00:03 -08001789#ifdef USE_HWC2
1790void Layer::releasePendingBuffer() {
1791 mSurfaceFlingerConsumer->releasePendingBuffer();
1792}
1793#endif
1794
Mathias Agopianda27af92012-09-13 18:17:13 -07001795bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001796 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001797#ifdef USE_HWC2
1798 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1799 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1800#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001801 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001802 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001803#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001804}
1805
Mathias Agopian4fec8732012-06-29 14:12:52 -07001806Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001808 ATRACE_CALL();
1809
Jesse Hall399184a2014-03-03 15:42:54 -08001810 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1811 // mSidebandStreamChanged was true
1812 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001813 if (mSidebandStream != NULL) {
1814 setTransactionFlags(eTransactionNeeded);
1815 mFlinger->setTransactionFlags(eTraversalNeeded);
1816 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001817 recomputeVisibleRegions = true;
1818
1819 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001820 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001821 }
1822
Mathias Agopian4fec8732012-06-29 14:12:52 -07001823 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001824 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001825
1826 // if we've already called updateTexImage() without going through
1827 // a composition step, we have to skip this layer at this point
1828 // because we cannot call updateTeximage() without a corresponding
1829 // compositionComplete() call.
1830 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001831 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001832 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001833 }
1834
Dan Stoza1ce65812016-06-15 16:26:27 -07001835 // If the head buffer's acquire fence hasn't signaled yet, return and
1836 // try again later
1837 if (!headFenceHasSignaled()) {
1838 mFlinger->signalLayerUpdate();
1839 return outDirtyRegion;
1840 }
1841
Jamie Gennis351a5132011-09-14 18:23:37 -07001842 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001843 const State& s(getDrawingState());
1844 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001845 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001846
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001847 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001848 Layer::State& front;
1849 Layer::State& current;
1850 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001851 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001852 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001853 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001854 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001855
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001856 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001857 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001858 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001859 int32_t overrideScalingMode,
1860 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001861 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001862 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001863 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001864 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001865 overrideScalingMode(overrideScalingMode),
1866 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001867 }
1868
1869 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001870 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001871 if (buf == NULL) {
1872 return false;
1873 }
1874
1875 uint32_t bufWidth = buf->getWidth();
1876 uint32_t bufHeight = buf->getHeight();
1877
1878 // check that we received a buffer of the right size
1879 // (Take the buffer's orientation into account)
1880 if (item.mTransform & Transform::ROT_90) {
1881 swap(bufWidth, bufHeight);
1882 }
1883
Robert Carrc3574f72016-03-24 12:19:32 -07001884 int actualScalingMode = overrideScalingMode >= 0 ?
1885 overrideScalingMode : item.mScalingMode;
1886 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001887 if (front.active != front.requested) {
1888
1889 if (isFixedSize ||
1890 (bufWidth == front.requested.w &&
1891 bufHeight == front.requested.h))
1892 {
1893 // Here we pretend the transaction happened by updating the
1894 // current and drawing states. Drawing state is only accessed
1895 // in this thread, no need to have it locked
1896 front.active = front.requested;
1897
1898 // We also need to update the current state so that
1899 // we don't end-up overwriting the drawing state with
1900 // this stale current state during the next transaction
1901 //
1902 // NOTE: We don't need to hold the transaction lock here
1903 // because State::active is only accessed from this thread.
1904 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001905 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001906
1907 // recompute visible region
1908 recomputeVisibleRegions = true;
1909 }
1910
1911 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001912 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001913 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001914 " requested={ wh={%4u,%4u} }}\n",
1915 name,
Andy McFadden69052052012-09-14 16:10:11 -07001916 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001917 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001918 front.crop.left,
1919 front.crop.top,
1920 front.crop.right,
1921 front.crop.bottom,
1922 front.crop.getWidth(),
1923 front.crop.getHeight(),
1924 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001925 }
1926
Ruben Brunk1681d952014-06-27 15:51:55 -07001927 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001928 if (front.active.w != bufWidth ||
1929 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001930 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001931 ALOGE("[%s] rejecting buffer: "
1932 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1933 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001934 return true;
1935 }
1936 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001937
1938 // if the transparent region has changed (this test is
1939 // conservative, but that's fine, worst case we're doing
1940 // a bit of extra work), we latch the new one and we
1941 // trigger a visible-region recompute.
1942 if (!front.activeTransparentRegion.isTriviallyEqual(
1943 front.requestedTransparentRegion)) {
1944 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001945
1946 // We also need to update the current state so that
1947 // we don't end-up overwriting the drawing state with
1948 // this stale current state during the next transaction
1949 //
1950 // NOTE: We don't need to hold the transaction lock here
1951 // because State::active is only accessed from this thread.
1952 current.activeTransparentRegion = front.activeTransparentRegion;
1953
1954 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001955 recomputeVisibleRegions = true;
1956 }
1957
Robert Carr99e27f02016-06-16 15:18:02 -07001958 if (front.crop != front.requestedCrop) {
1959 front.crop = front.requestedCrop;
1960 current.crop = front.requestedCrop;
1961 recomputeVisibleRegions = true;
1962 }
Robert Carra3920732016-06-20 21:49:49 -07001963 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001964
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001965 return false;
1966 }
1967 };
1968
Ruben Brunk1681d952014-06-27 15:51:55 -07001969 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001970 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001971 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001972
Dan Stozacac35382016-01-27 12:21:06 -08001973
1974 // Check all of our local sync points to ensure that all transactions
1975 // which need to have been applied prior to the frame which is about to
1976 // be latched have signaled
1977
1978 auto headFrameNumber = getHeadFrameNumber();
1979 bool matchingFramesFound = false;
1980 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001981 {
Dan Stozacac35382016-01-27 12:21:06 -08001982 Mutex::Autolock lock(mLocalSyncPointMutex);
1983 for (auto& point : mLocalSyncPoints) {
1984 if (point->getFrameNumber() > headFrameNumber) {
1985 break;
1986 }
1987
1988 matchingFramesFound = true;
1989
1990 if (!point->frameIsAvailable()) {
1991 // We haven't notified the remote layer that the frame for
1992 // this point is available yet. Notify it now, and then
1993 // abort this attempt to latch.
1994 point->setFrameAvailable();
1995 allTransactionsApplied = false;
1996 break;
1997 }
1998
1999 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07002000 }
2001 }
2002
Dan Stozacac35382016-01-27 12:21:06 -08002003 if (matchingFramesFound && !allTransactionsApplied) {
2004 mFlinger->signalLayerUpdate();
2005 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07002006 }
2007
Pablo Ceballos06312182015-10-07 16:32:12 -07002008 // This boolean is used to make sure that SurfaceFlinger's shadow copy
2009 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002010 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07002011 // buffer mode.
2012 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07002013 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002014 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08002015 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07002016 if (updateResult == BufferQueue::PRESENT_LATER) {
2017 // Producer doesn't want buffer to be displayed yet. Signal a
2018 // layer update so we check again at the next opportunity.
2019 mFlinger->signalLayerUpdate();
2020 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07002021 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2022 // If the buffer has been rejected, remove it from the shadow queue
2023 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07002024 if (queuedBuffer) {
2025 Mutex::Autolock lock(mQueueItemLock);
2026 mQueueItems.removeAt(0);
2027 android_atomic_dec(&mQueuedFrames);
2028 }
Dan Stozaecc50402015-04-28 14:42:06 -07002029 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07002030 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2031 // This can occur if something goes wrong when trying to create the
2032 // EGLImage for this buffer. If this happens, the buffer has already
2033 // been released, so we need to clean up the queue and bug out
2034 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07002035 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07002036 Mutex::Autolock lock(mQueueItemLock);
2037 mQueueItems.clear();
2038 android_atomic_and(0, &mQueuedFrames);
2039 }
2040
2041 // Once we have hit this state, the shadow queue may no longer
2042 // correctly reflect the incoming BufferQueue's contents, so even if
2043 // updateTexImage starts working, the only safe course of action is
2044 // to continue to ignore updates.
2045 mUpdateTexImageFailed = true;
2046
2047 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07002048 }
2049
Pablo Ceballos06312182015-10-07 16:32:12 -07002050 if (queuedBuffer) {
2051 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07002052 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2053
Dan Stoza6b9454d2014-11-07 16:00:59 -08002054 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07002055
2056 // Remove any stale buffers that have been dropped during
2057 // updateTexImage
2058 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2059 mQueueItems.removeAt(0);
2060 android_atomic_dec(&mQueuedFrames);
2061 }
2062
Dan Stoza6b9454d2014-11-07 16:00:59 -08002063 mQueueItems.removeAt(0);
2064 }
2065
Dan Stozaecc50402015-04-28 14:42:06 -07002066
Andy McFadden1585c4d2013-06-28 13:52:40 -07002067 // Decrement the queued-frames count. Signal another event if we
2068 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002069 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002070 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002071 mFlinger->signalLayerUpdate();
2072 }
2073
2074 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002075 // something happened!
2076 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002077 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002078 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002079
Jamie Gennis351a5132011-09-14 18:23:37 -07002080 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002081 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002082 if (mActiveBuffer == NULL) {
2083 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002084 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002085 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002086
Mathias Agopian4824d402012-06-04 18:16:30 -07002087 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002088 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002089 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002090 // the first time we receive a buffer, we need to trigger a
2091 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002092 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002093 }
2094
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002095 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2096 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2097 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002098 if ((crop != mCurrentCrop) ||
2099 (transform != mCurrentTransform) ||
2100 (scalingMode != mCurrentScalingMode))
2101 {
2102 mCurrentCrop = crop;
2103 mCurrentTransform = transform;
2104 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002105 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002106 }
2107
2108 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002109 uint32_t bufWidth = mActiveBuffer->getWidth();
2110 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002111 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2112 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002113 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002114 }
2115 }
2116
2117 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002118 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002119 recomputeVisibleRegions = true;
2120 }
2121
Dan Stozacac35382016-01-27 12:21:06 -08002122 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2123
2124 // Remove any sync points corresponding to the buffer which was just
2125 // latched
2126 {
2127 Mutex::Autolock lock(mLocalSyncPointMutex);
2128 auto point = mLocalSyncPoints.begin();
2129 while (point != mLocalSyncPoints.end()) {
2130 if (!(*point)->frameIsAvailable() ||
2131 !(*point)->transactionIsApplied()) {
2132 // This sync point must have been added since we started
2133 // latching. Don't drop it yet.
2134 ++point;
2135 continue;
2136 }
2137
2138 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2139 point = mLocalSyncPoints.erase(point);
2140 } else {
2141 ++point;
2142 }
2143 }
2144 }
2145
Mathias Agopian4fec8732012-06-29 14:12:52 -07002146 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002147 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002148
2149 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002150 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002151 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002152 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002153}
2154
Mathias Agopiana67932f2011-04-20 14:20:59 -07002155uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002156{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002157 // TODO: should we do something special if mSecure is set?
2158 if (mProtectedByApp) {
2159 // need a hardware-protected path to external video sink
2160 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002161 }
Riley Andrews03414a12014-07-01 14:22:59 -07002162 if (mPotentialCursor) {
2163 usage |= GraphicBuffer::USAGE_CURSOR;
2164 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002165 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002166 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002167}
2168
Mathias Agopian84300952012-11-21 16:02:13 -08002169void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002170 uint32_t orientation = 0;
2171 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002172 // The transform hint is used to improve performance, but we can
2173 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002174 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002175 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002176 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002177 if (orientation & Transform::ROT_INVALID) {
2178 orientation = 0;
2179 }
2180 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002181 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002182}
2183
Mathias Agopian13127d82013-03-05 17:47:11 -08002184// ----------------------------------------------------------------------------
2185// debugging
2186// ----------------------------------------------------------------------------
2187
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002188void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002189{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002190 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002191
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002192 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002193 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002194 "+ %s %p (%s)\n",
2195 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002196 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002197
Mathias Agopian2ca79392013-04-02 18:30:32 -07002198 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002199 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002200 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002201 sp<Client> client(mClientRef.promote());
2202
Mathias Agopian74d211a2013-04-22 16:55:35 +02002203 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002204 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2205 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002207#ifdef USE_HWC2
2208 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2209#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002210 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002211#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002212 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002213 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 -07002214 s.crop.left, s.crop.top,
2215 s.crop.right, s.crop.bottom,
2216 s.finalCrop.left, s.finalCrop.top,
2217 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002218 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002219 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002220 s.active.transform[0][0], s.active.transform[0][1],
2221 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002222 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002223
2224 sp<const GraphicBuffer> buf0(mActiveBuffer);
2225 uint32_t w0=0, h0=0, s0=0, f0=0;
2226 if (buf0 != 0) {
2227 w0 = buf0->getWidth();
2228 h0 = buf0->getHeight();
2229 s0 = buf0->getStride();
2230 f0 = buf0->format;
2231 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002232 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002233 " "
2234 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2235 " queued-frames=%d, mRefreshPending=%d\n",
2236 mFormat, w0, h0, s0,f0,
2237 mQueuedFrames, mRefreshPending);
2238
Mathias Agopian13127d82013-03-05 17:47:11 -08002239 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002240 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002241 }
2242}
2243
Dan Stozae22aec72016-08-01 13:20:59 -07002244#ifdef USE_HWC2
2245void Layer::miniDumpHeader(String8& result) {
2246 result.append("----------------------------------------");
2247 result.append("---------------------------------------\n");
2248 result.append(" Layer name\n");
2249 result.append(" Z | ");
2250 result.append(" Comp Type | ");
2251 result.append(" Disp Frame (LTRB) | ");
2252 result.append(" Source Crop (LTRB)\n");
2253 result.append("----------------------------------------");
2254 result.append("---------------------------------------\n");
2255}
2256
2257void Layer::miniDump(String8& result, int32_t hwcId) const {
2258 if (mHwcLayers.count(hwcId) == 0) {
2259 return;
2260 }
2261
2262 String8 name;
2263 if (mName.length() > 77) {
2264 std::string shortened;
2265 shortened.append(mName.string(), 36);
2266 shortened.append("[...]");
2267 shortened.append(mName.string() + (mName.length() - 36), 36);
2268 name = shortened.c_str();
2269 } else {
2270 name = mName;
2271 }
2272
2273 result.appendFormat(" %s\n", name.string());
2274
2275 const Layer::State& layerState(getDrawingState());
2276 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2277 result.appendFormat(" %10u | ", layerState.z);
2278 result.appendFormat("%10s | ",
2279 to_string(getCompositionType(hwcId)).c_str());
2280 const Rect& frame = hwcInfo.displayFrame;
2281 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2282 frame.right, frame.bottom);
2283 const FloatRect& crop = hwcInfo.sourceCrop;
2284 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2285 crop.right, crop.bottom);
2286
2287 result.append("- - - - - - - - - - - - - - - - - - - - ");
2288 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2289}
2290#endif
2291
Svetoslavd85084b2014-03-20 10:28:31 -07002292void Layer::dumpFrameStats(String8& result) const {
2293 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002294}
2295
Svetoslavd85084b2014-03-20 10:28:31 -07002296void Layer::clearFrameStats() {
2297 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002298}
2299
Jamie Gennis6547ff42013-07-16 20:12:42 -07002300void Layer::logFrameStats() {
2301 mFrameTracker.logAndResetStats(mName);
2302}
2303
Svetoslavd85084b2014-03-20 10:28:31 -07002304void Layer::getFrameStats(FrameStats* outStats) const {
2305 mFrameTracker.getStats(outStats);
2306}
2307
Pablo Ceballos40845df2016-01-25 17:41:15 -08002308void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2309 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2310 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2311 *outName = mName;
2312 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2313
2314#ifdef USE_HWC2
2315 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2316 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2317 HWC2::Composition::Client : true;
2318#else
2319 *outIsGlesComposition = mIsGlesComposition;
2320#endif
2321 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2322 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2323 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2324}
Dan Stozae77c7662016-05-13 11:37:28 -07002325
2326std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2327 bool forceFlush) {
2328 std::vector<OccupancyTracker::Segment> history;
2329 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2330 &history);
2331 if (result != NO_ERROR) {
2332 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2333 result);
2334 return {};
2335 }
2336 return history;
2337}
2338
Robert Carr367c5682016-06-20 11:55:28 -07002339bool Layer::getTransformToDisplayInverse() const {
2340 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2341}
2342
Mathias Agopian13127d82013-03-05 17:47:11 -08002343// ---------------------------------------------------------------------------
2344
2345Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2346 const sp<Layer>& layer)
2347 : mFlinger(flinger), mLayer(layer) {
2348}
2349
2350Layer::LayerCleaner::~LayerCleaner() {
2351 // destroy client resources
2352 mFlinger->onLayerDestroyed(mLayer);
2353}
2354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002355// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002356}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002357
2358#if defined(__gl_h_)
2359#error "don't include gl/gl.h in this file"
2360#endif
2361
2362#if defined(__gl2_h_)
2363#error "don't include gl2/gl2.h in this file"
2364#endif