blob: 6024fa99d89e6a2c98a3072d3435b3b6c8ffa89b [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
Dan Stozac9d97202016-03-03 08:20:27 -0800164#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700165 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800166#endif
Andy McFadden69052052012-09-14 16:10:11 -0700167
Mathias Agopian84300952012-11-21 16:02:13 -0800168 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
169 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700170}
171
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700172Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800173 sp<Client> c(mClientRef.promote());
174 if (c != 0) {
175 c->detachLayer(this);
176 }
177
Dan Stozacac35382016-01-27 12:21:06 -0800178 for (auto& point : mRemoteSyncPoints) {
179 point->setTransactionApplied();
180 }
Dan Stozac8145172016-04-28 16:29:06 -0700181 for (auto& point : mLocalSyncPoints) {
182 point->setFrameAvailable();
183 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700184 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700185 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// callbacks
190// ---------------------------------------------------------------------------
191
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192#ifdef USE_HWC2
193void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
194 if (mHwcLayers.empty()) {
195 return;
196 }
197 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
198}
199#else
Dan Stozac7014012014-02-14 15:03:43 -0800200void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 HWComposer::HWCLayerInterface* layer) {
202 if (layer) {
203 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700204 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800205 }
206}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800208
Dan Stoza6b9454d2014-11-07 16:00:59 -0800209void Layer::onFrameAvailable(const BufferItem& item) {
210 // Add this buffer from our internal queue tracker
211 { // Autolock scope
212 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700213
214 // Reset the frame number tracker when we receive the first buffer after
215 // a frame number reset
216 if (item.mFrameNumber == 1) {
217 mLastFrameNumberReceived = 0;
218 }
219
220 // Ensure that callbacks are handled in order
221 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
222 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
223 ms2ns(500));
224 if (result != NO_ERROR) {
225 ALOGE("[%s] Timed out waiting on callback", mName.string());
226 }
227 }
228
Dan Stoza6b9454d2014-11-07 16:00:59 -0800229 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700230 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700231
232 // Wake up any pending callbacks
233 mLastFrameNumberReceived = item.mFrameNumber;
234 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800235 }
236
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800237 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700238}
239
Dan Stoza6b9454d2014-11-07 16:00:59 -0800240void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700241 { // Autolock scope
242 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700243
Dan Stoza7dde5992015-05-22 09:51:44 -0700244 // Ensure that callbacks are handled in order
245 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
246 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
247 ms2ns(500));
248 if (result != NO_ERROR) {
249 ALOGE("[%s] Timed out waiting on callback", mName.string());
250 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700251 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700252
253 if (mQueueItems.empty()) {
254 ALOGE("Can't replace a frame on an empty queue");
255 return;
256 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700257 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700258
259 // Wake up any pending callbacks
260 mLastFrameNumberReceived = item.mFrameNumber;
261 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700262 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800263}
264
Jesse Hall399184a2014-03-03 15:42:54 -0800265void Layer::onSidebandStreamChanged() {
266 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
267 // mSidebandStreamChanged was false
268 mFlinger->signalLayerUpdate();
269 }
270}
271
Mathias Agopian67106042013-03-14 19:18:13 -0700272// called with SurfaceFlinger::mStateLock from the drawing thread after
273// the layer has been remove from the current state list (and just before
274// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800275void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800276 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700277}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700278
Mathias Agopian13127d82013-03-05 17:47:11 -0800279// ---------------------------------------------------------------------------
280// set-up
281// ---------------------------------------------------------------------------
282
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700283const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800284 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Mathias Agopianf9d93272009-06-19 17:00:27 -0700287status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 PixelFormat format, uint32_t flags)
289{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700290 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700292
293 // never allow a surface larger than what our underlying GL implementation
294 // can handle.
295 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800296 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700297 return BAD_VALUE;
298 }
299
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700300 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700301
Riley Andrews03414a12014-07-01 14:22:59 -0700302 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700303 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304 mCurrentOpacity = getOpacityForFormat(format);
305
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800306 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
307 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
308 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return NO_ERROR;
311}
312
Dan Stoza7dde5992015-05-22 09:51:44 -0700313/*
314 * The layer handle is just a BBinder object passed to the client
315 * (remote process) -- we don't keep any reference on our side such that
316 * the dtor is called when the remote side let go of its reference.
317 *
318 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
319 * this layer when the handle is destroyed.
320 */
321class Layer::Handle : public BBinder, public LayerCleaner {
322 public:
323 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
324 : LayerCleaner(flinger, layer), owner(layer) {}
325
326 wp<Layer> owner;
327};
328
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700329sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800330 Mutex::Autolock _l(mLock);
331
332 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700333 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800334
335 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700336
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700337 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800338}
339
Dan Stozab9b08832014-03-13 11:55:57 -0700340sp<IGraphicBufferProducer> Layer::getProducer() const {
341 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700342}
343
Mathias Agopian13127d82013-03-05 17:47:11 -0800344// ---------------------------------------------------------------------------
345// h/w composer set-up
346// ---------------------------------------------------------------------------
347
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800348Rect Layer::getContentCrop() const {
349 // this is the crop rectangle that applies to the buffer
350 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700351 Rect crop;
352 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800353 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700354 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800355 } else if (mActiveBuffer != NULL) {
356 // otherwise we use the whole buffer
357 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700358 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800359 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700360 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700361 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 return crop;
363}
364
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700365static Rect reduce(const Rect& win, const Region& exclude) {
366 if (CC_LIKELY(exclude.isEmpty())) {
367 return win;
368 }
369 if (exclude.isRect()) {
370 return win.reduce(exclude.getBounds());
371 }
372 return Region(win).subtract(exclude).getBounds();
373}
374
Mathias Agopian13127d82013-03-05 17:47:11 -0800375Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700376 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700377 return computeBounds(s.activeTransparentRegion);
378}
379
380Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
381 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800382 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700383
384 if (!s.crop.isEmpty()) {
385 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700387 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700388 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800389}
390
Mathias Agopian6b442672013-07-09 21:24:52 -0700391FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800392 // the content crop is the area of the content that gets scaled to the
393 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700394 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800395
Robert Carrb5d3d262016-03-25 15:08:13 -0700396 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800397 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700398 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800399
400 // apply the projection's clipping to the window crop in
401 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700402 // if there are no window scaling involved, this operation will map to full
403 // pixels in the buffer.
404 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
405 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700406
407 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700408 if (!s.crop.isEmpty()) {
409 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700410 }
411
Robert Carr3dcabfa2016-03-01 18:36:58 -0800412 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
414 activeCrop.clear();
415 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700416 if (!s.finalCrop.isEmpty()) {
417 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 activeCrop.clear();
419 }
420 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800421 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800422
Michael Lentine28ea2172014-11-19 18:32:37 -0800423 // This needs to be here as transform.transform(Rect) computes the
424 // transformed rect and then takes the bounding box of the result before
425 // returning. This means
426 // transform.inverse().transform(transform.transform(Rect)) != Rect
427 // in which case we need to make sure the final rect is clipped to the
428 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
430 activeCrop.clear();
431 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800432
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700433 // subtract the transparent region and snap to the bounds
434 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
435
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 // Transform the window crop to match the buffer coordinate system,
437 // which means using the inverse of the current transform set on the
438 // SurfaceFlingerConsumer.
439 uint32_t invTransform = mCurrentTransform;
440 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
441 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700442 * the code below applies the primary display's inverse transform to the
443 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700445 uint32_t invTransformOrient =
446 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // calculate the inverse transform
448 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
449 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
450 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700453 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
454 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456
457 int winWidth = s.active.w;
458 int winHeight = s.active.h;
459 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
460 // If the activeCrop has been rotate the ends are rotated but not
461 // the space itself so when transforming ends back we can't rely on
462 // a modification of the axes of rotation. To account for this we
463 // need to reorient the inverse rotation in terms of the current
464 // axes of rotation.
465 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
466 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
467 if (is_h_flipped == is_v_flipped) {
468 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
469 NATIVE_WINDOW_TRANSFORM_FLIP_H;
470 }
471 winWidth = s.active.h;
472 winHeight = s.active.w;
473 }
474 const Rect winCrop = activeCrop.transform(
475 invTransform, s.active.w, s.active.h);
476
477 // below, crop is intersected with winCrop expressed in crop's coordinate space
478 float xScale = crop.getWidth() / float(winWidth);
479 float yScale = crop.getHeight() / float(winHeight);
480
481 float insetL = winCrop.left * xScale;
482 float insetT = winCrop.top * yScale;
483 float insetR = (winWidth - winCrop.right ) * xScale;
484 float insetB = (winHeight - winCrop.bottom) * yScale;
485
486 crop.left += insetL;
487 crop.top += insetT;
488 crop.right -= insetR;
489 crop.bottom -= insetB;
490
Mathias Agopian13127d82013-03-05 17:47:11 -0800491 return crop;
492}
493
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494#ifdef USE_HWC2
495void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
496#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700497void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700498 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700499 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700501{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502#ifdef USE_HWC2
503 const auto hwcId = displayDevice->getHwcDisplayId();
504 auto& hwcInfo = mHwcLayers[hwcId];
505#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800506 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700508
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700509 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510#ifdef USE_HWC2
511 hwcInfo.forceClientComposition = false;
512
513 if (isSecure() && !displayDevice->isSecure()) {
514 hwcInfo.forceClientComposition = true;
515 }
516
517 auto& hwcLayer = hwcInfo.layer;
518#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700519 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700520
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700521 if (isSecure() && !hw->isSecure()) {
522 layer.setSkip(true);
523 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800524#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700525
Mathias Agopian13127d82013-03-05 17:47:11 -0800526 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700527 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800528#ifdef USE_HWC2
529 if (!isOpaque(s) || s.alpha != 1.0f) {
530 auto blendMode = mPremultipliedAlpha ?
531 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
532 auto error = hwcLayer->setBlendMode(blendMode);
533 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
534 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
535 to_string(error).c_str(), static_cast<int32_t>(error));
536 }
537#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800538 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800539 layer.setBlending(mPremultipliedAlpha ?
540 HWC_BLENDING_PREMULT :
541 HWC_BLENDING_COVERAGE);
542 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800544
545 // apply the layer's transform, followed by the display's global transform
546 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700547 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700548 if (!s.crop.isEmpty()) {
549 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800550 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000552 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000554 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 activeCrop.clear();
557 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800558 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800559 // This needs to be here as transform.transform(Rect) computes the
560 // transformed rect and then takes the bounding box of the result before
561 // returning. This means
562 // transform.inverse().transform(transform.transform(Rect)) != Rect
563 // in which case we need to make sure the final rect is clipped to the
564 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000565 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
566 activeCrop.clear();
567 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700568 // mark regions outside the crop as transparent
569 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
570 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
571 s.active.w, s.active.h));
572 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
573 activeCrop.left, activeCrop.bottom));
574 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
575 s.active.w, activeCrop.bottom));
576 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800577 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700578 if (!s.finalCrop.isEmpty()) {
579 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000580 frame.clear();
581 }
582 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000584 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
585 frame.clear();
586 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 const Transform& tr(displayDevice->getTransform());
588 Rect transformedFrame = tr.transform(frame);
589 auto error = hwcLayer->setDisplayFrame(transformedFrame);
590 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
591 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
592 transformedFrame.top, transformedFrame.right,
593 transformedFrame.bottom, to_string(error).c_str(),
594 static_cast<int32_t>(error));
595
596 FloatRect sourceCrop = computeCrop(displayDevice);
597 error = hwcLayer->setSourceCrop(sourceCrop);
598 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
599 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
600 sourceCrop.left, sourceCrop.top, sourceCrop.right,
601 sourceCrop.bottom, to_string(error).c_str(),
602 static_cast<int32_t>(error));
603
604 error = hwcLayer->setPlaneAlpha(s.alpha);
605 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
606 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
607 static_cast<int32_t>(error));
608
609 error = hwcLayer->setZOrder(s.z);
610 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
611 mName.string(), s.z, to_string(error).c_str(),
612 static_cast<int32_t>(error));
613#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000614 if (!frame.intersect(hw->getViewport(), &frame)) {
615 frame.clear();
616 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800617 const Transform& tr(hw->getTransform());
618 layer.setFrame(tr.transform(frame));
619 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800620 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800622
Mathias Agopian29a367b2011-07-12 14:51:45 -0700623 /*
624 * Transformations are applied in this order:
625 * 1) buffer orientation/flip/mirror
626 * 2) state transformation (window manager)
627 * 3) layer orientation (screen orientation)
628 * (NOTE: the matrices are multiplied in reverse order)
629 */
630
631 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800632 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633
634 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
635 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700636 * the code below applies the primary display's inverse transform to the
637 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700638 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700639 uint32_t invTransform =
640 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700641 // calculate the inverse transform
642 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
643 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
644 NATIVE_WINDOW_TRANSFORM_FLIP_H;
645 }
646 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700647 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700648 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700649
650 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800651 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652#ifdef USE_HWC2
653 if (orientation & Transform::ROT_INVALID) {
654 // we can only handle simple transformation
655 hwcInfo.forceClientComposition = true;
656 } else {
657 auto transform = static_cast<HWC2::Transform>(orientation);
658 auto error = hwcLayer->setTransform(transform);
659 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
660 "%s (%d)", mName.string(), to_string(transform).c_str(),
661 to_string(error).c_str(), static_cast<int32_t>(error));
662 }
663#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800664 if (orientation & Transform::ROT_INVALID) {
665 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700666 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700667 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800668 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700669 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700671}
672
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673#ifdef USE_HWC2
674void Layer::forceClientComposition(int32_t hwcId) {
675 if (mHwcLayers.count(hwcId) == 0) {
676 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
677 return;
678 }
679
680 mHwcLayers[hwcId].forceClientComposition = true;
681}
682#endif
683
684#ifdef USE_HWC2
685void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
686 // Apply this display's projection's viewport to the visible region
687 // before giving it to the HWC HAL.
688 const Transform& tr = displayDevice->getTransform();
689 const auto& viewport = displayDevice->getViewport();
690 Region visible = tr.transform(visibleRegion.intersect(viewport));
691 auto hwcId = displayDevice->getHwcDisplayId();
692 auto& hwcLayer = mHwcLayers[hwcId].layer;
693 auto error = hwcLayer->setVisibleRegion(visible);
694 if (error != HWC2::Error::None) {
695 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
696 to_string(error).c_str(), static_cast<int32_t>(error));
697 visible.dump(LOG_TAG);
698 }
699
700 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
701 if (error != HWC2::Error::None) {
702 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
703 to_string(error).c_str(), static_cast<int32_t>(error));
704 surfaceDamageRegion.dump(LOG_TAG);
705 }
706
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700707 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700709 setCompositionType(hwcId, HWC2::Composition::Sideband);
710 ALOGV("[%s] Requesting Sideband composition", mName.string());
711 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 if (error != HWC2::Error::None) {
713 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
714 mName.string(), mSidebandStream->handle(),
715 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700717 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 }
719
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700720 // Client or SolidColor layers
721 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr ||
722 mHwcLayers[hwcId].forceClientComposition) {
723 // TODO: This also includes solid color layers, but no API exists to
724 // setup a solid color layer yet
725 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700727 return;
728 }
729
730 // Device or Cursor layers
731 if (mPotentialCursor) {
732 ALOGV("[%s] Requesting Cursor composition", mName.string());
733 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 } else {
735 ALOGV("[%s] Requesting Device composition", mName.string());
736 setCompositionType(hwcId, HWC2::Composition::Device);
737 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700738
739 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
740 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
741 if (error != HWC2::Error::None) {
742 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
743 mActiveBuffer->handle, to_string(error).c_str(),
744 static_cast<int32_t>(error));
745 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746}
747#else
Mathias Agopian42977342012-08-05 00:40:46 -0700748void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700749 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800750 // we have to set the visible region on every frame because
751 // we currently free it during onLayerDisplayed(), which is called
752 // after HWComposer::commit() -- every frame.
753 // Apply this display's projection's viewport to the visible region
754 // before giving it to the HWC HAL.
755 const Transform& tr = hw->getTransform();
756 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
757 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700758 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800759 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700760
Jesse Hall399184a2014-03-03 15:42:54 -0800761 if (mSidebandStream.get()) {
762 layer.setSidebandStream(mSidebandStream);
763 } else {
764 // NOTE: buffer can be NULL if the client never drew into this
765 // layer yet, or if we ran out of memory
766 layer.setBuffer(mActiveBuffer);
767 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700768}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700770
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771#ifdef USE_HWC2
772void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
773 auto hwcId = displayDevice->getHwcDisplayId();
774 if (mHwcLayers.count(hwcId) == 0 ||
775 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
776 return;
777 }
778
779 // This gives us only the "orientation" component of the transform
780 const State& s(getCurrentState());
781
782 // Apply the layer's transform, followed by the display's global transform
783 // Here we're guaranteed that the layer's transform preserves rects
784 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700785 if (!s.crop.isEmpty()) {
786 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800787 }
788 // Subtract the transparent region and snap to the bounds
789 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800790 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700792 if (!s.finalCrop.isEmpty()) {
793 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000794 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800795 auto& displayTransform(displayDevice->getTransform());
796 auto position = displayTransform.transform(frame);
797
798 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
799 position.top);
800 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
801 "to (%d, %d): %s (%d)", mName.string(), position.left,
802 position.top, to_string(error).c_str(),
803 static_cast<int32_t>(error));
804}
805#else
Dan Stozac7014012014-02-14 15:03:43 -0800806void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700807 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700808 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700809
810 // TODO: there is a possible optimization here: we only need to set the
811 // acquire fence the first time a new buffer is acquired on EACH display.
812
Riley Andrews03414a12014-07-01 14:22:59 -0700813 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800814 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800815 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700816 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700817 if (fenceFd == -1) {
818 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
819 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700820 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700821 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700822 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700823}
824
Riley Andrews03414a12014-07-01 14:22:59 -0700825Rect Layer::getPosition(
826 const sp<const DisplayDevice>& hw)
827{
828 // this gives us only the "orientation" component of the transform
829 const State& s(getCurrentState());
830
831 // apply the layer's transform, followed by the display's global transform
832 // here we're guaranteed that the layer's transform preserves rects
833 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700834 if (!s.crop.isEmpty()) {
835 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700836 }
837 // subtract the transparent region and snap to the bounds
838 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800839 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700840 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700841 if (!s.finalCrop.isEmpty()) {
842 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000843 }
Riley Andrews03414a12014-07-01 14:22:59 -0700844 const Transform& tr(hw->getTransform());
845 return Rect(tr.transform(frame));
846}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800847#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700848
Mathias Agopian13127d82013-03-05 17:47:11 -0800849// ---------------------------------------------------------------------------
850// drawing...
851// ---------------------------------------------------------------------------
852
853void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800854 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800855}
856
Dan Stozac7014012014-02-14 15:03:43 -0800857void Layer::draw(const sp<const DisplayDevice>& hw,
858 bool useIdentityTransform) const {
859 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800860}
861
Dan Stozac7014012014-02-14 15:03:43 -0800862void Layer::draw(const sp<const DisplayDevice>& hw) const {
863 onDraw(hw, Region(hw->bounds()), false);
864}
865
866void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
867 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800869 ATRACE_CALL();
870
Mathias Agopiana67932f2011-04-20 14:20:59 -0700871 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700873 // in fact never been drawn into. This happens frequently with
874 // SurfaceView because the WindowManager can't know when the client
875 // has drawn the first time.
876
877 // If there is nothing under us, we paint the screen in black, otherwise
878 // we just skip this update.
879
880 // figure out if there is something below us
881 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700882 const SurfaceFlinger::LayerVector& drawingLayers(
883 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700884 const size_t count = drawingLayers.size();
885 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800886 const sp<Layer>& layer(drawingLayers[i]);
887 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700888 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700889 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700890 }
891 // if not everything below us is covered, we plug the holes!
892 Region holes(clip.subtract(under));
893 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700894 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700895 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 return;
897 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700898
Andy McFadden97eba892012-12-11 15:21:45 -0800899 // Bind the current buffer to the GL texture, and wait for it to be
900 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800901 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
902 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800903 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700904 // Go ahead and draw the buffer anyway; no matter what we do the screen
905 // is probably going to have something visibly wrong.
906 }
907
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700908 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
909
Mathias Agopian875d8e12013-06-07 15:35:48 -0700910 RenderEngine& engine(mFlinger->getRenderEngine());
911
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700912 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700913 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700914 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700915
916 // Query the texture matrix given our current filtering mode.
917 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800918 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
919 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700920
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700921 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
922
923 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700924 * the code below applies the primary display's inverse transform to
925 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700926 */
927
928 // create a 4x4 transform matrix from the display transform flags
929 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
930 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
931 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
932
933 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700934 uint32_t transform =
935 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700936 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
937 tr = tr * rot90;
938 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
939 tr = tr * flipH;
940 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
941 tr = tr * flipV;
942
943 // calculate the inverse
944 tr = inverse(tr);
945
946 // and finally apply it to the original texture matrix
947 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
948 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
949 }
950
Jamie Genniscbb1a952012-05-08 17:05:52 -0700951 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700952 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
953 mTexture.setFiltering(useFiltering);
954 mTexture.setMatrix(textureMatrix);
955
956 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700957 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700958 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700959 }
Dan Stozac7014012014-02-14 15:03:43 -0800960 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700961 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962}
963
Mathias Agopian13127d82013-03-05 17:47:11 -0800964
Dan Stozac7014012014-02-14 15:03:43 -0800965void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
966 const Region& /* clip */, float red, float green, float blue,
967 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800968{
Mathias Agopian19733a32013-08-28 18:13:56 -0700969 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800970 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700971 engine.setupFillWithColor(red, green, blue, alpha);
972 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800973}
974
975void Layer::clearWithOpenGL(
976 const sp<const DisplayDevice>& hw, const Region& clip) const {
977 clearWithOpenGL(hw, clip, 0,0,0,0);
978}
979
Dan Stozac7014012014-02-14 15:03:43 -0800980void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
981 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700982 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800983
Dan Stozac7014012014-02-14 15:03:43 -0800984 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800985
Mathias Agopian13127d82013-03-05 17:47:11 -0800986 /*
987 * NOTE: the way we compute the texture coordinates here produces
988 * different results than when we take the HWC path -- in the later case
989 * the "source crop" is rounded to texel boundaries.
990 * This can produce significantly different results when the texture
991 * is scaled by a large amount.
992 *
993 * The GL code below is more logical (imho), and the difference with
994 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700995 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 * GL composition when a buffer scaling is applied (maybe with some
997 * minimal value)? Or, we could make GL behave like HWC -- but this feel
998 * like more of a hack.
999 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001000 Rect win(computeBounds());
1001
Robert Carrb5d3d262016-03-25 15:08:13 -07001002 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001003 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001004 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001005 win.clear();
1006 }
1007 win = s.active.transform.inverse().transform(win);
1008 if (!win.intersect(computeBounds(), &win)) {
1009 win.clear();
1010 }
1011 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001012
Mathias Agopian3f844832013-08-07 21:24:32 -07001013 float left = float(win.left) / float(s.active.w);
1014 float top = float(win.top) / float(s.active.h);
1015 float right = float(win.right) / float(s.active.w);
1016 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001017
Mathias Agopian875d8e12013-06-07 15:35:48 -07001018 // TODO: we probably want to generate the texture coords with the mesh
1019 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001020 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1021 texCoords[0] = vec2(left, 1.0f - top);
1022 texCoords[1] = vec2(left, 1.0f - bottom);
1023 texCoords[2] = vec2(right, 1.0f - bottom);
1024 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001025
Mathias Agopian875d8e12013-06-07 15:35:48 -07001026 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001027 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001028 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001029 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001030}
1031
Dan Stoza9e56aa02015-11-02 13:00:03 -08001032#ifdef USE_HWC2
1033void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1034 bool callIntoHwc) {
1035 if (mHwcLayers.count(hwcId) == 0) {
1036 ALOGE("setCompositionType called without a valid HWC layer");
1037 return;
1038 }
1039 auto& hwcInfo = mHwcLayers[hwcId];
1040 auto& hwcLayer = hwcInfo.layer;
1041 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1042 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1043 if (hwcInfo.compositionType != type) {
1044 ALOGV(" actually setting");
1045 hwcInfo.compositionType = type;
1046 if (callIntoHwc) {
1047 auto error = hwcLayer->setCompositionType(type);
1048 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1049 "composition type %s: %s (%d)", mName.string(),
1050 to_string(type).c_str(), to_string(error).c_str(),
1051 static_cast<int32_t>(error));
1052 }
1053 }
1054}
1055
1056HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1057 if (mHwcLayers.count(hwcId) == 0) {
1058 ALOGE("getCompositionType called without a valid HWC layer");
1059 return HWC2::Composition::Invalid;
1060 }
1061 return mHwcLayers.at(hwcId).compositionType;
1062}
1063
1064void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1065 if (mHwcLayers.count(hwcId) == 0) {
1066 ALOGE("setClearClientTarget called without a valid HWC layer");
1067 return;
1068 }
1069 mHwcLayers[hwcId].clearClientTarget = clear;
1070}
1071
1072bool Layer::getClearClientTarget(int32_t hwcId) const {
1073 if (mHwcLayers.count(hwcId) == 0) {
1074 ALOGE("getClearClientTarget called without a valid HWC layer");
1075 return false;
1076 }
1077 return mHwcLayers.at(hwcId).clearClientTarget;
1078}
1079#endif
1080
Ruben Brunk1681d952014-06-27 15:51:55 -07001081uint32_t Layer::getProducerStickyTransform() const {
1082 int producerStickyTransform = 0;
1083 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1084 if (ret != OK) {
1085 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1086 strerror(-ret), ret);
1087 return 0;
1088 }
1089 return static_cast<uint32_t>(producerStickyTransform);
1090}
1091
Dan Stozacac35382016-01-27 12:21:06 -08001092uint64_t Layer::getHeadFrameNumber() const {
1093 Mutex::Autolock lock(mQueueItemLock);
1094 if (!mQueueItems.empty()) {
1095 return mQueueItems[0].mFrameNumber;
1096 } else {
1097 return mCurrentFrameNumber;
1098 }
1099}
1100
Dan Stoza1ce65812016-06-15 16:26:27 -07001101bool Layer::headFenceHasSignaled() const {
1102#ifdef USE_HWC2
1103 Mutex::Autolock lock(mQueueItemLock);
1104 if (mQueueItems.empty()) {
1105 return true;
1106 }
1107 if (mQueueItems[0].mIsDroppable) {
1108 // Even though this buffer's fence may not have signaled yet, it could
1109 // be replaced by another buffer before it has a chance to, which means
1110 // that it's possible to get into a situation where a buffer is never
1111 // able to be latched. To avoid this, grab this buffer anyway.
1112 return true;
1113 }
1114 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1115#else
1116 return true;
1117#endif
1118}
1119
Dan Stozacac35382016-01-27 12:21:06 -08001120bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1121 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1122 // Don't bother with a SyncPoint, since we've already latched the
1123 // relevant frame
1124 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001125 }
1126
Dan Stozacac35382016-01-27 12:21:06 -08001127 Mutex::Autolock lock(mLocalSyncPointMutex);
1128 mLocalSyncPoints.push_back(point);
1129 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001130}
1131
Mathias Agopian13127d82013-03-05 17:47:11 -08001132void Layer::setFiltering(bool filtering) {
1133 mFiltering = filtering;
1134}
1135
1136bool Layer::getFiltering() const {
1137 return mFiltering;
1138}
1139
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001140// As documented in libhardware header, formats in the range
1141// 0x100 - 0x1FF are specific to the HAL implementation, and
1142// are known to have no alpha channel
1143// TODO: move definition for device-specific range into
1144// hardware.h, instead of using hard-coded values here.
1145#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1146
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001147bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001148 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1149 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001150 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001151 switch (format) {
1152 case HAL_PIXEL_FORMAT_RGBA_8888:
1153 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001154 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001155 }
1156 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001157 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001158}
1159
Mathias Agopian13127d82013-03-05 17:47:11 -08001160// ----------------------------------------------------------------------------
1161// local state
1162// ----------------------------------------------------------------------------
1163
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001164static void boundPoint(vec2* point, const Rect& crop) {
1165 if (point->x < crop.left) {
1166 point->x = crop.left;
1167 }
1168 if (point->x > crop.right) {
1169 point->x = crop.right;
1170 }
1171 if (point->y < crop.top) {
1172 point->y = crop.top;
1173 }
1174 if (point->y > crop.bottom) {
1175 point->y = crop.bottom;
1176 }
1177}
1178
Dan Stozac7014012014-02-14 15:03:43 -08001179void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1180 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001181{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001182 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001183 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001184 const uint32_t hw_h = hw->getHeight();
1185 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001186 if (!s.crop.isEmpty()) {
1187 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001188 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001189 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001190 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001191
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001192 vec2 lt = vec2(win.left, win.top);
1193 vec2 lb = vec2(win.left, win.bottom);
1194 vec2 rb = vec2(win.right, win.bottom);
1195 vec2 rt = vec2(win.right, win.top);
1196
1197 if (!useIdentityTransform) {
1198 lt = s.active.transform.transform(lt);
1199 lb = s.active.transform.transform(lb);
1200 rb = s.active.transform.transform(rb);
1201 rt = s.active.transform.transform(rt);
1202 }
1203
Robert Carrb5d3d262016-03-25 15:08:13 -07001204 if (!s.finalCrop.isEmpty()) {
1205 boundPoint(&lt, s.finalCrop);
1206 boundPoint(&lb, s.finalCrop);
1207 boundPoint(&rb, s.finalCrop);
1208 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001209 }
1210
Mathias Agopianff2ed702013-09-01 21:36:12 -07001211 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001212 position[0] = tr.transform(lt);
1213 position[1] = tr.transform(lb);
1214 position[2] = tr.transform(rb);
1215 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001216 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001217 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 }
1219}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001220
Andy McFadden4125a4f2014-01-29 17:17:11 -08001221bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001222{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001223 // if we don't have a buffer yet, we're translucent regardless of the
1224 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001225 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001226 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001227 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228
1229 // if the layer has the opaque flag, then we're always opaque,
1230 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001231 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001232}
1233
Dan Stoza23116082015-06-18 14:58:39 -07001234bool Layer::isSecure() const
1235{
1236 const Layer::State& s(mDrawingState);
1237 return (s.flags & layer_state_t::eLayerSecure);
1238}
1239
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001240bool Layer::isProtected() const
1241{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001242 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001243 return (activeBuffer != 0) &&
1244 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1245}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001246
Mathias Agopian13127d82013-03-05 17:47:11 -08001247bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001248 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001249}
1250
1251bool Layer::isCropped() const {
1252 return !mCurrentCrop.isEmpty();
1253}
1254
1255bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1256 return mNeedsFiltering || hw->needsFiltering();
1257}
1258
1259void Layer::setVisibleRegion(const Region& visibleRegion) {
1260 // always called from main thread
1261 this->visibleRegion = visibleRegion;
1262}
1263
1264void Layer::setCoveredRegion(const Region& coveredRegion) {
1265 // always called from main thread
1266 this->coveredRegion = coveredRegion;
1267}
1268
1269void Layer::setVisibleNonTransparentRegion(const Region&
1270 setVisibleNonTransparentRegion) {
1271 // always called from main thread
1272 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1273}
1274
1275// ----------------------------------------------------------------------------
1276// transaction
1277// ----------------------------------------------------------------------------
1278
Dan Stoza7dde5992015-05-22 09:51:44 -07001279void Layer::pushPendingState() {
1280 if (!mCurrentState.modified) {
1281 return;
1282 }
1283
Dan Stoza7dde5992015-05-22 09:51:44 -07001284 // If this transaction is waiting on the receipt of a frame, generate a sync
1285 // point and send it to the remote layer.
1286 if (mCurrentState.handle != nullptr) {
1287 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1288 sp<Layer> handleLayer = handle->owner.promote();
1289 if (handleLayer == nullptr) {
1290 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1291 // If we can't promote the layer we are intended to wait on,
1292 // then it is expired or otherwise invalid. Allow this transaction
1293 // to be applied as per normal (no synchronization).
1294 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001295 } else {
1296 auto syncPoint = std::make_shared<SyncPoint>(
1297 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001298 if (handleLayer->addSyncPoint(syncPoint)) {
1299 mRemoteSyncPoints.push_back(std::move(syncPoint));
1300 } else {
1301 // We already missed the frame we're supposed to synchronize
1302 // on, so go ahead and apply the state update
1303 mCurrentState.handle = nullptr;
1304 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001305 }
1306
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 // Wake us up to check if the frame has been received
1308 setTransactionFlags(eTransactionNeeded);
1309 }
1310 mPendingStates.push_back(mCurrentState);
1311}
1312
Pablo Ceballos05289c22016-04-14 15:49:55 -07001313void Layer::popPendingState(State* stateToCommit) {
1314 auto oldFlags = stateToCommit->flags;
1315 *stateToCommit = mPendingStates[0];
1316 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1317 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001318
1319 mPendingStates.removeAt(0);
1320}
1321
Pablo Ceballos05289c22016-04-14 15:49:55 -07001322bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 bool stateUpdateAvailable = false;
1324 while (!mPendingStates.empty()) {
1325 if (mPendingStates[0].handle != nullptr) {
1326 if (mRemoteSyncPoints.empty()) {
1327 // If we don't have a sync point for this, apply it anyway. It
1328 // will be visually wrong, but it should keep us from getting
1329 // into too much trouble.
1330 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001331 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001332 stateUpdateAvailable = true;
1333 continue;
1334 }
1335
Dan Stozacac35382016-01-27 12:21:06 -08001336 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1337 mPendingStates[0].frameNumber) {
1338 ALOGE("[%s] Unexpected sync point frame number found",
1339 mName.string());
1340
1341 // Signal our end of the sync point and then dispose of it
1342 mRemoteSyncPoints.front()->setTransactionApplied();
1343 mRemoteSyncPoints.pop_front();
1344 continue;
1345 }
1346
Dan Stoza7dde5992015-05-22 09:51:44 -07001347 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1348 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001349 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001350 stateUpdateAvailable = true;
1351
1352 // Signal our end of the sync point and then dispose of it
1353 mRemoteSyncPoints.front()->setTransactionApplied();
1354 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001355 } else {
1356 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001358 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001359 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 stateUpdateAvailable = true;
1361 }
1362 }
1363
1364 // If we still have pending updates, wake SurfaceFlinger back up and point
1365 // it at this layer so we can process them
1366 if (!mPendingStates.empty()) {
1367 setTransactionFlags(eTransactionNeeded);
1368 mFlinger->setTransactionFlags(eTraversalNeeded);
1369 }
1370
1371 mCurrentState.modified = false;
1372 return stateUpdateAvailable;
1373}
1374
1375void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001376 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001377 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001378 Mutex::Autolock lock(mLocalSyncPointMutex);
1379 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001380 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001381 point->setFrameAvailable();
1382 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001383 }
1384}
1385
Mathias Agopian13127d82013-03-05 17:47:11 -08001386uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001387 ATRACE_CALL();
1388
Dan Stoza7dde5992015-05-22 09:51:44 -07001389 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001390 Layer::State c = getCurrentState();
1391 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001392 return 0;
1393 }
1394
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001395 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001397 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1398 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001399
1400 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001401 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001402 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001403 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001404 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001405 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001406 " drawing={ 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",
Robert Carrc3574f72016-03-24 12:19:32 -07001408 this, getName().string(), mCurrentTransform,
1409 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001410 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001411 c.crop.left,
1412 c.crop.top,
1413 c.crop.right,
1414 c.crop.bottom,
1415 c.crop.getWidth(),
1416 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001417 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001418 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001419 s.crop.left,
1420 s.crop.top,
1421 s.crop.right,
1422 s.crop.bottom,
1423 s.crop.getWidth(),
1424 s.crop.getHeight(),
1425 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001427 // record the new size, form this point on, when the client request
1428 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001429 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001430 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001432
Robert Carr82364e32016-05-15 11:27:47 -07001433 const bool resizePending = (c.requested.w != c.active.w) ||
1434 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001435 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001436 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001437 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001438 // if we have a pending resize, unless we are in fixed-size mode.
1439 // the drawing state will be updated only once we receive a buffer
1440 // with the correct size.
1441 //
1442 // in particular, we want to make sure the clip (which is part
1443 // of the geometry state) is latched together with the size but is
1444 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001445 //
1446 // If a sideband stream is attached, however, we want to skip this
1447 // optimization so that transactions aren't missed when a buffer
1448 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001449
1450 flags |= eDontUpdateGeometryState;
1451 }
1452 }
1453
Mathias Agopian13127d82013-03-05 17:47:11 -08001454 // always set active to requested, unless we're asked not to
1455 // this is used by Layer, which special cases resizes.
1456 if (flags & eDontUpdateGeometryState) {
1457 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001458 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001459 if (mFreezePositionUpdates) {
1460 float tx = c.active.transform.tx();
1461 float ty = c.active.transform.ty();
1462 c.active = c.requested;
1463 c.active.transform.set(tx, ty);
1464 editCurrentState.active = c.active;
1465 } else {
1466 editCurrentState.active = editCurrentState.requested;
1467 c.active = c.requested;
1468 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001469 }
1470
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001471 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001472 // invalidate and recompute the visible regions if needed
1473 flags |= Layer::eVisibleRegion;
1474 }
1475
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001476 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 // invalidate and recompute the visible regions if needed
1478 flags |= eVisibleRegion;
1479 this->contentDirty = true;
1480
1481 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001482 const uint8_t type = c.active.transform.getType();
1483 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001484 (type >= Transform::SCALE));
1485 }
1486
Dan Stozac8145172016-04-28 16:29:06 -07001487 // If the layer is hidden, signal and clear out all local sync points so
1488 // that transactions for layers depending on this layer's frames becoming
1489 // visible are not blocked
1490 if (c.flags & layer_state_t::eLayerHidden) {
1491 Mutex::Autolock lock(mLocalSyncPointMutex);
1492 for (auto& point : mLocalSyncPoints) {
1493 point->setFrameAvailable();
1494 }
1495 mLocalSyncPoints.clear();
1496 }
1497
Mathias Agopian13127d82013-03-05 17:47:11 -08001498 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001499 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001500 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501}
1502
Pablo Ceballos05289c22016-04-14 15:49:55 -07001503void Layer::commitTransaction(const State& stateToCommit) {
1504 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001505}
1506
Mathias Agopian13127d82013-03-05 17:47:11 -08001507uint32_t Layer::getTransactionFlags(uint32_t flags) {
1508 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1509}
1510
1511uint32_t Layer::setTransactionFlags(uint32_t flags) {
1512 return android_atomic_or(flags, &mTransactionFlags);
1513}
1514
Robert Carr82364e32016-05-15 11:27:47 -07001515bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001516 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001517 return false;
1518 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001519
1520 // We update the requested and active position simultaneously because
1521 // we want to apply the position portion of the transform matrix immediately,
1522 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001523 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001524 if (immediate && !mFreezePositionUpdates) {
1525 mCurrentState.active.transform.set(x, y);
1526 }
1527 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001528
Dan Stoza7dde5992015-05-22 09:51:44 -07001529 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001530 setTransactionFlags(eTransactionNeeded);
1531 return true;
1532}
Robert Carr82364e32016-05-15 11:27:47 -07001533
Mathias Agopian13127d82013-03-05 17:47:11 -08001534bool Layer::setLayer(uint32_t z) {
1535 if (mCurrentState.z == z)
1536 return false;
1537 mCurrentState.sequence++;
1538 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001539 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001540 setTransactionFlags(eTransactionNeeded);
1541 return true;
1542}
1543bool Layer::setSize(uint32_t w, uint32_t h) {
1544 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1545 return false;
1546 mCurrentState.requested.w = w;
1547 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001548 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001549 setTransactionFlags(eTransactionNeeded);
1550 return true;
1551}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001552#ifdef USE_HWC2
1553bool Layer::setAlpha(float alpha) {
1554#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001555bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001556#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001557 if (mCurrentState.alpha == alpha)
1558 return false;
1559 mCurrentState.sequence++;
1560 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001561 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001562 setTransactionFlags(eTransactionNeeded);
1563 return true;
1564}
1565bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1566 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001567 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001568 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001569 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 setTransactionFlags(eTransactionNeeded);
1571 return true;
1572}
1573bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001574 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001575 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001576 setTransactionFlags(eTransactionNeeded);
1577 return true;
1578}
1579bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1580 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1581 if (mCurrentState.flags == newFlags)
1582 return false;
1583 mCurrentState.sequence++;
1584 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001585 mCurrentState.mask = mask;
1586 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001587 setTransactionFlags(eTransactionNeeded);
1588 return true;
1589}
Robert Carr99e27f02016-06-16 15:18:02 -07001590
1591bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001592 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001593 return false;
1594 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001595 mCurrentState.requestedCrop = crop;
1596 if (immediate) {
1597 mCurrentState.crop = crop;
1598 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001599 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001600 setTransactionFlags(eTransactionNeeded);
1601 return true;
1602}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001603bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001604 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001605 return false;
1606 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001607 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001608 mCurrentState.modified = true;
1609 setTransactionFlags(eTransactionNeeded);
1610 return true;
1611}
Mathias Agopian13127d82013-03-05 17:47:11 -08001612
Robert Carrc3574f72016-03-24 12:19:32 -07001613bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1614 if (scalingMode == mOverrideScalingMode)
1615 return false;
1616 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001617 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001618 return true;
1619}
1620
1621uint32_t Layer::getEffectiveScalingMode() const {
1622 if (mOverrideScalingMode >= 0) {
1623 return mOverrideScalingMode;
1624 }
1625 return mCurrentScalingMode;
1626}
1627
Mathias Agopian13127d82013-03-05 17:47:11 -08001628bool Layer::setLayerStack(uint32_t layerStack) {
1629 if (mCurrentState.layerStack == layerStack)
1630 return false;
1631 mCurrentState.sequence++;
1632 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001633 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001634 setTransactionFlags(eTransactionNeeded);
1635 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001636}
1637
Dan Stoza7dde5992015-05-22 09:51:44 -07001638void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1639 uint64_t frameNumber) {
1640 mCurrentState.handle = handle;
1641 mCurrentState.frameNumber = frameNumber;
1642 // We don't set eTransactionNeeded, because just receiving a deferral
1643 // request without any other state updates shouldn't actually induce a delay
1644 mCurrentState.modified = true;
1645 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001646 mCurrentState.handle = nullptr;
1647 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001648 mCurrentState.modified = false;
1649}
1650
Dan Stozaee44edd2015-03-23 15:50:23 -07001651void Layer::useSurfaceDamage() {
1652 if (mFlinger->mForceFullDamage) {
1653 surfaceDamageRegion = Region::INVALID_REGION;
1654 } else {
1655 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1656 }
1657}
1658
1659void Layer::useEmptyDamage() {
1660 surfaceDamageRegion.clear();
1661}
1662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663// ----------------------------------------------------------------------------
1664// pageflip handling...
1665// ----------------------------------------------------------------------------
1666
Dan Stoza6b9454d2014-11-07 16:00:59 -08001667bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001668 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001669 return true;
1670 }
1671
Dan Stoza6b9454d2014-11-07 16:00:59 -08001672 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001673 if (mQueueItems.empty()) {
1674 return false;
1675 }
1676 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001677 nsecs_t expectedPresent =
1678 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001679
1680 // Ignore timestamps more than a second in the future
1681 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1682 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1683 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1684 expectedPresent);
1685
1686 bool isDue = timestamp < expectedPresent;
1687 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001688}
1689
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001690bool Layer::onPreComposition() {
1691 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001692 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001693}
1694
Dan Stozae77c7662016-05-13 11:37:28 -07001695bool Layer::onPostComposition() {
1696 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001697 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001698 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001699 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1700
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001701 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001702 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001703 mFrameTracker.setFrameReadyFence(frameReadyFence);
1704 } else {
1705 // There was no fence for this frame, so assume that it was ready
1706 // to be presented at the desired present time.
1707 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1708 }
1709
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001710 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001711#ifdef USE_HWC2
1712 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1713#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001714 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001715#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001716 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001717 mFrameTracker.setActualPresentFence(presentFence);
1718 } else {
1719 // The HWC doesn't support present fences, so use the refresh
1720 // timestamp instead.
1721 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1722 mFrameTracker.setActualPresentTime(presentTime);
1723 }
1724
1725 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001726 mFrameLatencyNeeded = false;
1727 }
Dan Stozae77c7662016-05-13 11:37:28 -07001728 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001729}
1730
Dan Stoza9e56aa02015-11-02 13:00:03 -08001731#ifdef USE_HWC2
1732void Layer::releasePendingBuffer() {
1733 mSurfaceFlingerConsumer->releasePendingBuffer();
1734}
1735#endif
1736
Mathias Agopianda27af92012-09-13 18:17:13 -07001737bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001738 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001739#ifdef USE_HWC2
1740 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1741 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1742#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001743 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001744 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001745#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001746}
1747
Mathias Agopian4fec8732012-06-29 14:12:52 -07001748Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001750 ATRACE_CALL();
1751
Jesse Hall399184a2014-03-03 15:42:54 -08001752 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1753 // mSidebandStreamChanged was true
1754 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001755 if (mSidebandStream != NULL) {
1756 setTransactionFlags(eTransactionNeeded);
1757 mFlinger->setTransactionFlags(eTraversalNeeded);
1758 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001759 recomputeVisibleRegions = true;
1760
1761 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001762 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001763 }
1764
Mathias Agopian4fec8732012-06-29 14:12:52 -07001765 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001766 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001767
1768 // if we've already called updateTexImage() without going through
1769 // a composition step, we have to skip this layer at this point
1770 // because we cannot call updateTeximage() without a corresponding
1771 // compositionComplete() call.
1772 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001773 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001774 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001775 }
1776
Dan Stoza1ce65812016-06-15 16:26:27 -07001777 // If the head buffer's acquire fence hasn't signaled yet, return and
1778 // try again later
1779 if (!headFenceHasSignaled()) {
1780 mFlinger->signalLayerUpdate();
1781 return outDirtyRegion;
1782 }
1783
Jamie Gennis351a5132011-09-14 18:23:37 -07001784 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001785 const State& s(getDrawingState());
1786 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001787 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001788
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001789 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001790 Layer::State& front;
1791 Layer::State& current;
1792 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001793 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001794 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001795 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001796 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001797
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001798 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001799 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001800 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001801 int32_t overrideScalingMode,
1802 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001803 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001804 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001805 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001806 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001807 overrideScalingMode(overrideScalingMode),
1808 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001809 }
1810
1811 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001812 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001813 if (buf == NULL) {
1814 return false;
1815 }
1816
1817 uint32_t bufWidth = buf->getWidth();
1818 uint32_t bufHeight = buf->getHeight();
1819
1820 // check that we received a buffer of the right size
1821 // (Take the buffer's orientation into account)
1822 if (item.mTransform & Transform::ROT_90) {
1823 swap(bufWidth, bufHeight);
1824 }
1825
Robert Carrc3574f72016-03-24 12:19:32 -07001826 int actualScalingMode = overrideScalingMode >= 0 ?
1827 overrideScalingMode : item.mScalingMode;
1828 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001829 if (front.active != front.requested) {
1830
1831 if (isFixedSize ||
1832 (bufWidth == front.requested.w &&
1833 bufHeight == front.requested.h))
1834 {
1835 // Here we pretend the transaction happened by updating the
1836 // current and drawing states. Drawing state is only accessed
1837 // in this thread, no need to have it locked
1838 front.active = front.requested;
1839
1840 // We also need to update the current state so that
1841 // we don't end-up overwriting the drawing state with
1842 // this stale current state during the next transaction
1843 //
1844 // NOTE: We don't need to hold the transaction lock here
1845 // because State::active is only accessed from this thread.
1846 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001847 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001848
1849 // recompute visible region
1850 recomputeVisibleRegions = true;
1851 }
1852
1853 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001854 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001855 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001856 " requested={ wh={%4u,%4u} }}\n",
1857 name,
Andy McFadden69052052012-09-14 16:10:11 -07001858 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001859 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001860 front.crop.left,
1861 front.crop.top,
1862 front.crop.right,
1863 front.crop.bottom,
1864 front.crop.getWidth(),
1865 front.crop.getHeight(),
1866 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001867 }
1868
Ruben Brunk1681d952014-06-27 15:51:55 -07001869 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001870 if (front.active.w != bufWidth ||
1871 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001872 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001873 ALOGE("[%s] rejecting buffer: "
1874 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1875 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001876 return true;
1877 }
1878 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001879
1880 // if the transparent region has changed (this test is
1881 // conservative, but that's fine, worst case we're doing
1882 // a bit of extra work), we latch the new one and we
1883 // trigger a visible-region recompute.
1884 if (!front.activeTransparentRegion.isTriviallyEqual(
1885 front.requestedTransparentRegion)) {
1886 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001887
1888 // We also need to update the current state so that
1889 // we don't end-up overwriting the drawing state with
1890 // this stale current state during the next transaction
1891 //
1892 // NOTE: We don't need to hold the transaction lock here
1893 // because State::active is only accessed from this thread.
1894 current.activeTransparentRegion = front.activeTransparentRegion;
1895
1896 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001897 recomputeVisibleRegions = true;
1898 }
1899
Robert Carr99e27f02016-06-16 15:18:02 -07001900 if (front.crop != front.requestedCrop) {
1901 front.crop = front.requestedCrop;
1902 current.crop = front.requestedCrop;
1903 recomputeVisibleRegions = true;
1904 }
Robert Carra3920732016-06-20 21:49:49 -07001905 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001906
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001907 return false;
1908 }
1909 };
1910
Ruben Brunk1681d952014-06-27 15:51:55 -07001911 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001912 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001913 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001914
Dan Stozacac35382016-01-27 12:21:06 -08001915
1916 // Check all of our local sync points to ensure that all transactions
1917 // which need to have been applied prior to the frame which is about to
1918 // be latched have signaled
1919
1920 auto headFrameNumber = getHeadFrameNumber();
1921 bool matchingFramesFound = false;
1922 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001923 {
Dan Stozacac35382016-01-27 12:21:06 -08001924 Mutex::Autolock lock(mLocalSyncPointMutex);
1925 for (auto& point : mLocalSyncPoints) {
1926 if (point->getFrameNumber() > headFrameNumber) {
1927 break;
1928 }
1929
1930 matchingFramesFound = true;
1931
1932 if (!point->frameIsAvailable()) {
1933 // We haven't notified the remote layer that the frame for
1934 // this point is available yet. Notify it now, and then
1935 // abort this attempt to latch.
1936 point->setFrameAvailable();
1937 allTransactionsApplied = false;
1938 break;
1939 }
1940
1941 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001942 }
1943 }
1944
Dan Stozacac35382016-01-27 12:21:06 -08001945 if (matchingFramesFound && !allTransactionsApplied) {
1946 mFlinger->signalLayerUpdate();
1947 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001948 }
1949
Pablo Ceballos06312182015-10-07 16:32:12 -07001950 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1951 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos2dcb3632016-03-17 15:50:23 -07001952 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001953 // buffer mode.
1954 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001955 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001956 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001957 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001958 if (updateResult == BufferQueue::PRESENT_LATER) {
1959 // Producer doesn't want buffer to be displayed yet. Signal a
1960 // layer update so we check again at the next opportunity.
1961 mFlinger->signalLayerUpdate();
1962 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001963 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1964 // If the buffer has been rejected, remove it from the shadow queue
1965 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001966 if (queuedBuffer) {
1967 Mutex::Autolock lock(mQueueItemLock);
1968 mQueueItems.removeAt(0);
1969 android_atomic_dec(&mQueuedFrames);
1970 }
Dan Stozaecc50402015-04-28 14:42:06 -07001971 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001972 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1973 // This can occur if something goes wrong when trying to create the
1974 // EGLImage for this buffer. If this happens, the buffer has already
1975 // been released, so we need to clean up the queue and bug out
1976 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001977 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001978 Mutex::Autolock lock(mQueueItemLock);
1979 mQueueItems.clear();
1980 android_atomic_and(0, &mQueuedFrames);
1981 }
1982
1983 // Once we have hit this state, the shadow queue may no longer
1984 // correctly reflect the incoming BufferQueue's contents, so even if
1985 // updateTexImage starts working, the only safe course of action is
1986 // to continue to ignore updates.
1987 mUpdateTexImageFailed = true;
1988
1989 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001990 }
1991
Pablo Ceballos06312182015-10-07 16:32:12 -07001992 if (queuedBuffer) {
1993 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001994 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1995
Dan Stoza6b9454d2014-11-07 16:00:59 -08001996 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001997
1998 // Remove any stale buffers that have been dropped during
1999 // updateTexImage
2000 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2001 mQueueItems.removeAt(0);
2002 android_atomic_dec(&mQueuedFrames);
2003 }
2004
Dan Stoza6b9454d2014-11-07 16:00:59 -08002005 mQueueItems.removeAt(0);
2006 }
2007
Dan Stozaecc50402015-04-28 14:42:06 -07002008
Andy McFadden1585c4d2013-06-28 13:52:40 -07002009 // Decrement the queued-frames count. Signal another event if we
2010 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002011 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002012 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002013 mFlinger->signalLayerUpdate();
2014 }
2015
2016 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002017 // something happened!
2018 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002019 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002020 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002021
Jamie Gennis351a5132011-09-14 18:23:37 -07002022 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002023 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002024 if (mActiveBuffer == NULL) {
2025 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002026 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002027 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002028
Mathias Agopian4824d402012-06-04 18:16:30 -07002029 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002030 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002031 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002032 // the first time we receive a buffer, we need to trigger a
2033 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002034 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002035 }
2036
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002037 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2038 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2039 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002040 if ((crop != mCurrentCrop) ||
2041 (transform != mCurrentTransform) ||
2042 (scalingMode != mCurrentScalingMode))
2043 {
2044 mCurrentCrop = crop;
2045 mCurrentTransform = transform;
2046 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002047 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002048 }
2049
2050 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002051 uint32_t bufWidth = mActiveBuffer->getWidth();
2052 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002053 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2054 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002055 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002056 }
2057 }
2058
2059 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002060 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002061 recomputeVisibleRegions = true;
2062 }
2063
Dan Stozacac35382016-01-27 12:21:06 -08002064 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2065
2066 // Remove any sync points corresponding to the buffer which was just
2067 // latched
2068 {
2069 Mutex::Autolock lock(mLocalSyncPointMutex);
2070 auto point = mLocalSyncPoints.begin();
2071 while (point != mLocalSyncPoints.end()) {
2072 if (!(*point)->frameIsAvailable() ||
2073 !(*point)->transactionIsApplied()) {
2074 // This sync point must have been added since we started
2075 // latching. Don't drop it yet.
2076 ++point;
2077 continue;
2078 }
2079
2080 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2081 point = mLocalSyncPoints.erase(point);
2082 } else {
2083 ++point;
2084 }
2085 }
2086 }
2087
Mathias Agopian4fec8732012-06-29 14:12:52 -07002088 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002089 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002090
2091 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002092 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002093 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002094 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095}
2096
Mathias Agopiana67932f2011-04-20 14:20:59 -07002097uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002098{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002099 // TODO: should we do something special if mSecure is set?
2100 if (mProtectedByApp) {
2101 // need a hardware-protected path to external video sink
2102 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002103 }
Riley Andrews03414a12014-07-01 14:22:59 -07002104 if (mPotentialCursor) {
2105 usage |= GraphicBuffer::USAGE_CURSOR;
2106 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002107 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002108 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002109}
2110
Mathias Agopian84300952012-11-21 16:02:13 -08002111void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002112 uint32_t orientation = 0;
2113 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002114 // The transform hint is used to improve performance, but we can
2115 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002116 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002117 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002118 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002119 if (orientation & Transform::ROT_INVALID) {
2120 orientation = 0;
2121 }
2122 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002123 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002124}
2125
Mathias Agopian13127d82013-03-05 17:47:11 -08002126// ----------------------------------------------------------------------------
2127// debugging
2128// ----------------------------------------------------------------------------
2129
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002130void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002131{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002132 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002133
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002134 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002135 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002136 "+ %s %p (%s)\n",
2137 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002138 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002139
Mathias Agopian2ca79392013-04-02 18:30:32 -07002140 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002141 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002142 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002143 sp<Client> client(mClientRef.promote());
2144
Mathias Agopian74d211a2013-04-22 16:55:35 +02002145 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002146 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2147 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002148 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002149#ifdef USE_HWC2
2150 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2151#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002152 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002153#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002154 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002155 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 -07002156 s.crop.left, s.crop.top,
2157 s.crop.right, s.crop.bottom,
2158 s.finalCrop.left, s.finalCrop.top,
2159 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002160 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002161 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002162 s.active.transform[0][0], s.active.transform[0][1],
2163 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002164 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002165
2166 sp<const GraphicBuffer> buf0(mActiveBuffer);
2167 uint32_t w0=0, h0=0, s0=0, f0=0;
2168 if (buf0 != 0) {
2169 w0 = buf0->getWidth();
2170 h0 = buf0->getHeight();
2171 s0 = buf0->getStride();
2172 f0 = buf0->format;
2173 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002174 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002175 " "
2176 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2177 " queued-frames=%d, mRefreshPending=%d\n",
2178 mFormat, w0, h0, s0,f0,
2179 mQueuedFrames, mRefreshPending);
2180
Mathias Agopian13127d82013-03-05 17:47:11 -08002181 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002182 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002183 }
2184}
2185
Svetoslavd85084b2014-03-20 10:28:31 -07002186void Layer::dumpFrameStats(String8& result) const {
2187 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002188}
2189
Svetoslavd85084b2014-03-20 10:28:31 -07002190void Layer::clearFrameStats() {
2191 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002192}
2193
Jamie Gennis6547ff42013-07-16 20:12:42 -07002194void Layer::logFrameStats() {
2195 mFrameTracker.logAndResetStats(mName);
2196}
2197
Svetoslavd85084b2014-03-20 10:28:31 -07002198void Layer::getFrameStats(FrameStats* outStats) const {
2199 mFrameTracker.getStats(outStats);
2200}
2201
Pablo Ceballos40845df2016-01-25 17:41:15 -08002202void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2203 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2204 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2205 *outName = mName;
2206 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2207
2208#ifdef USE_HWC2
2209 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2210 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2211 HWC2::Composition::Client : true;
2212#else
2213 *outIsGlesComposition = mIsGlesComposition;
2214#endif
2215 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2216 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2217 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2218}
Dan Stozae77c7662016-05-13 11:37:28 -07002219
2220std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2221 bool forceFlush) {
2222 std::vector<OccupancyTracker::Segment> history;
2223 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2224 &history);
2225 if (result != NO_ERROR) {
2226 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2227 result);
2228 return {};
2229 }
2230 return history;
2231}
2232
Robert Carr367c5682016-06-20 11:55:28 -07002233bool Layer::getTransformToDisplayInverse() const {
2234 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2235}
2236
Mathias Agopian13127d82013-03-05 17:47:11 -08002237// ---------------------------------------------------------------------------
2238
2239Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2240 const sp<Layer>& layer)
2241 : mFlinger(flinger), mLayer(layer) {
2242}
2243
2244Layer::LayerCleaner::~LayerCleaner() {
2245 // destroy client resources
2246 mFlinger->onLayerDestroyed(mLayer);
2247}
2248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002249// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002250}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002251
2252#if defined(__gl_h_)
2253#error "don't include gl/gl.h in this file"
2254#endif
2255
2256#if defined(__gl2_h_)
2257#error "don't include gl2/gl2.h in this file"
2258#endif