blob: 6a3d62becd7233cd6182376ea8e8786ddb036cd0 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Dan Stoza6b9454d2014-11-07 16:00:59 -080040#include <gui/BufferItem.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020044#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include "Layer.h"
Dan Stozab9b08832014-03-13 11:55:57 -070047#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian1b031492012-06-20 17:51:20 -070050#include "DisplayHardware/HWComposer.h"
51
Mathias Agopian875d8e12013-06-07 15:35:48 -070052#include "RenderEngine/RenderEngine.h"
53
Dan Stozac5da2712016-07-20 15:38:12 -070054#include <mutex>
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#define DEBUG_RESIZE 0
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058namespace android {
59
60// ---------------------------------------------------------------------------
61
Mathias Agopian13127d82013-03-05 17:47:11 -080062int32_t Layer::sSequence = 1;
63
Mathias Agopian4d9b8222013-03-12 17:11:48 -070064Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
65 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080066 : contentDirty(false),
67 sequence(uint32_t(android_atomic_inc(&sSequence))),
68 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070069 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mPremultipliedAlpha(true),
71 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070074 mPendingStateMutex(),
75 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080077 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070079 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070080 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080082 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080083 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080084 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080085 mFiltering(false),
86 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070087 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Pablo Ceballos40845df2016-01-25 17:41:15 -080088#ifndef USE_HWC2
89 mIsGlesComposition(false),
90#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080091 mProtectedByApp(false),
92 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070093 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070094 mPotentialCursor(false),
95 mQueueItemLock(),
96 mQueueItemCondition(),
97 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070098 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080099 mUpdateTexImageFailed(false),
Robert Carr82364e32016-05-15 11:27:47 -0700100 mAutoRefresh(false),
101 mFreezePositionUpdates(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103#ifdef USE_HWC2
104 ALOGV("Creating Layer %s", name.string());
105#endif
106
Mathias Agopiana67932f2011-04-20 14:20:59 -0700107 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700108 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700109 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
111 uint32_t layerFlags = 0;
112 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800113 layerFlags |= layer_state_t::eLayerHidden;
114 if (flags & ISurfaceComposerClient::eOpaque)
115 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700116 if (flags & ISurfaceComposerClient::eSecure)
117 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118
119 if (flags & ISurfaceComposerClient::eNonPremultiplied)
120 mPremultipliedAlpha = false;
121
122 mName = name;
123
124 mCurrentState.active.w = w;
125 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800126 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700127 mCurrentState.crop.makeInvalid();
128 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130#ifdef USE_HWC2
131 mCurrentState.alpha = 1.0f;
132#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800134#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135 mCurrentState.layerStack = 0;
136 mCurrentState.flags = layerFlags;
137 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138 mCurrentState.requested = mCurrentState.active;
139
140 // drawing state & current state are identical
141 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700142
Dan Stoza9e56aa02015-11-02 13:00:03 -0800143#ifdef USE_HWC2
144 const auto& hwc = flinger->getHwComposer();
145 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
146 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
147#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 nsecs_t displayPeriod =
149 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700151 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800152}
153
Mathias Agopian3f844832013-08-07 21:24:32 -0700154void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800155 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700156 sp<IGraphicBufferProducer> producer;
157 sp<IGraphicBufferConsumer> consumer;
Irvel468051e2016-06-13 16:44:44 -0700158 BufferQueue::createBufferQueue(&producer, &consumer, nullptr, true);
Dan Stozab9b08832014-03-13 11:55:57 -0700159 mProducer = new MonitoredProducer(producer, mFlinger);
Irvel468051e2016-06-13 16:44:44 -0700160 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800161 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800162 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700163 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800164
Dan Stozac9d97202016-03-03 08:20:27 -0800165#ifndef TARGET_DISABLE_TRIPLE_BUFFERING
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700166 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800167#endif
Andy McFadden69052052012-09-14 16:10:11 -0700168
Mathias Agopian84300952012-11-21 16:02:13 -0800169 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
170 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700171}
172
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700173Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800174 sp<Client> c(mClientRef.promote());
175 if (c != 0) {
176 c->detachLayer(this);
177 }
178
Dan Stozacac35382016-01-27 12:21:06 -0800179 for (auto& point : mRemoteSyncPoints) {
180 point->setTransactionApplied();
181 }
Dan Stozac8145172016-04-28 16:29:06 -0700182 for (auto& point : mLocalSyncPoints) {
183 point->setFrameAvailable();
184 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700186 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// callbacks
191// ---------------------------------------------------------------------------
192
Dan Stoza9e56aa02015-11-02 13:00:03 -0800193#ifdef USE_HWC2
194void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
195 if (mHwcLayers.empty()) {
196 return;
197 }
198 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
199}
200#else
Dan Stozac7014012014-02-14 15:03:43 -0800201void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 HWComposer::HWCLayerInterface* layer) {
203 if (layer) {
204 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700205 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 }
207}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800209
Dan Stoza6b9454d2014-11-07 16:00:59 -0800210void Layer::onFrameAvailable(const BufferItem& item) {
211 // Add this buffer from our internal queue tracker
212 { // Autolock scope
213 Mutex::Autolock lock(mQueueItemLock);
Irvel468051e2016-06-13 16:44:44 -0700214 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
215 item.mGraphicBuffer->getHeight(), item.mFrameNumber);
Dan Stozaa4650a52015-05-12 12:56:16 -0700216 // Reset the frame number tracker when we receive the first buffer after
217 // a frame number reset
218 if (item.mFrameNumber == 1) {
219 mLastFrameNumberReceived = 0;
220 }
221
222 // Ensure that callbacks are handled in order
223 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
224 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
225 ms2ns(500));
226 if (result != NO_ERROR) {
227 ALOGE("[%s] Timed out waiting on callback", mName.string());
228 }
229 }
230
Dan Stoza6b9454d2014-11-07 16:00:59 -0800231 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700232 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700233
234 // Wake up any pending callbacks
235 mLastFrameNumberReceived = item.mFrameNumber;
236 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800237 }
238
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800239 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700240}
241
Dan Stoza6b9454d2014-11-07 16:00:59 -0800242void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700243 { // Autolock scope
244 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700245
Dan Stoza7dde5992015-05-22 09:51:44 -0700246 // Ensure that callbacks are handled in order
247 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
248 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
249 ms2ns(500));
250 if (result != NO_ERROR) {
251 ALOGE("[%s] Timed out waiting on callback", mName.string());
252 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700253 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700254
255 if (mQueueItems.empty()) {
256 ALOGE("Can't replace a frame on an empty queue");
257 return;
258 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700259 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700260
261 // Wake up any pending callbacks
262 mLastFrameNumberReceived = item.mFrameNumber;
263 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700264 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800265}
266
Jesse Hall399184a2014-03-03 15:42:54 -0800267void Layer::onSidebandStreamChanged() {
268 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
269 // mSidebandStreamChanged was false
270 mFlinger->signalLayerUpdate();
271 }
272}
273
Mathias Agopian67106042013-03-14 19:18:13 -0700274// called with SurfaceFlinger::mStateLock from the drawing thread after
275// the layer has been remove from the current state list (and just before
276// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800277void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800278 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700279}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280
Mathias Agopian13127d82013-03-05 17:47:11 -0800281// ---------------------------------------------------------------------------
282// set-up
283// ---------------------------------------------------------------------------
284
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700285const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800286 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287}
288
Mathias Agopianf9d93272009-06-19 17:00:27 -0700289status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 PixelFormat format, uint32_t flags)
291{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700292 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700294
295 // never allow a surface larger than what our underlying GL implementation
296 // can handle.
297 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800298 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700299 return BAD_VALUE;
300 }
301
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700303
Riley Andrews03414a12014-07-01 14:22:59 -0700304 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700305 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700306 mCurrentOpacity = getOpacityForFormat(format);
307
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800308 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
309 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
310 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700315sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800316 Mutex::Autolock _l(mLock);
317
318 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700319 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800320
321 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700322
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700323 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800324}
325
Dan Stozab9b08832014-03-13 11:55:57 -0700326sp<IGraphicBufferProducer> Layer::getProducer() const {
327 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700328}
329
Mathias Agopian13127d82013-03-05 17:47:11 -0800330// ---------------------------------------------------------------------------
331// h/w composer set-up
332// ---------------------------------------------------------------------------
333
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800334Rect Layer::getContentCrop() const {
335 // this is the crop rectangle that applies to the buffer
336 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700337 Rect crop;
338 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800339 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700340 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800341 } else if (mActiveBuffer != NULL) {
342 // otherwise we use the whole buffer
343 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700344 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800345 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700346 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700347 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700348 return crop;
349}
350
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700351static Rect reduce(const Rect& win, const Region& exclude) {
352 if (CC_LIKELY(exclude.isEmpty())) {
353 return win;
354 }
355 if (exclude.isRect()) {
356 return win.reduce(exclude.getBounds());
357 }
358 return Region(win).subtract(exclude).getBounds();
359}
360
Mathias Agopian13127d82013-03-05 17:47:11 -0800361Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700362 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700363 return computeBounds(s.activeTransparentRegion);
364}
365
366Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
367 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800368 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700369
370 if (!s.crop.isEmpty()) {
371 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800372 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700373 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700374 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800375}
376
Mathias Agopian6b442672013-07-09 21:24:52 -0700377FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800378 // the content crop is the area of the content that gets scaled to the
379 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700380 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800381
Robert Carrb5d3d262016-03-25 15:08:13 -0700382 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700384 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800385
386 // apply the projection's clipping to the window crop in
387 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700388 // if there are no window scaling involved, this operation will map to full
389 // pixels in the buffer.
390 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
391 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700392
393 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700394 if (!s.crop.isEmpty()) {
395 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700396 }
397
Robert Carr3dcabfa2016-03-01 18:36:58 -0800398 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000399 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
400 activeCrop.clear();
401 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700402 if (!s.finalCrop.isEmpty()) {
403 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 activeCrop.clear();
405 }
406 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800407 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800408
Michael Lentine28ea2172014-11-19 18:32:37 -0800409 // This needs to be here as transform.transform(Rect) computes the
410 // transformed rect and then takes the bounding box of the result before
411 // returning. This means
412 // transform.inverse().transform(transform.transform(Rect)) != Rect
413 // in which case we need to make sure the final rect is clipped to the
414 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
416 activeCrop.clear();
417 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700419 // subtract the transparent region and snap to the bounds
420 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
421
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 // Transform the window crop to match the buffer coordinate system,
423 // which means using the inverse of the current transform set on the
424 // SurfaceFlingerConsumer.
425 uint32_t invTransform = mCurrentTransform;
426 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
427 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700428 * the code below applies the primary display's inverse transform to the
429 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700431 uint32_t invTransformOrient =
432 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // calculate the inverse transform
434 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
435 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
436 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800437 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700439 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
440 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800441 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442
443 int winWidth = s.active.w;
444 int winHeight = s.active.h;
445 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
446 // If the activeCrop has been rotate the ends are rotated but not
447 // the space itself so when transforming ends back we can't rely on
448 // a modification of the axes of rotation. To account for this we
449 // need to reorient the inverse rotation in terms of the current
450 // axes of rotation.
451 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
452 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
453 if (is_h_flipped == is_v_flipped) {
454 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
455 NATIVE_WINDOW_TRANSFORM_FLIP_H;
456 }
457 winWidth = s.active.h;
458 winHeight = s.active.w;
459 }
460 const Rect winCrop = activeCrop.transform(
461 invTransform, s.active.w, s.active.h);
462
463 // below, crop is intersected with winCrop expressed in crop's coordinate space
464 float xScale = crop.getWidth() / float(winWidth);
465 float yScale = crop.getHeight() / float(winHeight);
466
467 float insetL = winCrop.left * xScale;
468 float insetT = winCrop.top * yScale;
469 float insetR = (winWidth - winCrop.right ) * xScale;
470 float insetB = (winHeight - winCrop.bottom) * yScale;
471
472 crop.left += insetL;
473 crop.top += insetT;
474 crop.right -= insetR;
475 crop.bottom -= insetB;
476
Mathias Agopian13127d82013-03-05 17:47:11 -0800477 return crop;
478}
479
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480#ifdef USE_HWC2
481void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
482#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700483void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700484 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700485 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700487{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488#ifdef USE_HWC2
489 const auto hwcId = displayDevice->getHwcDisplayId();
490 auto& hwcInfo = mHwcLayers[hwcId];
491#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800492 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700494
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700495 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496#ifdef USE_HWC2
497 hwcInfo.forceClientComposition = false;
498
499 if (isSecure() && !displayDevice->isSecure()) {
500 hwcInfo.forceClientComposition = true;
501 }
502
503 auto& hwcLayer = hwcInfo.layer;
504#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700506
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700507 if (isSecure() && !hw->isSecure()) {
508 layer.setSkip(true);
509 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700511
Mathias Agopian13127d82013-03-05 17:47:11 -0800512 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700513 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514#ifdef USE_HWC2
515 if (!isOpaque(s) || s.alpha != 1.0f) {
516 auto blendMode = mPremultipliedAlpha ?
517 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
518 auto error = hwcLayer->setBlendMode(blendMode);
519 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
520 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
521 to_string(error).c_str(), static_cast<int32_t>(error));
522 }
523#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800524 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800525 layer.setBlending(mPremultipliedAlpha ?
526 HWC_BLENDING_PREMULT :
527 HWC_BLENDING_COVERAGE);
528 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800530
531 // apply the layer's transform, followed by the display's global transform
532 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700534 if (!s.crop.isEmpty()) {
535 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800536 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800537#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800539#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000540 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 activeCrop.clear();
543 }
Pablo Ceballos51450032016-08-03 10:20:45 -0700544 activeCrop = s.active.transform.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800545 // This needs to be here as transform.transform(Rect) computes the
546 // transformed rect and then takes the bounding box of the result before
547 // returning. This means
548 // transform.inverse().transform(transform.transform(Rect)) != Rect
549 // in which case we need to make sure the final rect is clipped to the
550 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000551 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
552 activeCrop.clear();
553 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700554 // mark regions outside the crop as transparent
555 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
556 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
557 s.active.w, s.active.h));
558 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
559 activeCrop.left, activeCrop.bottom));
560 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
561 s.active.w, activeCrop.bottom));
562 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800563 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700564 if (!s.finalCrop.isEmpty()) {
565 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000566 frame.clear();
567 }
568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000570 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
571 frame.clear();
572 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 const Transform& tr(displayDevice->getTransform());
574 Rect transformedFrame = tr.transform(frame);
575 auto error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700576 if (error != HWC2::Error::None) {
577 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
578 mName.string(), transformedFrame.left, transformedFrame.top,
579 transformedFrame.right, transformedFrame.bottom,
580 to_string(error).c_str(), static_cast<int32_t>(error));
581 } else {
582 hwcInfo.displayFrame = transformedFrame;
583 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584
585 FloatRect sourceCrop = computeCrop(displayDevice);
586 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700587 if (error != HWC2::Error::None) {
588 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
589 "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top,
590 sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(),
591 static_cast<int32_t>(error));
592 } else {
593 hwcInfo.sourceCrop = sourceCrop;
594 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595
596 error = hwcLayer->setPlaneAlpha(s.alpha);
597 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
598 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
599 static_cast<int32_t>(error));
600
601 error = hwcLayer->setZOrder(s.z);
602 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
603 mName.string(), s.z, to_string(error).c_str(),
604 static_cast<int32_t>(error));
605#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000606 if (!frame.intersect(hw->getViewport(), &frame)) {
607 frame.clear();
608 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800609 const Transform& tr(hw->getTransform());
610 layer.setFrame(tr.transform(frame));
611 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800612 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800614
Mathias Agopian29a367b2011-07-12 14:51:45 -0700615 /*
616 * Transformations are applied in this order:
617 * 1) buffer orientation/flip/mirror
618 * 2) state transformation (window manager)
619 * 3) layer orientation (screen orientation)
620 * (NOTE: the matrices are multiplied in reverse order)
621 */
622
623 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800624 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700625
626 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
627 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 * the code below applies the primary display's inverse transform to the
629 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700631 uint32_t invTransform =
632 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 // calculate the inverse transform
634 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
635 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
636 NATIVE_WINDOW_TRANSFORM_FLIP_H;
637 }
638 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700639 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700641
642 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800643 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644#ifdef USE_HWC2
645 if (orientation & Transform::ROT_INVALID) {
646 // we can only handle simple transformation
647 hwcInfo.forceClientComposition = true;
648 } else {
649 auto transform = static_cast<HWC2::Transform>(orientation);
650 auto error = hwcLayer->setTransform(transform);
651 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
652 "%s (%d)", mName.string(), to_string(transform).c_str(),
653 to_string(error).c_str(), static_cast<int32_t>(error));
654 }
655#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800656 if (orientation & Transform::ROT_INVALID) {
657 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700658 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700659 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800660 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700661 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700663}
664
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665#ifdef USE_HWC2
666void Layer::forceClientComposition(int32_t hwcId) {
667 if (mHwcLayers.count(hwcId) == 0) {
668 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
669 return;
670 }
671
672 mHwcLayers[hwcId].forceClientComposition = true;
673}
674#endif
675
676#ifdef USE_HWC2
677void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
678 // Apply this display's projection's viewport to the visible region
679 // before giving it to the HWC HAL.
680 const Transform& tr = displayDevice->getTransform();
681 const auto& viewport = displayDevice->getViewport();
682 Region visible = tr.transform(visibleRegion.intersect(viewport));
683 auto hwcId = displayDevice->getHwcDisplayId();
684 auto& hwcLayer = mHwcLayers[hwcId].layer;
685 auto error = hwcLayer->setVisibleRegion(visible);
686 if (error != HWC2::Error::None) {
687 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
688 to_string(error).c_str(), static_cast<int32_t>(error));
689 visible.dump(LOG_TAG);
690 }
691
692 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
693 if (error != HWC2::Error::None) {
694 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
695 to_string(error).c_str(), static_cast<int32_t>(error));
696 surfaceDamageRegion.dump(LOG_TAG);
697 }
698
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700699 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700701 setCompositionType(hwcId, HWC2::Composition::Sideband);
702 ALOGV("[%s] Requesting Sideband composition", mName.string());
703 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 if (error != HWC2::Error::None) {
705 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
706 mName.string(), mSidebandStream->handle(),
707 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700709 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 }
711
Dan Stoza0a21df72016-07-20 12:52:38 -0700712 // Client layers
713 if (mHwcLayers[hwcId].forceClientComposition ||
714 (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700715 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700717 return;
718 }
719
Dan Stoza0a21df72016-07-20 12:52:38 -0700720 // SolidColor layers
721 if (mActiveBuffer == nullptr) {
722 setCompositionType(hwcId, HWC2::Composition::SolidColor);
Dan Stozac6c89542016-07-27 10:16:52 -0700723
724 // For now, we only support black for DimLayer
Dan Stoza0a21df72016-07-20 12:52:38 -0700725 error = hwcLayer->setColor({0, 0, 0, 255});
726 if (error != HWC2::Error::None) {
727 ALOGE("[%s] Failed to set color: %s (%d)", mName.string(),
728 to_string(error).c_str(), static_cast<int32_t>(error));
729 }
Dan Stozac6c89542016-07-27 10:16:52 -0700730
731 // Clear out the transform, because it doesn't make sense absent a
732 // source buffer
733 error = hwcLayer->setTransform(HWC2::Transform::None);
734 if (error != HWC2::Error::None) {
735 ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(),
736 to_string(error).c_str(), static_cast<int32_t>(error));
737 }
738
Dan Stoza0a21df72016-07-20 12:52:38 -0700739 return;
740 }
741
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700742 // Device or Cursor layers
743 if (mPotentialCursor) {
744 ALOGV("[%s] Requesting Cursor composition", mName.string());
745 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746 } else {
747 ALOGV("[%s] Requesting Device composition", mName.string());
748 setCompositionType(hwcId, HWC2::Composition::Device);
749 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700750
751 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
752 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
753 if (error != HWC2::Error::None) {
754 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
755 mActiveBuffer->handle, to_string(error).c_str(),
756 static_cast<int32_t>(error));
757 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758}
759#else
Mathias Agopian42977342012-08-05 00:40:46 -0700760void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700761 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800762 // we have to set the visible region on every frame because
763 // we currently free it during onLayerDisplayed(), which is called
764 // after HWComposer::commit() -- every frame.
765 // Apply this display's projection's viewport to the visible region
766 // before giving it to the HWC HAL.
767 const Transform& tr = hw->getTransform();
768 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
769 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700770 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800771 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700772
Jesse Hall399184a2014-03-03 15:42:54 -0800773 if (mSidebandStream.get()) {
774 layer.setSidebandStream(mSidebandStream);
775 } else {
776 // NOTE: buffer can be NULL if the client never drew into this
777 // layer yet, or if we ran out of memory
778 layer.setBuffer(mActiveBuffer);
779 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700780}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700782
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783#ifdef USE_HWC2
784void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
785 auto hwcId = displayDevice->getHwcDisplayId();
786 if (mHwcLayers.count(hwcId) == 0 ||
787 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
788 return;
789 }
790
791 // This gives us only the "orientation" component of the transform
792 const State& s(getCurrentState());
793
794 // Apply the layer's transform, followed by the display's global transform
795 // Here we're guaranteed that the layer's transform preserves rects
796 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700797 if (!s.crop.isEmpty()) {
798 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799 }
800 // Subtract the transparent region and snap to the bounds
801 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800802 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800803 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700804 if (!s.finalCrop.isEmpty()) {
805 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000806 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807 auto& displayTransform(displayDevice->getTransform());
808 auto position = displayTransform.transform(frame);
809
810 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
811 position.top);
812 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
813 "to (%d, %d): %s (%d)", mName.string(), position.left,
814 position.top, to_string(error).c_str(),
815 static_cast<int32_t>(error));
816}
817#else
Dan Stozac7014012014-02-14 15:03:43 -0800818void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700819 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700820 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700821
822 // TODO: there is a possible optimization here: we only need to set the
823 // acquire fence the first time a new buffer is acquired on EACH display.
824
Riley Andrews03414a12014-07-01 14:22:59 -0700825 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800826 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800827 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700828 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700829 if (fenceFd == -1) {
830 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
831 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700832 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700833 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700834 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700835}
836
Riley Andrews03414a12014-07-01 14:22:59 -0700837Rect Layer::getPosition(
838 const sp<const DisplayDevice>& hw)
839{
840 // this gives us only the "orientation" component of the transform
841 const State& s(getCurrentState());
842
843 // apply the layer's transform, followed by the display's global transform
844 // here we're guaranteed that the layer's transform preserves rects
845 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700846 if (!s.crop.isEmpty()) {
847 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700848 }
849 // subtract the transparent region and snap to the bounds
850 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800851 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700852 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700853 if (!s.finalCrop.isEmpty()) {
854 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000855 }
Riley Andrews03414a12014-07-01 14:22:59 -0700856 const Transform& tr(hw->getTransform());
857 return Rect(tr.transform(frame));
858}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800859#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700860
Mathias Agopian13127d82013-03-05 17:47:11 -0800861// ---------------------------------------------------------------------------
862// drawing...
863// ---------------------------------------------------------------------------
864
865void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800866 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800867}
868
Dan Stozac7014012014-02-14 15:03:43 -0800869void Layer::draw(const sp<const DisplayDevice>& hw,
870 bool useIdentityTransform) const {
871 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800872}
873
Dan Stozac7014012014-02-14 15:03:43 -0800874void Layer::draw(const sp<const DisplayDevice>& hw) const {
875 onDraw(hw, Region(hw->bounds()), false);
876}
877
878void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
879 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800881 ATRACE_CALL();
882
Mathias Agopiana67932f2011-04-20 14:20:59 -0700883 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700885 // in fact never been drawn into. This happens frequently with
886 // SurfaceView because the WindowManager can't know when the client
887 // has drawn the first time.
888
889 // If there is nothing under us, we paint the screen in black, otherwise
890 // we just skip this update.
891
892 // figure out if there is something below us
893 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700894 const SurfaceFlinger::LayerVector& drawingLayers(
895 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700896 const size_t count = drawingLayers.size();
897 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800898 const sp<Layer>& layer(drawingLayers[i]);
899 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700900 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700901 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700902 }
903 // if not everything below us is covered, we plug the holes!
904 Region holes(clip.subtract(under));
905 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700906 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700907 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 return;
909 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700910
Andy McFadden97eba892012-12-11 15:21:45 -0800911 // Bind the current buffer to the GL texture, and wait for it to be
912 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800913 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
914 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800915 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700916 // Go ahead and draw the buffer anyway; no matter what we do the screen
917 // is probably going to have something visibly wrong.
918 }
919
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700920 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
921
Mathias Agopian875d8e12013-06-07 15:35:48 -0700922 RenderEngine& engine(mFlinger->getRenderEngine());
923
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700924 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700925 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700926 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700927
928 // Query the texture matrix given our current filtering mode.
929 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800930 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
931 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700932
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700933 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
934
935 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700936 * the code below applies the primary display's inverse transform to
937 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700938 */
939
940 // create a 4x4 transform matrix from the display transform flags
941 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
942 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
943 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
944
945 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700946 uint32_t transform =
947 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700948 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
949 tr = tr * rot90;
950 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
951 tr = tr * flipH;
952 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
953 tr = tr * flipV;
954
955 // calculate the inverse
956 tr = inverse(tr);
957
958 // and finally apply it to the original texture matrix
959 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
960 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
961 }
962
Jamie Genniscbb1a952012-05-08 17:05:52 -0700963 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700964 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
965 mTexture.setFiltering(useFiltering);
966 mTexture.setMatrix(textureMatrix);
967
968 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700969 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700970 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700971 }
Dan Stozac7014012014-02-14 15:03:43 -0800972 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700973 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974}
975
Mathias Agopian13127d82013-03-05 17:47:11 -0800976
Dan Stozac7014012014-02-14 15:03:43 -0800977void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
978 const Region& /* clip */, float red, float green, float blue,
979 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800980{
Mathias Agopian19733a32013-08-28 18:13:56 -0700981 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800982 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700983 engine.setupFillWithColor(red, green, blue, alpha);
984 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800985}
986
987void Layer::clearWithOpenGL(
988 const sp<const DisplayDevice>& hw, const Region& clip) const {
989 clearWithOpenGL(hw, clip, 0,0,0,0);
990}
991
Dan Stozac7014012014-02-14 15:03:43 -0800992void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
993 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800995
Dan Stozac7014012014-02-14 15:03:43 -0800996 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800997
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 /*
999 * NOTE: the way we compute the texture coordinates here produces
1000 * different results than when we take the HWC path -- in the later case
1001 * the "source crop" is rounded to texel boundaries.
1002 * This can produce significantly different results when the texture
1003 * is scaled by a large amount.
1004 *
1005 * The GL code below is more logical (imho), and the difference with
1006 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001007 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001008 * GL composition when a buffer scaling is applied (maybe with some
1009 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1010 * like more of a hack.
1011 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001012 Rect win(computeBounds());
1013
Robert Carrb5d3d262016-03-25 15:08:13 -07001014 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001015 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001016 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001017 win.clear();
1018 }
1019 win = s.active.transform.inverse().transform(win);
1020 if (!win.intersect(computeBounds(), &win)) {
1021 win.clear();
1022 }
1023 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001024
Mathias Agopian3f844832013-08-07 21:24:32 -07001025 float left = float(win.left) / float(s.active.w);
1026 float top = float(win.top) / float(s.active.h);
1027 float right = float(win.right) / float(s.active.w);
1028 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001029
Mathias Agopian875d8e12013-06-07 15:35:48 -07001030 // TODO: we probably want to generate the texture coords with the mesh
1031 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001032 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1033 texCoords[0] = vec2(left, 1.0f - top);
1034 texCoords[1] = vec2(left, 1.0f - bottom);
1035 texCoords[2] = vec2(right, 1.0f - bottom);
1036 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001037
Mathias Agopian875d8e12013-06-07 15:35:48 -07001038 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001039 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001040 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001041 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001042}
1043
Dan Stoza9e56aa02015-11-02 13:00:03 -08001044#ifdef USE_HWC2
1045void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1046 bool callIntoHwc) {
1047 if (mHwcLayers.count(hwcId) == 0) {
1048 ALOGE("setCompositionType called without a valid HWC layer");
1049 return;
1050 }
1051 auto& hwcInfo = mHwcLayers[hwcId];
1052 auto& hwcLayer = hwcInfo.layer;
1053 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1054 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1055 if (hwcInfo.compositionType != type) {
1056 ALOGV(" actually setting");
1057 hwcInfo.compositionType = type;
1058 if (callIntoHwc) {
1059 auto error = hwcLayer->setCompositionType(type);
1060 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1061 "composition type %s: %s (%d)", mName.string(),
1062 to_string(type).c_str(), to_string(error).c_str(),
1063 static_cast<int32_t>(error));
1064 }
1065 }
1066}
1067
1068HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -07001069 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
1070 // If we're querying the composition type for a display that does not
1071 // have a HWC counterpart, then it will always be Client
1072 return HWC2::Composition::Client;
1073 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001074 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -07001075 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -08001076 return HWC2::Composition::Invalid;
1077 }
1078 return mHwcLayers.at(hwcId).compositionType;
1079}
1080
1081void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1082 if (mHwcLayers.count(hwcId) == 0) {
1083 ALOGE("setClearClientTarget called without a valid HWC layer");
1084 return;
1085 }
1086 mHwcLayers[hwcId].clearClientTarget = clear;
1087}
1088
1089bool Layer::getClearClientTarget(int32_t hwcId) const {
1090 if (mHwcLayers.count(hwcId) == 0) {
1091 ALOGE("getClearClientTarget called without a valid HWC layer");
1092 return false;
1093 }
1094 return mHwcLayers.at(hwcId).clearClientTarget;
1095}
1096#endif
1097
Ruben Brunk1681d952014-06-27 15:51:55 -07001098uint32_t Layer::getProducerStickyTransform() const {
1099 int producerStickyTransform = 0;
1100 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1101 if (ret != OK) {
1102 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1103 strerror(-ret), ret);
1104 return 0;
1105 }
1106 return static_cast<uint32_t>(producerStickyTransform);
1107}
1108
Dan Stozac5da2712016-07-20 15:38:12 -07001109bool Layer::latchUnsignaledBuffers() {
1110 static bool propertyLoaded = false;
1111 static bool latch = false;
1112 static std::mutex mutex;
1113 std::lock_guard<std::mutex> lock(mutex);
1114 if (!propertyLoaded) {
1115 char value[PROPERTY_VALUE_MAX] = {};
1116 property_get("debug.sf.latch_unsignaled", value, "0");
1117 latch = atoi(value);
1118 propertyLoaded = true;
1119 }
1120 return latch;
1121}
1122
Dan Stozacac35382016-01-27 12:21:06 -08001123uint64_t Layer::getHeadFrameNumber() const {
1124 Mutex::Autolock lock(mQueueItemLock);
1125 if (!mQueueItems.empty()) {
1126 return mQueueItems[0].mFrameNumber;
1127 } else {
1128 return mCurrentFrameNumber;
1129 }
1130}
1131
Dan Stoza1ce65812016-06-15 16:26:27 -07001132bool Layer::headFenceHasSignaled() const {
1133#ifdef USE_HWC2
Dan Stozac5da2712016-07-20 15:38:12 -07001134 if (latchUnsignaledBuffers()) {
1135 return true;
1136 }
1137
Dan Stoza1ce65812016-06-15 16:26:27 -07001138 Mutex::Autolock lock(mQueueItemLock);
1139 if (mQueueItems.empty()) {
1140 return true;
1141 }
1142 if (mQueueItems[0].mIsDroppable) {
1143 // Even though this buffer's fence may not have signaled yet, it could
1144 // be replaced by another buffer before it has a chance to, which means
1145 // that it's possible to get into a situation where a buffer is never
1146 // able to be latched. To avoid this, grab this buffer anyway.
1147 return true;
1148 }
1149 return mQueueItems[0].mFence->getSignalTime() != INT64_MAX;
1150#else
1151 return true;
1152#endif
1153}
1154
Dan Stozacac35382016-01-27 12:21:06 -08001155bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1156 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1157 // Don't bother with a SyncPoint, since we've already latched the
1158 // relevant frame
1159 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001160 }
1161
Dan Stozacac35382016-01-27 12:21:06 -08001162 Mutex::Autolock lock(mLocalSyncPointMutex);
1163 mLocalSyncPoints.push_back(point);
1164 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001165}
1166
Mathias Agopian13127d82013-03-05 17:47:11 -08001167void Layer::setFiltering(bool filtering) {
1168 mFiltering = filtering;
1169}
1170
1171bool Layer::getFiltering() const {
1172 return mFiltering;
1173}
1174
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001175// As documented in libhardware header, formats in the range
1176// 0x100 - 0x1FF are specific to the HAL implementation, and
1177// are known to have no alpha channel
1178// TODO: move definition for device-specific range into
1179// hardware.h, instead of using hard-coded values here.
1180#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1181
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001182bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001183 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1184 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001185 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001186 switch (format) {
1187 case HAL_PIXEL_FORMAT_RGBA_8888:
1188 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001189 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001190 }
1191 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001192 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001193}
1194
Mathias Agopian13127d82013-03-05 17:47:11 -08001195// ----------------------------------------------------------------------------
1196// local state
1197// ----------------------------------------------------------------------------
1198
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001199static void boundPoint(vec2* point, const Rect& crop) {
1200 if (point->x < crop.left) {
1201 point->x = crop.left;
1202 }
1203 if (point->x > crop.right) {
1204 point->x = crop.right;
1205 }
1206 if (point->y < crop.top) {
1207 point->y = crop.top;
1208 }
1209 if (point->y > crop.bottom) {
1210 point->y = crop.bottom;
1211 }
1212}
1213
Dan Stozac7014012014-02-14 15:03:43 -08001214void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1215 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001216{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001217 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001218 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001219 const uint32_t hw_h = hw->getHeight();
1220 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001221 if (!s.crop.isEmpty()) {
1222 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001224 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001225 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001226
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001227 vec2 lt = vec2(win.left, win.top);
1228 vec2 lb = vec2(win.left, win.bottom);
1229 vec2 rb = vec2(win.right, win.bottom);
1230 vec2 rt = vec2(win.right, win.top);
1231
1232 if (!useIdentityTransform) {
1233 lt = s.active.transform.transform(lt);
1234 lb = s.active.transform.transform(lb);
1235 rb = s.active.transform.transform(rb);
1236 rt = s.active.transform.transform(rt);
1237 }
1238
Robert Carrb5d3d262016-03-25 15:08:13 -07001239 if (!s.finalCrop.isEmpty()) {
1240 boundPoint(&lt, s.finalCrop);
1241 boundPoint(&lb, s.finalCrop);
1242 boundPoint(&rb, s.finalCrop);
1243 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001244 }
1245
Mathias Agopianff2ed702013-09-01 21:36:12 -07001246 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001247 position[0] = tr.transform(lt);
1248 position[1] = tr.transform(lb);
1249 position[2] = tr.transform(rb);
1250 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001251 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001252 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001253 }
1254}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001255
Andy McFadden4125a4f2014-01-29 17:17:11 -08001256bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001257{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001258 // if we don't have a buffer yet, we're translucent regardless of the
1259 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001260 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001261 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001262 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001263
1264 // if the layer has the opaque flag, then we're always opaque,
1265 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001266 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001267}
1268
Dan Stoza23116082015-06-18 14:58:39 -07001269bool Layer::isSecure() const
1270{
1271 const Layer::State& s(mDrawingState);
1272 return (s.flags & layer_state_t::eLayerSecure);
1273}
1274
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001275bool Layer::isProtected() const
1276{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001277 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001278 return (activeBuffer != 0) &&
1279 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1280}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001281
Mathias Agopian13127d82013-03-05 17:47:11 -08001282bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001283 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284}
1285
1286bool Layer::isCropped() const {
1287 return !mCurrentCrop.isEmpty();
1288}
1289
1290bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1291 return mNeedsFiltering || hw->needsFiltering();
1292}
1293
1294void Layer::setVisibleRegion(const Region& visibleRegion) {
1295 // always called from main thread
1296 this->visibleRegion = visibleRegion;
1297}
1298
1299void Layer::setCoveredRegion(const Region& coveredRegion) {
1300 // always called from main thread
1301 this->coveredRegion = coveredRegion;
1302}
1303
1304void Layer::setVisibleNonTransparentRegion(const Region&
1305 setVisibleNonTransparentRegion) {
1306 // always called from main thread
1307 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1308}
1309
1310// ----------------------------------------------------------------------------
1311// transaction
1312// ----------------------------------------------------------------------------
1313
Dan Stoza7dde5992015-05-22 09:51:44 -07001314void Layer::pushPendingState() {
1315 if (!mCurrentState.modified) {
1316 return;
1317 }
1318
Dan Stoza7dde5992015-05-22 09:51:44 -07001319 // If this transaction is waiting on the receipt of a frame, generate a sync
1320 // point and send it to the remote layer.
1321 if (mCurrentState.handle != nullptr) {
Dan Stoza22851c32016-08-09 13:21:03 -07001322 sp<IBinder> strongBinder = mCurrentState.handle.promote();
1323 sp<Handle> handle = nullptr;
1324 sp<Layer> handleLayer = nullptr;
1325 if (strongBinder != nullptr) {
1326 handle = static_cast<Handle*>(strongBinder.get());
1327 handleLayer = handle->owner.promote();
1328 }
1329 if (strongBinder == nullptr || handleLayer == nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001330 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1331 // If we can't promote the layer we are intended to wait on,
1332 // then it is expired or otherwise invalid. Allow this transaction
1333 // to be applied as per normal (no synchronization).
1334 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001335 } else {
1336 auto syncPoint = std::make_shared<SyncPoint>(
1337 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001338 if (handleLayer->addSyncPoint(syncPoint)) {
1339 mRemoteSyncPoints.push_back(std::move(syncPoint));
1340 } else {
1341 // We already missed the frame we're supposed to synchronize
1342 // on, so go ahead and apply the state update
1343 mCurrentState.handle = nullptr;
1344 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001345 }
1346
Dan Stoza7dde5992015-05-22 09:51:44 -07001347 // Wake us up to check if the frame has been received
1348 setTransactionFlags(eTransactionNeeded);
1349 }
1350 mPendingStates.push_back(mCurrentState);
1351}
1352
Pablo Ceballos05289c22016-04-14 15:49:55 -07001353void Layer::popPendingState(State* stateToCommit) {
1354 auto oldFlags = stateToCommit->flags;
1355 *stateToCommit = mPendingStates[0];
1356 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1357 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001358
1359 mPendingStates.removeAt(0);
1360}
1361
Pablo Ceballos05289c22016-04-14 15:49:55 -07001362bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001363 bool stateUpdateAvailable = false;
1364 while (!mPendingStates.empty()) {
1365 if (mPendingStates[0].handle != nullptr) {
1366 if (mRemoteSyncPoints.empty()) {
1367 // If we don't have a sync point for this, apply it anyway. It
1368 // will be visually wrong, but it should keep us from getting
1369 // into too much trouble.
1370 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001371 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001372 stateUpdateAvailable = true;
1373 continue;
1374 }
1375
Dan Stozacac35382016-01-27 12:21:06 -08001376 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1377 mPendingStates[0].frameNumber) {
1378 ALOGE("[%s] Unexpected sync point frame number found",
1379 mName.string());
1380
1381 // Signal our end of the sync point and then dispose of it
1382 mRemoteSyncPoints.front()->setTransactionApplied();
1383 mRemoteSyncPoints.pop_front();
1384 continue;
1385 }
1386
Dan Stoza7dde5992015-05-22 09:51:44 -07001387 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1388 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001389 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001390 stateUpdateAvailable = true;
1391
1392 // Signal our end of the sync point and then dispose of it
1393 mRemoteSyncPoints.front()->setTransactionApplied();
1394 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001395 } else {
1396 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001397 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001398 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001399 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001400 stateUpdateAvailable = true;
1401 }
1402 }
1403
1404 // If we still have pending updates, wake SurfaceFlinger back up and point
1405 // it at this layer so we can process them
1406 if (!mPendingStates.empty()) {
1407 setTransactionFlags(eTransactionNeeded);
1408 mFlinger->setTransactionFlags(eTraversalNeeded);
1409 }
1410
1411 mCurrentState.modified = false;
1412 return stateUpdateAvailable;
1413}
1414
1415void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001416 auto headFrameNumber = getHeadFrameNumber();
Dan Stoza1ce65812016-06-15 16:26:27 -07001417 bool headFenceSignaled = headFenceHasSignaled();
Dan Stozacac35382016-01-27 12:21:06 -08001418 Mutex::Autolock lock(mLocalSyncPointMutex);
1419 for (auto& point : mLocalSyncPoints) {
Dan Stoza1ce65812016-06-15 16:26:27 -07001420 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
Dan Stozacac35382016-01-27 12:21:06 -08001421 point->setFrameAvailable();
1422 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001423 }
1424}
1425
Mathias Agopian13127d82013-03-05 17:47:11 -08001426uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001427 ATRACE_CALL();
1428
Dan Stoza7dde5992015-05-22 09:51:44 -07001429 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001430 Layer::State c = getCurrentState();
1431 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001432 return 0;
1433 }
1434
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001435 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001437 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1438 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001439
1440 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001441 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001442 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001443 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001444 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001445 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001446 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001447 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001448 this, getName().string(), mCurrentTransform,
1449 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001450 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001451 c.crop.left,
1452 c.crop.top,
1453 c.crop.right,
1454 c.crop.bottom,
1455 c.crop.getWidth(),
1456 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001457 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001458 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001459 s.crop.left,
1460 s.crop.top,
1461 s.crop.right,
1462 s.crop.bottom,
1463 s.crop.getWidth(),
1464 s.crop.getHeight(),
1465 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001467 // record the new size, form this point on, when the client request
1468 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001469 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001470 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001472
Robert Carr82364e32016-05-15 11:27:47 -07001473 const bool resizePending = (c.requested.w != c.active.w) ||
1474 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001475 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -07001476 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001478 // if we have a pending resize, unless we are in fixed-size mode.
1479 // the drawing state will be updated only once we receive a buffer
1480 // with the correct size.
1481 //
1482 // in particular, we want to make sure the clip (which is part
1483 // of the geometry state) is latched together with the size but is
1484 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001485 //
1486 // If a sideband stream is attached, however, we want to skip this
1487 // optimization so that transactions aren't missed when a buffer
1488 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001489
1490 flags |= eDontUpdateGeometryState;
1491 }
1492 }
1493
Mathias Agopian13127d82013-03-05 17:47:11 -08001494 // always set active to requested, unless we're asked not to
1495 // this is used by Layer, which special cases resizes.
1496 if (flags & eDontUpdateGeometryState) {
1497 } else {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001498 Layer::State& editCurrentState(getCurrentState());
Robert Carr82364e32016-05-15 11:27:47 -07001499 if (mFreezePositionUpdates) {
1500 float tx = c.active.transform.tx();
1501 float ty = c.active.transform.ty();
1502 c.active = c.requested;
1503 c.active.transform.set(tx, ty);
1504 editCurrentState.active = c.active;
1505 } else {
1506 editCurrentState.active = editCurrentState.requested;
1507 c.active = c.requested;
1508 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001509 }
1510
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001511 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001512 // invalidate and recompute the visible regions if needed
1513 flags |= Layer::eVisibleRegion;
1514 }
1515
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001516 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001517 // invalidate and recompute the visible regions if needed
1518 flags |= eVisibleRegion;
1519 this->contentDirty = true;
1520
1521 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001522 const uint8_t type = c.active.transform.getType();
1523 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001524 (type >= Transform::SCALE));
1525 }
1526
Dan Stozac8145172016-04-28 16:29:06 -07001527 // If the layer is hidden, signal and clear out all local sync points so
1528 // that transactions for layers depending on this layer's frames becoming
1529 // visible are not blocked
1530 if (c.flags & layer_state_t::eLayerHidden) {
1531 Mutex::Autolock lock(mLocalSyncPointMutex);
1532 for (auto& point : mLocalSyncPoints) {
1533 point->setFrameAvailable();
1534 }
1535 mLocalSyncPoints.clear();
1536 }
1537
Mathias Agopian13127d82013-03-05 17:47:11 -08001538 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001539 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001540 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541}
1542
Pablo Ceballos05289c22016-04-14 15:49:55 -07001543void Layer::commitTransaction(const State& stateToCommit) {
1544 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001545}
1546
Mathias Agopian13127d82013-03-05 17:47:11 -08001547uint32_t Layer::getTransactionFlags(uint32_t flags) {
1548 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1549}
1550
1551uint32_t Layer::setTransactionFlags(uint32_t flags) {
1552 return android_atomic_or(flags, &mTransactionFlags);
1553}
1554
Robert Carr82364e32016-05-15 11:27:47 -07001555bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001556 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001557 return false;
1558 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001559
1560 // We update the requested and active position simultaneously because
1561 // we want to apply the position portion of the transform matrix immediately,
1562 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001563 mCurrentState.requested.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001564 if (immediate && !mFreezePositionUpdates) {
1565 mCurrentState.active.transform.set(x, y);
1566 }
1567 mFreezePositionUpdates = mFreezePositionUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001568
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}
Robert Carr82364e32016-05-15 11:27:47 -07001573
Mathias Agopian13127d82013-03-05 17:47:11 -08001574bool Layer::setLayer(uint32_t z) {
1575 if (mCurrentState.z == z)
1576 return false;
1577 mCurrentState.sequence++;
1578 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001579 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001580 setTransactionFlags(eTransactionNeeded);
1581 return true;
1582}
1583bool Layer::setSize(uint32_t w, uint32_t h) {
1584 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1585 return false;
1586 mCurrentState.requested.w = w;
1587 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001588 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001589 setTransactionFlags(eTransactionNeeded);
1590 return true;
1591}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001592#ifdef USE_HWC2
1593bool Layer::setAlpha(float alpha) {
1594#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001595bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001596#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001597 if (mCurrentState.alpha == alpha)
1598 return false;
1599 mCurrentState.sequence++;
1600 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001601 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 setTransactionFlags(eTransactionNeeded);
1603 return true;
1604}
1605bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1606 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001607 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001608 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001609 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001610 setTransactionFlags(eTransactionNeeded);
1611 return true;
1612}
1613bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001614 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001615 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001616 setTransactionFlags(eTransactionNeeded);
1617 return true;
1618}
1619bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1620 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1621 if (mCurrentState.flags == newFlags)
1622 return false;
1623 mCurrentState.sequence++;
1624 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001625 mCurrentState.mask = mask;
1626 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001627 setTransactionFlags(eTransactionNeeded);
1628 return true;
1629}
Robert Carr99e27f02016-06-16 15:18:02 -07001630
1631bool Layer::setCrop(const Rect& crop, bool immediate) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001632 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001633 return false;
1634 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001635 mCurrentState.requestedCrop = crop;
1636 if (immediate) {
1637 mCurrentState.crop = crop;
1638 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001639 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001640 setTransactionFlags(eTransactionNeeded);
1641 return true;
1642}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001643bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001644 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001645 return false;
1646 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001647 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001648 mCurrentState.modified = true;
1649 setTransactionFlags(eTransactionNeeded);
1650 return true;
1651}
Mathias Agopian13127d82013-03-05 17:47:11 -08001652
Robert Carrc3574f72016-03-24 12:19:32 -07001653bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1654 if (scalingMode == mOverrideScalingMode)
1655 return false;
1656 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001657 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001658 return true;
1659}
1660
1661uint32_t Layer::getEffectiveScalingMode() const {
1662 if (mOverrideScalingMode >= 0) {
1663 return mOverrideScalingMode;
1664 }
1665 return mCurrentScalingMode;
1666}
1667
Mathias Agopian13127d82013-03-05 17:47:11 -08001668bool Layer::setLayerStack(uint32_t layerStack) {
1669 if (mCurrentState.layerStack == layerStack)
1670 return false;
1671 mCurrentState.sequence++;
1672 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001673 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001674 setTransactionFlags(eTransactionNeeded);
1675 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001676}
1677
Dan Stoza7dde5992015-05-22 09:51:44 -07001678void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1679 uint64_t frameNumber) {
1680 mCurrentState.handle = handle;
1681 mCurrentState.frameNumber = frameNumber;
1682 // We don't set eTransactionNeeded, because just receiving a deferral
1683 // request without any other state updates shouldn't actually induce a delay
1684 mCurrentState.modified = true;
1685 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001686 mCurrentState.handle = nullptr;
1687 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001688 mCurrentState.modified = false;
1689}
1690
Dan Stozaee44edd2015-03-23 15:50:23 -07001691void Layer::useSurfaceDamage() {
1692 if (mFlinger->mForceFullDamage) {
1693 surfaceDamageRegion = Region::INVALID_REGION;
1694 } else {
1695 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1696 }
1697}
1698
1699void Layer::useEmptyDamage() {
1700 surfaceDamageRegion.clear();
1701}
1702
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703// ----------------------------------------------------------------------------
1704// pageflip handling...
1705// ----------------------------------------------------------------------------
1706
Dan Stoza6b9454d2014-11-07 16:00:59 -08001707bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001708 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001709 return true;
1710 }
1711
Dan Stoza6b9454d2014-11-07 16:00:59 -08001712 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001713 if (mQueueItems.empty()) {
1714 return false;
1715 }
1716 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001717 nsecs_t expectedPresent =
1718 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001719
1720 // Ignore timestamps more than a second in the future
1721 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1722 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1723 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1724 expectedPresent);
1725
1726 bool isDue = timestamp < expectedPresent;
1727 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001728}
1729
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001730bool Layer::onPreComposition() {
1731 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001732 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001733}
1734
Dan Stozae77c7662016-05-13 11:37:28 -07001735bool Layer::onPostComposition() {
1736 bool frameLatencyNeeded = mFrameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001737 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001738 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001739 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1740
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001741 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001742 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001743 mFrameTracker.setFrameReadyFence(frameReadyFence);
1744 } else {
1745 // There was no fence for this frame, so assume that it was ready
1746 // to be presented at the desired present time.
1747 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1748 }
1749
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001750 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001751#ifdef USE_HWC2
1752 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1753#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001754 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001755#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001756 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001757 mFrameTracker.setActualPresentFence(presentFence);
1758 } else {
1759 // The HWC doesn't support present fences, so use the refresh
1760 // timestamp instead.
1761 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1762 mFrameTracker.setActualPresentTime(presentTime);
1763 }
1764
1765 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001766 mFrameLatencyNeeded = false;
1767 }
Dan Stozae77c7662016-05-13 11:37:28 -07001768 return frameLatencyNeeded;
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001769}
1770
Dan Stoza9e56aa02015-11-02 13:00:03 -08001771#ifdef USE_HWC2
1772void Layer::releasePendingBuffer() {
1773 mSurfaceFlingerConsumer->releasePendingBuffer();
1774}
1775#endif
1776
Mathias Agopianda27af92012-09-13 18:17:13 -07001777bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001778 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001779#ifdef USE_HWC2
1780 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1781 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1782#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001783 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001784 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001785#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001786}
1787
Mathias Agopian4fec8732012-06-29 14:12:52 -07001788Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001790 ATRACE_CALL();
1791
Jesse Hall399184a2014-03-03 15:42:54 -08001792 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1793 // mSidebandStreamChanged was true
1794 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001795 if (mSidebandStream != NULL) {
1796 setTransactionFlags(eTransactionNeeded);
1797 mFlinger->setTransactionFlags(eTraversalNeeded);
1798 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001799 recomputeVisibleRegions = true;
1800
1801 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001802 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001803 }
1804
Mathias Agopian4fec8732012-06-29 14:12:52 -07001805 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001806 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001807
1808 // if we've already called updateTexImage() without going through
1809 // a composition step, we have to skip this layer at this point
1810 // because we cannot call updateTeximage() without a corresponding
1811 // compositionComplete() call.
1812 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001813 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001814 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001815 }
1816
Dan Stoza1ce65812016-06-15 16:26:27 -07001817 // If the head buffer's acquire fence hasn't signaled yet, return and
1818 // try again later
1819 if (!headFenceHasSignaled()) {
1820 mFlinger->signalLayerUpdate();
1821 return outDirtyRegion;
1822 }
1823
Jamie Gennis351a5132011-09-14 18:23:37 -07001824 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001825 const State& s(getDrawingState());
1826 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001827 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001828
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001829 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001830 Layer::State& front;
1831 Layer::State& current;
1832 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001833 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001834 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001835 int32_t overrideScalingMode;
Robert Carra3920732016-06-20 21:49:49 -07001836 bool& freezePositionUpdates;
Robert Carrb5d3d262016-03-25 15:08:13 -07001837
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001838 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001839 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001840 const char* name,
Robert Carra3920732016-06-20 21:49:49 -07001841 int32_t overrideScalingMode,
1842 bool& freezePositionUpdates)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001843 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001844 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001845 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001846 name(name),
Robert Carra3920732016-06-20 21:49:49 -07001847 overrideScalingMode(overrideScalingMode),
1848 freezePositionUpdates(freezePositionUpdates) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001849 }
1850
1851 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001852 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001853 if (buf == NULL) {
1854 return false;
1855 }
1856
1857 uint32_t bufWidth = buf->getWidth();
1858 uint32_t bufHeight = buf->getHeight();
1859
1860 // check that we received a buffer of the right size
1861 // (Take the buffer's orientation into account)
1862 if (item.mTransform & Transform::ROT_90) {
1863 swap(bufWidth, bufHeight);
1864 }
1865
Robert Carrc3574f72016-03-24 12:19:32 -07001866 int actualScalingMode = overrideScalingMode >= 0 ?
1867 overrideScalingMode : item.mScalingMode;
1868 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001869 if (front.active != front.requested) {
1870
1871 if (isFixedSize ||
1872 (bufWidth == front.requested.w &&
1873 bufHeight == front.requested.h))
1874 {
1875 // Here we pretend the transaction happened by updating the
1876 // current and drawing states. Drawing state is only accessed
1877 // in this thread, no need to have it locked
1878 front.active = front.requested;
1879
1880 // We also need to update the current state so that
1881 // we don't end-up overwriting the drawing state with
1882 // this stale current state during the next transaction
1883 //
1884 // NOTE: We don't need to hold the transaction lock here
1885 // because State::active is only accessed from this thread.
1886 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001887 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001888
1889 // recompute visible region
1890 recomputeVisibleRegions = true;
1891 }
1892
1893 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001894 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001895 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001896 " requested={ wh={%4u,%4u} }}\n",
1897 name,
Andy McFadden69052052012-09-14 16:10:11 -07001898 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001899 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001900 front.crop.left,
1901 front.crop.top,
1902 front.crop.right,
1903 front.crop.bottom,
1904 front.crop.getWidth(),
1905 front.crop.getHeight(),
1906 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001907 }
1908
Ruben Brunk1681d952014-06-27 15:51:55 -07001909 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001910 if (front.active.w != bufWidth ||
1911 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001912 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001913 ALOGE("[%s] rejecting buffer: "
1914 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1915 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001916 return true;
1917 }
1918 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001919
1920 // if the transparent region has changed (this test is
1921 // conservative, but that's fine, worst case we're doing
1922 // a bit of extra work), we latch the new one and we
1923 // trigger a visible-region recompute.
1924 if (!front.activeTransparentRegion.isTriviallyEqual(
1925 front.requestedTransparentRegion)) {
1926 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001927
1928 // We also need to update the current state so that
1929 // we don't end-up overwriting the drawing state with
1930 // this stale current state during the next transaction
1931 //
1932 // NOTE: We don't need to hold the transaction lock here
1933 // because State::active is only accessed from this thread.
1934 current.activeTransparentRegion = front.activeTransparentRegion;
1935
1936 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001937 recomputeVisibleRegions = true;
1938 }
1939
Robert Carr99e27f02016-06-16 15:18:02 -07001940 if (front.crop != front.requestedCrop) {
1941 front.crop = front.requestedCrop;
1942 current.crop = front.requestedCrop;
1943 recomputeVisibleRegions = true;
1944 }
Robert Carra3920732016-06-20 21:49:49 -07001945 freezePositionUpdates = false;
Robert Carr99e27f02016-06-16 15:18:02 -07001946
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001947 return false;
1948 }
1949 };
1950
Ruben Brunk1681d952014-06-27 15:51:55 -07001951 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001952 getProducerStickyTransform() != 0, mName.string(),
Robert Carra3920732016-06-20 21:49:49 -07001953 mOverrideScalingMode, mFreezePositionUpdates);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001954
Dan Stozacac35382016-01-27 12:21:06 -08001955
1956 // Check all of our local sync points to ensure that all transactions
1957 // which need to have been applied prior to the frame which is about to
1958 // be latched have signaled
1959
1960 auto headFrameNumber = getHeadFrameNumber();
1961 bool matchingFramesFound = false;
1962 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001963 {
Dan Stozacac35382016-01-27 12:21:06 -08001964 Mutex::Autolock lock(mLocalSyncPointMutex);
1965 for (auto& point : mLocalSyncPoints) {
1966 if (point->getFrameNumber() > headFrameNumber) {
1967 break;
1968 }
1969
1970 matchingFramesFound = true;
1971
1972 if (!point->frameIsAvailable()) {
1973 // We haven't notified the remote layer that the frame for
1974 // this point is available yet. Notify it now, and then
1975 // abort this attempt to latch.
1976 point->setFrameAvailable();
1977 allTransactionsApplied = false;
1978 break;
1979 }
1980
1981 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001982 }
1983 }
1984
Dan Stozacac35382016-01-27 12:21:06 -08001985 if (matchingFramesFound && !allTransactionsApplied) {
1986 mFlinger->signalLayerUpdate();
1987 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001988 }
1989
Pablo Ceballos06312182015-10-07 16:32:12 -07001990 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1991 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos2dcb3632016-03-17 15:50:23 -07001992 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001993 // buffer mode.
1994 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001995 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001996 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001997 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001998 if (updateResult == BufferQueue::PRESENT_LATER) {
1999 // Producer doesn't want buffer to be displayed yet. Signal a
2000 // layer update so we check again at the next opportunity.
2001 mFlinger->signalLayerUpdate();
2002 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07002003 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
2004 // If the buffer has been rejected, remove it from the shadow queue
2005 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07002006 if (queuedBuffer) {
2007 Mutex::Autolock lock(mQueueItemLock);
2008 mQueueItems.removeAt(0);
2009 android_atomic_dec(&mQueuedFrames);
2010 }
Dan Stozaecc50402015-04-28 14:42:06 -07002011 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07002012 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
2013 // This can occur if something goes wrong when trying to create the
2014 // EGLImage for this buffer. If this happens, the buffer has already
2015 // been released, so we need to clean up the queue and bug out
2016 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07002017 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07002018 Mutex::Autolock lock(mQueueItemLock);
2019 mQueueItems.clear();
2020 android_atomic_and(0, &mQueuedFrames);
2021 }
2022
2023 // Once we have hit this state, the shadow queue may no longer
2024 // correctly reflect the incoming BufferQueue's contents, so even if
2025 // updateTexImage starts working, the only safe course of action is
2026 // to continue to ignore updates.
2027 mUpdateTexImageFailed = true;
2028
2029 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07002030 }
2031
Pablo Ceballos06312182015-10-07 16:32:12 -07002032 if (queuedBuffer) {
2033 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07002034 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2035
Dan Stoza6b9454d2014-11-07 16:00:59 -08002036 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07002037
2038 // Remove any stale buffers that have been dropped during
2039 // updateTexImage
2040 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
2041 mQueueItems.removeAt(0);
2042 android_atomic_dec(&mQueuedFrames);
2043 }
2044
Dan Stoza6b9454d2014-11-07 16:00:59 -08002045 mQueueItems.removeAt(0);
2046 }
2047
Dan Stozaecc50402015-04-28 14:42:06 -07002048
Andy McFadden1585c4d2013-06-28 13:52:40 -07002049 // Decrement the queued-frames count. Signal another event if we
2050 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07002051 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002052 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07002053 mFlinger->signalLayerUpdate();
2054 }
2055
2056 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002057 // something happened!
2058 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07002059 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002060 }
Mathias Agopian96f08192010-06-02 23:28:45 -07002061
Jamie Gennis351a5132011-09-14 18:23:37 -07002062 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002063 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07002064 if (mActiveBuffer == NULL) {
2065 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07002066 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002067 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08002068
Mathias Agopian4824d402012-06-04 18:16:30 -07002069 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002070 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07002071 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002072 // the first time we receive a buffer, we need to trigger a
2073 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002074 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002075 }
2076
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002077 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2078 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2079 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002080 if ((crop != mCurrentCrop) ||
2081 (transform != mCurrentTransform) ||
2082 (scalingMode != mCurrentScalingMode))
2083 {
2084 mCurrentCrop = crop;
2085 mCurrentTransform = transform;
2086 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002087 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002088 }
2089
2090 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002091 uint32_t bufWidth = mActiveBuffer->getWidth();
2092 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002093 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2094 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002095 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002096 }
2097 }
2098
2099 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002100 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002101 recomputeVisibleRegions = true;
2102 }
2103
Dan Stozacac35382016-01-27 12:21:06 -08002104 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2105
2106 // Remove any sync points corresponding to the buffer which was just
2107 // latched
2108 {
2109 Mutex::Autolock lock(mLocalSyncPointMutex);
2110 auto point = mLocalSyncPoints.begin();
2111 while (point != mLocalSyncPoints.end()) {
2112 if (!(*point)->frameIsAvailable() ||
2113 !(*point)->transactionIsApplied()) {
2114 // This sync point must have been added since we started
2115 // latching. Don't drop it yet.
2116 ++point;
2117 continue;
2118 }
2119
2120 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2121 point = mLocalSyncPoints.erase(point);
2122 } else {
2123 ++point;
2124 }
2125 }
2126 }
2127
Mathias Agopian4fec8732012-06-29 14:12:52 -07002128 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002129 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002130
2131 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002132 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002133 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002134 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135}
2136
Mathias Agopiana67932f2011-04-20 14:20:59 -07002137uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002138{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002139 // TODO: should we do something special if mSecure is set?
2140 if (mProtectedByApp) {
2141 // need a hardware-protected path to external video sink
2142 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002143 }
Riley Andrews03414a12014-07-01 14:22:59 -07002144 if (mPotentialCursor) {
2145 usage |= GraphicBuffer::USAGE_CURSOR;
2146 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002147 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002148 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002149}
2150
Mathias Agopian84300952012-11-21 16:02:13 -08002151void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002152 uint32_t orientation = 0;
2153 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002154 // The transform hint is used to improve performance, but we can
2155 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002156 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002157 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002158 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002159 if (orientation & Transform::ROT_INVALID) {
2160 orientation = 0;
2161 }
2162 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002163 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002164}
2165
Mathias Agopian13127d82013-03-05 17:47:11 -08002166// ----------------------------------------------------------------------------
2167// debugging
2168// ----------------------------------------------------------------------------
2169
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002170void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002171{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002172 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002173
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002174 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002175 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002176 "+ %s %p (%s)\n",
2177 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002178 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002179
Mathias Agopian2ca79392013-04-02 18:30:32 -07002180 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002181 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002182 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002183 sp<Client> client(mClientRef.promote());
2184
Mathias Agopian74d211a2013-04-22 16:55:35 +02002185 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002186 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2187 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002188 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002189#ifdef USE_HWC2
2190 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2191#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002192 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002193#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002194 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002195 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 -07002196 s.crop.left, s.crop.top,
2197 s.crop.right, s.crop.bottom,
2198 s.finalCrop.left, s.finalCrop.top,
2199 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002200 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002201 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002202 s.active.transform[0][0], s.active.transform[0][1],
2203 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002204 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002205
2206 sp<const GraphicBuffer> buf0(mActiveBuffer);
2207 uint32_t w0=0, h0=0, s0=0, f0=0;
2208 if (buf0 != 0) {
2209 w0 = buf0->getWidth();
2210 h0 = buf0->getHeight();
2211 s0 = buf0->getStride();
2212 f0 = buf0->format;
2213 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002214 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002215 " "
2216 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2217 " queued-frames=%d, mRefreshPending=%d\n",
2218 mFormat, w0, h0, s0,f0,
2219 mQueuedFrames, mRefreshPending);
2220
Mathias Agopian13127d82013-03-05 17:47:11 -08002221 if (mSurfaceFlingerConsumer != 0) {
Colin Cross3d1d2802016-09-26 18:10:16 -07002222 mSurfaceFlingerConsumer->dumpState(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002223 }
2224}
2225
Dan Stozae22aec72016-08-01 13:20:59 -07002226#ifdef USE_HWC2
2227void Layer::miniDumpHeader(String8& result) {
2228 result.append("----------------------------------------");
2229 result.append("---------------------------------------\n");
2230 result.append(" Layer name\n");
2231 result.append(" Z | ");
2232 result.append(" Comp Type | ");
2233 result.append(" Disp Frame (LTRB) | ");
2234 result.append(" Source Crop (LTRB)\n");
2235 result.append("----------------------------------------");
2236 result.append("---------------------------------------\n");
2237}
2238
2239void Layer::miniDump(String8& result, int32_t hwcId) const {
2240 if (mHwcLayers.count(hwcId) == 0) {
2241 return;
2242 }
2243
2244 String8 name;
2245 if (mName.length() > 77) {
2246 std::string shortened;
2247 shortened.append(mName.string(), 36);
2248 shortened.append("[...]");
2249 shortened.append(mName.string() + (mName.length() - 36), 36);
2250 name = shortened.c_str();
2251 } else {
2252 name = mName;
2253 }
2254
2255 result.appendFormat(" %s\n", name.string());
2256
2257 const Layer::State& layerState(getDrawingState());
2258 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
2259 result.appendFormat(" %10u | ", layerState.z);
2260 result.appendFormat("%10s | ",
2261 to_string(getCompositionType(hwcId)).c_str());
2262 const Rect& frame = hwcInfo.displayFrame;
2263 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top,
2264 frame.right, frame.bottom);
2265 const FloatRect& crop = hwcInfo.sourceCrop;
2266 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top,
2267 crop.right, crop.bottom);
2268
2269 result.append("- - - - - - - - - - - - - - - - - - - - ");
2270 result.append("- - - - - - - - - - - - - - - - - - - -\n");
2271}
2272#endif
2273
Svetoslavd85084b2014-03-20 10:28:31 -07002274void Layer::dumpFrameStats(String8& result) const {
2275 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002276}
2277
Svetoslavd85084b2014-03-20 10:28:31 -07002278void Layer::clearFrameStats() {
2279 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002280}
2281
Jamie Gennis6547ff42013-07-16 20:12:42 -07002282void Layer::logFrameStats() {
2283 mFrameTracker.logAndResetStats(mName);
2284}
2285
Svetoslavd85084b2014-03-20 10:28:31 -07002286void Layer::getFrameStats(FrameStats* outStats) const {
2287 mFrameTracker.getStats(outStats);
2288}
2289
Pablo Ceballos40845df2016-01-25 17:41:15 -08002290void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2291 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2292 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2293 *outName = mName;
2294 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2295
2296#ifdef USE_HWC2
2297 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2298 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2299 HWC2::Composition::Client : true;
2300#else
2301 *outIsGlesComposition = mIsGlesComposition;
2302#endif
2303 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2304 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2305 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2306}
Dan Stozae77c7662016-05-13 11:37:28 -07002307
2308std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
2309 bool forceFlush) {
2310 std::vector<OccupancyTracker::Segment> history;
2311 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush,
2312 &history);
2313 if (result != NO_ERROR) {
2314 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(),
2315 result);
2316 return {};
2317 }
2318 return history;
2319}
2320
Robert Carr367c5682016-06-20 11:55:28 -07002321bool Layer::getTransformToDisplayInverse() const {
2322 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
2323}
2324
Mathias Agopian13127d82013-03-05 17:47:11 -08002325// ---------------------------------------------------------------------------
2326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002327}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002328
2329#if defined(__gl_h_)
2330#error "don't include gl/gl.h in this file"
2331#endif
2332
2333#if defined(__gl2_h_)
2334#error "don't include gl2/gl2.h in this file"
2335#endif