blob: 9173165d0c55b9bf5d5c27e2c10f649fe341e39d [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#define DEBUG_RESIZE 0
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056namespace android {
57
58// ---------------------------------------------------------------------------
59
Mathias Agopian13127d82013-03-05 17:47:11 -080060int32_t Layer::sSequence = 1;
61
Mathias Agopian4d9b8222013-03-12 17:11:48 -070062Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
63 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080064 : contentDirty(false),
65 sequence(uint32_t(android_atomic_inc(&sSequence))),
66 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070067 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080068 mPremultipliedAlpha(true),
69 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070072 mPendingStateMutex(),
73 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080075 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070077 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070078 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080080 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080081 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080082 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080083 mFiltering(false),
84 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070085 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Pablo Ceballos40845df2016-01-25 17:41:15 -080086#ifndef USE_HWC2
87 mIsGlesComposition(false),
88#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080089 mProtectedByApp(false),
90 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070091 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070092 mPotentialCursor(false),
93 mQueueItemLock(),
94 mQueueItemCondition(),
95 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070096 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080097 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -070098 mAutoRefresh(false),
99 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101#ifdef USE_HWC2
102 ALOGV("Creating Layer %s", name.string());
103#endif
104
Mathias Agopiana67932f2011-04-20 14:20:59 -0700105 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700106 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700107 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 uint32_t layerFlags = 0;
110 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800111 layerFlags |= layer_state_t::eLayerHidden;
112 if (flags & ISurfaceComposerClient::eOpaque)
113 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700114 if (flags & ISurfaceComposerClient::eSecure)
115 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116
117 if (flags & ISurfaceComposerClient::eNonPremultiplied)
118 mPremultipliedAlpha = false;
119
120 mName = name;
121
122 mCurrentState.active.w = w;
123 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800124 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700125 mCurrentState.crop.makeInvalid();
126 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800128#ifdef USE_HWC2
129 mCurrentState.alpha = 1.0f;
130#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133 mCurrentState.layerStack = 0;
134 mCurrentState.flags = layerFlags;
135 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136 mCurrentState.requested = mCurrentState.active;
137
138 // drawing state & current state are identical
139 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700140
Dan Stoza9e56aa02015-11-02 13:00:03 -0800141#ifdef USE_HWC2
142 const auto& hwc = flinger->getHwComposer();
143 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
144 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
145#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146 nsecs_t displayPeriod =
147 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800148#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700149 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800150}
151
Mathias Agopian3f844832013-08-07 21:24:32 -0700152void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800153 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700154 sp<IGraphicBufferProducer> producer;
155 sp<IGraphicBufferConsumer> consumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700156 BufferQueue::createBufferQueue(&producer, &consumer);
157 mProducer = new MonitoredProducer(producer, mFlinger);
158 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800159 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800160 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700161 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800162
Dan Stozac9d97202016-03-03 08:20:27 -0800163#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700164 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800165#endif
Andy McFadden69052052012-09-14 16:10:11 -0700166
Mathias Agopian84300952012-11-21 16:02:13 -0800167 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
168 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169}
170
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800172 sp<Client> c(mClientRef.promote());
173 if (c != 0) {
174 c->detachLayer(this);
175 }
176
Dan Stozacac35382016-01-27 12:21:06 -0800177 for (auto& point : mRemoteSyncPoints) {
178 point->setTransactionApplied();
179 }
Dan Stozac8145172016-04-28 16:29:06 -0700180 for (auto& point : mLocalSyncPoints) {
181 point->setFrameAvailable();
182 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700183 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700184 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700185}
186
Mathias Agopian13127d82013-03-05 17:47:11 -0800187// ---------------------------------------------------------------------------
188// callbacks
189// ---------------------------------------------------------------------------
190
Dan Stoza9e56aa02015-11-02 13:00:03 -0800191#ifdef USE_HWC2
192void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
193 if (mHwcLayers.empty()) {
194 return;
195 }
196 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
197}
198#else
Dan Stozac7014012014-02-14 15:03:43 -0800199void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 HWComposer::HWCLayerInterface* layer) {
201 if (layer) {
202 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700203 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 }
205}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800207
Dan Stoza6b9454d2014-11-07 16:00:59 -0800208void Layer::onFrameAvailable(const BufferItem& item) {
209 // Add this buffer from our internal queue tracker
210 { // Autolock scope
211 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700212
213 // Reset the frame number tracker when we receive the first buffer after
214 // a frame number reset
215 if (item.mFrameNumber == 1) {
216 mLastFrameNumberReceived = 0;
217 }
218
219 // Ensure that callbacks are handled in order
220 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
221 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
222 ms2ns(500));
223 if (result != NO_ERROR) {
224 ALOGE("[%s] Timed out waiting on callback", mName.string());
225 }
226 }
227
Dan Stoza6b9454d2014-11-07 16:00:59 -0800228 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700229 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700230
231 // Wake up any pending callbacks
232 mLastFrameNumberReceived = item.mFrameNumber;
233 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800234 }
235
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800236 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700237}
238
Dan Stoza6b9454d2014-11-07 16:00:59 -0800239void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700240 { // Autolock scope
241 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700242
Dan Stoza7dde5992015-05-22 09:51:44 -0700243 // Ensure that callbacks are handled in order
244 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
245 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
246 ms2ns(500));
247 if (result != NO_ERROR) {
248 ALOGE("[%s] Timed out waiting on callback", mName.string());
249 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700250 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700251
252 if (mQueueItems.empty()) {
253 ALOGE("Can't replace a frame on an empty queue");
254 return;
255 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700256 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700257
258 // Wake up any pending callbacks
259 mLastFrameNumberReceived = item.mFrameNumber;
260 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700261 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800262}
263
Jesse Hall399184a2014-03-03 15:42:54 -0800264void Layer::onSidebandStreamChanged() {
265 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
266 // mSidebandStreamChanged was false
267 mFlinger->signalLayerUpdate();
268 }
269}
270
Mathias Agopian67106042013-03-14 19:18:13 -0700271// called with SurfaceFlinger::mStateLock from the drawing thread after
272// the layer has been remove from the current state list (and just before
273// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800274void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800275 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700276}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277
Mathias Agopian13127d82013-03-05 17:47:11 -0800278// ---------------------------------------------------------------------------
279// set-up
280// ---------------------------------------------------------------------------
281
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700282const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800283 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284}
285
Mathias Agopianf9d93272009-06-19 17:00:27 -0700286status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 PixelFormat format, uint32_t flags)
288{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700289 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700291
292 // never allow a surface larger than what our underlying GL implementation
293 // can handle.
294 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800295 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700296 return BAD_VALUE;
297 }
298
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700299 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700300
Riley Andrews03414a12014-07-01 14:22:59 -0700301 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700302 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700303 mCurrentOpacity = getOpacityForFormat(format);
304
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800305 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
306 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
307 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 return NO_ERROR;
310}
311
Dan Stoza7dde5992015-05-22 09:51:44 -0700312/*
313 * The layer handle is just a BBinder object passed to the client
314 * (remote process) -- we don't keep any reference on our side such that
315 * the dtor is called when the remote side let go of its reference.
316 *
317 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
318 * this layer when the handle is destroyed.
319 */
320class Layer::Handle : public BBinder, public LayerCleaner {
321 public:
322 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
323 : LayerCleaner(flinger, layer), owner(layer) {}
324
325 wp<Layer> owner;
326};
327
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700328sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800329 Mutex::Autolock _l(mLock);
330
331 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700332 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800333
334 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700335
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700336 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800337}
338
Dan Stozab9b08832014-03-13 11:55:57 -0700339sp<IGraphicBufferProducer> Layer::getProducer() const {
340 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700341}
342
Mathias Agopian13127d82013-03-05 17:47:11 -0800343// ---------------------------------------------------------------------------
344// h/w composer set-up
345// ---------------------------------------------------------------------------
346
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800347Rect Layer::getContentCrop() const {
348 // this is the crop rectangle that applies to the buffer
349 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700350 Rect crop;
351 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800352 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700353 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800354 } else if (mActiveBuffer != NULL) {
355 // otherwise we use the whole buffer
356 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700357 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800358 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700359 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700360 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700361 return crop;
362}
363
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700364static Rect reduce(const Rect& win, const Region& exclude) {
365 if (CC_LIKELY(exclude.isEmpty())) {
366 return win;
367 }
368 if (exclude.isRect()) {
369 return win.reduce(exclude.getBounds());
370 }
371 return Region(win).subtract(exclude).getBounds();
372}
373
Mathias Agopian13127d82013-03-05 17:47:11 -0800374Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700375 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700376 return computeBounds(s.activeTransparentRegion);
377}
378
379Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
380 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800381 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700382
383 if (!s.crop.isEmpty()) {
384 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800385 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700386 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700387 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800388}
389
Mathias Agopian6b442672013-07-09 21:24:52 -0700390FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800391 // the content crop is the area of the content that gets scaled to the
392 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700393 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800394
Robert Carrb5d3d262016-03-25 15:08:13 -0700395 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800396 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700397 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800398
399 // apply the projection's clipping to the window crop in
400 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700401 // if there are no window scaling involved, this operation will map to full
402 // pixels in the buffer.
403 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
404 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700405
406 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700407 if (!s.crop.isEmpty()) {
408 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700409 }
410
Robert Carr3dcabfa2016-03-01 18:36:58 -0800411 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
413 activeCrop.clear();
414 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700415 if (!s.finalCrop.isEmpty()) {
416 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000417 activeCrop.clear();
418 }
419 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800420 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800421
Michael Lentine28ea2172014-11-19 18:32:37 -0800422 // This needs to be here as transform.transform(Rect) computes the
423 // transformed rect and then takes the bounding box of the result before
424 // returning. This means
425 // transform.inverse().transform(transform.transform(Rect)) != Rect
426 // in which case we need to make sure the final rect is clipped to the
427 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
429 activeCrop.clear();
430 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800431
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700432 // subtract the transparent region and snap to the bounds
433 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
434
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 // Transform the window crop to match the buffer coordinate system,
436 // which means using the inverse of the current transform set on the
437 // SurfaceFlingerConsumer.
438 uint32_t invTransform = mCurrentTransform;
439 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
440 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700441 * the code below applies the primary display's inverse transform to the
442 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700444 uint32_t invTransformOrient =
445 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 // calculate the inverse transform
447 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
448 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
449 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800450 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700452 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
453 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455
456 int winWidth = s.active.w;
457 int winHeight = s.active.h;
458 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
459 // If the activeCrop has been rotate the ends are rotated but not
460 // the space itself so when transforming ends back we can't rely on
461 // a modification of the axes of rotation. To account for this we
462 // need to reorient the inverse rotation in terms of the current
463 // axes of rotation.
464 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
465 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
466 if (is_h_flipped == is_v_flipped) {
467 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
468 NATIVE_WINDOW_TRANSFORM_FLIP_H;
469 }
470 winWidth = s.active.h;
471 winHeight = s.active.w;
472 }
473 const Rect winCrop = activeCrop.transform(
474 invTransform, s.active.w, s.active.h);
475
476 // below, crop is intersected with winCrop expressed in crop's coordinate space
477 float xScale = crop.getWidth() / float(winWidth);
478 float yScale = crop.getHeight() / float(winHeight);
479
480 float insetL = winCrop.left * xScale;
481 float insetT = winCrop.top * yScale;
482 float insetR = (winWidth - winCrop.right ) * xScale;
483 float insetB = (winHeight - winCrop.bottom) * yScale;
484
485 crop.left += insetL;
486 crop.top += insetT;
487 crop.right -= insetR;
488 crop.bottom -= insetB;
489
Mathias Agopian13127d82013-03-05 17:47:11 -0800490 return crop;
491}
492
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493#ifdef USE_HWC2
494void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
495#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700496void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700497 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700498 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700500{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501#ifdef USE_HWC2
502 const auto hwcId = displayDevice->getHwcDisplayId();
503 auto& hwcInfo = mHwcLayers[hwcId];
504#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800505 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700507
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700508 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509#ifdef USE_HWC2
510 hwcInfo.forceClientComposition = false;
511
512 if (isSecure() && !displayDevice->isSecure()) {
513 hwcInfo.forceClientComposition = true;
514 }
515
516 auto& hwcLayer = hwcInfo.layer;
517#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700518 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700519
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700520 if (isSecure() && !hw->isSecure()) {
521 layer.setSkip(true);
522 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700524
Mathias Agopian13127d82013-03-05 17:47:11 -0800525 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700526 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800527#ifdef USE_HWC2
528 if (!isOpaque(s) || s.alpha != 1.0f) {
529 auto blendMode = mPremultipliedAlpha ?
530 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
531 auto error = hwcLayer->setBlendMode(blendMode);
532 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
533 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
534 to_string(error).c_str(), static_cast<int32_t>(error));
535 }
536#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800537 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800538 layer.setBlending(mPremultipliedAlpha ?
539 HWC_BLENDING_PREMULT :
540 HWC_BLENDING_COVERAGE);
541 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800543
544 // apply the layer's transform, followed by the display's global transform
545 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700546 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700547 if (!s.crop.isEmpty()) {
548 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800549 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000551 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 activeCrop.clear();
556 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800557 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800558 // This needs to be here as transform.transform(Rect) computes the
559 // transformed rect and then takes the bounding box of the result before
560 // returning. This means
561 // transform.inverse().transform(transform.transform(Rect)) != Rect
562 // in which case we need to make sure the final rect is clipped to the
563 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000564 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
565 activeCrop.clear();
566 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700567 // mark regions outside the crop as transparent
568 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
569 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
570 s.active.w, s.active.h));
571 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
572 activeCrop.left, activeCrop.bottom));
573 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
574 s.active.w, activeCrop.bottom));
575 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800576 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700577 if (!s.finalCrop.isEmpty()) {
578 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000579 frame.clear();
580 }
581 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000583 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
584 frame.clear();
585 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 const Transform& tr(displayDevice->getTransform());
587 Rect transformedFrame = tr.transform(frame);
588 auto error = hwcLayer->setDisplayFrame(transformedFrame);
589 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
590 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
591 transformedFrame.top, transformedFrame.right,
592 transformedFrame.bottom, to_string(error).c_str(),
593 static_cast<int32_t>(error));
594
595 FloatRect sourceCrop = computeCrop(displayDevice);
596 error = hwcLayer->setSourceCrop(sourceCrop);
597 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
598 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
599 sourceCrop.left, sourceCrop.top, sourceCrop.right,
600 sourceCrop.bottom, to_string(error).c_str(),
601 static_cast<int32_t>(error));
602
603 error = hwcLayer->setPlaneAlpha(s.alpha);
604 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
605 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
606 static_cast<int32_t>(error));
607
608 error = hwcLayer->setZOrder(s.z);
609 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
610 mName.string(), s.z, to_string(error).c_str(),
611 static_cast<int32_t>(error));
612#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000613 if (!frame.intersect(hw->getViewport(), &frame)) {
614 frame.clear();
615 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800616 const Transform& tr(hw->getTransform());
617 layer.setFrame(tr.transform(frame));
618 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800619 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800621
Mathias Agopian29a367b2011-07-12 14:51:45 -0700622 /*
623 * Transformations are applied in this order:
624 * 1) buffer orientation/flip/mirror
625 * 2) state transformation (window manager)
626 * 3) layer orientation (screen orientation)
627 * (NOTE: the matrices are multiplied in reverse order)
628 */
629
630 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800631 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632
633 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
634 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700635 * the code below applies the primary display's inverse transform to the
636 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700637 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700638 uint32_t invTransform =
639 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640 // calculate the inverse transform
641 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
642 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
643 NATIVE_WINDOW_TRANSFORM_FLIP_H;
644 }
645 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700646 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700647 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700648
649 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800650 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651#ifdef USE_HWC2
652 if (orientation & Transform::ROT_INVALID) {
653 // we can only handle simple transformation
654 hwcInfo.forceClientComposition = true;
655 } else {
656 auto transform = static_cast<HWC2::Transform>(orientation);
657 auto error = hwcLayer->setTransform(transform);
658 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
659 "%s (%d)", mName.string(), to_string(transform).c_str(),
660 to_string(error).c_str(), static_cast<int32_t>(error));
661 }
662#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800663 if (orientation & Transform::ROT_INVALID) {
664 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700665 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700666 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800667 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700668 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700670}
671
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672#ifdef USE_HWC2
673void Layer::forceClientComposition(int32_t hwcId) {
674 if (mHwcLayers.count(hwcId) == 0) {
675 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
676 return;
677 }
678
679 mHwcLayers[hwcId].forceClientComposition = true;
680}
681#endif
682
683#ifdef USE_HWC2
684void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
685 // Apply this display's projection's viewport to the visible region
686 // before giving it to the HWC HAL.
687 const Transform& tr = displayDevice->getTransform();
688 const auto& viewport = displayDevice->getViewport();
689 Region visible = tr.transform(visibleRegion.intersect(viewport));
690 auto hwcId = displayDevice->getHwcDisplayId();
691 auto& hwcLayer = mHwcLayers[hwcId].layer;
692 auto error = hwcLayer->setVisibleRegion(visible);
693 if (error != HWC2::Error::None) {
694 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
695 to_string(error).c_str(), static_cast<int32_t>(error));
696 visible.dump(LOG_TAG);
697 }
698
699 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
700 if (error != HWC2::Error::None) {
701 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
702 to_string(error).c_str(), static_cast<int32_t>(error));
703 surfaceDamageRegion.dump(LOG_TAG);
704 }
705
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700706 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700708 setCompositionType(hwcId, HWC2::Composition::Sideband);
709 ALOGV("[%s] Requesting Sideband composition", mName.string());
710 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 if (error != HWC2::Error::None) {
712 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
713 mName.string(), mSidebandStream->handle(),
714 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700716 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800717 }
718
Dan Stoza0183f7a2016-07-20 12:52:38 -0700719 // Client layers
720 if (mHwcLayers[hwcId].forceClientComposition ||
721 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700722 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700724 return;
725 }
726
Dan Stoza0183f7a2016-07-20 12:52:38 -0700727 // SolidColor layers
728 if (mActiveBuffer == nullptr) {
729 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozaf1098ab2016-07-27 10:16:52 -0700730
731 // For now, we only support black for DimLayer
Dan Stoza0183f7a2016-07-20 12:52:38 -0700732 error = hwcLayer->setColor({0, 0, 0, 255});
733 if (error != HWC2::Error::None) {
734 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
735 to_string(error).c_str(), static_cast<int32_t>(error));
736 }
Dan Stozaf1098ab2016-07-27 10:16:52 -0700737
738 // Clear out the transform, because it doesn't make sense absent a
739 // source buffer
740 error = hwcLayer->setTransform(HWC2::Transform::None);
741 if (error != HWC2::Error::None) {
742 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
743 to_string(error).c_str(), static_cast<int32_t>(error));
744 }
745
Dan Stoza0183f7a2016-07-20 12:52:38 -0700746 return;
747 }
748
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700749 // Device or Cursor layers
750 if (mPotentialCursor) {
751 ALOGV("[%s] Requesting Cursor composition", mName.string());
752 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 } else {
754 ALOGV("[%s] Requesting Device composition", mName.string());
755 setCompositionType(hwcId, HWC2::Composition::Device);
756 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700757
758 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
759 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
760 if (error != HWC2::Error::None) {
761 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
762 mActiveBuffer->handle, to_string(error).c_str(),
763 static_cast<int32_t>(error));
764 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765}
766#else
Mathias Agopian42977342012-08-05 00:40:46 -0700767void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700768 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800769 // we have to set the visible region on every frame because
770 // we currently free it during onLayerDisplayed(), which is called
771 // after HWComposer::commit() -- every frame.
772 // Apply this display's projection's viewport to the visible region
773 // before giving it to the HWC HAL.
774 const Transform& tr = hw->getTransform();
775 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
776 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700777 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800778 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700779
Jesse Hall399184a2014-03-03 15:42:54 -0800780 if (mSidebandStream.get()) {
781 layer.setSidebandStream(mSidebandStream);
782 } else {
783 // NOTE: buffer can be NULL if the client never drew into this
784 // layer yet, or if we ran out of memory
785 layer.setBuffer(mActiveBuffer);
786 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700787}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800788#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700789
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790#ifdef USE_HWC2
791void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
792 auto hwcId = displayDevice->getHwcDisplayId();
793 if (mHwcLayers.count(hwcId) == 0 ||
794 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
795 return;
796 }
797
798 // This gives us only the "orientation" component of the transform
799 const State& s(getCurrentState());
800
801 // Apply the layer's transform, followed by the display's global transform
802 // Here we're guaranteed that the layer's transform preserves rects
803 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700804 if (!s.crop.isEmpty()) {
805 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806 }
807 // Subtract the transparent region and snap to the bounds
808 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800809 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800810 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700811 if (!s.finalCrop.isEmpty()) {
812 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000813 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800814 auto& displayTransform(displayDevice->getTransform());
815 auto position = displayTransform.transform(frame);
816
817 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
818 position.top);
819 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
820 "to (%d, %d): %s (%d)", mName.string(), position.left,
821 position.top, to_string(error).c_str(),
822 static_cast<int32_t>(error));
823}
824#else
Dan Stozac7014012014-02-14 15:03:43 -0800825void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700826 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700827 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700828
829 // TODO: there is a possible optimization here: we only need to set the
830 // acquire fence the first time a new buffer is acquired on EACH display.
831
Riley Andrews03414a12014-07-01 14:22:59 -0700832 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800833 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800834 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700835 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700836 if (fenceFd == -1) {
837 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
838 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700839 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700840 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700841 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700842}
843
Riley Andrews03414a12014-07-01 14:22:59 -0700844Rect Layer::getPosition(
845 const sp<const DisplayDevice>& hw)
846{
847 // this gives us only the "orientation" component of the transform
848 const State& s(getCurrentState());
849
850 // apply the layer's transform, followed by the display's global transform
851 // here we're guaranteed that the layer's transform preserves rects
852 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700853 if (!s.crop.isEmpty()) {
854 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700855 }
856 // subtract the transparent region and snap to the bounds
857 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800858 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700859 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700860 if (!s.finalCrop.isEmpty()) {
861 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000862 }
Riley Andrews03414a12014-07-01 14:22:59 -0700863 const Transform& tr(hw->getTransform());
864 return Rect(tr.transform(frame));
865}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800866#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700867
Mathias Agopian13127d82013-03-05 17:47:11 -0800868// ---------------------------------------------------------------------------
869// drawing...
870// ---------------------------------------------------------------------------
871
872void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800873 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800874}
875
Dan Stozac7014012014-02-14 15:03:43 -0800876void Layer::draw(const sp<const DisplayDevice>& hw,
877 bool useIdentityTransform) const {
878 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800879}
880
Dan Stozac7014012014-02-14 15:03:43 -0800881void Layer::draw(const sp<const DisplayDevice>& hw) const {
882 onDraw(hw, Region(hw->bounds()), false);
883}
884
885void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
886 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800888 ATRACE_CALL();
889
Mathias Agopiana67932f2011-04-20 14:20:59 -0700890 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700892 // in fact never been drawn into. This happens frequently with
893 // SurfaceView because the WindowManager can't know when the client
894 // has drawn the first time.
895
896 // If there is nothing under us, we paint the screen in black, otherwise
897 // we just skip this update.
898
899 // figure out if there is something below us
900 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700901 const SurfaceFlinger::LayerVector& drawingLayers(
902 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700903 const size_t count = drawingLayers.size();
904 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800905 const sp<Layer>& layer(drawingLayers[i]);
906 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700907 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700908 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700909 }
910 // if not everything below us is covered, we plug the holes!
911 Region holes(clip.subtract(under));
912 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700913 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700914 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 return;
916 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700917
Andy McFadden97eba892012-12-11 15:21:45 -0800918 // Bind the current buffer to the GL texture, and wait for it to be
919 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800920 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
921 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800922 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700923 // Go ahead and draw the buffer anyway; no matter what we do the screen
924 // is probably going to have something visibly wrong.
925 }
926
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700927 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
928
Mathias Agopian875d8e12013-06-07 15:35:48 -0700929 RenderEngine& engine(mFlinger->getRenderEngine());
930
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700931 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700932 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700933 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700934
935 // Query the texture matrix given our current filtering mode.
936 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800937 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
938 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700939
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700940 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
941
942 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700943 * the code below applies the primary display's inverse transform to
944 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700945 */
946
947 // create a 4x4 transform matrix from the display transform flags
948 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
949 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
950 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
951
952 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700953 uint32_t transform =
954 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700955 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
956 tr = tr * rot90;
957 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
958 tr = tr * flipH;
959 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
960 tr = tr * flipV;
961
962 // calculate the inverse
963 tr = inverse(tr);
964
965 // and finally apply it to the original texture matrix
966 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
967 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
968 }
969
Jamie Genniscbb1a952012-05-08 17:05:52 -0700970 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700971 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
972 mTexture.setFiltering(useFiltering);
973 mTexture.setMatrix(textureMatrix);
974
975 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700976 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700977 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700978 }
Dan Stozac7014012014-02-14 15:03:43 -0800979 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700980 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983
Dan Stozac7014012014-02-14 15:03:43 -0800984void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
985 const Region& /* clip */, float red, float green, float blue,
986 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800987{
Mathias Agopian19733a32013-08-28 18:13:56 -0700988 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800989 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700990 engine.setupFillWithColor(red, green, blue, alpha);
991 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800992}
993
994void Layer::clearWithOpenGL(
995 const sp<const DisplayDevice>& hw, const Region& clip) const {
996 clearWithOpenGL(hw, clip, 0,0,0,0);
997}
998
Dan Stozac7014012014-02-14 15:03:43 -0800999void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
1000 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001001 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001002
Dan Stozac7014012014-02-14 15:03:43 -08001003 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001004
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 /*
1006 * NOTE: the way we compute the texture coordinates here produces
1007 * different results than when we take the HWC path -- in the later case
1008 * the "source crop" is rounded to texel boundaries.
1009 * This can produce significantly different results when the texture
1010 * is scaled by a large amount.
1011 *
1012 * The GL code below is more logical (imho), and the difference with
1013 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001014 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001015 * GL composition when a buffer scaling is applied (maybe with some
1016 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1017 * like more of a hack.
1018 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001019 Rect win(computeBounds());
1020
Robert Carrb5d3d262016-03-25 15:08:13 -07001021 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001022 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001023 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001024 win.clear();
1025 }
1026 win = s.active.transform.inverse().transform(win);
1027 if (!win.intersect(computeBounds(), &win)) {
1028 win.clear();
1029 }
1030 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001031
Mathias Agopian3f844832013-08-07 21:24:32 -07001032 float left = float(win.left) / float(s.active.w);
1033 float top = float(win.top) / float(s.active.h);
1034 float right = float(win.right) / float(s.active.w);
1035 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001036
Mathias Agopian875d8e12013-06-07 15:35:48 -07001037 // TODO: we probably want to generate the texture coords with the mesh
1038 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001039 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1040 texCoords[0] = vec2(left, 1.0f - top);
1041 texCoords[1] = vec2(left, 1.0f - bottom);
1042 texCoords[2] = vec2(right, 1.0f - bottom);
1043 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001044
Mathias Agopian875d8e12013-06-07 15:35:48 -07001045 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001046 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001047 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001048 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001049}
1050
Dan Stoza9e56aa02015-11-02 13:00:03 -08001051#ifdef USE_HWC2
1052void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1053 bool callIntoHwc) {
1054 if (mHwcLayers.count(hwcId) == 0) {
1055 ALOGE("setCompositionType called without a valid HWC layer");
1056 return;
1057 }
1058 auto& hwcInfo = mHwcLayers[hwcId];
1059 auto& hwcLayer = hwcInfo.layer;
1060 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1061 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1062 if (hwcInfo.compositionType != type) {
1063 ALOGV(" actually setting");
1064 hwcInfo.compositionType = type;
1065 if (callIntoHwc) {
1066 auto error = hwcLayer->setCompositionType(type);
1067 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1068 "composition type %s: %s (%d)", mName.string(),
1069 to_string(type).c_str(), to_string(error).c_str(),
1070 static_cast<int32_t>(error));
1071 }
1072 }
1073}
1074
1075HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stoza179533d2016-07-21 11:09:40 -07001076 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1077 // If we're querying the composition type for a display that does not
1078 // have a HWC counterpart, then it will always be Client
1079 return HWC2::Composition::Client;
1080 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001081 if (mHwcLayers.count(hwcId) == 0) {
Dan Stoza179533d2016-07-21 11:09:40 -07001082 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001083 return HWC2::Composition::Invalid;
1084 }
1085 return mHwcLayers.at(hwcId).compositionType;
1086}
1087
1088void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1089 if (mHwcLayers.count(hwcId) == 0) {
1090 ALOGE("setClearClientTarget called without a valid HWC layer");
1091 return;
1092 }
1093 mHwcLayers[hwcId].clearClientTarget = clear;
1094}
1095
1096bool Layer::getClearClientTarget(int32_t hwcId) const {
1097 if (mHwcLayers.count(hwcId) == 0) {
1098 ALOGE("getClearClientTarget called without a valid HWC layer");
1099 return false;
1100 }
1101 return mHwcLayers.at(hwcId).clearClientTarget;
1102}
1103#endif
1104
Ruben Brunk1681d952014-06-27 15:51:55 -07001105uint32_t Layer::getProducerStickyTransform() const {
1106 int producerStickyTransform = 0;
1107 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1108 if (ret != OK) {
1109 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1110 strerror(-ret), ret);
1111 return 0;
1112 }
1113 return static_cast<uint32_t>(producerStickyTransform);
1114}
1115
Dan Stozacac35382016-01-27 12:21:06 -08001116uint64_t Layer::getHeadFrameNumber() const {
1117 Mutex::Autolock lock(mQueueItemLock);
1118 if (!mQueueItems.empty()) {
1119 return mQueueItems[0].mFrameNumber;
1120 } else {
1121 return mCurrentFrameNumber;
1122 }
1123}
1124
1125bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1126 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1127 // Don't bother with a SyncPoint, since we've already latched the
1128 // relevant frame
1129 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001130 }
1131
Dan Stozacac35382016-01-27 12:21:06 -08001132 Mutex::Autolock lock(mLocalSyncPointMutex);
1133 mLocalSyncPoints.push_back(point);
1134 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001135}
1136
Mathias Agopian13127d82013-03-05 17:47:11 -08001137void Layer::setFiltering(bool filtering) {
1138 mFiltering = filtering;
1139}
1140
1141bool Layer::getFiltering() const {
1142 return mFiltering;
1143}
1144
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001145// As documented in libhardware header, formats in the range
1146// 0x100 - 0x1FF are specific to the HAL implementation, and
1147// are known to have no alpha channel
1148// TODO: move definition for device-specific range into
1149// hardware.h, instead of using hard-coded values here.
1150#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1151
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001152bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001153 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1154 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001155 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001156 switch (format) {
1157 case HAL_PIXEL_FORMAT_RGBA_8888:
1158 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001159 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001160 }
1161 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001162 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001163}
1164
Mathias Agopian13127d82013-03-05 17:47:11 -08001165// ----------------------------------------------------------------------------
1166// local state
1167// ----------------------------------------------------------------------------
1168
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001169static void boundPoint(vec2* point, const Rect& crop) {
1170 if (point->x < crop.left) {
1171 point->x = crop.left;
1172 }
1173 if (point->x > crop.right) {
1174 point->x = crop.right;
1175 }
1176 if (point->y < crop.top) {
1177 point->y = crop.top;
1178 }
1179 if (point->y > crop.bottom) {
1180 point->y = crop.bottom;
1181 }
1182}
1183
Dan Stozac7014012014-02-14 15:03:43 -08001184void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1185 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001186{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001187 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001188 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001189 const uint32_t hw_h = hw->getHeight();
1190 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001191 if (!s.crop.isEmpty()) {
1192 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001194 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001195 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001196
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001197 vec2 lt = vec2(win.left, win.top);
1198 vec2 lb = vec2(win.left, win.bottom);
1199 vec2 rb = vec2(win.right, win.bottom);
1200 vec2 rt = vec2(win.right, win.top);
1201
1202 if (!useIdentityTransform) {
1203 lt = s.active.transform.transform(lt);
1204 lb = s.active.transform.transform(lb);
1205 rb = s.active.transform.transform(rb);
1206 rt = s.active.transform.transform(rt);
1207 }
1208
Robert Carrb5d3d262016-03-25 15:08:13 -07001209 if (!s.finalCrop.isEmpty()) {
1210 boundPoint(&lt, s.finalCrop);
1211 boundPoint(&lb, s.finalCrop);
1212 boundPoint(&rb, s.finalCrop);
1213 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001214 }
1215
Mathias Agopianff2ed702013-09-01 21:36:12 -07001216 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001217 position[0] = tr.transform(lt);
1218 position[1] = tr.transform(lb);
1219 position[2] = tr.transform(rb);
1220 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001221 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001222 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 }
1224}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001225
Andy McFadden4125a4f2014-01-29 17:17:11 -08001226bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001227{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 // if we don't have a buffer yet, we're translucent regardless of the
1229 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001230 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001231 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001232 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001233
1234 // if the layer has the opaque flag, then we're always opaque,
1235 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001236 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001237}
1238
Dan Stoza23116082015-06-18 14:58:39 -07001239bool Layer::isSecure() const
1240{
1241 const Layer::State& s(mDrawingState);
1242 return (s.flags & layer_state_t::eLayerSecure);
1243}
1244
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001245bool Layer::isProtected() const
1246{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001247 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001248 return (activeBuffer != 0) &&
1249 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1250}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001251
Mathias Agopian13127d82013-03-05 17:47:11 -08001252bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001253 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254}
1255
1256bool Layer::isCropped() const {
1257 return !mCurrentCrop.isEmpty();
1258}
1259
1260bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1261 return mNeedsFiltering || hw->needsFiltering();
1262}
1263
1264void Layer::setVisibleRegion(const Region& visibleRegion) {
1265 // always called from main thread
1266 this->visibleRegion = visibleRegion;
1267}
1268
1269void Layer::setCoveredRegion(const Region& coveredRegion) {
1270 // always called from main thread
1271 this->coveredRegion = coveredRegion;
1272}
1273
1274void Layer::setVisibleNonTransparentRegion(const Region&
1275 setVisibleNonTransparentRegion) {
1276 // always called from main thread
1277 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1278}
1279
1280// ----------------------------------------------------------------------------
1281// transaction
1282// ----------------------------------------------------------------------------
1283
Dan Stoza7dde5992015-05-22 09:51:44 -07001284void Layer::pushPendingState() {
1285 if (!mCurrentState.modified) {
1286 return;
1287 }
1288
Dan Stoza7dde5992015-05-22 09:51:44 -07001289 // If this transaction is waiting on the receipt of a frame, generate a sync
1290 // point and send it to the remote layer.
1291 if (mCurrentState.handle != nullptr) {
Dan Stozade84eb62016-08-09 13:21:03 -07001292 sp<IBinder> strongBinder = mCurrentState.handle.promote();
1293 sp<Handle> handle = nullptr;
1294 sp<Layer> handleLayer = nullptr;
1295 if (strongBinder != nullptr) {
1296 handle = static_cast<Handle*>(strongBinder.get());
1297 handleLayer = handle->owner.promote();
1298 }
1299 if (strongBinder == nullptr || handleLayer == nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001300 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1301 // If we can't promote the layer we are intended to wait on,
1302 // then it is expired or otherwise invalid. Allow this transaction
1303 // to be applied as per normal (no synchronization).
1304 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001305 } else {
1306 auto syncPoint = std::make_shared<SyncPoint>(
1307 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001308 if (handleLayer->addSyncPoint(syncPoint)) {
1309 mRemoteSyncPoints.push_back(std::move(syncPoint));
1310 } else {
1311 // We already missed the frame we're supposed to synchronize
1312 // on, so go ahead and apply the state update
1313 mCurrentState.handle = nullptr;
1314 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001315 }
1316
Dan Stoza7dde5992015-05-22 09:51:44 -07001317 // Wake us up to check if the frame has been received
1318 setTransactionFlags(eTransactionNeeded);
1319 }
1320 mPendingStates.push_back(mCurrentState);
1321}
1322
Pablo Ceballos05289c22016-04-14 15:49:55 -07001323void Layer::popPendingState(State* stateToCommit) {
1324 auto oldFlags = stateToCommit->flags;
1325 *stateToCommit = mPendingStates[0];
1326 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1327 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001328
1329 mPendingStates.removeAt(0);
1330}
1331
Pablo Ceballos05289c22016-04-14 15:49:55 -07001332bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001333 bool stateUpdateAvailable = false;
1334 while (!mPendingStates.empty()) {
1335 if (mPendingStates[0].handle != nullptr) {
1336 if (mRemoteSyncPoints.empty()) {
1337 // If we don't have a sync point for this, apply it anyway. It
1338 // will be visually wrong, but it should keep us from getting
1339 // into too much trouble.
1340 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001341 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001342 stateUpdateAvailable = true;
1343 continue;
1344 }
1345
Dan Stozacac35382016-01-27 12:21:06 -08001346 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1347 mPendingStates[0].frameNumber) {
1348 ALOGE("[%s] Unexpected sync point frame number found",
1349 mName.string());
1350
1351 // Signal our end of the sync point and then dispose of it
1352 mRemoteSyncPoints.front()->setTransactionApplied();
1353 mRemoteSyncPoints.pop_front();
1354 continue;
1355 }
1356
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1358 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001359 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 stateUpdateAvailable = true;
1361
1362 // Signal our end of the sync point and then dispose of it
1363 mRemoteSyncPoints.front()->setTransactionApplied();
1364 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001365 } else {
1366 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001367 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001369 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001370 stateUpdateAvailable = true;
1371 }
1372 }
1373
1374 // If we still have pending updates, wake SurfaceFlinger back up and point
1375 // it at this layer so we can process them
1376 if (!mPendingStates.empty()) {
1377 setTransactionFlags(eTransactionNeeded);
1378 mFlinger->setTransactionFlags(eTraversalNeeded);
1379 }
1380
1381 mCurrentState.modified = false;
1382 return stateUpdateAvailable;
1383}
1384
1385void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001386 auto headFrameNumber = getHeadFrameNumber();
1387 Mutex::Autolock lock(mLocalSyncPointMutex);
1388 for (auto& point : mLocalSyncPoints) {
1389 if (headFrameNumber >= point->getFrameNumber()) {
1390 point->setFrameAvailable();
1391 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001392 }
1393}
1394
Mathias Agopian13127d82013-03-05 17:47:11 -08001395uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001396 ATRACE_CALL();
1397
Dan Stoza7dde5992015-05-22 09:51:44 -07001398 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001399 Layer::State c = getCurrentState();
1400 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001401 return 0;
1402 }
1403
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001404 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001406 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1407 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001408
1409 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001410 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001411 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001412 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001413 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001414 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001415 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001416 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001417 this, getName().string(), mCurrentTransform,
1418 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001420 c.crop.left,
1421 c.crop.top,
1422 c.crop.right,
1423 c.crop.bottom,
1424 c.crop.getWidth(),
1425 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001426 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001427 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001428 s.crop.left,
1429 s.crop.top,
1430 s.crop.right,
1431 s.crop.bottom,
1432 s.crop.getWidth(),
1433 s.crop.getHeight(),
1434 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001436 // record the new size, form this point on, when the client request
1437 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001438 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001439 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001441
Robert Carr82364e32016-05-15 11:27:47 -07001442 const bool resizePending = (c.requested.w != c.active.w) ||
1443 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001444 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001445 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001446 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001447 // if we have a pending resize, unless we are in fixed-size mode.
1448 // the drawing state will be updated only once we receive a buffer
1449 // with the correct size.
1450 //
1451 // in particular, we want to make sure the clip (which is part
1452 // of the geometry state) is latched together with the size but is
1453 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001454 //
1455 // If a sideband stream is attached, however, we want to skip this
1456 // optimization so that transactions aren't missed when a buffer
1457 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001458
1459 flags |= eDontUpdateGeometryState;
1460 }
1461 }
1462
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 // always set active to requested, unless we're asked not to
1464 // this is used by Layer, which special cases resizes.
1465 if (flags & eDontUpdateGeometryState) {
1466 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001467 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001468 if (mFreezePositionUpdates) {
1469 float tx = c.active.transform.tx();
1470 float ty = c.active.transform.ty();
1471 c.active = c.requested;
1472 c.active.transform.set(tx, ty);
1473 editCurrentState.active = c.active;
1474 } else {
1475 editCurrentState.active = editCurrentState.requested;
1476 c.active = c.requested;
1477 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001478 }
1479
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001480 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001481 // invalidate and recompute the visible regions if needed
1482 flags |= Layer::eVisibleRegion;
1483 }
1484
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001485 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001486 // invalidate and recompute the visible regions if needed
1487 flags |= eVisibleRegion;
1488 this->contentDirty = true;
1489
1490 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001491 const uint8_t type = c.active.transform.getType();
1492 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001493 (type >= Transform::SCALE));
1494 }
1495
Dan Stozac8145172016-04-28 16:29:06 -07001496 // If the layer is hidden, signal and clear out all local sync points so
1497 // that transactions for layers depending on this layer's frames becoming
1498 // visible are not blocked
1499 if (c.flags & layer_state_t::eLayerHidden) {
1500 Mutex::Autolock lock(mLocalSyncPointMutex);
1501 for (auto& point : mLocalSyncPoints) {
1502 point->setFrameAvailable();
1503 }
1504 mLocalSyncPoints.clear();
1505 }
1506
Mathias Agopian13127d82013-03-05 17:47:11 -08001507 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001508 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001509 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510}
1511
Pablo Ceballos05289c22016-04-14 15:49:55 -07001512void Layer::commitTransaction(const State& stateToCommit) {
1513 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001514}
1515
Mathias Agopian13127d82013-03-05 17:47:11 -08001516uint32_t Layer::getTransactionFlags(uint32_t flags) {
1517 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1518}
1519
1520uint32_t Layer::setTransactionFlags(uint32_t flags) {
1521 return android_atomic_or(flags, &mTransactionFlags);
1522}
1523
Robert Carr82364e32016-05-15 11:27:47 -07001524bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001525 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001526 return false;
1527 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001528
1529 // We update the requested and active position simultaneously because
1530 // we want to apply the position portion of the transform matrix immediately,
1531 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001532 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001533 if (immediate && !mFreezePositionUpdates) {
1534 mCurrentState.active.transform.set(x, y);
1535 }
1536 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001537
Dan Stoza7dde5992015-05-22 09:51:44 -07001538 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001539 setTransactionFlags(eTransactionNeeded);
1540 return true;
1541}
Robert Carr82364e32016-05-15 11:27:47 -07001542
Mathias Agopian13127d82013-03-05 17:47:11 -08001543bool Layer::setLayer(uint32_t z) {
1544 if (mCurrentState.z == z)
1545 return false;
1546 mCurrentState.sequence++;
1547 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001548 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001549 setTransactionFlags(eTransactionNeeded);
1550 return true;
1551}
1552bool Layer::setSize(uint32_t w, uint32_t h) {
1553 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1554 return false;
1555 mCurrentState.requested.w = w;
1556 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001557 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001558 setTransactionFlags(eTransactionNeeded);
1559 return true;
1560}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001561#ifdef USE_HWC2
1562bool Layer::setAlpha(float alpha) {
1563#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001564bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001565#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001566 if (mCurrentState.alpha == alpha)
1567 return false;
1568 mCurrentState.sequence++;
1569 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001570 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001571 setTransactionFlags(eTransactionNeeded);
1572 return true;
1573}
1574bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1575 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001576 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001577 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001578 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001579 setTransactionFlags(eTransactionNeeded);
1580 return true;
1581}
1582bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001583 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001584 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001585 setTransactionFlags(eTransactionNeeded);
1586 return true;
1587}
1588bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1589 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1590 if (mCurrentState.flags == newFlags)
1591 return false;
1592 mCurrentState.sequence++;
1593 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001594 mCurrentState.mask = mask;
1595 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001596 setTransactionFlags(eTransactionNeeded);
1597 return true;
1598}
1599bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001600 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001601 return false;
1602 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001603 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001604 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001605 setTransactionFlags(eTransactionNeeded);
1606 return true;
1607}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001608bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001609 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001610 return false;
1611 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001612 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001613 mCurrentState.modified = true;
1614 setTransactionFlags(eTransactionNeeded);
1615 return true;
1616}
Mathias Agopian13127d82013-03-05 17:47:11 -08001617
Robert Carrc3574f72016-03-24 12:19:32 -07001618bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1619 if (scalingMode == mOverrideScalingMode)
1620 return false;
1621 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001622 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001623 return true;
1624}
1625
1626uint32_t Layer::getEffectiveScalingMode() const {
1627 if (mOverrideScalingMode >= 0) {
1628 return mOverrideScalingMode;
1629 }
1630 return mCurrentScalingMode;
1631}
1632
Mathias Agopian13127d82013-03-05 17:47:11 -08001633bool Layer::setLayerStack(uint32_t layerStack) {
1634 if (mCurrentState.layerStack == layerStack)
1635 return false;
1636 mCurrentState.sequence++;
1637 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001638 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001639 setTransactionFlags(eTransactionNeeded);
1640 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001641}
1642
Dan Stoza7dde5992015-05-22 09:51:44 -07001643void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1644 uint64_t frameNumber) {
1645 mCurrentState.handle = handle;
1646 mCurrentState.frameNumber = frameNumber;
1647 // We don't set eTransactionNeeded, because just receiving a deferral
1648 // request without any other state updates shouldn't actually induce a delay
1649 mCurrentState.modified = true;
1650 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001651 mCurrentState.handle = nullptr;
1652 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001653 mCurrentState.modified = false;
1654}
1655
Dan Stozaee44edd2015-03-23 15:50:23 -07001656void Layer::useSurfaceDamage() {
1657 if (mFlinger->mForceFullDamage) {
1658 surfaceDamageRegion = Region::INVALID_REGION;
1659 } else {
1660 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1661 }
1662}
1663
1664void Layer::useEmptyDamage() {
1665 surfaceDamageRegion.clear();
1666}
1667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668// ----------------------------------------------------------------------------
1669// pageflip handling...
1670// ----------------------------------------------------------------------------
1671
Dan Stoza6b9454d2014-11-07 16:00:59 -08001672bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001673 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001674 return true;
1675 }
1676
Dan Stoza6b9454d2014-11-07 16:00:59 -08001677 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001678 if (mQueueItems.empty()) {
1679 return false;
1680 }
1681 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001682 nsecs_t expectedPresent =
1683 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001684
1685 // Ignore timestamps more than a second in the future
1686 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1687 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1688 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1689 expectedPresent);
1690
1691 bool isDue = timestamp < expectedPresent;
1692 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001693}
1694
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001695bool Layer::onPreComposition() {
1696 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001697 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001698}
1699
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001700void Layer::onPostComposition() {
1701 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001702 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001703 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1704
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001705 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001706 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001707 mFrameTracker.setFrameReadyFence(frameReadyFence);
1708 } else {
1709 // There was no fence for this frame, so assume that it was ready
1710 // to be presented at the desired present time.
1711 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1712 }
1713
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001714 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001715#ifdef USE_HWC2
1716 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1717#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001718 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001719#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001720 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001721 mFrameTracker.setActualPresentFence(presentFence);
1722 } else {
1723 // The HWC doesn't support present fences, so use the refresh
1724 // timestamp instead.
1725 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1726 mFrameTracker.setActualPresentTime(presentTime);
1727 }
1728
1729 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001730 mFrameLatencyNeeded = false;
1731 }
1732}
1733
Dan Stoza9e56aa02015-11-02 13:00:03 -08001734#ifdef USE_HWC2
1735void Layer::releasePendingBuffer() {
1736 mSurfaceFlingerConsumer->releasePendingBuffer();
1737}
1738#endif
1739
Mathias Agopianda27af92012-09-13 18:17:13 -07001740bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001741 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001742#ifdef USE_HWC2
1743 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1744 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1745#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001746 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001747 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001748#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001749}
1750
Mathias Agopian4fec8732012-06-29 14:12:52 -07001751Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001753 ATRACE_CALL();
1754
Jesse Hall399184a2014-03-03 15:42:54 -08001755 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1756 // mSidebandStreamChanged was true
1757 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001758 if (mSidebandStream != NULL) {
1759 setTransactionFlags(eTransactionNeeded);
1760 mFlinger->setTransactionFlags(eTraversalNeeded);
1761 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001762 recomputeVisibleRegions = true;
1763
1764 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001765 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001766 }
1767
Mathias Agopian4fec8732012-06-29 14:12:52 -07001768 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001769 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001770
1771 // if we've already called updateTexImage() without going through
1772 // a composition step, we have to skip this layer at this point
1773 // because we cannot call updateTeximage() without a corresponding
1774 // compositionComplete() call.
1775 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001776 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001777 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001778 }
1779
Jamie Gennis351a5132011-09-14 18:23:37 -07001780 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001781 const State& s(getDrawingState());
1782 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001783 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001784
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001785 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001786 Layer::State& front;
1787 Layer::State& current;
1788 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001789 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001790 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001791 int32_t overrideScalingMode;
Robert Carrb5d3d262016-03-25 15:08:13 -07001792
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001793 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001794 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001795 const char* name,
1796 int32_t overrideScalingMode)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001797 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001798 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001799 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001800 name(name),
1801 overrideScalingMode(overrideScalingMode) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001802 }
1803
1804 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001805 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001806 if (buf == NULL) {
1807 return false;
1808 }
1809
1810 uint32_t bufWidth = buf->getWidth();
1811 uint32_t bufHeight = buf->getHeight();
1812
1813 // check that we received a buffer of the right size
1814 // (Take the buffer's orientation into account)
1815 if (item.mTransform & Transform::ROT_90) {
1816 swap(bufWidth, bufHeight);
1817 }
1818
Robert Carrc3574f72016-03-24 12:19:32 -07001819 int actualScalingMode = overrideScalingMode >= 0 ?
1820 overrideScalingMode : item.mScalingMode;
1821 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001822 if (front.active != front.requested) {
1823
1824 if (isFixedSize ||
1825 (bufWidth == front.requested.w &&
1826 bufHeight == front.requested.h))
1827 {
1828 // Here we pretend the transaction happened by updating the
1829 // current and drawing states. Drawing state is only accessed
1830 // in this thread, no need to have it locked
1831 front.active = front.requested;
1832
1833 // We also need to update the current state so that
1834 // we don't end-up overwriting the drawing state with
1835 // this stale current state during the next transaction
1836 //
1837 // NOTE: We don't need to hold the transaction lock here
1838 // because State::active is only accessed from this thread.
1839 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001840 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001841
1842 // recompute visible region
1843 recomputeVisibleRegions = true;
1844 }
1845
1846 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001847 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001848 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001849 " requested={ wh={%4u,%4u} }}\n",
1850 name,
Andy McFadden69052052012-09-14 16:10:11 -07001851 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001852 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001853 front.crop.left,
1854 front.crop.top,
1855 front.crop.right,
1856 front.crop.bottom,
1857 front.crop.getWidth(),
1858 front.crop.getHeight(),
1859 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001860 }
1861
Ruben Brunk1681d952014-06-27 15:51:55 -07001862 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001863 if (front.active.w != bufWidth ||
1864 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001865 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001866 ALOGE("[%s] rejecting buffer: "
1867 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1868 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001869 return true;
1870 }
1871 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001872
1873 // if the transparent region has changed (this test is
1874 // conservative, but that's fine, worst case we're doing
1875 // a bit of extra work), we latch the new one and we
1876 // trigger a visible-region recompute.
1877 if (!front.activeTransparentRegion.isTriviallyEqual(
1878 front.requestedTransparentRegion)) {
1879 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001880
1881 // We also need to update the current state so that
1882 // we don't end-up overwriting the drawing state with
1883 // this stale current state during the next transaction
1884 //
1885 // NOTE: We don't need to hold the transaction lock here
1886 // because State::active is only accessed from this thread.
1887 current.activeTransparentRegion = front.activeTransparentRegion;
1888
1889 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001890 recomputeVisibleRegions = true;
1891 }
1892
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001893 return false;
1894 }
1895 };
1896
Ruben Brunk1681d952014-06-27 15:51:55 -07001897 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001898 getProducerStickyTransform() != 0, mName.string(),
1899 mOverrideScalingMode);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001900
Dan Stozacac35382016-01-27 12:21:06 -08001901
1902 // Check all of our local sync points to ensure that all transactions
1903 // which need to have been applied prior to the frame which is about to
1904 // be latched have signaled
1905
1906 auto headFrameNumber = getHeadFrameNumber();
1907 bool matchingFramesFound = false;
1908 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001909 {
Dan Stozacac35382016-01-27 12:21:06 -08001910 Mutex::Autolock lock(mLocalSyncPointMutex);
1911 for (auto& point : mLocalSyncPoints) {
1912 if (point->getFrameNumber() > headFrameNumber) {
1913 break;
1914 }
1915
1916 matchingFramesFound = true;
1917
1918 if (!point->frameIsAvailable()) {
1919 // We haven't notified the remote layer that the frame for
1920 // this point is available yet. Notify it now, and then
1921 // abort this attempt to latch.
1922 point->setFrameAvailable();
1923 allTransactionsApplied = false;
1924 break;
1925 }
1926
1927 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001928 }
1929 }
1930
Dan Stozacac35382016-01-27 12:21:06 -08001931 if (matchingFramesFound && !allTransactionsApplied) {
1932 mFlinger->signalLayerUpdate();
1933 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001934 }
1935
Pablo Ceballos06312182015-10-07 16:32:12 -07001936 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1937 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos2dcb3632016-03-17 15:50:23 -07001938 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001939 // buffer mode.
1940 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001941 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001942 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001943 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001944 if (updateResult == BufferQueue::PRESENT_LATER) {
1945 // Producer doesn't want buffer to be displayed yet. Signal a
1946 // layer update so we check again at the next opportunity.
1947 mFlinger->signalLayerUpdate();
1948 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001949 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1950 // If the buffer has been rejected, remove it from the shadow queue
1951 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001952 if (queuedBuffer) {
1953 Mutex::Autolock lock(mQueueItemLock);
1954 mQueueItems.removeAt(0);
1955 android_atomic_dec(&mQueuedFrames);
1956 }
Dan Stozaecc50402015-04-28 14:42:06 -07001957 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001958 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1959 // This can occur if something goes wrong when trying to create the
1960 // EGLImage for this buffer. If this happens, the buffer has already
1961 // been released, so we need to clean up the queue and bug out
1962 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001963 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001964 Mutex::Autolock lock(mQueueItemLock);
1965 mQueueItems.clear();
1966 android_atomic_and(0, &mQueuedFrames);
1967 }
1968
1969 // Once we have hit this state, the shadow queue may no longer
1970 // correctly reflect the incoming BufferQueue's contents, so even if
1971 // updateTexImage starts working, the only safe course of action is
1972 // to continue to ignore updates.
1973 mUpdateTexImageFailed = true;
1974
1975 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001976 }
1977
Pablo Ceballos06312182015-10-07 16:32:12 -07001978 if (queuedBuffer) {
1979 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001980 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1981
Dan Stoza6b9454d2014-11-07 16:00:59 -08001982 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001983
1984 // Remove any stale buffers that have been dropped during
1985 // updateTexImage
1986 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1987 mQueueItems.removeAt(0);
1988 android_atomic_dec(&mQueuedFrames);
1989 }
1990
Dan Stoza6b9454d2014-11-07 16:00:59 -08001991 mQueueItems.removeAt(0);
1992 }
1993
Dan Stozaecc50402015-04-28 14:42:06 -07001994
Andy McFadden1585c4d2013-06-28 13:52:40 -07001995 // Decrement the queued-frames count. Signal another event if we
1996 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001997 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001998 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001999 mFlinger->signalLayerUpdate();
2000 }
2001
2002 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002003 // something happened!
2004 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002005 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002006 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002007
Jamie Gennis351a5132011-09-14 18:23:37 -07002008 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002009 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002010 if (mActiveBuffer == NULL) {
2011 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002012 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002013 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002014
Mathias Agopian4824d402012-06-04 18:16:30 -07002015 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002016 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002017 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002018 // the first time we receive a buffer, we need to trigger a
2019 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002020 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002021 }
2022
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002023 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2024 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2025 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002026 if ((crop != mCurrentCrop) ||
2027 (transform != mCurrentTransform) ||
2028 (scalingMode != mCurrentScalingMode))
2029 {
2030 mCurrentCrop = crop;
2031 mCurrentTransform = transform;
2032 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002033 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002034 }
2035
2036 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002037 uint32_t bufWidth = mActiveBuffer->getWidth();
2038 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002039 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2040 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002041 recomputeVisibleRegions = true;
Robert Carr82364e32016-05-15 11:27:47 -07002042 mFreezePositionUpdates = false;
Mathias Agopian702634a2012-05-23 17:50:31 -07002043 }
2044 }
2045
2046 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002047 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002048 recomputeVisibleRegions = true;
2049 }
2050
Dan Stozacac35382016-01-27 12:21:06 -08002051 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2052
2053 // Remove any sync points corresponding to the buffer which was just
2054 // latched
2055 {
2056 Mutex::Autolock lock(mLocalSyncPointMutex);
2057 auto point = mLocalSyncPoints.begin();
2058 while (point != mLocalSyncPoints.end()) {
2059 if (!(*point)->frameIsAvailable() ||
2060 !(*point)->transactionIsApplied()) {
2061 // This sync point must have been added since we started
2062 // latching. Don't drop it yet.
2063 ++point;
2064 continue;
2065 }
2066
2067 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2068 point = mLocalSyncPoints.erase(point);
2069 } else {
2070 ++point;
2071 }
2072 }
2073 }
2074
Mathias Agopian4fec8732012-06-29 14:12:52 -07002075 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002076 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002077
2078 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002079 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002080 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002081 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082}
2083
Mathias Agopiana67932f2011-04-20 14:20:59 -07002084uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002085{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002086 // TODO: should we do something special if mSecure is set?
2087 if (mProtectedByApp) {
2088 // need a hardware-protected path to external video sink
2089 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002090 }
Riley Andrews03414a12014-07-01 14:22:59 -07002091 if (mPotentialCursor) {
2092 usage |= GraphicBuffer::USAGE_CURSOR;
2093 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002094 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002095 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002096}
2097
Mathias Agopian84300952012-11-21 16:02:13 -08002098void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002099 uint32_t orientation = 0;
2100 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002101 // The transform hint is used to improve performance, but we can
2102 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002103 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002104 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002105 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002106 if (orientation & Transform::ROT_INVALID) {
2107 orientation = 0;
2108 }
2109 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002110 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002111}
2112
Mathias Agopian13127d82013-03-05 17:47:11 -08002113// ----------------------------------------------------------------------------
2114// debugging
2115// ----------------------------------------------------------------------------
2116
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002117void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002118{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002119 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002120
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002121 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002122 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002123 "+ %s %p (%s)\n",
2124 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002125 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002126
Mathias Agopian2ca79392013-04-02 18:30:32 -07002127 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002128 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002129 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002130 sp<Client> client(mClientRef.promote());
2131
Mathias Agopian74d211a2013-04-22 16:55:35 +02002132 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002133 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2134 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002135 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002136#ifdef USE_HWC2
2137 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2138#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002139 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002140#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002141 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002142 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 -07002143 s.crop.left, s.crop.top,
2144 s.crop.right, s.crop.bottom,
2145 s.finalCrop.left, s.finalCrop.top,
2146 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002147 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002148 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002149 s.active.transform[0][0], s.active.transform[0][1],
2150 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002151 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002152
2153 sp<const GraphicBuffer> buf0(mActiveBuffer);
2154 uint32_t w0=0, h0=0, s0=0, f0=0;
2155 if (buf0 != 0) {
2156 w0 = buf0->getWidth();
2157 h0 = buf0->getHeight();
2158 s0 = buf0->getStride();
2159 f0 = buf0->format;
2160 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002161 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002162 " "
2163 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2164 " queued-frames=%d, mRefreshPending=%d\n",
2165 mFormat, w0, h0, s0,f0,
2166 mQueuedFrames, mRefreshPending);
2167
Mathias Agopian13127d82013-03-05 17:47:11 -08002168 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002169 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002170 }
2171}
2172
Svetoslavd85084b2014-03-20 10:28:31 -07002173void Layer::dumpFrameStats(String8& result) const {
2174 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002175}
2176
Svetoslavd85084b2014-03-20 10:28:31 -07002177void Layer::clearFrameStats() {
2178 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002179}
2180
Jamie Gennis6547ff42013-07-16 20:12:42 -07002181void Layer::logFrameStats() {
2182 mFrameTracker.logAndResetStats(mName);
2183}
2184
Svetoslavd85084b2014-03-20 10:28:31 -07002185void Layer::getFrameStats(FrameStats* outStats) const {
2186 mFrameTracker.getStats(outStats);
2187}
2188
Pablo Ceballos40845df2016-01-25 17:41:15 -08002189void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2190 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2191 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2192 *outName = mName;
2193 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2194
2195#ifdef USE_HWC2
2196 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2197 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2198 HWC2::Composition::Client : true;
2199#else
2200 *outIsGlesComposition = mIsGlesComposition;
2201#endif
2202 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2203 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2204 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2205}
Mathias Agopian13127d82013-03-05 17:47:11 -08002206// ---------------------------------------------------------------------------
2207
2208Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2209 const sp<Layer>& layer)
2210 : mFlinger(flinger), mLayer(layer) {
2211}
2212
2213Layer::LayerCleaner::~LayerCleaner() {
2214 // destroy client resources
2215 mFlinger->onLayerDestroyed(mLayer);
2216}
2217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002218// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002220
2221#if defined(__gl_h_)
2222#error "don't include gl/gl.h in this file"
2223#endif
2224
2225#if defined(__gl2_h_)
2226#error "don't include gl2/gl2.h in this file"
2227#endif