blob: 548b048e29a1017a206c00b6df4b38ac77f74dd0 [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);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800158 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName,
159 this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800160 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800161 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700162 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800163
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700164#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
165#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700166#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700167 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800168#endif
Andy McFadden69052052012-09-14 16:10:11 -0700169
Mathias Agopian84300952012-11-21 16:02:13 -0800170 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
171 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700172}
173
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700174Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800175 sp<Client> c(mClientRef.promote());
176 if (c != 0) {
177 c->detachLayer(this);
178 }
179
Dan Stozacac35382016-01-27 12:21:06 -0800180 for (auto& point : mRemoteSyncPoints) {
181 point->setTransactionApplied();
182 }
Dan Stozac8145172016-04-28 16:29:06 -0700183 for (auto& point : mLocalSyncPoints) {
184 point->setFrameAvailable();
185 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700186 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700187 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700188}
189
Mathias Agopian13127d82013-03-05 17:47:11 -0800190// ---------------------------------------------------------------------------
191// callbacks
192// ---------------------------------------------------------------------------
193
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194#ifdef USE_HWC2
195void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
196 if (mHwcLayers.empty()) {
197 return;
198 }
199 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
200}
201#else
Dan Stozac7014012014-02-14 15:03:43 -0800202void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800203 HWComposer::HWCLayerInterface* layer) {
204 if (layer) {
205 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700206 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 }
208}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800209#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800210
Dan Stoza6b9454d2014-11-07 16:00:59 -0800211void Layer::onFrameAvailable(const BufferItem& item) {
212 // Add this buffer from our internal queue tracker
213 { // Autolock scope
214 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700215
216 // Reset the frame number tracker when we receive the first buffer after
217 // a frame number reset
218 if (item.mFrameNumber == 1) {
219 mLastFrameNumberReceived = 0;
220 }
221
222 // Ensure that callbacks are handled in order
223 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
224 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
225 ms2ns(500));
226 if (result != NO_ERROR) {
227 ALOGE("[%s] Timed out waiting on callback", mName.string());
228 }
229 }
230
Dan Stoza6b9454d2014-11-07 16:00:59 -0800231 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700232 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700233
234 // Wake up any pending callbacks
235 mLastFrameNumberReceived = item.mFrameNumber;
236 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800237 }
238
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800239 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700240}
241
Dan Stoza6b9454d2014-11-07 16:00:59 -0800242void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700243 { // Autolock scope
244 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700245
Dan Stoza7dde5992015-05-22 09:51:44 -0700246 // Ensure that callbacks are handled in order
247 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
248 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
249 ms2ns(500));
250 if (result != NO_ERROR) {
251 ALOGE("[%s] Timed out waiting on callback", mName.string());
252 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700253 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700254
255 if (mQueueItems.empty()) {
256 ALOGE("Can't replace a frame on an empty queue");
257 return;
258 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700259 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700260
261 // Wake up any pending callbacks
262 mLastFrameNumberReceived = item.mFrameNumber;
263 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700264 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800265}
266
Jesse Hall399184a2014-03-03 15:42:54 -0800267void Layer::onSidebandStreamChanged() {
268 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
269 // mSidebandStreamChanged was false
270 mFlinger->signalLayerUpdate();
271 }
272}
273
Mathias Agopian67106042013-03-14 19:18:13 -0700274// called with SurfaceFlinger::mStateLock from the drawing thread after
275// the layer has been remove from the current state list (and just before
276// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800277void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800278 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700279}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280
Mathias Agopian13127d82013-03-05 17:47:11 -0800281// ---------------------------------------------------------------------------
282// set-up
283// ---------------------------------------------------------------------------
284
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700285const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800286 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287}
288
Mathias Agopianf9d93272009-06-19 17:00:27 -0700289status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 PixelFormat format, uint32_t flags)
291{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700292 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700294
295 // never allow a surface larger than what our underlying GL implementation
296 // can handle.
297 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800298 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700299 return BAD_VALUE;
300 }
301
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700303
Riley Andrews03414a12014-07-01 14:22:59 -0700304 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700305 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700306 mCurrentOpacity = getOpacityForFormat(format);
307
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800308 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
309 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
310 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
Dan Stoza7dde5992015-05-22 09:51:44 -0700315/*
316 * The layer handle is just a BBinder object passed to the client
317 * (remote process) -- we don't keep any reference on our side such that
318 * the dtor is called when the remote side let go of its reference.
319 *
320 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
321 * this layer when the handle is destroyed.
322 */
323class Layer::Handle : public BBinder, public LayerCleaner {
324 public:
325 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
326 : LayerCleaner(flinger, layer), owner(layer) {}
327
328 wp<Layer> owner;
329};
330
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700331sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800332 Mutex::Autolock _l(mLock);
333
334 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700335 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800336
337 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700338
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700339 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800340}
341
Dan Stozab9b08832014-03-13 11:55:57 -0700342sp<IGraphicBufferProducer> Layer::getProducer() const {
343 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700344}
345
Mathias Agopian13127d82013-03-05 17:47:11 -0800346// ---------------------------------------------------------------------------
347// h/w composer set-up
348// ---------------------------------------------------------------------------
349
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800350Rect Layer::getContentCrop() const {
351 // this is the crop rectangle that applies to the buffer
352 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700353 Rect crop;
354 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800355 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700356 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800357 } else if (mActiveBuffer != NULL) {
358 // otherwise we use the whole buffer
359 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700360 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800361 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700362 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700363 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700364 return crop;
365}
366
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700367static Rect reduce(const Rect& win, const Region& exclude) {
368 if (CC_LIKELY(exclude.isEmpty())) {
369 return win;
370 }
371 if (exclude.isRect()) {
372 return win.reduce(exclude.getBounds());
373 }
374 return Region(win).subtract(exclude).getBounds();
375}
376
Mathias Agopian13127d82013-03-05 17:47:11 -0800377Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700378 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700379 return computeBounds(s.activeTransparentRegion);
380}
381
382Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
383 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800384 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700385
386 if (!s.crop.isEmpty()) {
387 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800388 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700389 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700390 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800391}
392
Mathias Agopian6b442672013-07-09 21:24:52 -0700393FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800394 // the content crop is the area of the content that gets scaled to the
395 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700396 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800397
Robert Carrb5d3d262016-03-25 15:08:13 -0700398 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800399 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700400 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800401
402 // apply the projection's clipping to the window crop in
403 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700404 // if there are no window scaling involved, this operation will map to full
405 // pixels in the buffer.
406 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
407 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700408
409 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700410 if (!s.crop.isEmpty()) {
411 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700412 }
413
Robert Carr3dcabfa2016-03-01 18:36:58 -0800414 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
416 activeCrop.clear();
417 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700418 if (!s.finalCrop.isEmpty()) {
419 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 activeCrop.clear();
421 }
422 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800423 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800424
Michael Lentine28ea2172014-11-19 18:32:37 -0800425 // This needs to be here as transform.transform(Rect) computes the
426 // transformed rect and then takes the bounding box of the result before
427 // returning. This means
428 // transform.inverse().transform(transform.transform(Rect)) != Rect
429 // in which case we need to make sure the final rect is clipped to the
430 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
432 activeCrop.clear();
433 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800434
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700435 // subtract the transparent region and snap to the bounds
436 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
437
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // Transform the window crop to match the buffer coordinate system,
439 // which means using the inverse of the current transform set on the
440 // SurfaceFlingerConsumer.
441 uint32_t invTransform = mCurrentTransform;
442 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
443 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700444 * the code below applies the primary display's inverse transform to the
445 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700447 uint32_t invTransformOrient =
448 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 // calculate the inverse transform
450 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
451 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
452 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800453 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700455 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
456 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800457 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458
459 int winWidth = s.active.w;
460 int winHeight = s.active.h;
461 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
462 // If the activeCrop has been rotate the ends are rotated but not
463 // the space itself so when transforming ends back we can't rely on
464 // a modification of the axes of rotation. To account for this we
465 // need to reorient the inverse rotation in terms of the current
466 // axes of rotation.
467 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
468 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
469 if (is_h_flipped == is_v_flipped) {
470 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
471 NATIVE_WINDOW_TRANSFORM_FLIP_H;
472 }
473 winWidth = s.active.h;
474 winHeight = s.active.w;
475 }
476 const Rect winCrop = activeCrop.transform(
477 invTransform, s.active.w, s.active.h);
478
479 // below, crop is intersected with winCrop expressed in crop's coordinate space
480 float xScale = crop.getWidth() / float(winWidth);
481 float yScale = crop.getHeight() / float(winHeight);
482
483 float insetL = winCrop.left * xScale;
484 float insetT = winCrop.top * yScale;
485 float insetR = (winWidth - winCrop.right ) * xScale;
486 float insetB = (winHeight - winCrop.bottom) * yScale;
487
488 crop.left += insetL;
489 crop.top += insetT;
490 crop.right -= insetR;
491 crop.bottom -= insetB;
492
Mathias Agopian13127d82013-03-05 17:47:11 -0800493 return crop;
494}
495
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496#ifdef USE_HWC2
497void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
498#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700499void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700500 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700501 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700503{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504#ifdef USE_HWC2
505 const auto hwcId = displayDevice->getHwcDisplayId();
506 auto& hwcInfo = mHwcLayers[hwcId];
507#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800508 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700510
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700511 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512#ifdef USE_HWC2
513 hwcInfo.forceClientComposition = false;
514
515 if (isSecure() && !displayDevice->isSecure()) {
516 hwcInfo.forceClientComposition = true;
517 }
518
519 auto& hwcLayer = hwcInfo.layer;
520#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700521 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700522
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700523 if (isSecure() && !hw->isSecure()) {
524 layer.setSkip(true);
525 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800526#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700527
Mathias Agopian13127d82013-03-05 17:47:11 -0800528 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700529 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530#ifdef USE_HWC2
531 if (!isOpaque(s) || s.alpha != 1.0f) {
532 auto blendMode = mPremultipliedAlpha ?
533 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
534 auto error = hwcLayer->setBlendMode(blendMode);
535 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
536 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
537 to_string(error).c_str(), static_cast<int32_t>(error));
538 }
539#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800540 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800541 layer.setBlending(mPremultipliedAlpha ?
542 HWC_BLENDING_PREMULT :
543 HWC_BLENDING_COVERAGE);
544 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800546
547 // apply the layer's transform, followed by the display's global transform
548 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700549 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700550 if (!s.crop.isEmpty()) {
551 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800552 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000554 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000558 activeCrop.clear();
559 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800560 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800561 // This needs to be here as transform.transform(Rect) computes the
562 // transformed rect and then takes the bounding box of the result before
563 // returning. This means
564 // transform.inverse().transform(transform.transform(Rect)) != Rect
565 // in which case we need to make sure the final rect is clipped to the
566 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000567 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
568 activeCrop.clear();
569 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700570 // mark regions outside the crop as transparent
571 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
572 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
573 s.active.w, s.active.h));
574 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
575 activeCrop.left, activeCrop.bottom));
576 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
577 s.active.w, activeCrop.bottom));
578 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800579 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700580 if (!s.finalCrop.isEmpty()) {
581 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000582 frame.clear();
583 }
584 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000586 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
587 frame.clear();
588 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 const Transform& tr(displayDevice->getTransform());
590 Rect transformedFrame = tr.transform(frame);
591 auto error = hwcLayer->setDisplayFrame(transformedFrame);
592 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
593 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
594 transformedFrame.top, transformedFrame.right,
595 transformedFrame.bottom, to_string(error).c_str(),
596 static_cast<int32_t>(error));
597
598 FloatRect sourceCrop = computeCrop(displayDevice);
599 error = hwcLayer->setSourceCrop(sourceCrop);
600 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
601 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
602 sourceCrop.left, sourceCrop.top, sourceCrop.right,
603 sourceCrop.bottom, to_string(error).c_str(),
604 static_cast<int32_t>(error));
605
606 error = hwcLayer->setPlaneAlpha(s.alpha);
607 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
608 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
609 static_cast<int32_t>(error));
610
611 error = hwcLayer->setZOrder(s.z);
612 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
613 mName.string(), s.z, to_string(error).c_str(),
614 static_cast<int32_t>(error));
615#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000616 if (!frame.intersect(hw->getViewport(), &frame)) {
617 frame.clear();
618 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800619 const Transform& tr(hw->getTransform());
620 layer.setFrame(tr.transform(frame));
621 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800622 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800624
Mathias Agopian29a367b2011-07-12 14:51:45 -0700625 /*
626 * Transformations are applied in this order:
627 * 1) buffer orientation/flip/mirror
628 * 2) state transformation (window manager)
629 * 3) layer orientation (screen orientation)
630 * (NOTE: the matrices are multiplied in reverse order)
631 */
632
633 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800634 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635
636 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
637 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700638 * the code below applies the primary display's inverse transform to the
639 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700641 uint32_t invTransform =
642 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700643 // calculate the inverse transform
644 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
645 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
646 NATIVE_WINDOW_TRANSFORM_FLIP_H;
647 }
648 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700649 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700651
652 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800653 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654#ifdef USE_HWC2
655 if (orientation & Transform::ROT_INVALID) {
656 // we can only handle simple transformation
657 hwcInfo.forceClientComposition = true;
658 } else {
659 auto transform = static_cast<HWC2::Transform>(orientation);
660 auto error = hwcLayer->setTransform(transform);
661 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
662 "%s (%d)", mName.string(), to_string(transform).c_str(),
663 to_string(error).c_str(), static_cast<int32_t>(error));
664 }
665#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800666 if (orientation & Transform::ROT_INVALID) {
667 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700668 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700669 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800670 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700671 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700673}
674
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675#ifdef USE_HWC2
676void Layer::forceClientComposition(int32_t hwcId) {
677 if (mHwcLayers.count(hwcId) == 0) {
678 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
679 return;
680 }
681
682 mHwcLayers[hwcId].forceClientComposition = true;
683}
684#endif
685
686#ifdef USE_HWC2
687void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
688 // Apply this display's projection's viewport to the visible region
689 // before giving it to the HWC HAL.
690 const Transform& tr = displayDevice->getTransform();
691 const auto& viewport = displayDevice->getViewport();
692 Region visible = tr.transform(visibleRegion.intersect(viewport));
693 auto hwcId = displayDevice->getHwcDisplayId();
694 auto& hwcLayer = mHwcLayers[hwcId].layer;
695 auto error = hwcLayer->setVisibleRegion(visible);
696 if (error != HWC2::Error::None) {
697 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
698 to_string(error).c_str(), static_cast<int32_t>(error));
699 visible.dump(LOG_TAG);
700 }
701
702 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
703 if (error != HWC2::Error::None) {
704 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
705 to_string(error).c_str(), static_cast<int32_t>(error));
706 surfaceDamageRegion.dump(LOG_TAG);
707 }
708
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700709 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700711 setCompositionType(hwcId, HWC2::Composition::Sideband);
712 ALOGV("[%s] Requesting Sideband composition", mName.string());
713 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714 if (error != HWC2::Error::None) {
715 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
716 mName.string(), mSidebandStream->handle(),
717 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700719 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 }
721
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700722 // Client or SolidColor layers
723 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr ||
724 mHwcLayers[hwcId].forceClientComposition) {
725 // TODO: This also includes solid color layers, but no API exists to
726 // setup a solid color layer yet
727 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700729 return;
730 }
731
732 // Device or Cursor layers
733 if (mPotentialCursor) {
734 ALOGV("[%s] Requesting Cursor composition", mName.string());
735 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736 } else {
737 ALOGV("[%s] Requesting Device composition", mName.string());
738 setCompositionType(hwcId, HWC2::Composition::Device);
739 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700740
741 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
742 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
743 if (error != HWC2::Error::None) {
744 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
745 mActiveBuffer->handle, to_string(error).c_str(),
746 static_cast<int32_t>(error));
747 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748}
749#else
Mathias Agopian42977342012-08-05 00:40:46 -0700750void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700751 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800752 // we have to set the visible region on every frame because
753 // we currently free it during onLayerDisplayed(), which is called
754 // after HWComposer::commit() -- every frame.
755 // Apply this display's projection's viewport to the visible region
756 // before giving it to the HWC HAL.
757 const Transform& tr = hw->getTransform();
758 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
759 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700760 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800761 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700762
Jesse Hall399184a2014-03-03 15:42:54 -0800763 if (mSidebandStream.get()) {
764 layer.setSidebandStream(mSidebandStream);
765 } else {
766 // NOTE: buffer can be NULL if the client never drew into this
767 // layer yet, or if we ran out of memory
768 layer.setBuffer(mActiveBuffer);
769 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700770}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700772
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773#ifdef USE_HWC2
774void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
775 auto hwcId = displayDevice->getHwcDisplayId();
776 if (mHwcLayers.count(hwcId) == 0 ||
777 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
778 return;
779 }
780
781 // This gives us only the "orientation" component of the transform
782 const State& s(getCurrentState());
783
784 // Apply the layer's transform, followed by the display's global transform
785 // Here we're guaranteed that the layer's transform preserves rects
786 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700787 if (!s.crop.isEmpty()) {
788 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789 }
790 // Subtract the transparent region and snap to the bounds
791 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800792 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700794 if (!s.finalCrop.isEmpty()) {
795 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000796 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797 auto& displayTransform(displayDevice->getTransform());
798 auto position = displayTransform.transform(frame);
799
800 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
801 position.top);
802 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
803 "to (%d, %d): %s (%d)", mName.string(), position.left,
804 position.top, to_string(error).c_str(),
805 static_cast<int32_t>(error));
806}
807#else
Dan Stozac7014012014-02-14 15:03:43 -0800808void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700809 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700810 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700811
812 // TODO: there is a possible optimization here: we only need to set the
813 // acquire fence the first time a new buffer is acquired on EACH display.
814
Riley Andrews03414a12014-07-01 14:22:59 -0700815 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800816 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800817 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700818 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700819 if (fenceFd == -1) {
820 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
821 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700822 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700823 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700824 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700825}
826
Riley Andrews03414a12014-07-01 14:22:59 -0700827Rect Layer::getPosition(
828 const sp<const DisplayDevice>& hw)
829{
830 // this gives us only the "orientation" component of the transform
831 const State& s(getCurrentState());
832
833 // apply the layer's transform, followed by the display's global transform
834 // here we're guaranteed that the layer's transform preserves rects
835 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700836 if (!s.crop.isEmpty()) {
837 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700838 }
839 // subtract the transparent region and snap to the bounds
840 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800841 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700842 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700843 if (!s.finalCrop.isEmpty()) {
844 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000845 }
Riley Andrews03414a12014-07-01 14:22:59 -0700846 const Transform& tr(hw->getTransform());
847 return Rect(tr.transform(frame));
848}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800849#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700850
Mathias Agopian13127d82013-03-05 17:47:11 -0800851// ---------------------------------------------------------------------------
852// drawing...
853// ---------------------------------------------------------------------------
854
855void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800856 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800857}
858
Dan Stozac7014012014-02-14 15:03:43 -0800859void Layer::draw(const sp<const DisplayDevice>& hw,
860 bool useIdentityTransform) const {
861 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800862}
863
Dan Stozac7014012014-02-14 15:03:43 -0800864void Layer::draw(const sp<const DisplayDevice>& hw) const {
865 onDraw(hw, Region(hw->bounds()), false);
866}
867
868void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
869 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800871 ATRACE_CALL();
872
Mathias Agopiana67932f2011-04-20 14:20:59 -0700873 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700875 // in fact never been drawn into. This happens frequently with
876 // SurfaceView because the WindowManager can't know when the client
877 // has drawn the first time.
878
879 // If there is nothing under us, we paint the screen in black, otherwise
880 // we just skip this update.
881
882 // figure out if there is something below us
883 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700884 const SurfaceFlinger::LayerVector& drawingLayers(
885 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700886 const size_t count = drawingLayers.size();
887 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 const sp<Layer>& layer(drawingLayers[i]);
889 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700890 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700891 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700892 }
893 // if not everything below us is covered, we plug the holes!
894 Region holes(clip.subtract(under));
895 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700896 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700897 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 return;
899 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700900
Andy McFadden97eba892012-12-11 15:21:45 -0800901 // Bind the current buffer to the GL texture, and wait for it to be
902 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800903 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
904 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800905 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700906 // Go ahead and draw the buffer anyway; no matter what we do the screen
907 // is probably going to have something visibly wrong.
908 }
909
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700910 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
911
Mathias Agopian875d8e12013-06-07 15:35:48 -0700912 RenderEngine& engine(mFlinger->getRenderEngine());
913
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700914 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700915 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700916 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700917
918 // Query the texture matrix given our current filtering mode.
919 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800920 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
921 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700922
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700923 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
924
925 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700926 * the code below applies the primary display's inverse transform to
927 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700928 */
929
930 // create a 4x4 transform matrix from the display transform flags
931 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
932 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
933 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
934
935 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700936 uint32_t transform =
937 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700938 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
939 tr = tr * rot90;
940 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
941 tr = tr * flipH;
942 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
943 tr = tr * flipV;
944
945 // calculate the inverse
946 tr = inverse(tr);
947
948 // and finally apply it to the original texture matrix
949 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
950 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
951 }
952
Jamie Genniscbb1a952012-05-08 17:05:52 -0700953 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700954 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
955 mTexture.setFiltering(useFiltering);
956 mTexture.setMatrix(textureMatrix);
957
958 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700959 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700960 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700961 }
Dan Stozac7014012014-02-14 15:03:43 -0800962 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700963 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964}
965
Mathias Agopian13127d82013-03-05 17:47:11 -0800966
Dan Stozac7014012014-02-14 15:03:43 -0800967void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
968 const Region& /* clip */, float red, float green, float blue,
969 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800970{
Mathias Agopian19733a32013-08-28 18:13:56 -0700971 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800972 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700973 engine.setupFillWithColor(red, green, blue, alpha);
974 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800975}
976
977void Layer::clearWithOpenGL(
978 const sp<const DisplayDevice>& hw, const Region& clip) const {
979 clearWithOpenGL(hw, clip, 0,0,0,0);
980}
981
Dan Stozac7014012014-02-14 15:03:43 -0800982void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
983 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700984 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800985
Dan Stozac7014012014-02-14 15:03:43 -0800986 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800987
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 /*
989 * NOTE: the way we compute the texture coordinates here produces
990 * different results than when we take the HWC path -- in the later case
991 * the "source crop" is rounded to texel boundaries.
992 * This can produce significantly different results when the texture
993 * is scaled by a large amount.
994 *
995 * The GL code below is more logical (imho), and the difference with
996 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700997 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 * GL composition when a buffer scaling is applied (maybe with some
999 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1000 * like more of a hack.
1001 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001002 Rect win(computeBounds());
1003
Robert Carrb5d3d262016-03-25 15:08:13 -07001004 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001005 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001006 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001007 win.clear();
1008 }
1009 win = s.active.transform.inverse().transform(win);
1010 if (!win.intersect(computeBounds(), &win)) {
1011 win.clear();
1012 }
1013 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001014
Mathias Agopian3f844832013-08-07 21:24:32 -07001015 float left = float(win.left) / float(s.active.w);
1016 float top = float(win.top) / float(s.active.h);
1017 float right = float(win.right) / float(s.active.w);
1018 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001019
Mathias Agopian875d8e12013-06-07 15:35:48 -07001020 // TODO: we probably want to generate the texture coords with the mesh
1021 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001022 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1023 texCoords[0] = vec2(left, 1.0f - top);
1024 texCoords[1] = vec2(left, 1.0f - bottom);
1025 texCoords[2] = vec2(right, 1.0f - bottom);
1026 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001027
Mathias Agopian875d8e12013-06-07 15:35:48 -07001028 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001029 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001030 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001031 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001032}
1033
Dan Stoza9e56aa02015-11-02 13:00:03 -08001034#ifdef USE_HWC2
1035void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1036 bool callIntoHwc) {
1037 if (mHwcLayers.count(hwcId) == 0) {
1038 ALOGE("setCompositionType called without a valid HWC layer");
1039 return;
1040 }
1041 auto& hwcInfo = mHwcLayers[hwcId];
1042 auto& hwcLayer = hwcInfo.layer;
1043 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1044 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1045 if (hwcInfo.compositionType != type) {
1046 ALOGV(" actually setting");
1047 hwcInfo.compositionType = type;
1048 if (callIntoHwc) {
1049 auto error = hwcLayer->setCompositionType(type);
1050 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1051 "composition type %s: %s (%d)", mName.string(),
1052 to_string(type).c_str(), to_string(error).c_str(),
1053 static_cast<int32_t>(error));
1054 }
1055 }
1056}
1057
1058HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1059 if (mHwcLayers.count(hwcId) == 0) {
1060 ALOGE("getCompositionType called without a valid HWC layer");
1061 return HWC2::Composition::Invalid;
1062 }
1063 return mHwcLayers.at(hwcId).compositionType;
1064}
1065
1066void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1067 if (mHwcLayers.count(hwcId) == 0) {
1068 ALOGE("setClearClientTarget called without a valid HWC layer");
1069 return;
1070 }
1071 mHwcLayers[hwcId].clearClientTarget = clear;
1072}
1073
1074bool Layer::getClearClientTarget(int32_t hwcId) const {
1075 if (mHwcLayers.count(hwcId) == 0) {
1076 ALOGE("getClearClientTarget called without a valid HWC layer");
1077 return false;
1078 }
1079 return mHwcLayers.at(hwcId).clearClientTarget;
1080}
1081#endif
1082
Ruben Brunk1681d952014-06-27 15:51:55 -07001083uint32_t Layer::getProducerStickyTransform() const {
1084 int producerStickyTransform = 0;
1085 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1086 if (ret != OK) {
1087 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1088 strerror(-ret), ret);
1089 return 0;
1090 }
1091 return static_cast<uint32_t>(producerStickyTransform);
1092}
1093
Dan Stozacac35382016-01-27 12:21:06 -08001094uint64_t Layer::getHeadFrameNumber() const {
1095 Mutex::Autolock lock(mQueueItemLock);
1096 if (!mQueueItems.empty()) {
1097 return mQueueItems[0].mFrameNumber;
1098 } else {
1099 return mCurrentFrameNumber;
1100 }
1101}
1102
Dan Stoza1ce65812016-06-15 16:26:27 -07001103bool Layer::headFenceHasSignaled() const {
1104#ifdef USE_HWC2
1105 Mutex::Autolock lock(mQueueItemLock);
1106 if (mQueueItems.empty()) {
1107 return true;
1108 }
1109 if (mQueueItems[0].mIsDroppable) {
1110 // Even though this buffer's fence may not have signaled yet, it could
1111 // be replaced by another buffer before it has a chance to, which means
1112 // that it's possible to get into a situation where a buffer is never
1113 // able to be latched. To avoid this, grab this buffer anyway.
1114 return true;
1115 }
1116 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1117#else
1118 return true;
1119#endif
1120}
1121
Dan Stozacac35382016-01-27 12:21:06 -08001122bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1123 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1124 // Don't bother with a SyncPoint, since we've already latched the
1125 // relevant frame
1126 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001127 }
1128
Dan Stozacac35382016-01-27 12:21:06 -08001129 Mutex::Autolock lock(mLocalSyncPointMutex);
1130 mLocalSyncPoints.push_back(point);
1131 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001132}
1133
Mathias Agopian13127d82013-03-05 17:47:11 -08001134void Layer::setFiltering(bool filtering) {
1135 mFiltering = filtering;
1136}
1137
1138bool Layer::getFiltering() const {
1139 return mFiltering;
1140}
1141
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001142// As documented in libhardware header, formats in the range
1143// 0x100 - 0x1FF are specific to the HAL implementation, and
1144// are known to have no alpha channel
1145// TODO: move definition for device-specific range into
1146// hardware.h, instead of using hard-coded values here.
1147#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1148
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001149bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001150 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1151 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001152 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001153 switch (format) {
1154 case HAL_PIXEL_FORMAT_RGBA_8888:
1155 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001156 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001157 }
1158 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001159 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001160}
1161
Mathias Agopian13127d82013-03-05 17:47:11 -08001162// ----------------------------------------------------------------------------
1163// local state
1164// ----------------------------------------------------------------------------
1165
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001166static void boundPoint(vec2* point, const Rect& crop) {
1167 if (point->x < crop.left) {
1168 point->x = crop.left;
1169 }
1170 if (point->x > crop.right) {
1171 point->x = crop.right;
1172 }
1173 if (point->y < crop.top) {
1174 point->y = crop.top;
1175 }
1176 if (point->y > crop.bottom) {
1177 point->y = crop.bottom;
1178 }
1179}
1180
Dan Stozac7014012014-02-14 15:03:43 -08001181void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1182 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001183{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001184 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001185 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001186 const uint32_t hw_h = hw->getHeight();
1187 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001188 if (!s.crop.isEmpty()) {
1189 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001190 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001191 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001192 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001193
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001194 vec2 lt = vec2(win.left, win.top);
1195 vec2 lb = vec2(win.left, win.bottom);
1196 vec2 rb = vec2(win.right, win.bottom);
1197 vec2 rt = vec2(win.right, win.top);
1198
1199 if (!useIdentityTransform) {
1200 lt = s.active.transform.transform(lt);
1201 lb = s.active.transform.transform(lb);
1202 rb = s.active.transform.transform(rb);
1203 rt = s.active.transform.transform(rt);
1204 }
1205
Robert Carrb5d3d262016-03-25 15:08:13 -07001206 if (!s.finalCrop.isEmpty()) {
1207 boundPoint(&lt, s.finalCrop);
1208 boundPoint(&lb, s.finalCrop);
1209 boundPoint(&rb, s.finalCrop);
1210 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001211 }
1212
Mathias Agopianff2ed702013-09-01 21:36:12 -07001213 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001214 position[0] = tr.transform(lt);
1215 position[1] = tr.transform(lb);
1216 position[2] = tr.transform(rb);
1217 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001218 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001219 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 }
1221}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001222
Andy McFadden4125a4f2014-01-29 17:17:11 -08001223bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001224{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225 // if we don't have a buffer yet, we're translucent regardless of the
1226 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001227 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001229 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001230
1231 // if the layer has the opaque flag, then we're always opaque,
1232 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001233 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001234}
1235
Dan Stoza23116082015-06-18 14:58:39 -07001236bool Layer::isSecure() const
1237{
1238 const Layer::State& s(mDrawingState);
1239 return (s.flags & layer_state_t::eLayerSecure);
1240}
1241
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001242bool Layer::isProtected() const
1243{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001244 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001245 return (activeBuffer != 0) &&
1246 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1247}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001248
Mathias Agopian13127d82013-03-05 17:47:11 -08001249bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001250 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001251}
1252
1253bool Layer::isCropped() const {
1254 return !mCurrentCrop.isEmpty();
1255}
1256
1257bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1258 return mNeedsFiltering || hw->needsFiltering();
1259}
1260
1261void Layer::setVisibleRegion(const Region& visibleRegion) {
1262 // always called from main thread
1263 this->visibleRegion = visibleRegion;
1264}
1265
1266void Layer::setCoveredRegion(const Region& coveredRegion) {
1267 // always called from main thread
1268 this->coveredRegion = coveredRegion;
1269}
1270
1271void Layer::setVisibleNonTransparentRegion(const Region&
1272 setVisibleNonTransparentRegion) {
1273 // always called from main thread
1274 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1275}
1276
1277// ----------------------------------------------------------------------------
1278// transaction
1279// ----------------------------------------------------------------------------
1280
Dan Stoza7dde5992015-05-22 09:51:44 -07001281void Layer::pushPendingState() {
1282 if (!mCurrentState.modified) {
1283 return;
1284 }
1285
Dan Stoza7dde5992015-05-22 09:51:44 -07001286 // If this transaction is waiting on the receipt of a frame, generate a sync
1287 // point and send it to the remote layer.
1288 if (mCurrentState.handle != nullptr) {
1289 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1290 sp<Layer> handleLayer = handle->owner.promote();
1291 if (handleLayer == nullptr) {
1292 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1293 // If we can't promote the layer we are intended to wait on,
1294 // then it is expired or otherwise invalid. Allow this transaction
1295 // to be applied as per normal (no synchronization).
1296 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001297 } else {
1298 auto syncPoint = std::make_shared<SyncPoint>(
1299 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001300 if (handleLayer->addSyncPoint(syncPoint)) {
1301 mRemoteSyncPoints.push_back(std::move(syncPoint));
1302 } else {
1303 // We already missed the frame we're supposed to synchronize
1304 // on, so go ahead and apply the state update
1305 mCurrentState.handle = nullptr;
1306 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 }
1308
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 // Wake us up to check if the frame has been received
1310 setTransactionFlags(eTransactionNeeded);
1311 }
1312 mPendingStates.push_back(mCurrentState);
1313}
1314
Pablo Ceballos05289c22016-04-14 15:49:55 -07001315void Layer::popPendingState(State* stateToCommit) {
1316 auto oldFlags = stateToCommit->flags;
1317 *stateToCommit = mPendingStates[0];
1318 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1319 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001320
1321 mPendingStates.removeAt(0);
1322}
1323
Pablo Ceballos05289c22016-04-14 15:49:55 -07001324bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001325 bool stateUpdateAvailable = false;
1326 while (!mPendingStates.empty()) {
1327 if (mPendingStates[0].handle != nullptr) {
1328 if (mRemoteSyncPoints.empty()) {
1329 // If we don't have a sync point for this, apply it anyway. It
1330 // will be visually wrong, but it should keep us from getting
1331 // into too much trouble.
1332 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001333 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001334 stateUpdateAvailable = true;
1335 continue;
1336 }
1337
Dan Stozacac35382016-01-27 12:21:06 -08001338 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1339 mPendingStates[0].frameNumber) {
1340 ALOGE("[%s] Unexpected sync point frame number found",
1341 mName.string());
1342
1343 // Signal our end of the sync point and then dispose of it
1344 mRemoteSyncPoints.front()->setTransactionApplied();
1345 mRemoteSyncPoints.pop_front();
1346 continue;
1347 }
1348
Dan Stoza7dde5992015-05-22 09:51:44 -07001349 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1350 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001351 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001352 stateUpdateAvailable = true;
1353
1354 // Signal our end of the sync point and then dispose of it
1355 mRemoteSyncPoints.front()->setTransactionApplied();
1356 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001357 } else {
1358 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001361 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001362 stateUpdateAvailable = true;
1363 }
1364 }
1365
1366 // If we still have pending updates, wake SurfaceFlinger back up and point
1367 // it at this layer so we can process them
1368 if (!mPendingStates.empty()) {
1369 setTransactionFlags(eTransactionNeeded);
1370 mFlinger->setTransactionFlags(eTraversalNeeded);
1371 }
1372
1373 mCurrentState.modified = false;
1374 return stateUpdateAvailable;
1375}
1376
1377void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001378 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001379 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001380 Mutex::Autolock lock(mLocalSyncPointMutex);
1381 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001382 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001383 point->setFrameAvailable();
1384 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001385 }
1386}
1387
Mathias Agopian13127d82013-03-05 17:47:11 -08001388uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001389 ATRACE_CALL();
1390
Dan Stoza7dde5992015-05-22 09:51:44 -07001391 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001392 Layer::State c = getCurrentState();
1393 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001394 return 0;
1395 }
1396
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001397 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001399 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1400 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001401
1402 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001403 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001404 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001405 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001406 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001407 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001408 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001409 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001410 this, getName().string(), mCurrentTransform,
1411 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001412 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001413 c.crop.left,
1414 c.crop.top,
1415 c.crop.right,
1416 c.crop.bottom,
1417 c.crop.getWidth(),
1418 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001421 s.crop.left,
1422 s.crop.top,
1423 s.crop.right,
1424 s.crop.bottom,
1425 s.crop.getWidth(),
1426 s.crop.getHeight(),
1427 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001429 // record the new size, form this point on, when the client request
1430 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001431 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001432 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001434
Robert Carr82364e32016-05-15 11:27:47 -07001435 const bool resizePending = (c.requested.w != c.active.w) ||
1436 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001437 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001438 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001439 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001440 // if we have a pending resize, unless we are in fixed-size mode.
1441 // the drawing state will be updated only once we receive a buffer
1442 // with the correct size.
1443 //
1444 // in particular, we want to make sure the clip (which is part
1445 // of the geometry state) is latched together with the size but is
1446 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001447 //
1448 // If a sideband stream is attached, however, we want to skip this
1449 // optimization so that transactions aren't missed when a buffer
1450 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001451
1452 flags |= eDontUpdateGeometryState;
1453 }
1454 }
1455
Mathias Agopian13127d82013-03-05 17:47:11 -08001456 // always set active to requested, unless we're asked not to
1457 // this is used by Layer, which special cases resizes.
1458 if (flags & eDontUpdateGeometryState) {
1459 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001460 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001461 if (mFreezePositionUpdates) {
1462 float tx = c.active.transform.tx();
1463 float ty = c.active.transform.ty();
1464 c.active = c.requested;
1465 c.active.transform.set(tx, ty);
1466 editCurrentState.active = c.active;
1467 } else {
1468 editCurrentState.active = editCurrentState.requested;
1469 c.active = c.requested;
1470 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001471 }
1472
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001473 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 // invalidate and recompute the visible regions if needed
1475 flags |= Layer::eVisibleRegion;
1476 }
1477
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001478 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001479 // invalidate and recompute the visible regions if needed
1480 flags |= eVisibleRegion;
1481 this->contentDirty = true;
1482
1483 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001484 const uint8_t type = c.active.transform.getType();
1485 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001486 (type >= Transform::SCALE));
1487 }
1488
Dan Stozac8145172016-04-28 16:29:06 -07001489 // If the layer is hidden, signal and clear out all local sync points so
1490 // that transactions for layers depending on this layer's frames becoming
1491 // visible are not blocked
1492 if (c.flags & layer_state_t::eLayerHidden) {
1493 Mutex::Autolock lock(mLocalSyncPointMutex);
1494 for (auto& point : mLocalSyncPoints) {
1495 point->setFrameAvailable();
1496 }
1497 mLocalSyncPoints.clear();
1498 }
1499
Mathias Agopian13127d82013-03-05 17:47:11 -08001500 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001501 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001502 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503}
1504
Pablo Ceballos05289c22016-04-14 15:49:55 -07001505void Layer::commitTransaction(const State& stateToCommit) {
1506 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001507}
1508
Mathias Agopian13127d82013-03-05 17:47:11 -08001509uint32_t Layer::getTransactionFlags(uint32_t flags) {
1510 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1511}
1512
1513uint32_t Layer::setTransactionFlags(uint32_t flags) {
1514 return android_atomic_or(flags, &mTransactionFlags);
1515}
1516
Robert Carr82364e32016-05-15 11:27:47 -07001517bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001518 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001519 return false;
1520 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001521
1522 // We update the requested and active position simultaneously because
1523 // we want to apply the position portion of the transform matrix immediately,
1524 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001525 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001526 if (immediate && !mFreezePositionUpdates) {
1527 mCurrentState.active.transform.set(x, y);
1528 }
1529 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001530
Dan Stoza7dde5992015-05-22 09:51:44 -07001531 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001532 setTransactionFlags(eTransactionNeeded);
1533 return true;
1534}
Robert Carr82364e32016-05-15 11:27:47 -07001535
Mathias Agopian13127d82013-03-05 17:47:11 -08001536bool Layer::setLayer(uint32_t z) {
1537 if (mCurrentState.z == z)
1538 return false;
1539 mCurrentState.sequence++;
1540 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001541 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001542 setTransactionFlags(eTransactionNeeded);
1543 return true;
1544}
1545bool Layer::setSize(uint32_t w, uint32_t h) {
1546 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1547 return false;
1548 mCurrentState.requested.w = w;
1549 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001550 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001551 setTransactionFlags(eTransactionNeeded);
1552 return true;
1553}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001554#ifdef USE_HWC2
1555bool Layer::setAlpha(float alpha) {
1556#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001557bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001558#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001559 if (mCurrentState.alpha == alpha)
1560 return false;
1561 mCurrentState.sequence++;
1562 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001563 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001564 setTransactionFlags(eTransactionNeeded);
1565 return true;
1566}
1567bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1568 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001569 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001571 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001572 setTransactionFlags(eTransactionNeeded);
1573 return true;
1574}
1575bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001576 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001577 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001578 setTransactionFlags(eTransactionNeeded);
1579 return true;
1580}
1581bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1582 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1583 if (mCurrentState.flags == newFlags)
1584 return false;
1585 mCurrentState.sequence++;
1586 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001587 mCurrentState.mask = mask;
1588 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001589 setTransactionFlags(eTransactionNeeded);
1590 return true;
1591}
Robert Carr99e27f02016-06-16 15:18:02 -07001592
1593bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001594 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001595 return false;
1596 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001597 mCurrentState.requestedCrop = crop;
1598 if (immediate) {
1599 mCurrentState.crop = crop;
1600 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001601 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 setTransactionFlags(eTransactionNeeded);
1603 return true;
1604}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001605bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001606 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001607 return false;
1608 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001609 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001610 mCurrentState.modified = true;
1611 setTransactionFlags(eTransactionNeeded);
1612 return true;
1613}
Mathias Agopian13127d82013-03-05 17:47:11 -08001614
Robert Carrc3574f72016-03-24 12:19:32 -07001615bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1616 if (scalingMode == mOverrideScalingMode)
1617 return false;
1618 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001619 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001620 return true;
1621}
1622
1623uint32_t Layer::getEffectiveScalingMode() const {
1624 if (mOverrideScalingMode >= 0) {
1625 return mOverrideScalingMode;
1626 }
1627 return mCurrentScalingMode;
1628}
1629
Mathias Agopian13127d82013-03-05 17:47:11 -08001630bool Layer::setLayerStack(uint32_t layerStack) {
1631 if (mCurrentState.layerStack == layerStack)
1632 return false;
1633 mCurrentState.sequence++;
1634 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001635 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 setTransactionFlags(eTransactionNeeded);
1637 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001638}
1639
Dan Stoza7dde5992015-05-22 09:51:44 -07001640void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1641 uint64_t frameNumber) {
1642 mCurrentState.handle = handle;
1643 mCurrentState.frameNumber = frameNumber;
1644 // We don't set eTransactionNeeded, because just receiving a deferral
1645 // request without any other state updates shouldn't actually induce a delay
1646 mCurrentState.modified = true;
1647 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001648 mCurrentState.handle = nullptr;
1649 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001650 mCurrentState.modified = false;
1651}
1652
Dan Stozaee44edd2015-03-23 15:50:23 -07001653void Layer::useSurfaceDamage() {
1654 if (mFlinger->mForceFullDamage) {
1655 surfaceDamageRegion = Region::INVALID_REGION;
1656 } else {
1657 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1658 }
1659}
1660
1661void Layer::useEmptyDamage() {
1662 surfaceDamageRegion.clear();
1663}
1664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665// ----------------------------------------------------------------------------
1666// pageflip handling...
1667// ----------------------------------------------------------------------------
1668
Dan Stoza6b9454d2014-11-07 16:00:59 -08001669bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001670 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001671 return true;
1672 }
1673
Dan Stoza6b9454d2014-11-07 16:00:59 -08001674 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001675 if (mQueueItems.empty()) {
1676 return false;
1677 }
1678 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001679 nsecs_t expectedPresent =
1680 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001681
1682 // Ignore timestamps more than a second in the future
1683 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1684 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1685 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1686 expectedPresent);
1687
1688 bool isDue = timestamp < expectedPresent;
1689 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001690}
1691
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001692bool Layer::onPreComposition() {
1693 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001694 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001695}
1696
Dan Stozae77c7662016-05-13 11:37:28 -07001697bool Layer::onPostComposition() {
1698 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001699 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001700 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001701 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1702
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001703 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001704 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001705 mFrameTracker.setFrameReadyFence(frameReadyFence);
1706 } else {
1707 // There was no fence for this frame, so assume that it was ready
1708 // to be presented at the desired present time.
1709 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1710 }
1711
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001712 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001713#ifdef USE_HWC2
1714 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1715#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001716 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001717#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001718 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001719 mFrameTracker.setActualPresentFence(presentFence);
1720 } else {
1721 // The HWC doesn't support present fences, so use the refresh
1722 // timestamp instead.
1723 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1724 mFrameTracker.setActualPresentTime(presentTime);
1725 }
1726
1727 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001728 mFrameLatencyNeeded = false;
1729 }
Dan Stozae77c7662016-05-13 11:37:28 -07001730 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001731}
1732
Dan Stoza9e56aa02015-11-02 13:00:03 -08001733#ifdef USE_HWC2
1734void Layer::releasePendingBuffer() {
1735 mSurfaceFlingerConsumer->releasePendingBuffer();
1736}
1737#endif
1738
Mathias Agopianda27af92012-09-13 18:17:13 -07001739bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001740 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001741#ifdef USE_HWC2
1742 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1743 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1744#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001745 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001746 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001747#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001748}
1749
Mathias Agopian4fec8732012-06-29 14:12:52 -07001750Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001752 ATRACE_CALL();
1753
Jesse Hall399184a2014-03-03 15:42:54 -08001754 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1755 // mSidebandStreamChanged was true
1756 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001757 if (mSidebandStream != NULL) {
1758 setTransactionFlags(eTransactionNeeded);
1759 mFlinger->setTransactionFlags(eTraversalNeeded);
1760 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001761 recomputeVisibleRegions = true;
1762
1763 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001764 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001765 }
1766
Mathias Agopian4fec8732012-06-29 14:12:52 -07001767 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001768 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001769
1770 // if we've already called updateTexImage() without going through
1771 // a composition step, we have to skip this layer at this point
1772 // because we cannot call updateTeximage() without a corresponding
1773 // compositionComplete() call.
1774 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001775 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001776 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001777 }
1778
Dan Stoza1ce65812016-06-15 16:26:27 -07001779 // If the head buffer's acquire fence hasn't signaled yet, return and
1780 // try again later
1781 if (!headFenceHasSignaled()) {
1782 mFlinger->signalLayerUpdate();
1783 return outDirtyRegion;
1784 }
1785
Jamie Gennis351a5132011-09-14 18:23:37 -07001786 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001787 const State& s(getDrawingState());
1788 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001789 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001790
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001791 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001792 Layer::State& front;
1793 Layer::State& current;
1794 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001795 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001796 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001797 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001798 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001799
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001800 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001801 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001802 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001803 int32_t overrideScalingMode,
1804 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001805 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001806 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001807 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001808 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001809 overrideScalingMode(overrideScalingMode),
1810 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001811 }
1812
1813 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001814 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001815 if (buf == NULL) {
1816 return false;
1817 }
1818
1819 uint32_t bufWidth = buf->getWidth();
1820 uint32_t bufHeight = buf->getHeight();
1821
1822 // check that we received a buffer of the right size
1823 // (Take the buffer's orientation into account)
1824 if (item.mTransform & Transform::ROT_90) {
1825 swap(bufWidth, bufHeight);
1826 }
1827
Robert Carrc3574f72016-03-24 12:19:32 -07001828 int actualScalingMode = overrideScalingMode >= 0 ?
1829 overrideScalingMode : item.mScalingMode;
1830 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001831 if (front.active != front.requested) {
1832
1833 if (isFixedSize ||
1834 (bufWidth == front.requested.w &&
1835 bufHeight == front.requested.h))
1836 {
1837 // Here we pretend the transaction happened by updating the
1838 // current and drawing states. Drawing state is only accessed
1839 // in this thread, no need to have it locked
1840 front.active = front.requested;
1841
1842 // We also need to update the current state so that
1843 // we don't end-up overwriting the drawing state with
1844 // this stale current state during the next transaction
1845 //
1846 // NOTE: We don't need to hold the transaction lock here
1847 // because State::active is only accessed from this thread.
1848 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001849 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001850
1851 // recompute visible region
1852 recomputeVisibleRegions = true;
1853 }
1854
1855 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001856 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001857 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001858 " requested={ wh={%4u,%4u} }}\n",
1859 name,
Andy McFadden69052052012-09-14 16:10:11 -07001860 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001861 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001862 front.crop.left,
1863 front.crop.top,
1864 front.crop.right,
1865 front.crop.bottom,
1866 front.crop.getWidth(),
1867 front.crop.getHeight(),
1868 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001869 }
1870
Ruben Brunk1681d952014-06-27 15:51:55 -07001871 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001872 if (front.active.w != bufWidth ||
1873 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001874 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001875 ALOGE("[%s] rejecting buffer: "
1876 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1877 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001878 return true;
1879 }
1880 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001881
1882 // if the transparent region has changed (this test is
1883 // conservative, but that's fine, worst case we're doing
1884 // a bit of extra work), we latch the new one and we
1885 // trigger a visible-region recompute.
1886 if (!front.activeTransparentRegion.isTriviallyEqual(
1887 front.requestedTransparentRegion)) {
1888 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001889
1890 // We also need to update the current state so that
1891 // we don't end-up overwriting the drawing state with
1892 // this stale current state during the next transaction
1893 //
1894 // NOTE: We don't need to hold the transaction lock here
1895 // because State::active is only accessed from this thread.
1896 current.activeTransparentRegion = front.activeTransparentRegion;
1897
1898 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001899 recomputeVisibleRegions = true;
1900 }
1901
Robert Carr99e27f02016-06-16 15:18:02 -07001902 if (front.crop != front.requestedCrop) {
1903 front.crop = front.requestedCrop;
1904 current.crop = front.requestedCrop;
1905 recomputeVisibleRegions = true;
1906 }
Robert Carra3920732016-06-20 21:49:49 -07001907 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001908
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001909 return false;
1910 }
1911 };
1912
Ruben Brunk1681d952014-06-27 15:51:55 -07001913 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001914 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001915 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001916
Dan Stozacac35382016-01-27 12:21:06 -08001917
1918 // Check all of our local sync points to ensure that all transactions
1919 // which need to have been applied prior to the frame which is about to
1920 // be latched have signaled
1921
1922 auto headFrameNumber = getHeadFrameNumber();
1923 bool matchingFramesFound = false;
1924 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001925 {
Dan Stozacac35382016-01-27 12:21:06 -08001926 Mutex::Autolock lock(mLocalSyncPointMutex);
1927 for (auto& point : mLocalSyncPoints) {
1928 if (point->getFrameNumber() > headFrameNumber) {
1929 break;
1930 }
1931
1932 matchingFramesFound = true;
1933
1934 if (!point->frameIsAvailable()) {
1935 // We haven't notified the remote layer that the frame for
1936 // this point is available yet. Notify it now, and then
1937 // abort this attempt to latch.
1938 point->setFrameAvailable();
1939 allTransactionsApplied = false;
1940 break;
1941 }
1942
1943 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001944 }
1945 }
1946
Dan Stozacac35382016-01-27 12:21:06 -08001947 if (matchingFramesFound && !allTransactionsApplied) {
1948 mFlinger->signalLayerUpdate();
1949 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001950 }
1951
Pablo Ceballos06312182015-10-07 16:32:12 -07001952 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1953 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001954 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001955 // buffer mode.
1956 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001957 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001958 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001959 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001960 if (updateResult == BufferQueue::PRESENT_LATER) {
1961 // Producer doesn't want buffer to be displayed yet. Signal a
1962 // layer update so we check again at the next opportunity.
1963 mFlinger->signalLayerUpdate();
1964 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001965 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1966 // If the buffer has been rejected, remove it from the shadow queue
1967 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001968 if (queuedBuffer) {
1969 Mutex::Autolock lock(mQueueItemLock);
1970 mQueueItems.removeAt(0);
1971 android_atomic_dec(&mQueuedFrames);
1972 }
Dan Stozaecc50402015-04-28 14:42:06 -07001973 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001974 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1975 // This can occur if something goes wrong when trying to create the
1976 // EGLImage for this buffer. If this happens, the buffer has already
1977 // been released, so we need to clean up the queue and bug out
1978 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001979 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001980 Mutex::Autolock lock(mQueueItemLock);
1981 mQueueItems.clear();
1982 android_atomic_and(0, &mQueuedFrames);
1983 }
1984
1985 // Once we have hit this state, the shadow queue may no longer
1986 // correctly reflect the incoming BufferQueue's contents, so even if
1987 // updateTexImage starts working, the only safe course of action is
1988 // to continue to ignore updates.
1989 mUpdateTexImageFailed = true;
1990
1991 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001992 }
1993
Pablo Ceballos06312182015-10-07 16:32:12 -07001994 if (queuedBuffer) {
1995 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001996 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1997
Dan Stoza6b9454d2014-11-07 16:00:59 -08001998 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001999
2000 // Remove any stale buffers that have been dropped during
2001 // updateTexImage
2002 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2003 mQueueItems.removeAt(0);
2004 android_atomic_dec(&mQueuedFrames);
2005 }
2006
Dan Stoza6b9454d2014-11-07 16:00:59 -08002007 mQueueItems.removeAt(0);
2008 }
2009
Dan Stozaecc50402015-04-28 14:42:06 -07002010
Andy McFadden1585c4d2013-06-28 13:52:40 -07002011 // Decrement the queued-frames count. Signal another event if we
2012 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002013 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002014 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002015 mFlinger->signalLayerUpdate();
2016 }
2017
2018 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002019 // something happened!
2020 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002021 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002022 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002023
Jamie Gennis351a5132011-09-14 18:23:37 -07002024 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002025 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002026 if (mActiveBuffer == NULL) {
2027 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002028 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002029 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002030
Mathias Agopian4824d402012-06-04 18:16:30 -07002031 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002032 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002033 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002034 // the first time we receive a buffer, we need to trigger a
2035 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002036 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002037 }
2038
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002039 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2040 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2041 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002042 if ((crop != mCurrentCrop) ||
2043 (transform != mCurrentTransform) ||
2044 (scalingMode != mCurrentScalingMode))
2045 {
2046 mCurrentCrop = crop;
2047 mCurrentTransform = transform;
2048 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002049 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002050 }
2051
2052 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002053 uint32_t bufWidth = mActiveBuffer->getWidth();
2054 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002055 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2056 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002057 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002058 }
2059 }
2060
2061 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002062 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002063 recomputeVisibleRegions = true;
2064 }
2065
Dan Stozacac35382016-01-27 12:21:06 -08002066 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2067
2068 // Remove any sync points corresponding to the buffer which was just
2069 // latched
2070 {
2071 Mutex::Autolock lock(mLocalSyncPointMutex);
2072 auto point = mLocalSyncPoints.begin();
2073 while (point != mLocalSyncPoints.end()) {
2074 if (!(*point)->frameIsAvailable() ||
2075 !(*point)->transactionIsApplied()) {
2076 // This sync point must have been added since we started
2077 // latching. Don't drop it yet.
2078 ++point;
2079 continue;
2080 }
2081
2082 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2083 point = mLocalSyncPoints.erase(point);
2084 } else {
2085 ++point;
2086 }
2087 }
2088 }
2089
Mathias Agopian4fec8732012-06-29 14:12:52 -07002090 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002091 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002092
2093 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002094 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002095 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002096 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097}
2098
Mathias Agopiana67932f2011-04-20 14:20:59 -07002099uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002100{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002101 // TODO: should we do something special if mSecure is set?
2102 if (mProtectedByApp) {
2103 // need a hardware-protected path to external video sink
2104 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002105 }
Riley Andrews03414a12014-07-01 14:22:59 -07002106 if (mPotentialCursor) {
2107 usage |= GraphicBuffer::USAGE_CURSOR;
2108 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002109 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002110 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002111}
2112
Mathias Agopian84300952012-11-21 16:02:13 -08002113void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002114 uint32_t orientation = 0;
2115 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002116 // The transform hint is used to improve performance, but we can
2117 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002118 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002119 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002120 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002121 if (orientation & Transform::ROT_INVALID) {
2122 orientation = 0;
2123 }
2124 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002125 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002126}
2127
Mathias Agopian13127d82013-03-05 17:47:11 -08002128// ----------------------------------------------------------------------------
2129// debugging
2130// ----------------------------------------------------------------------------
2131
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002132void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002133{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002134 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002135
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002136 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002137 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002138 "+ %s %p (%s)\n",
2139 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002140 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002141
Mathias Agopian2ca79392013-04-02 18:30:32 -07002142 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002143 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002144 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002145 sp<Client> client(mClientRef.promote());
2146
Mathias Agopian74d211a2013-04-22 16:55:35 +02002147 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002148 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2149 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002150 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002151#ifdef USE_HWC2
2152 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2153#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002154 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002155#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002156 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002157 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 -07002158 s.crop.left, s.crop.top,
2159 s.crop.right, s.crop.bottom,
2160 s.finalCrop.left, s.finalCrop.top,
2161 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002162 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002163 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002164 s.active.transform[0][0], s.active.transform[0][1],
2165 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002166 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002167
2168 sp<const GraphicBuffer> buf0(mActiveBuffer);
2169 uint32_t w0=0, h0=0, s0=0, f0=0;
2170 if (buf0 != 0) {
2171 w0 = buf0->getWidth();
2172 h0 = buf0->getHeight();
2173 s0 = buf0->getStride();
2174 f0 = buf0->format;
2175 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002176 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002177 " "
2178 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2179 " queued-frames=%d, mRefreshPending=%d\n",
2180 mFormat, w0, h0, s0,f0,
2181 mQueuedFrames, mRefreshPending);
2182
Mathias Agopian13127d82013-03-05 17:47:11 -08002183 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002184 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002185 }
2186}
2187
Svetoslavd85084b2014-03-20 10:28:31 -07002188void Layer::dumpFrameStats(String8& result) const {
2189 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002190}
2191
Svetoslavd85084b2014-03-20 10:28:31 -07002192void Layer::clearFrameStats() {
2193 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002194}
2195
Jamie Gennis6547ff42013-07-16 20:12:42 -07002196void Layer::logFrameStats() {
2197 mFrameTracker.logAndResetStats(mName);
2198}
2199
Svetoslavd85084b2014-03-20 10:28:31 -07002200void Layer::getFrameStats(FrameStats* outStats) const {
2201 mFrameTracker.getStats(outStats);
2202}
2203
Pablo Ceballos40845df2016-01-25 17:41:15 -08002204void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2205 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2206 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2207 *outName = mName;
2208 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2209
2210#ifdef USE_HWC2
2211 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2212 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2213 HWC2::Composition::Client : true;
2214#else
2215 *outIsGlesComposition = mIsGlesComposition;
2216#endif
2217 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2218 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2219 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2220}
Dan Stozae77c7662016-05-13 11:37:28 -07002221
2222std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2223 bool forceFlush) {
2224 std::vector<OccupancyTracker::Segment> history;
2225 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2226 &history);
2227 if (result != NO_ERROR) {
2228 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2229 result);
2230 return {};
2231 }
2232 return history;
2233}
2234
Robert Carr367c5682016-06-20 11:55:28 -07002235bool Layer::getTransformToDisplayInverse() const {
2236 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2237}
2238
Mathias Agopian13127d82013-03-05 17:47:11 -08002239// ---------------------------------------------------------------------------
2240
2241Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2242 const sp<Layer>& layer)
2243 : mFlinger(flinger), mLayer(layer) {
2244}
2245
2246Layer::LayerCleaner::~LayerCleaner() {
2247 // destroy client resources
2248 mFlinger->onLayerDestroyed(mLayer);
2249}
2250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002251// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002252}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002253
2254#if defined(__gl_h_)
2255#error "don't include gl/gl.h in this file"
2256#endif
2257
2258#if defined(__gl2_h_)
2259#error "don't include gl2/gl2.h in this file"
2260#endif