blob: 02477239b7ab9c1832d1e3e77d2c74626ebdbebc [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
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700163#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
164#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700165#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700166 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800167#endif
Andy McFadden69052052012-09-14 16:10:11 -0700168
Mathias Agopian84300952012-11-21 16:02:13 -0800169 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
170 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700171}
172
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700173Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800174 sp<Client> c(mClientRef.promote());
175 if (c != 0) {
176 c->detachLayer(this);
177 }
178
Dan Stozacac35382016-01-27 12:21:06 -0800179 for (auto& point : mRemoteSyncPoints) {
180 point->setTransactionApplied();
181 }
Dan Stozac8145172016-04-28 16:29:06 -0700182 for (auto& point : mLocalSyncPoints) {
183 point->setFrameAvailable();
184 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700186 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// callbacks
191// ---------------------------------------------------------------------------
192
Dan Stoza9e56aa02015-11-02 13:00:03 -0800193#ifdef USE_HWC2
194void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
195 if (mHwcLayers.empty()) {
196 return;
197 }
198 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
199}
200#else
Dan Stozac7014012014-02-14 15:03:43 -0800201void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 HWComposer::HWCLayerInterface* layer) {
203 if (layer) {
204 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700205 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 }
207}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800209
Dan Stoza6b9454d2014-11-07 16:00:59 -0800210void Layer::onFrameAvailable(const BufferItem& item) {
211 // Add this buffer from our internal queue tracker
212 { // Autolock scope
213 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700214
215 // Reset the frame number tracker when we receive the first buffer after
216 // a frame number reset
217 if (item.mFrameNumber == 1) {
218 mLastFrameNumberReceived = 0;
219 }
220
221 // Ensure that callbacks are handled in order
222 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
223 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
224 ms2ns(500));
225 if (result != NO_ERROR) {
226 ALOGE("[%s] Timed out waiting on callback", mName.string());
227 }
228 }
229
Dan Stoza6b9454d2014-11-07 16:00:59 -0800230 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700231 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700232
233 // Wake up any pending callbacks
234 mLastFrameNumberReceived = item.mFrameNumber;
235 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800236 }
237
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800238 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700239}
240
Dan Stoza6b9454d2014-11-07 16:00:59 -0800241void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700242 { // Autolock scope
243 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700244
Dan Stoza7dde5992015-05-22 09:51:44 -0700245 // Ensure that callbacks are handled in order
246 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
247 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
248 ms2ns(500));
249 if (result != NO_ERROR) {
250 ALOGE("[%s] Timed out waiting on callback", mName.string());
251 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700252 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700253
254 if (mQueueItems.empty()) {
255 ALOGE("Can't replace a frame on an empty queue");
256 return;
257 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700258 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700259
260 // Wake up any pending callbacks
261 mLastFrameNumberReceived = item.mFrameNumber;
262 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700263 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800264}
265
Jesse Hall399184a2014-03-03 15:42:54 -0800266void Layer::onSidebandStreamChanged() {
267 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
268 // mSidebandStreamChanged was false
269 mFlinger->signalLayerUpdate();
270 }
271}
272
Mathias Agopian67106042013-03-14 19:18:13 -0700273// called with SurfaceFlinger::mStateLock from the drawing thread after
274// the layer has been remove from the current state list (and just before
275// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800276void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800277 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700278}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700279
Mathias Agopian13127d82013-03-05 17:47:11 -0800280// ---------------------------------------------------------------------------
281// set-up
282// ---------------------------------------------------------------------------
283
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700284const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800285 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
Mathias Agopianf9d93272009-06-19 17:00:27 -0700288status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 PixelFormat format, uint32_t flags)
290{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700291 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700292 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700293
294 // never allow a surface larger than what our underlying GL implementation
295 // can handle.
296 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800297 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700298 return BAD_VALUE;
299 }
300
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700301 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700302
Riley Andrews03414a12014-07-01 14:22:59 -0700303 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700304 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700305 mCurrentOpacity = getOpacityForFormat(format);
306
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800307 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
308 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
309 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 return NO_ERROR;
312}
313
Dan Stoza7dde5992015-05-22 09:51:44 -0700314/*
315 * The layer handle is just a BBinder object passed to the client
316 * (remote process) -- we don't keep any reference on our side such that
317 * the dtor is called when the remote side let go of its reference.
318 *
319 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
320 * this layer when the handle is destroyed.
321 */
322class Layer::Handle : public BBinder, public LayerCleaner {
323 public:
324 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
325 : LayerCleaner(flinger, layer), owner(layer) {}
326
327 wp<Layer> owner;
328};
329
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700330sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800331 Mutex::Autolock _l(mLock);
332
333 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700334 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800335
336 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700337
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700338 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800339}
340
Dan Stozab9b08832014-03-13 11:55:57 -0700341sp<IGraphicBufferProducer> Layer::getProducer() const {
342 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700343}
344
Mathias Agopian13127d82013-03-05 17:47:11 -0800345// ---------------------------------------------------------------------------
346// h/w composer set-up
347// ---------------------------------------------------------------------------
348
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800349Rect Layer::getContentCrop() const {
350 // this is the crop rectangle that applies to the buffer
351 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700352 Rect crop;
353 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800354 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700355 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800356 } else if (mActiveBuffer != NULL) {
357 // otherwise we use the whole buffer
358 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700359 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800360 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700361 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700363 return crop;
364}
365
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700366static Rect reduce(const Rect& win, const Region& exclude) {
367 if (CC_LIKELY(exclude.isEmpty())) {
368 return win;
369 }
370 if (exclude.isRect()) {
371 return win.reduce(exclude.getBounds());
372 }
373 return Region(win).subtract(exclude).getBounds();
374}
375
Mathias Agopian13127d82013-03-05 17:47:11 -0800376Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700377 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700378 return computeBounds(s.activeTransparentRegion);
379}
380
381Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
382 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700384
385 if (!s.crop.isEmpty()) {
386 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800387 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700388 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700389 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800390}
391
Mathias Agopian6b442672013-07-09 21:24:52 -0700392FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800393 // the content crop is the area of the content that gets scaled to the
394 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700395 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800396
Robert Carrb5d3d262016-03-25 15:08:13 -0700397 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800398 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700399 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800400
401 // apply the projection's clipping to the window crop in
402 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700403 // if there are no window scaling involved, this operation will map to full
404 // pixels in the buffer.
405 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
406 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700407
408 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700409 if (!s.crop.isEmpty()) {
410 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700411 }
412
Robert Carr3dcabfa2016-03-01 18:36:58 -0800413 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
415 activeCrop.clear();
416 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700417 if (!s.finalCrop.isEmpty()) {
418 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 activeCrop.clear();
420 }
421 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800422 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800423
Michael Lentine28ea2172014-11-19 18:32:37 -0800424 // This needs to be here as transform.transform(Rect) computes the
425 // transformed rect and then takes the bounding box of the result before
426 // returning. This means
427 // transform.inverse().transform(transform.transform(Rect)) != Rect
428 // in which case we need to make sure the final rect is clipped to the
429 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
431 activeCrop.clear();
432 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800433
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700434 // subtract the transparent region and snap to the bounds
435 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
436
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 // Transform the window crop to match the buffer coordinate system,
438 // which means using the inverse of the current transform set on the
439 // SurfaceFlingerConsumer.
440 uint32_t invTransform = mCurrentTransform;
441 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
442 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700443 * the code below applies the primary display's inverse transform to the
444 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700446 uint32_t invTransformOrient =
447 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // calculate the inverse transform
449 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
450 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
451 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800452 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700454 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
455 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457
458 int winWidth = s.active.w;
459 int winHeight = s.active.h;
460 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
461 // If the activeCrop has been rotate the ends are rotated but not
462 // the space itself so when transforming ends back we can't rely on
463 // a modification of the axes of rotation. To account for this we
464 // need to reorient the inverse rotation in terms of the current
465 // axes of rotation.
466 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
467 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
468 if (is_h_flipped == is_v_flipped) {
469 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
470 NATIVE_WINDOW_TRANSFORM_FLIP_H;
471 }
472 winWidth = s.active.h;
473 winHeight = s.active.w;
474 }
475 const Rect winCrop = activeCrop.transform(
476 invTransform, s.active.w, s.active.h);
477
478 // below, crop is intersected with winCrop expressed in crop's coordinate space
479 float xScale = crop.getWidth() / float(winWidth);
480 float yScale = crop.getHeight() / float(winHeight);
481
482 float insetL = winCrop.left * xScale;
483 float insetT = winCrop.top * yScale;
484 float insetR = (winWidth - winCrop.right ) * xScale;
485 float insetB = (winHeight - winCrop.bottom) * yScale;
486
487 crop.left += insetL;
488 crop.top += insetT;
489 crop.right -= insetR;
490 crop.bottom -= insetB;
491
Mathias Agopian13127d82013-03-05 17:47:11 -0800492 return crop;
493}
494
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495#ifdef USE_HWC2
496void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
497#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700498void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700499 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700500 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700502{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503#ifdef USE_HWC2
504 const auto hwcId = displayDevice->getHwcDisplayId();
505 auto& hwcInfo = mHwcLayers[hwcId];
506#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800507 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700509
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700510 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511#ifdef USE_HWC2
512 hwcInfo.forceClientComposition = false;
513
514 if (isSecure() && !displayDevice->isSecure()) {
515 hwcInfo.forceClientComposition = true;
516 }
517
518 auto& hwcLayer = hwcInfo.layer;
519#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700520 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700521
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700522 if (isSecure() && !hw->isSecure()) {
523 layer.setSkip(true);
524 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800525#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700526
Mathias Agopian13127d82013-03-05 17:47:11 -0800527 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700528 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529#ifdef USE_HWC2
530 if (!isOpaque(s) || s.alpha != 1.0f) {
531 auto blendMode = mPremultipliedAlpha ?
532 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
533 auto error = hwcLayer->setBlendMode(blendMode);
534 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
535 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
536 to_string(error).c_str(), static_cast<int32_t>(error));
537 }
538#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800539 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800540 layer.setBlending(mPremultipliedAlpha ?
541 HWC_BLENDING_PREMULT :
542 HWC_BLENDING_COVERAGE);
543 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800545
546 // apply the layer's transform, followed by the display's global transform
547 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700548 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700549 if (!s.crop.isEmpty()) {
550 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800551 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 activeCrop.clear();
558 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800559 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800560 // This needs to be here as transform.transform(Rect) computes the
561 // transformed rect and then takes the bounding box of the result before
562 // returning. This means
563 // transform.inverse().transform(transform.transform(Rect)) != Rect
564 // in which case we need to make sure the final rect is clipped to the
565 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000566 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
567 activeCrop.clear();
568 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700569 // mark regions outside the crop as transparent
570 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
571 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
572 s.active.w, s.active.h));
573 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
574 activeCrop.left, activeCrop.bottom));
575 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
576 s.active.w, activeCrop.bottom));
577 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800578 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700579 if (!s.finalCrop.isEmpty()) {
580 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000581 frame.clear();
582 }
583 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000585 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
586 frame.clear();
587 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588 const Transform& tr(displayDevice->getTransform());
589 Rect transformedFrame = tr.transform(frame);
590 auto error = hwcLayer->setDisplayFrame(transformedFrame);
591 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
592 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
593 transformedFrame.top, transformedFrame.right,
594 transformedFrame.bottom, to_string(error).c_str(),
595 static_cast<int32_t>(error));
596
597 FloatRect sourceCrop = computeCrop(displayDevice);
598 error = hwcLayer->setSourceCrop(sourceCrop);
599 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
600 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
601 sourceCrop.left, sourceCrop.top, sourceCrop.right,
602 sourceCrop.bottom, to_string(error).c_str(),
603 static_cast<int32_t>(error));
604
605 error = hwcLayer->setPlaneAlpha(s.alpha);
606 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
607 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
608 static_cast<int32_t>(error));
609
610 error = hwcLayer->setZOrder(s.z);
611 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
612 mName.string(), s.z, to_string(error).c_str(),
613 static_cast<int32_t>(error));
614#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000615 if (!frame.intersect(hw->getViewport(), &frame)) {
616 frame.clear();
617 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800618 const Transform& tr(hw->getTransform());
619 layer.setFrame(tr.transform(frame));
620 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800621 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800623
Mathias Agopian29a367b2011-07-12 14:51:45 -0700624 /*
625 * Transformations are applied in this order:
626 * 1) buffer orientation/flip/mirror
627 * 2) state transformation (window manager)
628 * 3) layer orientation (screen orientation)
629 * (NOTE: the matrices are multiplied in reverse order)
630 */
631
632 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800633 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634
635 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
636 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700637 * the code below applies the primary display's inverse transform to the
638 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700639 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700640 uint32_t invTransform =
641 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700642 // calculate the inverse transform
643 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
644 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
645 NATIVE_WINDOW_TRANSFORM_FLIP_H;
646 }
647 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700648 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700649 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700650
651 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800652 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653#ifdef USE_HWC2
654 if (orientation & Transform::ROT_INVALID) {
655 // we can only handle simple transformation
656 hwcInfo.forceClientComposition = true;
657 } else {
658 auto transform = static_cast<HWC2::Transform>(orientation);
659 auto error = hwcLayer->setTransform(transform);
660 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
661 "%s (%d)", mName.string(), to_string(transform).c_str(),
662 to_string(error).c_str(), static_cast<int32_t>(error));
663 }
664#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800665 if (orientation & Transform::ROT_INVALID) {
666 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700667 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700668 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800669 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700670 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700672}
673
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674#ifdef USE_HWC2
675void Layer::forceClientComposition(int32_t hwcId) {
676 if (mHwcLayers.count(hwcId) == 0) {
677 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
678 return;
679 }
680
681 mHwcLayers[hwcId].forceClientComposition = true;
682}
683#endif
684
685#ifdef USE_HWC2
686void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
687 // Apply this display's projection's viewport to the visible region
688 // before giving it to the HWC HAL.
689 const Transform& tr = displayDevice->getTransform();
690 const auto& viewport = displayDevice->getViewport();
691 Region visible = tr.transform(visibleRegion.intersect(viewport));
692 auto hwcId = displayDevice->getHwcDisplayId();
693 auto& hwcLayer = mHwcLayers[hwcId].layer;
694 auto error = hwcLayer->setVisibleRegion(visible);
695 if (error != HWC2::Error::None) {
696 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
697 to_string(error).c_str(), static_cast<int32_t>(error));
698 visible.dump(LOG_TAG);
699 }
700
701 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
702 if (error != HWC2::Error::None) {
703 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
704 to_string(error).c_str(), static_cast<int32_t>(error));
705 surfaceDamageRegion.dump(LOG_TAG);
706 }
707
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700708 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700710 setCompositionType(hwcId, HWC2::Composition::Sideband);
711 ALOGV("[%s] Requesting Sideband composition", mName.string());
712 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800713 if (error != HWC2::Error::None) {
714 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
715 mName.string(), mSidebandStream->handle(),
716 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800717 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700718 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800719 }
720
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700721 // Client or SolidColor layers
722 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr ||
723 mHwcLayers[hwcId].forceClientComposition) {
724 // TODO: This also includes solid color layers, but no API exists to
725 // setup a solid color layer yet
726 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700728 error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
729 if (error != HWC2::Error::None) {
730 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
731 to_string(error).c_str(), static_cast<int32_t>(error));
732 }
733 return;
734 }
735
736 // Device or Cursor layers
737 if (mPotentialCursor) {
738 ALOGV("[%s] Requesting Cursor composition", mName.string());
739 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 } else {
741 ALOGV("[%s] Requesting Device composition", mName.string());
742 setCompositionType(hwcId, HWC2::Composition::Device);
743 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700744
745 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
746 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
747 if (error != HWC2::Error::None) {
748 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
749 mActiveBuffer->handle, to_string(error).c_str(),
750 static_cast<int32_t>(error));
751 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752}
753#else
Mathias Agopian42977342012-08-05 00:40:46 -0700754void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700755 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800756 // we have to set the visible region on every frame because
757 // we currently free it during onLayerDisplayed(), which is called
758 // after HWComposer::commit() -- every frame.
759 // Apply this display's projection's viewport to the visible region
760 // before giving it to the HWC HAL.
761 const Transform& tr = hw->getTransform();
762 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
763 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700764 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800765 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700766
Jesse Hall399184a2014-03-03 15:42:54 -0800767 if (mSidebandStream.get()) {
768 layer.setSidebandStream(mSidebandStream);
769 } else {
770 // NOTE: buffer can be NULL if the client never drew into this
771 // layer yet, or if we ran out of memory
772 layer.setBuffer(mActiveBuffer);
773 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700774}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700776
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777#ifdef USE_HWC2
778void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
779 auto hwcId = displayDevice->getHwcDisplayId();
780 if (mHwcLayers.count(hwcId) == 0 ||
781 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
782 return;
783 }
784
785 // This gives us only the "orientation" component of the transform
786 const State& s(getCurrentState());
787
788 // Apply the layer's transform, followed by the display's global transform
789 // Here we're guaranteed that the layer's transform preserves rects
790 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700791 if (!s.crop.isEmpty()) {
792 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793 }
794 // Subtract the transparent region and snap to the bounds
795 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800796 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700798 if (!s.finalCrop.isEmpty()) {
799 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000800 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800801 auto& displayTransform(displayDevice->getTransform());
802 auto position = displayTransform.transform(frame);
803
804 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
805 position.top);
806 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
807 "to (%d, %d): %s (%d)", mName.string(), position.left,
808 position.top, to_string(error).c_str(),
809 static_cast<int32_t>(error));
810}
811#else
Dan Stozac7014012014-02-14 15:03:43 -0800812void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700813 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700814 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700815
816 // TODO: there is a possible optimization here: we only need to set the
817 // acquire fence the first time a new buffer is acquired on EACH display.
818
Riley Andrews03414a12014-07-01 14:22:59 -0700819 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800820 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800821 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700822 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700823 if (fenceFd == -1) {
824 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
825 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700826 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700827 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700828 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700829}
830
Riley Andrews03414a12014-07-01 14:22:59 -0700831Rect Layer::getPosition(
832 const sp<const DisplayDevice>& hw)
833{
834 // this gives us only the "orientation" component of the transform
835 const State& s(getCurrentState());
836
837 // apply the layer's transform, followed by the display's global transform
838 // here we're guaranteed that the layer's transform preserves rects
839 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700840 if (!s.crop.isEmpty()) {
841 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700842 }
843 // subtract the transparent region and snap to the bounds
844 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800845 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700846 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700847 if (!s.finalCrop.isEmpty()) {
848 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 }
Riley Andrews03414a12014-07-01 14:22:59 -0700850 const Transform& tr(hw->getTransform());
851 return Rect(tr.transform(frame));
852}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800853#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700854
Mathias Agopian13127d82013-03-05 17:47:11 -0800855// ---------------------------------------------------------------------------
856// drawing...
857// ---------------------------------------------------------------------------
858
859void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800860 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800861}
862
Dan Stozac7014012014-02-14 15:03:43 -0800863void Layer::draw(const sp<const DisplayDevice>& hw,
864 bool useIdentityTransform) const {
865 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800866}
867
Dan Stozac7014012014-02-14 15:03:43 -0800868void Layer::draw(const sp<const DisplayDevice>& hw) const {
869 onDraw(hw, Region(hw->bounds()), false);
870}
871
872void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
873 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800875 ATRACE_CALL();
876
Mathias Agopiana67932f2011-04-20 14:20:59 -0700877 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700879 // in fact never been drawn into. This happens frequently with
880 // SurfaceView because the WindowManager can't know when the client
881 // has drawn the first time.
882
883 // If there is nothing under us, we paint the screen in black, otherwise
884 // we just skip this update.
885
886 // figure out if there is something below us
887 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700888 const SurfaceFlinger::LayerVector& drawingLayers(
889 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700890 const size_t count = drawingLayers.size();
891 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 const sp<Layer>& layer(drawingLayers[i]);
893 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700894 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700895 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700896 }
897 // if not everything below us is covered, we plug the holes!
898 Region holes(clip.subtract(under));
899 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700900 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700901 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 return;
903 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700904
Andy McFadden97eba892012-12-11 15:21:45 -0800905 // Bind the current buffer to the GL texture, and wait for it to be
906 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800907 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
908 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800909 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700910 // Go ahead and draw the buffer anyway; no matter what we do the screen
911 // is probably going to have something visibly wrong.
912 }
913
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700914 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
915
Mathias Agopian875d8e12013-06-07 15:35:48 -0700916 RenderEngine& engine(mFlinger->getRenderEngine());
917
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700918 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700919 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700920 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700921
922 // Query the texture matrix given our current filtering mode.
923 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800924 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
925 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700926
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700927 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
928
929 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700930 * the code below applies the primary display's inverse transform to
931 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700932 */
933
934 // create a 4x4 transform matrix from the display transform flags
935 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
936 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
937 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
938
939 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700940 uint32_t transform =
941 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700942 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
943 tr = tr * rot90;
944 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
945 tr = tr * flipH;
946 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
947 tr = tr * flipV;
948
949 // calculate the inverse
950 tr = inverse(tr);
951
952 // and finally apply it to the original texture matrix
953 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
954 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
955 }
956
Jamie Genniscbb1a952012-05-08 17:05:52 -0700957 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700958 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
959 mTexture.setFiltering(useFiltering);
960 mTexture.setMatrix(textureMatrix);
961
962 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700963 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700964 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700965 }
Dan Stozac7014012014-02-14 15:03:43 -0800966 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700967 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968}
969
Mathias Agopian13127d82013-03-05 17:47:11 -0800970
Dan Stozac7014012014-02-14 15:03:43 -0800971void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
972 const Region& /* clip */, float red, float green, float blue,
973 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800974{
Mathias Agopian19733a32013-08-28 18:13:56 -0700975 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800976 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700977 engine.setupFillWithColor(red, green, blue, alpha);
978 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800979}
980
981void Layer::clearWithOpenGL(
982 const sp<const DisplayDevice>& hw, const Region& clip) const {
983 clearWithOpenGL(hw, clip, 0,0,0,0);
984}
985
Dan Stozac7014012014-02-14 15:03:43 -0800986void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
987 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700988 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800989
Dan Stozac7014012014-02-14 15:03:43 -0800990 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800991
Mathias Agopian13127d82013-03-05 17:47:11 -0800992 /*
993 * NOTE: the way we compute the texture coordinates here produces
994 * different results than when we take the HWC path -- in the later case
995 * the "source crop" is rounded to texel boundaries.
996 * This can produce significantly different results when the texture
997 * is scaled by a large amount.
998 *
999 * The GL code below is more logical (imho), and the difference with
1000 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001001 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001002 * GL composition when a buffer scaling is applied (maybe with some
1003 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1004 * like more of a hack.
1005 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001006 Rect win(computeBounds());
1007
Robert Carrb5d3d262016-03-25 15:08:13 -07001008 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001009 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001010 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001011 win.clear();
1012 }
1013 win = s.active.transform.inverse().transform(win);
1014 if (!win.intersect(computeBounds(), &win)) {
1015 win.clear();
1016 }
1017 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001018
Mathias Agopian3f844832013-08-07 21:24:32 -07001019 float left = float(win.left) / float(s.active.w);
1020 float top = float(win.top) / float(s.active.h);
1021 float right = float(win.right) / float(s.active.w);
1022 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001023
Mathias Agopian875d8e12013-06-07 15:35:48 -07001024 // TODO: we probably want to generate the texture coords with the mesh
1025 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001026 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1027 texCoords[0] = vec2(left, 1.0f - top);
1028 texCoords[1] = vec2(left, 1.0f - bottom);
1029 texCoords[2] = vec2(right, 1.0f - bottom);
1030 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001031
Mathias Agopian875d8e12013-06-07 15:35:48 -07001032 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001033 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001034 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001035 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001036}
1037
Dan Stoza9e56aa02015-11-02 13:00:03 -08001038#ifdef USE_HWC2
1039void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1040 bool callIntoHwc) {
1041 if (mHwcLayers.count(hwcId) == 0) {
1042 ALOGE("setCompositionType called without a valid HWC layer");
1043 return;
1044 }
1045 auto& hwcInfo = mHwcLayers[hwcId];
1046 auto& hwcLayer = hwcInfo.layer;
1047 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1048 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1049 if (hwcInfo.compositionType != type) {
1050 ALOGV(" actually setting");
1051 hwcInfo.compositionType = type;
1052 if (callIntoHwc) {
1053 auto error = hwcLayer->setCompositionType(type);
1054 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1055 "composition type %s: %s (%d)", mName.string(),
1056 to_string(type).c_str(), to_string(error).c_str(),
1057 static_cast<int32_t>(error));
1058 }
1059 }
1060}
1061
1062HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1063 if (mHwcLayers.count(hwcId) == 0) {
1064 ALOGE("getCompositionType called without a valid HWC layer");
1065 return HWC2::Composition::Invalid;
1066 }
1067 return mHwcLayers.at(hwcId).compositionType;
1068}
1069
1070void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1071 if (mHwcLayers.count(hwcId) == 0) {
1072 ALOGE("setClearClientTarget called without a valid HWC layer");
1073 return;
1074 }
1075 mHwcLayers[hwcId].clearClientTarget = clear;
1076}
1077
1078bool Layer::getClearClientTarget(int32_t hwcId) const {
1079 if (mHwcLayers.count(hwcId) == 0) {
1080 ALOGE("getClearClientTarget called without a valid HWC layer");
1081 return false;
1082 }
1083 return mHwcLayers.at(hwcId).clearClientTarget;
1084}
1085#endif
1086
Ruben Brunk1681d952014-06-27 15:51:55 -07001087uint32_t Layer::getProducerStickyTransform() const {
1088 int producerStickyTransform = 0;
1089 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1090 if (ret != OK) {
1091 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1092 strerror(-ret), ret);
1093 return 0;
1094 }
1095 return static_cast<uint32_t>(producerStickyTransform);
1096}
1097
Dan Stozacac35382016-01-27 12:21:06 -08001098uint64_t Layer::getHeadFrameNumber() const {
1099 Mutex::Autolock lock(mQueueItemLock);
1100 if (!mQueueItems.empty()) {
1101 return mQueueItems[0].mFrameNumber;
1102 } else {
1103 return mCurrentFrameNumber;
1104 }
1105}
1106
1107bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1108 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1109 // Don't bother with a SyncPoint, since we've already latched the
1110 // relevant frame
1111 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001112 }
1113
Dan Stozacac35382016-01-27 12:21:06 -08001114 Mutex::Autolock lock(mLocalSyncPointMutex);
1115 mLocalSyncPoints.push_back(point);
1116 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001117}
1118
Mathias Agopian13127d82013-03-05 17:47:11 -08001119void Layer::setFiltering(bool filtering) {
1120 mFiltering = filtering;
1121}
1122
1123bool Layer::getFiltering() const {
1124 return mFiltering;
1125}
1126
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001127// As documented in libhardware header, formats in the range
1128// 0x100 - 0x1FF are specific to the HAL implementation, and
1129// are known to have no alpha channel
1130// TODO: move definition for device-specific range into
1131// hardware.h, instead of using hard-coded values here.
1132#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1133
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001134bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001135 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1136 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001137 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001138 switch (format) {
1139 case HAL_PIXEL_FORMAT_RGBA_8888:
1140 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001141 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001142 }
1143 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001144 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001145}
1146
Mathias Agopian13127d82013-03-05 17:47:11 -08001147// ----------------------------------------------------------------------------
1148// local state
1149// ----------------------------------------------------------------------------
1150
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001151static void boundPoint(vec2* point, const Rect& crop) {
1152 if (point->x < crop.left) {
1153 point->x = crop.left;
1154 }
1155 if (point->x > crop.right) {
1156 point->x = crop.right;
1157 }
1158 if (point->y < crop.top) {
1159 point->y = crop.top;
1160 }
1161 if (point->y > crop.bottom) {
1162 point->y = crop.bottom;
1163 }
1164}
1165
Dan Stozac7014012014-02-14 15:03:43 -08001166void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1167 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001168{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001169 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001170 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001171 const uint32_t hw_h = hw->getHeight();
1172 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001173 if (!s.crop.isEmpty()) {
1174 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001176 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001177 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001178
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001179 vec2 lt = vec2(win.left, win.top);
1180 vec2 lb = vec2(win.left, win.bottom);
1181 vec2 rb = vec2(win.right, win.bottom);
1182 vec2 rt = vec2(win.right, win.top);
1183
1184 if (!useIdentityTransform) {
1185 lt = s.active.transform.transform(lt);
1186 lb = s.active.transform.transform(lb);
1187 rb = s.active.transform.transform(rb);
1188 rt = s.active.transform.transform(rt);
1189 }
1190
Robert Carrb5d3d262016-03-25 15:08:13 -07001191 if (!s.finalCrop.isEmpty()) {
1192 boundPoint(&lt, s.finalCrop);
1193 boundPoint(&lb, s.finalCrop);
1194 boundPoint(&rb, s.finalCrop);
1195 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001196 }
1197
Mathias Agopianff2ed702013-09-01 21:36:12 -07001198 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001199 position[0] = tr.transform(lt);
1200 position[1] = tr.transform(lb);
1201 position[2] = tr.transform(rb);
1202 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001203 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001204 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001205 }
1206}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001207
Andy McFadden4125a4f2014-01-29 17:17:11 -08001208bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001209{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001210 // if we don't have a buffer yet, we're translucent regardless of the
1211 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001212 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001213 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001214 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001215
1216 // if the layer has the opaque flag, then we're always opaque,
1217 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001218 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001219}
1220
Dan Stoza23116082015-06-18 14:58:39 -07001221bool Layer::isSecure() const
1222{
1223 const Layer::State& s(mDrawingState);
1224 return (s.flags & layer_state_t::eLayerSecure);
1225}
1226
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001227bool Layer::isProtected() const
1228{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001229 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001230 return (activeBuffer != 0) &&
1231 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1232}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001233
Mathias Agopian13127d82013-03-05 17:47:11 -08001234bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001235 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236}
1237
1238bool Layer::isCropped() const {
1239 return !mCurrentCrop.isEmpty();
1240}
1241
1242bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1243 return mNeedsFiltering || hw->needsFiltering();
1244}
1245
1246void Layer::setVisibleRegion(const Region& visibleRegion) {
1247 // always called from main thread
1248 this->visibleRegion = visibleRegion;
1249}
1250
1251void Layer::setCoveredRegion(const Region& coveredRegion) {
1252 // always called from main thread
1253 this->coveredRegion = coveredRegion;
1254}
1255
1256void Layer::setVisibleNonTransparentRegion(const Region&
1257 setVisibleNonTransparentRegion) {
1258 // always called from main thread
1259 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1260}
1261
1262// ----------------------------------------------------------------------------
1263// transaction
1264// ----------------------------------------------------------------------------
1265
Dan Stoza7dde5992015-05-22 09:51:44 -07001266void Layer::pushPendingState() {
1267 if (!mCurrentState.modified) {
1268 return;
1269 }
1270
Dan Stoza7dde5992015-05-22 09:51:44 -07001271 // If this transaction is waiting on the receipt of a frame, generate a sync
1272 // point and send it to the remote layer.
1273 if (mCurrentState.handle != nullptr) {
1274 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1275 sp<Layer> handleLayer = handle->owner.promote();
1276 if (handleLayer == nullptr) {
1277 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1278 // If we can't promote the layer we are intended to wait on,
1279 // then it is expired or otherwise invalid. Allow this transaction
1280 // to be applied as per normal (no synchronization).
1281 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001282 } else {
1283 auto syncPoint = std::make_shared<SyncPoint>(
1284 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001285 if (handleLayer->addSyncPoint(syncPoint)) {
1286 mRemoteSyncPoints.push_back(std::move(syncPoint));
1287 } else {
1288 // We already missed the frame we're supposed to synchronize
1289 // on, so go ahead and apply the state update
1290 mCurrentState.handle = nullptr;
1291 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001292 }
1293
Dan Stoza7dde5992015-05-22 09:51:44 -07001294 // Wake us up to check if the frame has been received
1295 setTransactionFlags(eTransactionNeeded);
1296 }
1297 mPendingStates.push_back(mCurrentState);
1298}
1299
Pablo Ceballos05289c22016-04-14 15:49:55 -07001300void Layer::popPendingState(State* stateToCommit) {
1301 auto oldFlags = stateToCommit->flags;
1302 *stateToCommit = mPendingStates[0];
1303 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1304 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001305
1306 mPendingStates.removeAt(0);
1307}
1308
Pablo Ceballos05289c22016-04-14 15:49:55 -07001309bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001310 bool stateUpdateAvailable = false;
1311 while (!mPendingStates.empty()) {
1312 if (mPendingStates[0].handle != nullptr) {
1313 if (mRemoteSyncPoints.empty()) {
1314 // If we don't have a sync point for this, apply it anyway. It
1315 // will be visually wrong, but it should keep us from getting
1316 // into too much trouble.
1317 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001318 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001319 stateUpdateAvailable = true;
1320 continue;
1321 }
1322
Dan Stozacac35382016-01-27 12:21:06 -08001323 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1324 mPendingStates[0].frameNumber) {
1325 ALOGE("[%s] Unexpected sync point frame number found",
1326 mName.string());
1327
1328 // Signal our end of the sync point and then dispose of it
1329 mRemoteSyncPoints.front()->setTransactionApplied();
1330 mRemoteSyncPoints.pop_front();
1331 continue;
1332 }
1333
Dan Stoza7dde5992015-05-22 09:51:44 -07001334 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1335 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001336 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001337 stateUpdateAvailable = true;
1338
1339 // Signal our end of the sync point and then dispose of it
1340 mRemoteSyncPoints.front()->setTransactionApplied();
1341 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001342 } else {
1343 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001344 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001345 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001346 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001347 stateUpdateAvailable = true;
1348 }
1349 }
1350
1351 // If we still have pending updates, wake SurfaceFlinger back up and point
1352 // it at this layer so we can process them
1353 if (!mPendingStates.empty()) {
1354 setTransactionFlags(eTransactionNeeded);
1355 mFlinger->setTransactionFlags(eTraversalNeeded);
1356 }
1357
1358 mCurrentState.modified = false;
1359 return stateUpdateAvailable;
1360}
1361
1362void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001363 auto headFrameNumber = getHeadFrameNumber();
1364 Mutex::Autolock lock(mLocalSyncPointMutex);
1365 for (auto& point : mLocalSyncPoints) {
1366 if (headFrameNumber >= point->getFrameNumber()) {
1367 point->setFrameAvailable();
1368 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001369 }
1370}
1371
Mathias Agopian13127d82013-03-05 17:47:11 -08001372uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001373 ATRACE_CALL();
1374
Dan Stoza7dde5992015-05-22 09:51:44 -07001375 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001376 Layer::State c = getCurrentState();
1377 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001378 return 0;
1379 }
1380
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001381 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001383 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1384 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001385
1386 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001387 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001388 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001389 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001390 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001391 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001392 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001393 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001394 this, getName().string(), mCurrentTransform,
1395 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001396 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001397 c.crop.left,
1398 c.crop.top,
1399 c.crop.right,
1400 c.crop.bottom,
1401 c.crop.getWidth(),
1402 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001403 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001404 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001405 s.crop.left,
1406 s.crop.top,
1407 s.crop.right,
1408 s.crop.bottom,
1409 s.crop.getWidth(),
1410 s.crop.getHeight(),
1411 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001413 // record the new size, form this point on, when the client request
1414 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001415 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001416 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001418
Robert Carr82364e32016-05-15 11:27:47 -07001419 const bool resizePending = (c.requested.w != c.active.w) ||
1420 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001421 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001422 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001423 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001424 // if we have a pending resize, unless we are in fixed-size mode.
1425 // the drawing state will be updated only once we receive a buffer
1426 // with the correct size.
1427 //
1428 // in particular, we want to make sure the clip (which is part
1429 // of the geometry state) is latched together with the size but is
1430 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001431 //
1432 // If a sideband stream is attached, however, we want to skip this
1433 // optimization so that transactions aren't missed when a buffer
1434 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001435
1436 flags |= eDontUpdateGeometryState;
1437 }
1438 }
1439
Mathias Agopian13127d82013-03-05 17:47:11 -08001440 // always set active to requested, unless we're asked not to
1441 // this is used by Layer, which special cases resizes.
1442 if (flags & eDontUpdateGeometryState) {
1443 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001444 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001445 if (mFreezePositionUpdates) {
1446 float tx = c.active.transform.tx();
1447 float ty = c.active.transform.ty();
1448 c.active = c.requested;
1449 c.active.transform.set(tx, ty);
1450 editCurrentState.active = c.active;
1451 } else {
1452 editCurrentState.active = editCurrentState.requested;
1453 c.active = c.requested;
1454 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 }
1456
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001457 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001458 // invalidate and recompute the visible regions if needed
1459 flags |= Layer::eVisibleRegion;
1460 }
1461
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001462 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 // invalidate and recompute the visible regions if needed
1464 flags |= eVisibleRegion;
1465 this->contentDirty = true;
1466
1467 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001468 const uint8_t type = c.active.transform.getType();
1469 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001470 (type >= Transform::SCALE));
1471 }
1472
Dan Stozac8145172016-04-28 16:29:06 -07001473 // If the layer is hidden, signal and clear out all local sync points so
1474 // that transactions for layers depending on this layer's frames becoming
1475 // visible are not blocked
1476 if (c.flags & layer_state_t::eLayerHidden) {
1477 Mutex::Autolock lock(mLocalSyncPointMutex);
1478 for (auto& point : mLocalSyncPoints) {
1479 point->setFrameAvailable();
1480 }
1481 mLocalSyncPoints.clear();
1482 }
1483
Mathias Agopian13127d82013-03-05 17:47:11 -08001484 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001485 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001486 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487}
1488
Pablo Ceballos05289c22016-04-14 15:49:55 -07001489void Layer::commitTransaction(const State& stateToCommit) {
1490 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001491}
1492
Mathias Agopian13127d82013-03-05 17:47:11 -08001493uint32_t Layer::getTransactionFlags(uint32_t flags) {
1494 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1495}
1496
1497uint32_t Layer::setTransactionFlags(uint32_t flags) {
1498 return android_atomic_or(flags, &mTransactionFlags);
1499}
1500
Robert Carr82364e32016-05-15 11:27:47 -07001501bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001502 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001503 return false;
1504 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001505
1506 // We update the requested and active position simultaneously because
1507 // we want to apply the position portion of the transform matrix immediately,
1508 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001509 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001510 if (immediate && !mFreezePositionUpdates) {
1511 mCurrentState.active.transform.set(x, y);
1512 }
1513 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001514
Dan Stoza7dde5992015-05-22 09:51:44 -07001515 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001516 setTransactionFlags(eTransactionNeeded);
1517 return true;
1518}
Robert Carr82364e32016-05-15 11:27:47 -07001519
Mathias Agopian13127d82013-03-05 17:47:11 -08001520bool Layer::setLayer(uint32_t z) {
1521 if (mCurrentState.z == z)
1522 return false;
1523 mCurrentState.sequence++;
1524 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001525 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001526 setTransactionFlags(eTransactionNeeded);
1527 return true;
1528}
1529bool Layer::setSize(uint32_t w, uint32_t h) {
1530 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1531 return false;
1532 mCurrentState.requested.w = w;
1533 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001534 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001535 setTransactionFlags(eTransactionNeeded);
1536 return true;
1537}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001538#ifdef USE_HWC2
1539bool Layer::setAlpha(float alpha) {
1540#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001541bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001542#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001543 if (mCurrentState.alpha == alpha)
1544 return false;
1545 mCurrentState.sequence++;
1546 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001547 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001548 setTransactionFlags(eTransactionNeeded);
1549 return true;
1550}
1551bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1552 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001553 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001554 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001555 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001556 setTransactionFlags(eTransactionNeeded);
1557 return true;
1558}
1559bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001560 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001561 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001562 setTransactionFlags(eTransactionNeeded);
1563 return true;
1564}
1565bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1566 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1567 if (mCurrentState.flags == newFlags)
1568 return false;
1569 mCurrentState.sequence++;
1570 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001571 mCurrentState.mask = mask;
1572 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001573 setTransactionFlags(eTransactionNeeded);
1574 return true;
1575}
1576bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001577 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001578 return false;
1579 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001580 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001581 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001582 setTransactionFlags(eTransactionNeeded);
1583 return true;
1584}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001585bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001586 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001587 return false;
1588 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001589 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001590 mCurrentState.modified = true;
1591 setTransactionFlags(eTransactionNeeded);
1592 return true;
1593}
Mathias Agopian13127d82013-03-05 17:47:11 -08001594
Robert Carrc3574f72016-03-24 12:19:32 -07001595bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1596 if (scalingMode == mOverrideScalingMode)
1597 return false;
1598 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001599 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001600 return true;
1601}
1602
1603uint32_t Layer::getEffectiveScalingMode() const {
1604 if (mOverrideScalingMode >= 0) {
1605 return mOverrideScalingMode;
1606 }
1607 return mCurrentScalingMode;
1608}
1609
Mathias Agopian13127d82013-03-05 17:47:11 -08001610bool Layer::setLayerStack(uint32_t layerStack) {
1611 if (mCurrentState.layerStack == layerStack)
1612 return false;
1613 mCurrentState.sequence++;
1614 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001615 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001616 setTransactionFlags(eTransactionNeeded);
1617 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001618}
1619
Dan Stoza7dde5992015-05-22 09:51:44 -07001620void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1621 uint64_t frameNumber) {
1622 mCurrentState.handle = handle;
1623 mCurrentState.frameNumber = frameNumber;
1624 // We don't set eTransactionNeeded, because just receiving a deferral
1625 // request without any other state updates shouldn't actually induce a delay
1626 mCurrentState.modified = true;
1627 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001628 mCurrentState.handle = nullptr;
1629 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001630 mCurrentState.modified = false;
1631}
1632
Dan Stozaee44edd2015-03-23 15:50:23 -07001633void Layer::useSurfaceDamage() {
1634 if (mFlinger->mForceFullDamage) {
1635 surfaceDamageRegion = Region::INVALID_REGION;
1636 } else {
1637 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1638 }
1639}
1640
1641void Layer::useEmptyDamage() {
1642 surfaceDamageRegion.clear();
1643}
1644
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645// ----------------------------------------------------------------------------
1646// pageflip handling...
1647// ----------------------------------------------------------------------------
1648
Dan Stoza6b9454d2014-11-07 16:00:59 -08001649bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001650 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001651 return true;
1652 }
1653
Dan Stoza6b9454d2014-11-07 16:00:59 -08001654 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001655 if (mQueueItems.empty()) {
1656 return false;
1657 }
1658 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001659 nsecs_t expectedPresent =
1660 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001661
1662 // Ignore timestamps more than a second in the future
1663 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1664 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1665 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1666 expectedPresent);
1667
1668 bool isDue = timestamp < expectedPresent;
1669 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001670}
1671
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001672bool Layer::onPreComposition() {
1673 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001674 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001675}
1676
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001677void Layer::onPostComposition() {
1678 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001679 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001680 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1681
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001682 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001683 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001684 mFrameTracker.setFrameReadyFence(frameReadyFence);
1685 } else {
1686 // There was no fence for this frame, so assume that it was ready
1687 // to be presented at the desired present time.
1688 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1689 }
1690
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001691 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001692#ifdef USE_HWC2
1693 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1694#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001695 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001696#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001697 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001698 mFrameTracker.setActualPresentFence(presentFence);
1699 } else {
1700 // The HWC doesn't support present fences, so use the refresh
1701 // timestamp instead.
1702 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1703 mFrameTracker.setActualPresentTime(presentTime);
1704 }
1705
1706 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001707 mFrameLatencyNeeded = false;
1708 }
1709}
1710
Dan Stoza9e56aa02015-11-02 13:00:03 -08001711#ifdef USE_HWC2
1712void Layer::releasePendingBuffer() {
1713 mSurfaceFlingerConsumer->releasePendingBuffer();
1714}
1715#endif
1716
Mathias Agopianda27af92012-09-13 18:17:13 -07001717bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001718 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001719#ifdef USE_HWC2
1720 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1721 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1722#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001723 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001724 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001725#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001726}
1727
Mathias Agopian4fec8732012-06-29 14:12:52 -07001728Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001730 ATRACE_CALL();
1731
Jesse Hall399184a2014-03-03 15:42:54 -08001732 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1733 // mSidebandStreamChanged was true
1734 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001735 if (mSidebandStream != NULL) {
1736 setTransactionFlags(eTransactionNeeded);
1737 mFlinger->setTransactionFlags(eTraversalNeeded);
1738 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001739 recomputeVisibleRegions = true;
1740
1741 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001742 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001743 }
1744
Mathias Agopian4fec8732012-06-29 14:12:52 -07001745 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001746 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001747
1748 // if we've already called updateTexImage() without going through
1749 // a composition step, we have to skip this layer at this point
1750 // because we cannot call updateTeximage() without a corresponding
1751 // compositionComplete() call.
1752 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001753 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001754 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001755 }
1756
Jamie Gennis351a5132011-09-14 18:23:37 -07001757 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001758 const State& s(getDrawingState());
1759 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001760 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001761
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001762 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001763 Layer::State& front;
1764 Layer::State& current;
1765 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001766 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001767 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001768 int32_t overrideScalingMode;
Robert Carrb5d3d262016-03-25 15:08:13 -07001769
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001770 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001771 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001772 const char* name,
1773 int32_t overrideScalingMode)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001774 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001775 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001776 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001777 name(name),
1778 overrideScalingMode(overrideScalingMode) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001779 }
1780
1781 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001782 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001783 if (buf == NULL) {
1784 return false;
1785 }
1786
1787 uint32_t bufWidth = buf->getWidth();
1788 uint32_t bufHeight = buf->getHeight();
1789
1790 // check that we received a buffer of the right size
1791 // (Take the buffer's orientation into account)
1792 if (item.mTransform & Transform::ROT_90) {
1793 swap(bufWidth, bufHeight);
1794 }
1795
Robert Carrc3574f72016-03-24 12:19:32 -07001796 int actualScalingMode = overrideScalingMode >= 0 ?
1797 overrideScalingMode : item.mScalingMode;
1798 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001799 if (front.active != front.requested) {
1800
1801 if (isFixedSize ||
1802 (bufWidth == front.requested.w &&
1803 bufHeight == front.requested.h))
1804 {
1805 // Here we pretend the transaction happened by updating the
1806 // current and drawing states. Drawing state is only accessed
1807 // in this thread, no need to have it locked
1808 front.active = front.requested;
1809
1810 // We also need to update the current state so that
1811 // we don't end-up overwriting the drawing state with
1812 // this stale current state during the next transaction
1813 //
1814 // NOTE: We don't need to hold the transaction lock here
1815 // because State::active is only accessed from this thread.
1816 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001817 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001818
1819 // recompute visible region
1820 recomputeVisibleRegions = true;
1821 }
1822
1823 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001824 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001825 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001826 " requested={ wh={%4u,%4u} }}\n",
1827 name,
Andy McFadden69052052012-09-14 16:10:11 -07001828 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001829 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001830 front.crop.left,
1831 front.crop.top,
1832 front.crop.right,
1833 front.crop.bottom,
1834 front.crop.getWidth(),
1835 front.crop.getHeight(),
1836 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001837 }
1838
Ruben Brunk1681d952014-06-27 15:51:55 -07001839 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001840 if (front.active.w != bufWidth ||
1841 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001842 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001843 ALOGE("[%s] rejecting buffer: "
1844 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1845 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001846 return true;
1847 }
1848 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001849
1850 // if the transparent region has changed (this test is
1851 // conservative, but that's fine, worst case we're doing
1852 // a bit of extra work), we latch the new one and we
1853 // trigger a visible-region recompute.
1854 if (!front.activeTransparentRegion.isTriviallyEqual(
1855 front.requestedTransparentRegion)) {
1856 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001857
1858 // We also need to update the current state so that
1859 // we don't end-up overwriting the drawing state with
1860 // this stale current state during the next transaction
1861 //
1862 // NOTE: We don't need to hold the transaction lock here
1863 // because State::active is only accessed from this thread.
1864 current.activeTransparentRegion = front.activeTransparentRegion;
1865
1866 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001867 recomputeVisibleRegions = true;
1868 }
1869
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001870 return false;
1871 }
1872 };
1873
Ruben Brunk1681d952014-06-27 15:51:55 -07001874 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001875 getProducerStickyTransform() != 0, mName.string(),
1876 mOverrideScalingMode);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001877
Dan Stozacac35382016-01-27 12:21:06 -08001878
1879 // Check all of our local sync points to ensure that all transactions
1880 // which need to have been applied prior to the frame which is about to
1881 // be latched have signaled
1882
1883 auto headFrameNumber = getHeadFrameNumber();
1884 bool matchingFramesFound = false;
1885 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001886 {
Dan Stozacac35382016-01-27 12:21:06 -08001887 Mutex::Autolock lock(mLocalSyncPointMutex);
1888 for (auto& point : mLocalSyncPoints) {
1889 if (point->getFrameNumber() > headFrameNumber) {
1890 break;
1891 }
1892
1893 matchingFramesFound = true;
1894
1895 if (!point->frameIsAvailable()) {
1896 // We haven't notified the remote layer that the frame for
1897 // this point is available yet. Notify it now, and then
1898 // abort this attempt to latch.
1899 point->setFrameAvailable();
1900 allTransactionsApplied = false;
1901 break;
1902 }
1903
1904 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001905 }
1906 }
1907
Dan Stozacac35382016-01-27 12:21:06 -08001908 if (matchingFramesFound && !allTransactionsApplied) {
1909 mFlinger->signalLayerUpdate();
1910 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001911 }
1912
Pablo Ceballos06312182015-10-07 16:32:12 -07001913 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1914 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001915 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001916 // buffer mode.
1917 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001918 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001919 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001920 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001921 if (updateResult == BufferQueue::PRESENT_LATER) {
1922 // Producer doesn't want buffer to be displayed yet. Signal a
1923 // layer update so we check again at the next opportunity.
1924 mFlinger->signalLayerUpdate();
1925 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001926 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1927 // If the buffer has been rejected, remove it from the shadow queue
1928 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001929 if (queuedBuffer) {
1930 Mutex::Autolock lock(mQueueItemLock);
1931 mQueueItems.removeAt(0);
1932 android_atomic_dec(&mQueuedFrames);
1933 }
Dan Stozaecc50402015-04-28 14:42:06 -07001934 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001935 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1936 // This can occur if something goes wrong when trying to create the
1937 // EGLImage for this buffer. If this happens, the buffer has already
1938 // been released, so we need to clean up the queue and bug out
1939 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001940 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001941 Mutex::Autolock lock(mQueueItemLock);
1942 mQueueItems.clear();
1943 android_atomic_and(0, &mQueuedFrames);
1944 }
1945
1946 // Once we have hit this state, the shadow queue may no longer
1947 // correctly reflect the incoming BufferQueue's contents, so even if
1948 // updateTexImage starts working, the only safe course of action is
1949 // to continue to ignore updates.
1950 mUpdateTexImageFailed = true;
1951
1952 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001953 }
1954
Pablo Ceballos06312182015-10-07 16:32:12 -07001955 if (queuedBuffer) {
1956 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001957 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1958
Dan Stoza6b9454d2014-11-07 16:00:59 -08001959 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001960
1961 // Remove any stale buffers that have been dropped during
1962 // updateTexImage
1963 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1964 mQueueItems.removeAt(0);
1965 android_atomic_dec(&mQueuedFrames);
1966 }
1967
Dan Stoza6b9454d2014-11-07 16:00:59 -08001968 mQueueItems.removeAt(0);
1969 }
1970
Dan Stozaecc50402015-04-28 14:42:06 -07001971
Andy McFadden1585c4d2013-06-28 13:52:40 -07001972 // Decrement the queued-frames count. Signal another event if we
1973 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001974 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001975 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001976 mFlinger->signalLayerUpdate();
1977 }
1978
1979 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001980 // something happened!
1981 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001982 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001983 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001984
Jamie Gennis351a5132011-09-14 18:23:37 -07001985 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001986 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001987 if (mActiveBuffer == NULL) {
1988 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001989 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001990 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001991
Mathias Agopian4824d402012-06-04 18:16:30 -07001992 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001993 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001994 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001995 // the first time we receive a buffer, we need to trigger a
1996 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001997 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001998 }
1999
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002000 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2001 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2002 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002003 if ((crop != mCurrentCrop) ||
2004 (transform != mCurrentTransform) ||
2005 (scalingMode != mCurrentScalingMode))
2006 {
2007 mCurrentCrop = crop;
2008 mCurrentTransform = transform;
2009 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002010 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002011 }
2012
2013 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002014 uint32_t bufWidth = mActiveBuffer->getWidth();
2015 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002016 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2017 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002018 recomputeVisibleRegions = true;
Robert Carr82364e32016-05-15 11:27:47 -07002019 mFreezePositionUpdates = false;
Mathias Agopian702634a2012-05-23 17:50:31 -07002020 }
2021 }
2022
2023 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002024 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002025 recomputeVisibleRegions = true;
2026 }
2027
Dan Stozacac35382016-01-27 12:21:06 -08002028 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2029
2030 // Remove any sync points corresponding to the buffer which was just
2031 // latched
2032 {
2033 Mutex::Autolock lock(mLocalSyncPointMutex);
2034 auto point = mLocalSyncPoints.begin();
2035 while (point != mLocalSyncPoints.end()) {
2036 if (!(*point)->frameIsAvailable() ||
2037 !(*point)->transactionIsApplied()) {
2038 // This sync point must have been added since we started
2039 // latching. Don't drop it yet.
2040 ++point;
2041 continue;
2042 }
2043
2044 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2045 point = mLocalSyncPoints.erase(point);
2046 } else {
2047 ++point;
2048 }
2049 }
2050 }
2051
Mathias Agopian4fec8732012-06-29 14:12:52 -07002052 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002053 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002054
2055 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002056 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002057 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002058 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002059}
2060
Mathias Agopiana67932f2011-04-20 14:20:59 -07002061uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002062{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002063 // TODO: should we do something special if mSecure is set?
2064 if (mProtectedByApp) {
2065 // need a hardware-protected path to external video sink
2066 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002067 }
Riley Andrews03414a12014-07-01 14:22:59 -07002068 if (mPotentialCursor) {
2069 usage |= GraphicBuffer::USAGE_CURSOR;
2070 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002071 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002072 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002073}
2074
Mathias Agopian84300952012-11-21 16:02:13 -08002075void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002076 uint32_t orientation = 0;
2077 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002078 // The transform hint is used to improve performance, but we can
2079 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002080 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002081 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002082 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002083 if (orientation & Transform::ROT_INVALID) {
2084 orientation = 0;
2085 }
2086 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002087 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002088}
2089
Mathias Agopian13127d82013-03-05 17:47:11 -08002090// ----------------------------------------------------------------------------
2091// debugging
2092// ----------------------------------------------------------------------------
2093
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002094void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002095{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002096 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002097
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002098 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002099 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002100 "+ %s %p (%s)\n",
2101 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002102 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002103
Mathias Agopian2ca79392013-04-02 18:30:32 -07002104 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002105 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002106 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002107 sp<Client> client(mClientRef.promote());
2108
Mathias Agopian74d211a2013-04-22 16:55:35 +02002109 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002110 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2111 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002112 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002113#ifdef USE_HWC2
2114 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2115#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002116 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002117#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002118 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002119 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 -07002120 s.crop.left, s.crop.top,
2121 s.crop.right, s.crop.bottom,
2122 s.finalCrop.left, s.finalCrop.top,
2123 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002124 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002125 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002126 s.active.transform[0][0], s.active.transform[0][1],
2127 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002128 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002129
2130 sp<const GraphicBuffer> buf0(mActiveBuffer);
2131 uint32_t w0=0, h0=0, s0=0, f0=0;
2132 if (buf0 != 0) {
2133 w0 = buf0->getWidth();
2134 h0 = buf0->getHeight();
2135 s0 = buf0->getStride();
2136 f0 = buf0->format;
2137 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002138 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002139 " "
2140 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2141 " queued-frames=%d, mRefreshPending=%d\n",
2142 mFormat, w0, h0, s0,f0,
2143 mQueuedFrames, mRefreshPending);
2144
Mathias Agopian13127d82013-03-05 17:47:11 -08002145 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002146 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002147 }
2148}
2149
Svetoslavd85084b2014-03-20 10:28:31 -07002150void Layer::dumpFrameStats(String8& result) const {
2151 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002152}
2153
Svetoslavd85084b2014-03-20 10:28:31 -07002154void Layer::clearFrameStats() {
2155 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002156}
2157
Jamie Gennis6547ff42013-07-16 20:12:42 -07002158void Layer::logFrameStats() {
2159 mFrameTracker.logAndResetStats(mName);
2160}
2161
Svetoslavd85084b2014-03-20 10:28:31 -07002162void Layer::getFrameStats(FrameStats* outStats) const {
2163 mFrameTracker.getStats(outStats);
2164}
2165
Pablo Ceballos40845df2016-01-25 17:41:15 -08002166void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2167 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2168 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2169 *outName = mName;
2170 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2171
2172#ifdef USE_HWC2
2173 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2174 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2175 HWC2::Composition::Client : true;
2176#else
2177 *outIsGlesComposition = mIsGlesComposition;
2178#endif
2179 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2180 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2181 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2182}
Mathias Agopian13127d82013-03-05 17:47:11 -08002183// ---------------------------------------------------------------------------
2184
2185Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2186 const sp<Layer>& layer)
2187 : mFlinger(flinger), mLayer(layer) {
2188}
2189
2190Layer::LayerCleaner::~LayerCleaner() {
2191 // destroy client resources
2192 mFlinger->onLayerDestroyed(mLayer);
2193}
2194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002196}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002197
2198#if defined(__gl_h_)
2199#error "don't include gl/gl.h in this file"
2200#endif
2201
2202#if defined(__gl2_h_)
2203#error "don't include gl2/gl2.h in this file"
2204#endif