blob: 2e16d8467ff69e4837b67ab2d0db35dbab0b9b9b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Dan Stoza6b9454d2014-11-07 16:00:59 -080040#include <gui/BufferItem.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020044#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include "Layer.h"
Dan Stozab9b08832014-03-13 11:55:57 -070047#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian1b031492012-06-20 17:51:20 -070050#include "DisplayHardware/HWComposer.h"
51
Mathias Agopian875d8e12013-06-07 15:35:48 -070052#include "RenderEngine/RenderEngine.h"
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#define DEBUG_RESIZE 0
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056namespace android {
57
58// ---------------------------------------------------------------------------
59
Mathias Agopian13127d82013-03-05 17:47:11 -080060int32_t Layer::sSequence = 1;
61
Mathias Agopian4d9b8222013-03-12 17:11:48 -070062Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
63 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080064 : contentDirty(false),
65 sequence(uint32_t(android_atomic_inc(&sSequence))),
66 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070067 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080068 mPremultipliedAlpha(true),
69 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070072 mPendingStateMutex(),
73 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080075 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070077 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070078 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080080 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080081 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080082 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080083 mFiltering(false),
84 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070085 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Pablo Ceballos40845df2016-01-25 17:41:15 -080086#ifndef USE_HWC2
87 mIsGlesComposition(false),
88#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080089 mProtectedByApp(false),
90 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070091 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070092 mPotentialCursor(false),
93 mQueueItemLock(),
94 mQueueItemCondition(),
95 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070096 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080097 mUpdateTexImageFailed(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080098 mAutoRefresh(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100#ifdef USE_HWC2
101 ALOGV("Creating Layer %s", name.string());
102#endif
103
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700105 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700106 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 uint32_t layerFlags = 0;
109 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800110 layerFlags |= layer_state_t::eLayerHidden;
111 if (flags & ISurfaceComposerClient::eOpaque)
112 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700113 if (flags & ISurfaceComposerClient::eSecure)
114 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 if (flags & ISurfaceComposerClient::eNonPremultiplied)
117 mPremultipliedAlpha = false;
118
119 mName = name;
120
121 mCurrentState.active.w = w;
122 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800123 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700124 mCurrentState.crop.makeInvalid();
125 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127#ifdef USE_HWC2
128 mCurrentState.alpha = 1.0f;
129#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800131#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132 mCurrentState.layerStack = 0;
133 mCurrentState.flags = layerFlags;
134 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135 mCurrentState.requested = mCurrentState.active;
136
137 // drawing state & current state are identical
138 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700139
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140#ifdef USE_HWC2
141 const auto& hwc = flinger->getHwComposer();
142 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
143 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
144#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700145 nsecs_t displayPeriod =
146 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800147#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800149}
150
Mathias Agopian3f844832013-08-07 21:24:32 -0700151void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800152 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700153 sp<IGraphicBufferProducer> producer;
154 sp<IGraphicBufferConsumer> consumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700155 BufferQueue::createBufferQueue(&producer, &consumer);
156 mProducer = new MonitoredProducer(producer, mFlinger);
157 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800158 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800159 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700160 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800161
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700162#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
163#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700164#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700165 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800166#endif
Andy McFadden69052052012-09-14 16:10:11 -0700167
Mathias Agopian84300952012-11-21 16:02:13 -0800168 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
169 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700170}
171
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700172Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800173 sp<Client> c(mClientRef.promote());
174 if (c != 0) {
175 c->detachLayer(this);
176 }
177
Dan Stozacac35382016-01-27 12:21:06 -0800178 for (auto& point : mRemoteSyncPoints) {
179 point->setTransactionApplied();
180 }
Dan Stozac8145172016-04-28 16:29:06 -0700181 for (auto& point : mLocalSyncPoints) {
182 point->setFrameAvailable();
183 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700184 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700185 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// callbacks
190// ---------------------------------------------------------------------------
191
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192#ifdef USE_HWC2
193void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
194 if (mHwcLayers.empty()) {
195 return;
196 }
197 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
198}
199#else
Dan Stozac7014012014-02-14 15:03:43 -0800200void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 HWComposer::HWCLayerInterface* layer) {
202 if (layer) {
203 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700204 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800205 }
206}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800208
Dan Stoza6b9454d2014-11-07 16:00:59 -0800209void Layer::onFrameAvailable(const BufferItem& item) {
210 // Add this buffer from our internal queue tracker
211 { // Autolock scope
212 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700213
214 // Reset the frame number tracker when we receive the first buffer after
215 // a frame number reset
216 if (item.mFrameNumber == 1) {
217 mLastFrameNumberReceived = 0;
218 }
219
220 // Ensure that callbacks are handled in order
221 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
222 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
223 ms2ns(500));
224 if (result != NO_ERROR) {
225 ALOGE("[%s] Timed out waiting on callback", mName.string());
226 }
227 }
228
Dan Stoza6b9454d2014-11-07 16:00:59 -0800229 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700230 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700231
232 // Wake up any pending callbacks
233 mLastFrameNumberReceived = item.mFrameNumber;
234 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800235 }
236
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800237 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700238}
239
Dan Stoza6b9454d2014-11-07 16:00:59 -0800240void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700241 { // Autolock scope
242 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700243
Dan Stoza7dde5992015-05-22 09:51:44 -0700244 // Ensure that callbacks are handled in order
245 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
246 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
247 ms2ns(500));
248 if (result != NO_ERROR) {
249 ALOGE("[%s] Timed out waiting on callback", mName.string());
250 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700251 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700252
253 if (mQueueItems.empty()) {
254 ALOGE("Can't replace a frame on an empty queue");
255 return;
256 }
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700257 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
Dan Stoza7dde5992015-05-22 09:51:44 -0700258
259 // Wake up any pending callbacks
260 mLastFrameNumberReceived = item.mFrameNumber;
261 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700262 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800263}
264
Jesse Hall399184a2014-03-03 15:42:54 -0800265void Layer::onSidebandStreamChanged() {
266 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
267 // mSidebandStreamChanged was false
268 mFlinger->signalLayerUpdate();
269 }
270}
271
Mathias Agopian67106042013-03-14 19:18:13 -0700272// called with SurfaceFlinger::mStateLock from the drawing thread after
273// the layer has been remove from the current state list (and just before
274// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800275void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800276 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700277}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700278
Mathias Agopian13127d82013-03-05 17:47:11 -0800279// ---------------------------------------------------------------------------
280// set-up
281// ---------------------------------------------------------------------------
282
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700283const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800284 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Mathias Agopianf9d93272009-06-19 17:00:27 -0700287status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 PixelFormat format, uint32_t flags)
289{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700290 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700292
293 // never allow a surface larger than what our underlying GL implementation
294 // can handle.
295 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800296 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700297 return BAD_VALUE;
298 }
299
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700300 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700301
Riley Andrews03414a12014-07-01 14:22:59 -0700302 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700303 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304 mCurrentOpacity = getOpacityForFormat(format);
305
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800306 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
307 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
308 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return NO_ERROR;
311}
312
Dan Stoza7dde5992015-05-22 09:51:44 -0700313/*
314 * The layer handle is just a BBinder object passed to the client
315 * (remote process) -- we don't keep any reference on our side such that
316 * the dtor is called when the remote side let go of its reference.
317 *
318 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
319 * this layer when the handle is destroyed.
320 */
321class Layer::Handle : public BBinder, public LayerCleaner {
322 public:
323 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
324 : LayerCleaner(flinger, layer), owner(layer) {}
325
326 wp<Layer> owner;
327};
328
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700329sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800330 Mutex::Autolock _l(mLock);
331
332 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700333 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800334
335 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700336
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700337 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800338}
339
Dan Stozab9b08832014-03-13 11:55:57 -0700340sp<IGraphicBufferProducer> Layer::getProducer() const {
341 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700342}
343
Mathias Agopian13127d82013-03-05 17:47:11 -0800344// ---------------------------------------------------------------------------
345// h/w composer set-up
346// ---------------------------------------------------------------------------
347
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800348Rect Layer::getContentCrop() const {
349 // this is the crop rectangle that applies to the buffer
350 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700351 Rect crop;
352 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800353 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700354 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800355 } else if (mActiveBuffer != NULL) {
356 // otherwise we use the whole buffer
357 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700358 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800359 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700360 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700361 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700362 return crop;
363}
364
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700365static Rect reduce(const Rect& win, const Region& exclude) {
366 if (CC_LIKELY(exclude.isEmpty())) {
367 return win;
368 }
369 if (exclude.isRect()) {
370 return win.reduce(exclude.getBounds());
371 }
372 return Region(win).subtract(exclude).getBounds();
373}
374
Mathias Agopian13127d82013-03-05 17:47:11 -0800375Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700376 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700377 return computeBounds(s.activeTransparentRegion);
378}
379
380Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
381 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800382 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700383
384 if (!s.crop.isEmpty()) {
385 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700387 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700388 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800389}
390
Mathias Agopian6b442672013-07-09 21:24:52 -0700391FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800392 // the content crop is the area of the content that gets scaled to the
393 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700394 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800395
Robert Carrb5d3d262016-03-25 15:08:13 -0700396 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800397 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700398 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800399
400 // apply the projection's clipping to the window crop in
401 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700402 // if there are no window scaling involved, this operation will map to full
403 // pixels in the buffer.
404 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
405 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700406
407 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700408 if (!s.crop.isEmpty()) {
409 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700410 }
411
Robert Carr3dcabfa2016-03-01 18:36:58 -0800412 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
414 activeCrop.clear();
415 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700416 if (!s.finalCrop.isEmpty()) {
417 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 activeCrop.clear();
419 }
420 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800421 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800422
Michael Lentine28ea2172014-11-19 18:32:37 -0800423 // This needs to be here as transform.transform(Rect) computes the
424 // transformed rect and then takes the bounding box of the result before
425 // returning. This means
426 // transform.inverse().transform(transform.transform(Rect)) != Rect
427 // in which case we need to make sure the final rect is clipped to the
428 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
430 activeCrop.clear();
431 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800432
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700433 // subtract the transparent region and snap to the bounds
434 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
435
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 // Transform the window crop to match the buffer coordinate system,
437 // which means using the inverse of the current transform set on the
438 // SurfaceFlingerConsumer.
439 uint32_t invTransform = mCurrentTransform;
440 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
441 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700442 * the code below applies the primary display's inverse transform to the
443 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700445 uint32_t invTransformOrient =
446 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // calculate the inverse transform
448 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
449 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
450 NATIVE_WINDOW_TRANSFORM_FLIP_H;
451 // If the transform has been rotated the axis of flip has been swapped
452 // so we need to swap which flip operations we are performing
Michael Lentine7b902582014-08-19 18:14:06 -0700453 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
454 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 if (is_h_flipped != is_v_flipped) {
Michael Lentine7b902582014-08-19 18:14:06 -0700456 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
457 NATIVE_WINDOW_TRANSFORM_FLIP_H;
458 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800459 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 // and apply to the current transform
461 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800462 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463
464 int winWidth = s.active.w;
465 int winHeight = s.active.h;
466 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
467 // If the activeCrop has been rotate the ends are rotated but not
468 // the space itself so when transforming ends back we can't rely on
469 // a modification of the axes of rotation. To account for this we
470 // need to reorient the inverse rotation in terms of the current
471 // axes of rotation.
472 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
473 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
474 if (is_h_flipped == is_v_flipped) {
475 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
476 NATIVE_WINDOW_TRANSFORM_FLIP_H;
477 }
478 winWidth = s.active.h;
479 winHeight = s.active.w;
480 }
481 const Rect winCrop = activeCrop.transform(
482 invTransform, s.active.w, s.active.h);
483
484 // below, crop is intersected with winCrop expressed in crop's coordinate space
485 float xScale = crop.getWidth() / float(winWidth);
486 float yScale = crop.getHeight() / float(winHeight);
487
488 float insetL = winCrop.left * xScale;
489 float insetT = winCrop.top * yScale;
490 float insetR = (winWidth - winCrop.right ) * xScale;
491 float insetB = (winHeight - winCrop.bottom) * yScale;
492
493 crop.left += insetL;
494 crop.top += insetT;
495 crop.right -= insetR;
496 crop.bottom -= insetB;
497
Mathias Agopian13127d82013-03-05 17:47:11 -0800498 return crop;
499}
500
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501#ifdef USE_HWC2
502void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
503#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700504void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700505 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700506 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700508{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509#ifdef USE_HWC2
510 const auto hwcId = displayDevice->getHwcDisplayId();
511 auto& hwcInfo = mHwcLayers[hwcId];
512#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800513 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700515
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700516 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800517#ifdef USE_HWC2
518 hwcInfo.forceClientComposition = false;
519
520 if (isSecure() && !displayDevice->isSecure()) {
521 hwcInfo.forceClientComposition = true;
522 }
523
524 auto& hwcLayer = hwcInfo.layer;
525#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700526 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700527
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700528 if (isSecure() && !hw->isSecure()) {
529 layer.setSkip(true);
530 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700532
Mathias Agopian13127d82013-03-05 17:47:11 -0800533 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700534 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800535#ifdef USE_HWC2
536 if (!isOpaque(s) || s.alpha != 1.0f) {
537 auto blendMode = mPremultipliedAlpha ?
538 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
539 auto error = hwcLayer->setBlendMode(blendMode);
540 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
541 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
542 to_string(error).c_str(), static_cast<int32_t>(error));
543 }
544#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800545 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800546 layer.setBlending(mPremultipliedAlpha ?
547 HWC_BLENDING_PREMULT :
548 HWC_BLENDING_COVERAGE);
549 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800551
552 // apply the layer's transform, followed by the display's global transform
553 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700554 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700555 if (!s.crop.isEmpty()) {
556 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800557 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000559 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000561 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000563 activeCrop.clear();
564 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800565 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800566 // This needs to be here as transform.transform(Rect) computes the
567 // transformed rect and then takes the bounding box of the result before
568 // returning. This means
569 // transform.inverse().transform(transform.transform(Rect)) != Rect
570 // in which case we need to make sure the final rect is clipped to the
571 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000572 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
573 activeCrop.clear();
574 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700575 // mark regions outside the crop as transparent
576 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
577 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
578 s.active.w, s.active.h));
579 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
580 activeCrop.left, activeCrop.bottom));
581 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
582 s.active.w, activeCrop.bottom));
583 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800584 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700585 if (!s.finalCrop.isEmpty()) {
586 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000587 frame.clear();
588 }
589 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000591 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
592 frame.clear();
593 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594 const Transform& tr(displayDevice->getTransform());
595 Rect transformedFrame = tr.transform(frame);
596 auto error = hwcLayer->setDisplayFrame(transformedFrame);
597 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
598 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
599 transformedFrame.top, transformedFrame.right,
600 transformedFrame.bottom, to_string(error).c_str(),
601 static_cast<int32_t>(error));
602
603 FloatRect sourceCrop = computeCrop(displayDevice);
604 error = hwcLayer->setSourceCrop(sourceCrop);
605 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
606 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
607 sourceCrop.left, sourceCrop.top, sourceCrop.right,
608 sourceCrop.bottom, to_string(error).c_str(),
609 static_cast<int32_t>(error));
610
611 error = hwcLayer->setPlaneAlpha(s.alpha);
612 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
613 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
614 static_cast<int32_t>(error));
615
616 error = hwcLayer->setZOrder(s.z);
617 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
618 mName.string(), s.z, to_string(error).c_str(),
619 static_cast<int32_t>(error));
620#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000621 if (!frame.intersect(hw->getViewport(), &frame)) {
622 frame.clear();
623 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800624 const Transform& tr(hw->getTransform());
625 layer.setFrame(tr.transform(frame));
626 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800627 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800629
Mathias Agopian29a367b2011-07-12 14:51:45 -0700630 /*
631 * Transformations are applied in this order:
632 * 1) buffer orientation/flip/mirror
633 * 2) state transformation (window manager)
634 * 3) layer orientation (screen orientation)
635 * (NOTE: the matrices are multiplied in reverse order)
636 */
637
638 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800639 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640
641 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
642 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700643 * the code below applies the primary display's inverse transform to the
644 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700646 uint32_t invTransform =
647 DisplayDevice::getPrimaryDisplayOrientationTransform();
648
Michael Lentine14409632014-08-19 11:27:30 -0700649 uint32_t t_orientation = transform.getOrientation();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650 // calculate the inverse transform
651 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
652 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
653 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Michael Lentine14409632014-08-19 11:27:30 -0700654 // If the transform has been rotated the axis of flip has been swapped
655 // so we need to swap which flip operations we are performing
656 bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
657 bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
658 if (is_h_flipped != is_v_flipped) {
659 t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
660 NATIVE_WINDOW_TRANSFORM_FLIP_H;
661 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700662 }
663 // and apply to the current transform
Michael Lentine14409632014-08-19 11:27:30 -0700664 transform = Transform(t_orientation) * Transform(invTransform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700665 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700666
667 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800668 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669#ifdef USE_HWC2
670 if (orientation & Transform::ROT_INVALID) {
671 // we can only handle simple transformation
672 hwcInfo.forceClientComposition = true;
673 } else {
674 auto transform = static_cast<HWC2::Transform>(orientation);
675 auto error = hwcLayer->setTransform(transform);
676 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
677 "%s (%d)", mName.string(), to_string(transform).c_str(),
678 to_string(error).c_str(), static_cast<int32_t>(error));
679 }
680#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800681 if (orientation & Transform::ROT_INVALID) {
682 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700683 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700684 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800685 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700686 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700688}
689
Dan Stoza9e56aa02015-11-02 13:00:03 -0800690#ifdef USE_HWC2
691void Layer::forceClientComposition(int32_t hwcId) {
692 if (mHwcLayers.count(hwcId) == 0) {
693 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
694 return;
695 }
696
697 mHwcLayers[hwcId].forceClientComposition = true;
698}
699#endif
700
701#ifdef USE_HWC2
702void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
703 // Apply this display's projection's viewport to the visible region
704 // before giving it to the HWC HAL.
705 const Transform& tr = displayDevice->getTransform();
706 const auto& viewport = displayDevice->getViewport();
707 Region visible = tr.transform(visibleRegion.intersect(viewport));
708 auto hwcId = displayDevice->getHwcDisplayId();
709 auto& hwcLayer = mHwcLayers[hwcId].layer;
710 auto error = hwcLayer->setVisibleRegion(visible);
711 if (error != HWC2::Error::None) {
712 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
713 to_string(error).c_str(), static_cast<int32_t>(error));
714 visible.dump(LOG_TAG);
715 }
716
717 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
718 if (error != HWC2::Error::None) {
719 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
720 to_string(error).c_str(), static_cast<int32_t>(error));
721 surfaceDamageRegion.dump(LOG_TAG);
722 }
723
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700724 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700726 setCompositionType(hwcId, HWC2::Composition::Sideband);
727 ALOGV("[%s] Requesting Sideband composition", mName.string());
728 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800729 if (error != HWC2::Error::None) {
730 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
731 mName.string(), mSidebandStream->handle(),
732 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700734 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 }
736
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700737 // Client or SolidColor layers
738 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr ||
739 mHwcLayers[hwcId].forceClientComposition) {
740 // TODO: This also includes solid color layers, but no API exists to
741 // setup a solid color layer yet
742 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800743 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700744 error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
745 if (error != HWC2::Error::None) {
746 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
747 to_string(error).c_str(), static_cast<int32_t>(error));
748 }
749 return;
750 }
751
752 // Device or Cursor layers
753 if (mPotentialCursor) {
754 ALOGV("[%s] Requesting Cursor composition", mName.string());
755 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756 } else {
757 ALOGV("[%s] Requesting Device composition", mName.string());
758 setCompositionType(hwcId, HWC2::Composition::Device);
759 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700760
761 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
762 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
763 if (error != HWC2::Error::None) {
764 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
765 mActiveBuffer->handle, to_string(error).c_str(),
766 static_cast<int32_t>(error));
767 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768}
769#else
Mathias Agopian42977342012-08-05 00:40:46 -0700770void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700771 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800772 // we have to set the visible region on every frame because
773 // we currently free it during onLayerDisplayed(), which is called
774 // after HWComposer::commit() -- every frame.
775 // Apply this display's projection's viewport to the visible region
776 // before giving it to the HWC HAL.
777 const Transform& tr = hw->getTransform();
778 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
779 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700780 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800781 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700782
Jesse Hall399184a2014-03-03 15:42:54 -0800783 if (mSidebandStream.get()) {
784 layer.setSidebandStream(mSidebandStream);
785 } else {
786 // NOTE: buffer can be NULL if the client never drew into this
787 // layer yet, or if we ran out of memory
788 layer.setBuffer(mActiveBuffer);
789 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700790}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700792
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793#ifdef USE_HWC2
794void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
795 auto hwcId = displayDevice->getHwcDisplayId();
796 if (mHwcLayers.count(hwcId) == 0 ||
797 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
798 return;
799 }
800
801 // This gives us only the "orientation" component of the transform
802 const State& s(getCurrentState());
803
804 // Apply the layer's transform, followed by the display's global transform
805 // Here we're guaranteed that the layer's transform preserves rects
806 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700807 if (!s.crop.isEmpty()) {
808 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800809 }
810 // Subtract the transparent region and snap to the bounds
811 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800812 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800813 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700814 if (!s.finalCrop.isEmpty()) {
815 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000816 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800817 auto& displayTransform(displayDevice->getTransform());
818 auto position = displayTransform.transform(frame);
819
820 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
821 position.top);
822 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
823 "to (%d, %d): %s (%d)", mName.string(), position.left,
824 position.top, to_string(error).c_str(),
825 static_cast<int32_t>(error));
826}
827#else
Dan Stozac7014012014-02-14 15:03:43 -0800828void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700829 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700830 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700831
832 // TODO: there is a possible optimization here: we only need to set the
833 // acquire fence the first time a new buffer is acquired on EACH display.
834
Riley Andrews03414a12014-07-01 14:22:59 -0700835 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800836 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800837 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700838 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700839 if (fenceFd == -1) {
840 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
841 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700842 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700843 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700844 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700845}
846
Riley Andrews03414a12014-07-01 14:22:59 -0700847Rect Layer::getPosition(
848 const sp<const DisplayDevice>& hw)
849{
850 // this gives us only the "orientation" component of the transform
851 const State& s(getCurrentState());
852
853 // apply the layer's transform, followed by the display's global transform
854 // here we're guaranteed that the layer's transform preserves rects
855 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700856 if (!s.crop.isEmpty()) {
857 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700858 }
859 // subtract the transparent region and snap to the bounds
860 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800861 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700862 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700863 if (!s.finalCrop.isEmpty()) {
864 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000865 }
Riley Andrews03414a12014-07-01 14:22:59 -0700866 const Transform& tr(hw->getTransform());
867 return Rect(tr.transform(frame));
868}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800869#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700870
Mathias Agopian13127d82013-03-05 17:47:11 -0800871// ---------------------------------------------------------------------------
872// drawing...
873// ---------------------------------------------------------------------------
874
875void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800876 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800877}
878
Dan Stozac7014012014-02-14 15:03:43 -0800879void Layer::draw(const sp<const DisplayDevice>& hw,
880 bool useIdentityTransform) const {
881 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800882}
883
Dan Stozac7014012014-02-14 15:03:43 -0800884void Layer::draw(const sp<const DisplayDevice>& hw) const {
885 onDraw(hw, Region(hw->bounds()), false);
886}
887
888void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
889 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800891 ATRACE_CALL();
892
Mathias Agopiana67932f2011-04-20 14:20:59 -0700893 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700895 // in fact never been drawn into. This happens frequently with
896 // SurfaceView because the WindowManager can't know when the client
897 // has drawn the first time.
898
899 // If there is nothing under us, we paint the screen in black, otherwise
900 // we just skip this update.
901
902 // figure out if there is something below us
903 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700904 const SurfaceFlinger::LayerVector& drawingLayers(
905 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700906 const size_t count = drawingLayers.size();
907 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 const sp<Layer>& layer(drawingLayers[i]);
909 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700910 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700911 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700912 }
913 // if not everything below us is covered, we plug the holes!
914 Region holes(clip.subtract(under));
915 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700916 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700917 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 return;
919 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700920
Andy McFadden97eba892012-12-11 15:21:45 -0800921 // Bind the current buffer to the GL texture, and wait for it to be
922 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800923 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
924 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800925 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700926 // Go ahead and draw the buffer anyway; no matter what we do the screen
927 // is probably going to have something visibly wrong.
928 }
929
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700930 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
931
Mathias Agopian875d8e12013-06-07 15:35:48 -0700932 RenderEngine& engine(mFlinger->getRenderEngine());
933
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700934 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700935 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700936 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700937
938 // Query the texture matrix given our current filtering mode.
939 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800940 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
941 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700942
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700943 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
944
945 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700946 * the code below applies the primary display's inverse transform to
947 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700948 */
949
950 // create a 4x4 transform matrix from the display transform flags
951 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
952 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
953 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
954
955 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700956 uint32_t transform =
957 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700958 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
959 tr = tr * rot90;
960 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
961 tr = tr * flipH;
962 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
963 tr = tr * flipV;
964
965 // calculate the inverse
966 tr = inverse(tr);
967
968 // and finally apply it to the original texture matrix
969 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
970 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
971 }
972
Jamie Genniscbb1a952012-05-08 17:05:52 -0700973 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700974 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
975 mTexture.setFiltering(useFiltering);
976 mTexture.setMatrix(textureMatrix);
977
978 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700979 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700980 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700981 }
Dan Stozac7014012014-02-14 15:03:43 -0800982 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700983 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984}
985
Mathias Agopian13127d82013-03-05 17:47:11 -0800986
Dan Stozac7014012014-02-14 15:03:43 -0800987void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
988 const Region& /* clip */, float red, float green, float blue,
989 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800990{
Mathias Agopian19733a32013-08-28 18:13:56 -0700991 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800992 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700993 engine.setupFillWithColor(red, green, blue, alpha);
994 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800995}
996
997void Layer::clearWithOpenGL(
998 const sp<const DisplayDevice>& hw, const Region& clip) const {
999 clearWithOpenGL(hw, clip, 0,0,0,0);
1000}
1001
Dan Stozac7014012014-02-14 15:03:43 -08001002void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
1003 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001004 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001005
Dan Stozac7014012014-02-14 15:03:43 -08001006 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001007
Mathias Agopian13127d82013-03-05 17:47:11 -08001008 /*
1009 * NOTE: the way we compute the texture coordinates here produces
1010 * different results than when we take the HWC path -- in the later case
1011 * the "source crop" is rounded to texel boundaries.
1012 * This can produce significantly different results when the texture
1013 * is scaled by a large amount.
1014 *
1015 * The GL code below is more logical (imho), and the difference with
1016 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001017 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001018 * GL composition when a buffer scaling is applied (maybe with some
1019 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1020 * like more of a hack.
1021 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001022 Rect win(computeBounds());
1023
Robert Carrb5d3d262016-03-25 15:08:13 -07001024 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001025 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001026 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001027 win.clear();
1028 }
1029 win = s.active.transform.inverse().transform(win);
1030 if (!win.intersect(computeBounds(), &win)) {
1031 win.clear();
1032 }
1033 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001034
Mathias Agopian3f844832013-08-07 21:24:32 -07001035 float left = float(win.left) / float(s.active.w);
1036 float top = float(win.top) / float(s.active.h);
1037 float right = float(win.right) / float(s.active.w);
1038 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001039
Mathias Agopian875d8e12013-06-07 15:35:48 -07001040 // TODO: we probably want to generate the texture coords with the mesh
1041 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001042 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1043 texCoords[0] = vec2(left, 1.0f - top);
1044 texCoords[1] = vec2(left, 1.0f - bottom);
1045 texCoords[2] = vec2(right, 1.0f - bottom);
1046 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001047
Mathias Agopian875d8e12013-06-07 15:35:48 -07001048 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001049 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001050 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001051 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001052}
1053
Dan Stoza9e56aa02015-11-02 13:00:03 -08001054#ifdef USE_HWC2
1055void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1056 bool callIntoHwc) {
1057 if (mHwcLayers.count(hwcId) == 0) {
1058 ALOGE("setCompositionType called without a valid HWC layer");
1059 return;
1060 }
1061 auto& hwcInfo = mHwcLayers[hwcId];
1062 auto& hwcLayer = hwcInfo.layer;
1063 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1064 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1065 if (hwcInfo.compositionType != type) {
1066 ALOGV(" actually setting");
1067 hwcInfo.compositionType = type;
1068 if (callIntoHwc) {
1069 auto error = hwcLayer->setCompositionType(type);
1070 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1071 "composition type %s: %s (%d)", mName.string(),
1072 to_string(type).c_str(), to_string(error).c_str(),
1073 static_cast<int32_t>(error));
1074 }
1075 }
1076}
1077
1078HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1079 if (mHwcLayers.count(hwcId) == 0) {
1080 ALOGE("getCompositionType called without a valid HWC layer");
1081 return HWC2::Composition::Invalid;
1082 }
1083 return mHwcLayers.at(hwcId).compositionType;
1084}
1085
1086void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1087 if (mHwcLayers.count(hwcId) == 0) {
1088 ALOGE("setClearClientTarget called without a valid HWC layer");
1089 return;
1090 }
1091 mHwcLayers[hwcId].clearClientTarget = clear;
1092}
1093
1094bool Layer::getClearClientTarget(int32_t hwcId) const {
1095 if (mHwcLayers.count(hwcId) == 0) {
1096 ALOGE("getClearClientTarget called without a valid HWC layer");
1097 return false;
1098 }
1099 return mHwcLayers.at(hwcId).clearClientTarget;
1100}
1101#endif
1102
Ruben Brunk1681d952014-06-27 15:51:55 -07001103uint32_t Layer::getProducerStickyTransform() const {
1104 int producerStickyTransform = 0;
1105 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1106 if (ret != OK) {
1107 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1108 strerror(-ret), ret);
1109 return 0;
1110 }
1111 return static_cast<uint32_t>(producerStickyTransform);
1112}
1113
Dan Stozacac35382016-01-27 12:21:06 -08001114uint64_t Layer::getHeadFrameNumber() const {
1115 Mutex::Autolock lock(mQueueItemLock);
1116 if (!mQueueItems.empty()) {
1117 return mQueueItems[0].mFrameNumber;
1118 } else {
1119 return mCurrentFrameNumber;
1120 }
1121}
1122
1123bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1124 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1125 // Don't bother with a SyncPoint, since we've already latched the
1126 // relevant frame
1127 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001128 }
1129
Dan Stozacac35382016-01-27 12:21:06 -08001130 Mutex::Autolock lock(mLocalSyncPointMutex);
1131 mLocalSyncPoints.push_back(point);
1132 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001133}
1134
Mathias Agopian13127d82013-03-05 17:47:11 -08001135void Layer::setFiltering(bool filtering) {
1136 mFiltering = filtering;
1137}
1138
1139bool Layer::getFiltering() const {
1140 return mFiltering;
1141}
1142
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001143// As documented in libhardware header, formats in the range
1144// 0x100 - 0x1FF are specific to the HAL implementation, and
1145// are known to have no alpha channel
1146// TODO: move definition for device-specific range into
1147// hardware.h, instead of using hard-coded values here.
1148#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1149
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001150bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001151 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1152 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001153 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001154 switch (format) {
1155 case HAL_PIXEL_FORMAT_RGBA_8888:
1156 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001157 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001158 }
1159 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001160 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001161}
1162
Mathias Agopian13127d82013-03-05 17:47:11 -08001163// ----------------------------------------------------------------------------
1164// local state
1165// ----------------------------------------------------------------------------
1166
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001167static void boundPoint(vec2* point, const Rect& crop) {
1168 if (point->x < crop.left) {
1169 point->x = crop.left;
1170 }
1171 if (point->x > crop.right) {
1172 point->x = crop.right;
1173 }
1174 if (point->y < crop.top) {
1175 point->y = crop.top;
1176 }
1177 if (point->y > crop.bottom) {
1178 point->y = crop.bottom;
1179 }
1180}
1181
Dan Stozac7014012014-02-14 15:03:43 -08001182void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1183 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001184{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001185 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001186 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 const uint32_t hw_h = hw->getHeight();
1188 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001189 if (!s.crop.isEmpty()) {
1190 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001191 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001192 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001193 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001194
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001195 vec2 lt = vec2(win.left, win.top);
1196 vec2 lb = vec2(win.left, win.bottom);
1197 vec2 rb = vec2(win.right, win.bottom);
1198 vec2 rt = vec2(win.right, win.top);
1199
1200 if (!useIdentityTransform) {
1201 lt = s.active.transform.transform(lt);
1202 lb = s.active.transform.transform(lb);
1203 rb = s.active.transform.transform(rb);
1204 rt = s.active.transform.transform(rt);
1205 }
1206
Robert Carrb5d3d262016-03-25 15:08:13 -07001207 if (!s.finalCrop.isEmpty()) {
1208 boundPoint(&lt, s.finalCrop);
1209 boundPoint(&lb, s.finalCrop);
1210 boundPoint(&rb, s.finalCrop);
1211 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001212 }
1213
Mathias Agopianff2ed702013-09-01 21:36:12 -07001214 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001215 position[0] = tr.transform(lt);
1216 position[1] = tr.transform(lb);
1217 position[2] = tr.transform(rb);
1218 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001219 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001220 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 }
1222}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001223
Andy McFadden4125a4f2014-01-29 17:17:11 -08001224bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001225{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001226 // if we don't have a buffer yet, we're translucent regardless of the
1227 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001228 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001229 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001230 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001231
1232 // if the layer has the opaque flag, then we're always opaque,
1233 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001234 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001235}
1236
Dan Stoza23116082015-06-18 14:58:39 -07001237bool Layer::isSecure() const
1238{
1239 const Layer::State& s(mDrawingState);
1240 return (s.flags & layer_state_t::eLayerSecure);
1241}
1242
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001243bool Layer::isProtected() const
1244{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001245 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001246 return (activeBuffer != 0) &&
1247 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1248}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001249
Mathias Agopian13127d82013-03-05 17:47:11 -08001250bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001251 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001252}
1253
1254bool Layer::isCropped() const {
1255 return !mCurrentCrop.isEmpty();
1256}
1257
1258bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1259 return mNeedsFiltering || hw->needsFiltering();
1260}
1261
1262void Layer::setVisibleRegion(const Region& visibleRegion) {
1263 // always called from main thread
1264 this->visibleRegion = visibleRegion;
1265}
1266
1267void Layer::setCoveredRegion(const Region& coveredRegion) {
1268 // always called from main thread
1269 this->coveredRegion = coveredRegion;
1270}
1271
1272void Layer::setVisibleNonTransparentRegion(const Region&
1273 setVisibleNonTransparentRegion) {
1274 // always called from main thread
1275 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1276}
1277
1278// ----------------------------------------------------------------------------
1279// transaction
1280// ----------------------------------------------------------------------------
1281
Dan Stoza7dde5992015-05-22 09:51:44 -07001282void Layer::pushPendingState() {
1283 if (!mCurrentState.modified) {
1284 return;
1285 }
1286
Dan Stoza7dde5992015-05-22 09:51:44 -07001287 // If this transaction is waiting on the receipt of a frame, generate a sync
1288 // point and send it to the remote layer.
1289 if (mCurrentState.handle != nullptr) {
1290 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1291 sp<Layer> handleLayer = handle->owner.promote();
1292 if (handleLayer == nullptr) {
1293 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1294 // If we can't promote the layer we are intended to wait on,
1295 // then it is expired or otherwise invalid. Allow this transaction
1296 // to be applied as per normal (no synchronization).
1297 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001298 } else {
1299 auto syncPoint = std::make_shared<SyncPoint>(
1300 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001301 if (handleLayer->addSyncPoint(syncPoint)) {
1302 mRemoteSyncPoints.push_back(std::move(syncPoint));
1303 } else {
1304 // We already missed the frame we're supposed to synchronize
1305 // on, so go ahead and apply the state update
1306 mCurrentState.handle = nullptr;
1307 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001308 }
1309
Dan Stoza7dde5992015-05-22 09:51:44 -07001310 // Wake us up to check if the frame has been received
1311 setTransactionFlags(eTransactionNeeded);
1312 }
1313 mPendingStates.push_back(mCurrentState);
1314}
1315
Pablo Ceballos05289c22016-04-14 15:49:55 -07001316void Layer::popPendingState(State* stateToCommit) {
1317 auto oldFlags = stateToCommit->flags;
1318 *stateToCommit = mPendingStates[0];
1319 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1320 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001321
1322 mPendingStates.removeAt(0);
1323}
1324
Pablo Ceballos05289c22016-04-14 15:49:55 -07001325bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001326 bool stateUpdateAvailable = false;
1327 while (!mPendingStates.empty()) {
1328 if (mPendingStates[0].handle != nullptr) {
1329 if (mRemoteSyncPoints.empty()) {
1330 // If we don't have a sync point for this, apply it anyway. It
1331 // will be visually wrong, but it should keep us from getting
1332 // into too much trouble.
1333 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001334 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001335 stateUpdateAvailable = true;
1336 continue;
1337 }
1338
Dan Stozacac35382016-01-27 12:21:06 -08001339 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1340 mPendingStates[0].frameNumber) {
1341 ALOGE("[%s] Unexpected sync point frame number found",
1342 mName.string());
1343
1344 // Signal our end of the sync point and then dispose of it
1345 mRemoteSyncPoints.front()->setTransactionApplied();
1346 mRemoteSyncPoints.pop_front();
1347 continue;
1348 }
1349
Dan Stoza7dde5992015-05-22 09:51:44 -07001350 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1351 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001352 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001353 stateUpdateAvailable = true;
1354
1355 // Signal our end of the sync point and then dispose of it
1356 mRemoteSyncPoints.front()->setTransactionApplied();
1357 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001358 } else {
1359 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001361 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001362 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001363 stateUpdateAvailable = true;
1364 }
1365 }
1366
1367 // If we still have pending updates, wake SurfaceFlinger back up and point
1368 // it at this layer so we can process them
1369 if (!mPendingStates.empty()) {
1370 setTransactionFlags(eTransactionNeeded);
1371 mFlinger->setTransactionFlags(eTraversalNeeded);
1372 }
1373
1374 mCurrentState.modified = false;
1375 return stateUpdateAvailable;
1376}
1377
1378void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001379 auto headFrameNumber = getHeadFrameNumber();
1380 Mutex::Autolock lock(mLocalSyncPointMutex);
1381 for (auto& point : mLocalSyncPoints) {
1382 if (headFrameNumber >= point->getFrameNumber()) {
1383 point->setFrameAvailable();
1384 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001385 }
1386}
1387
Mathias Agopian13127d82013-03-05 17:47:11 -08001388uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001389 ATRACE_CALL();
1390
Dan Stoza7dde5992015-05-22 09:51:44 -07001391 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001392 Layer::State c = getCurrentState();
1393 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001394 return 0;
1395 }
1396
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001397 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001399 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1400 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001401
1402 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001403 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001404 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001405 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001406 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001407 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001408 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001409 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001410 this, getName().string(), mCurrentTransform,
1411 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001412 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001413 c.crop.left,
1414 c.crop.top,
1415 c.crop.right,
1416 c.crop.bottom,
1417 c.crop.getWidth(),
1418 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001421 s.crop.left,
1422 s.crop.top,
1423 s.crop.right,
1424 s.crop.bottom,
1425 s.crop.getWidth(),
1426 s.crop.getHeight(),
1427 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001429 // record the new size, form this point on, when the client request
1430 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001431 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001432 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001434
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001435 if (!isFixedSize()) {
1436
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001437 const bool resizePending = (c.requested.w != c.active.w) ||
1438 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001439
Dan Stoza9e9b0442015-04-22 14:59:08 -07001440 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001441 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001442 // if we have a pending resize, unless we are in fixed-size mode.
1443 // the drawing state will be updated only once we receive a buffer
1444 // with the correct size.
1445 //
1446 // in particular, we want to make sure the clip (which is part
1447 // of the geometry state) is latched together with the size but is
1448 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001449 //
1450 // If a sideband stream is attached, however, we want to skip this
1451 // optimization so that transactions aren't missed when a buffer
1452 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001453
1454 flags |= eDontUpdateGeometryState;
1455 }
1456 }
1457
Mathias Agopian13127d82013-03-05 17:47:11 -08001458 // always set active to requested, unless we're asked not to
1459 // this is used by Layer, which special cases resizes.
1460 if (flags & eDontUpdateGeometryState) {
1461 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001462 c.active = c.requested;
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 }
1464
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001465 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001466 // invalidate and recompute the visible regions if needed
1467 flags |= Layer::eVisibleRegion;
1468 }
1469
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001470 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001471 // invalidate and recompute the visible regions if needed
1472 flags |= eVisibleRegion;
1473 this->contentDirty = true;
1474
1475 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001476 const uint8_t type = c.active.transform.getType();
1477 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001478 (type >= Transform::SCALE));
1479 }
1480
Dan Stozac8145172016-04-28 16:29:06 -07001481 // If the layer is hidden, signal and clear out all local sync points so
1482 // that transactions for layers depending on this layer's frames becoming
1483 // visible are not blocked
1484 if (c.flags & layer_state_t::eLayerHidden) {
1485 Mutex::Autolock lock(mLocalSyncPointMutex);
1486 for (auto& point : mLocalSyncPoints) {
1487 point->setFrameAvailable();
1488 }
1489 mLocalSyncPoints.clear();
1490 }
1491
Mathias Agopian13127d82013-03-05 17:47:11 -08001492 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001493 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001494 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495}
1496
Pablo Ceballos05289c22016-04-14 15:49:55 -07001497void Layer::commitTransaction(const State& stateToCommit) {
1498 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001499}
1500
Mathias Agopian13127d82013-03-05 17:47:11 -08001501uint32_t Layer::getTransactionFlags(uint32_t flags) {
1502 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1503}
1504
1505uint32_t Layer::setTransactionFlags(uint32_t flags) {
1506 return android_atomic_or(flags, &mTransactionFlags);
1507}
1508
1509bool Layer::setPosition(float x, float y) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001510 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001511 return false;
1512 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001513
1514 // We update the requested and active position simultaneously because
1515 // we want to apply the position portion of the transform matrix immediately,
1516 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001517 mCurrentState.requested.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001518 mCurrentState.active.transform.set(x, y);
1519
Dan Stoza7dde5992015-05-22 09:51:44 -07001520 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001521 setTransactionFlags(eTransactionNeeded);
1522 return true;
1523}
1524bool Layer::setLayer(uint32_t z) {
1525 if (mCurrentState.z == z)
1526 return false;
1527 mCurrentState.sequence++;
1528 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001529 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001530 setTransactionFlags(eTransactionNeeded);
1531 return true;
1532}
1533bool Layer::setSize(uint32_t w, uint32_t h) {
1534 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1535 return false;
1536 mCurrentState.requested.w = w;
1537 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001538 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001539 setTransactionFlags(eTransactionNeeded);
1540 return true;
1541}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001542#ifdef USE_HWC2
1543bool Layer::setAlpha(float alpha) {
1544#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001545bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001546#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001547 if (mCurrentState.alpha == alpha)
1548 return false;
1549 mCurrentState.sequence++;
1550 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001551 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001552 setTransactionFlags(eTransactionNeeded);
1553 return true;
1554}
1555bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1556 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001557 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001558 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001559 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001560 setTransactionFlags(eTransactionNeeded);
1561 return true;
1562}
1563bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001564 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001565 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001566 setTransactionFlags(eTransactionNeeded);
1567 return true;
1568}
1569bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1570 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1571 if (mCurrentState.flags == newFlags)
1572 return false;
1573 mCurrentState.sequence++;
1574 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001575 mCurrentState.mask = mask;
1576 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001577 setTransactionFlags(eTransactionNeeded);
1578 return true;
1579}
1580bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001581 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001582 return false;
1583 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001584 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001585 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001586 setTransactionFlags(eTransactionNeeded);
1587 return true;
1588}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001589bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001590 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001591 return false;
1592 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001593 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001594 mCurrentState.modified = true;
1595 setTransactionFlags(eTransactionNeeded);
1596 return true;
1597}
Mathias Agopian13127d82013-03-05 17:47:11 -08001598
Robert Carrc3574f72016-03-24 12:19:32 -07001599bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1600 if (scalingMode == mOverrideScalingMode)
1601 return false;
1602 mOverrideScalingMode = scalingMode;
1603 return true;
1604}
1605
1606uint32_t Layer::getEffectiveScalingMode() const {
1607 if (mOverrideScalingMode >= 0) {
1608 return mOverrideScalingMode;
1609 }
1610 return mCurrentScalingMode;
1611}
1612
Mathias Agopian13127d82013-03-05 17:47:11 -08001613bool Layer::setLayerStack(uint32_t layerStack) {
1614 if (mCurrentState.layerStack == layerStack)
1615 return false;
1616 mCurrentState.sequence++;
1617 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001618 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001619 setTransactionFlags(eTransactionNeeded);
1620 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001621}
1622
Dan Stoza7dde5992015-05-22 09:51:44 -07001623void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1624 uint64_t frameNumber) {
1625 mCurrentState.handle = handle;
1626 mCurrentState.frameNumber = frameNumber;
1627 // We don't set eTransactionNeeded, because just receiving a deferral
1628 // request without any other state updates shouldn't actually induce a delay
1629 mCurrentState.modified = true;
1630 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001631 mCurrentState.handle = nullptr;
1632 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001633 mCurrentState.modified = false;
1634}
1635
Dan Stozaee44edd2015-03-23 15:50:23 -07001636void Layer::useSurfaceDamage() {
1637 if (mFlinger->mForceFullDamage) {
1638 surfaceDamageRegion = Region::INVALID_REGION;
1639 } else {
1640 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1641 }
1642}
1643
1644void Layer::useEmptyDamage() {
1645 surfaceDamageRegion.clear();
1646}
1647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648// ----------------------------------------------------------------------------
1649// pageflip handling...
1650// ----------------------------------------------------------------------------
1651
Dan Stoza6b9454d2014-11-07 16:00:59 -08001652bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001653 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001654 return true;
1655 }
1656
Dan Stoza6b9454d2014-11-07 16:00:59 -08001657 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001658 if (mQueueItems.empty()) {
1659 return false;
1660 }
1661 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001662 nsecs_t expectedPresent =
1663 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001664
1665 // Ignore timestamps more than a second in the future
1666 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1667 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1668 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1669 expectedPresent);
1670
1671 bool isDue = timestamp < expectedPresent;
1672 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001673}
1674
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001675bool Layer::onPreComposition() {
1676 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001677 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001678}
1679
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001680void Layer::onPostComposition() {
1681 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001682 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001683 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1684
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001685 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001686 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001687 mFrameTracker.setFrameReadyFence(frameReadyFence);
1688 } else {
1689 // There was no fence for this frame, so assume that it was ready
1690 // to be presented at the desired present time.
1691 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1692 }
1693
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001694 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001695#ifdef USE_HWC2
1696 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1697#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001698 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001699#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001700 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001701 mFrameTracker.setActualPresentFence(presentFence);
1702 } else {
1703 // The HWC doesn't support present fences, so use the refresh
1704 // timestamp instead.
1705 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1706 mFrameTracker.setActualPresentTime(presentTime);
1707 }
1708
1709 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001710 mFrameLatencyNeeded = false;
1711 }
1712}
1713
Dan Stoza9e56aa02015-11-02 13:00:03 -08001714#ifdef USE_HWC2
1715void Layer::releasePendingBuffer() {
1716 mSurfaceFlingerConsumer->releasePendingBuffer();
1717}
1718#endif
1719
Mathias Agopianda27af92012-09-13 18:17:13 -07001720bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001721 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001722#ifdef USE_HWC2
1723 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1724 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1725#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001726 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001727 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001728#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001729}
1730
Mathias Agopian4fec8732012-06-29 14:12:52 -07001731Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001733 ATRACE_CALL();
1734
Jesse Hall399184a2014-03-03 15:42:54 -08001735 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1736 // mSidebandStreamChanged was true
1737 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001738 if (mSidebandStream != NULL) {
1739 setTransactionFlags(eTransactionNeeded);
1740 mFlinger->setTransactionFlags(eTraversalNeeded);
1741 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001742 recomputeVisibleRegions = true;
1743
1744 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001745 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001746 }
1747
Mathias Agopian4fec8732012-06-29 14:12:52 -07001748 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001749 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001750
1751 // if we've already called updateTexImage() without going through
1752 // a composition step, we have to skip this layer at this point
1753 // because we cannot call updateTeximage() without a corresponding
1754 // compositionComplete() call.
1755 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001756 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001757 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001758 }
1759
Jamie Gennis351a5132011-09-14 18:23:37 -07001760 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001761 const State& s(getDrawingState());
1762 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001763 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001764
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001765 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001766 Layer::State& front;
1767 Layer::State& current;
1768 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001769 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001770 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001771 int32_t overrideScalingMode;
Robert Carrb5d3d262016-03-25 15:08:13 -07001772
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001773 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001774 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001775 const char* name,
1776 int32_t overrideScalingMode)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001777 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001778 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001779 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001780 name(name),
1781 overrideScalingMode(overrideScalingMode) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001782 }
1783
1784 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001785 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001786 if (buf == NULL) {
1787 return false;
1788 }
1789
1790 uint32_t bufWidth = buf->getWidth();
1791 uint32_t bufHeight = buf->getHeight();
1792
1793 // check that we received a buffer of the right size
1794 // (Take the buffer's orientation into account)
1795 if (item.mTransform & Transform::ROT_90) {
1796 swap(bufWidth, bufHeight);
1797 }
1798
Robert Carrc3574f72016-03-24 12:19:32 -07001799 int actualScalingMode = overrideScalingMode >= 0 ?
1800 overrideScalingMode : item.mScalingMode;
1801 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001802 if (front.active != front.requested) {
1803
1804 if (isFixedSize ||
1805 (bufWidth == front.requested.w &&
1806 bufHeight == front.requested.h))
1807 {
1808 // Here we pretend the transaction happened by updating the
1809 // current and drawing states. Drawing state is only accessed
1810 // in this thread, no need to have it locked
1811 front.active = front.requested;
1812
1813 // We also need to update the current state so that
1814 // we don't end-up overwriting the drawing state with
1815 // this stale current state during the next transaction
1816 //
1817 // NOTE: We don't need to hold the transaction lock here
1818 // because State::active is only accessed from this thread.
1819 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001820 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001821
1822 // recompute visible region
1823 recomputeVisibleRegions = true;
1824 }
1825
1826 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001827 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001828 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001829 " requested={ wh={%4u,%4u} }}\n",
1830 name,
Andy McFadden69052052012-09-14 16:10:11 -07001831 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001832 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001833 front.crop.left,
1834 front.crop.top,
1835 front.crop.right,
1836 front.crop.bottom,
1837 front.crop.getWidth(),
1838 front.crop.getHeight(),
1839 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001840 }
1841
Ruben Brunk1681d952014-06-27 15:51:55 -07001842 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001843 if (front.active.w != bufWidth ||
1844 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001845 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001846 ALOGE("[%s] rejecting buffer: "
1847 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1848 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001849 return true;
1850 }
1851 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001852
1853 // if the transparent region has changed (this test is
1854 // conservative, but that's fine, worst case we're doing
1855 // a bit of extra work), we latch the new one and we
1856 // trigger a visible-region recompute.
1857 if (!front.activeTransparentRegion.isTriviallyEqual(
1858 front.requestedTransparentRegion)) {
1859 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001860
1861 // We also need to update the current state so that
1862 // we don't end-up overwriting the drawing state with
1863 // this stale current state during the next transaction
1864 //
1865 // NOTE: We don't need to hold the transaction lock here
1866 // because State::active is only accessed from this thread.
1867 current.activeTransparentRegion = front.activeTransparentRegion;
1868
1869 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001870 recomputeVisibleRegions = true;
1871 }
1872
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001873 return false;
1874 }
1875 };
1876
Ruben Brunk1681d952014-06-27 15:51:55 -07001877 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001878 getProducerStickyTransform() != 0, mName.string(),
1879 mOverrideScalingMode);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001880
Dan Stozacac35382016-01-27 12:21:06 -08001881
1882 // Check all of our local sync points to ensure that all transactions
1883 // which need to have been applied prior to the frame which is about to
1884 // be latched have signaled
1885
1886 auto headFrameNumber = getHeadFrameNumber();
1887 bool matchingFramesFound = false;
1888 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001889 {
Dan Stozacac35382016-01-27 12:21:06 -08001890 Mutex::Autolock lock(mLocalSyncPointMutex);
1891 for (auto& point : mLocalSyncPoints) {
1892 if (point->getFrameNumber() > headFrameNumber) {
1893 break;
1894 }
1895
1896 matchingFramesFound = true;
1897
1898 if (!point->frameIsAvailable()) {
1899 // We haven't notified the remote layer that the frame for
1900 // this point is available yet. Notify it now, and then
1901 // abort this attempt to latch.
1902 point->setFrameAvailable();
1903 allTransactionsApplied = false;
1904 break;
1905 }
1906
1907 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001908 }
1909 }
1910
Dan Stozacac35382016-01-27 12:21:06 -08001911 if (matchingFramesFound && !allTransactionsApplied) {
1912 mFlinger->signalLayerUpdate();
1913 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001914 }
1915
Pablo Ceballos06312182015-10-07 16:32:12 -07001916 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1917 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001918 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001919 // buffer mode.
1920 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001921 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001922 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001923 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001924 if (updateResult == BufferQueue::PRESENT_LATER) {
1925 // Producer doesn't want buffer to be displayed yet. Signal a
1926 // layer update so we check again at the next opportunity.
1927 mFlinger->signalLayerUpdate();
1928 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001929 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1930 // If the buffer has been rejected, remove it from the shadow queue
1931 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001932 if (queuedBuffer) {
1933 Mutex::Autolock lock(mQueueItemLock);
1934 mQueueItems.removeAt(0);
1935 android_atomic_dec(&mQueuedFrames);
1936 }
Dan Stozaecc50402015-04-28 14:42:06 -07001937 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001938 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1939 // This can occur if something goes wrong when trying to create the
1940 // EGLImage for this buffer. If this happens, the buffer has already
1941 // been released, so we need to clean up the queue and bug out
1942 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001943 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001944 Mutex::Autolock lock(mQueueItemLock);
1945 mQueueItems.clear();
1946 android_atomic_and(0, &mQueuedFrames);
1947 }
1948
1949 // Once we have hit this state, the shadow queue may no longer
1950 // correctly reflect the incoming BufferQueue's contents, so even if
1951 // updateTexImage starts working, the only safe course of action is
1952 // to continue to ignore updates.
1953 mUpdateTexImageFailed = true;
1954
1955 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001956 }
1957
Pablo Ceballos06312182015-10-07 16:32:12 -07001958 if (queuedBuffer) {
1959 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001960 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1961
Dan Stoza6b9454d2014-11-07 16:00:59 -08001962 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001963
1964 // Remove any stale buffers that have been dropped during
1965 // updateTexImage
1966 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1967 mQueueItems.removeAt(0);
1968 android_atomic_dec(&mQueuedFrames);
1969 }
1970
Dan Stoza6b9454d2014-11-07 16:00:59 -08001971 mQueueItems.removeAt(0);
1972 }
1973
Dan Stozaecc50402015-04-28 14:42:06 -07001974
Andy McFadden1585c4d2013-06-28 13:52:40 -07001975 // Decrement the queued-frames count. Signal another event if we
1976 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001977 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001978 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001979 mFlinger->signalLayerUpdate();
1980 }
1981
1982 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001983 // something happened!
1984 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001985 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001986 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001987
Jamie Gennis351a5132011-09-14 18:23:37 -07001988 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001989 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001990 if (mActiveBuffer == NULL) {
1991 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001992 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001993 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001994
Mathias Agopian4824d402012-06-04 18:16:30 -07001995 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001996 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001997 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001998 // the first time we receive a buffer, we need to trigger a
1999 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07002000 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07002001 }
2002
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002003 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
2004 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
2005 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07002006 if ((crop != mCurrentCrop) ||
2007 (transform != mCurrentTransform) ||
2008 (scalingMode != mCurrentScalingMode))
2009 {
2010 mCurrentCrop = crop;
2011 mCurrentTransform = transform;
2012 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07002013 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002014 }
2015
2016 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002017 uint32_t bufWidth = mActiveBuffer->getWidth();
2018 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002019 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2020 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002021 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002022 }
2023 }
2024
2025 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002026 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002027 recomputeVisibleRegions = true;
2028 }
2029
Dan Stozacac35382016-01-27 12:21:06 -08002030 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2031
2032 // Remove any sync points corresponding to the buffer which was just
2033 // latched
2034 {
2035 Mutex::Autolock lock(mLocalSyncPointMutex);
2036 auto point = mLocalSyncPoints.begin();
2037 while (point != mLocalSyncPoints.end()) {
2038 if (!(*point)->frameIsAvailable() ||
2039 !(*point)->transactionIsApplied()) {
2040 // This sync point must have been added since we started
2041 // latching. Don't drop it yet.
2042 ++point;
2043 continue;
2044 }
2045
2046 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2047 point = mLocalSyncPoints.erase(point);
2048 } else {
2049 ++point;
2050 }
2051 }
2052 }
2053
Mathias Agopian4fec8732012-06-29 14:12:52 -07002054 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002055 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002056
2057 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002058 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002059 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002060 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002061}
2062
Mathias Agopiana67932f2011-04-20 14:20:59 -07002063uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002064{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002065 // TODO: should we do something special if mSecure is set?
2066 if (mProtectedByApp) {
2067 // need a hardware-protected path to external video sink
2068 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002069 }
Riley Andrews03414a12014-07-01 14:22:59 -07002070 if (mPotentialCursor) {
2071 usage |= GraphicBuffer::USAGE_CURSOR;
2072 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002073 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002074 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002075}
2076
Mathias Agopian84300952012-11-21 16:02:13 -08002077void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002078 uint32_t orientation = 0;
2079 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002080 // The transform hint is used to improve performance, but we can
2081 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002082 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002083 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002084 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002085 if (orientation & Transform::ROT_INVALID) {
2086 orientation = 0;
2087 }
2088 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002089 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002090}
2091
Mathias Agopian13127d82013-03-05 17:47:11 -08002092// ----------------------------------------------------------------------------
2093// debugging
2094// ----------------------------------------------------------------------------
2095
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002096void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002097{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002098 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002099
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002100 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002101 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002102 "+ %s %p (%s)\n",
2103 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002104 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002105
Mathias Agopian2ca79392013-04-02 18:30:32 -07002106 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002107 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002108 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002109 sp<Client> client(mClientRef.promote());
2110
Mathias Agopian74d211a2013-04-22 16:55:35 +02002111 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002112 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2113 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002114 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002115#ifdef USE_HWC2
2116 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2117#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002118 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002119#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002120 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002121 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 -07002122 s.crop.left, s.crop.top,
2123 s.crop.right, s.crop.bottom,
2124 s.finalCrop.left, s.finalCrop.top,
2125 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002126 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002127 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002128 s.active.transform[0][0], s.active.transform[0][1],
2129 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002130 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002131
2132 sp<const GraphicBuffer> buf0(mActiveBuffer);
2133 uint32_t w0=0, h0=0, s0=0, f0=0;
2134 if (buf0 != 0) {
2135 w0 = buf0->getWidth();
2136 h0 = buf0->getHeight();
2137 s0 = buf0->getStride();
2138 f0 = buf0->format;
2139 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002140 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002141 " "
2142 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2143 " queued-frames=%d, mRefreshPending=%d\n",
2144 mFormat, w0, h0, s0,f0,
2145 mQueuedFrames, mRefreshPending);
2146
Mathias Agopian13127d82013-03-05 17:47:11 -08002147 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002148 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002149 }
2150}
2151
Svetoslavd85084b2014-03-20 10:28:31 -07002152void Layer::dumpFrameStats(String8& result) const {
2153 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002154}
2155
Svetoslavd85084b2014-03-20 10:28:31 -07002156void Layer::clearFrameStats() {
2157 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002158}
2159
Jamie Gennis6547ff42013-07-16 20:12:42 -07002160void Layer::logFrameStats() {
2161 mFrameTracker.logAndResetStats(mName);
2162}
2163
Svetoslavd85084b2014-03-20 10:28:31 -07002164void Layer::getFrameStats(FrameStats* outStats) const {
2165 mFrameTracker.getStats(outStats);
2166}
2167
Pablo Ceballos40845df2016-01-25 17:41:15 -08002168void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2169 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2170 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2171 *outName = mName;
2172 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2173
2174#ifdef USE_HWC2
2175 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2176 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2177 HWC2::Composition::Client : true;
2178#else
2179 *outIsGlesComposition = mIsGlesComposition;
2180#endif
2181 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2182 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2183 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2184}
Mathias Agopian13127d82013-03-05 17:47:11 -08002185// ---------------------------------------------------------------------------
2186
2187Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2188 const sp<Layer>& layer)
2189 : mFlinger(flinger), mLayer(layer) {
2190}
2191
2192Layer::LayerCleaner::~LayerCleaner() {
2193 // destroy client resources
2194 mFlinger->onLayerDestroyed(mLayer);
2195}
2196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002198}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002199
2200#if defined(__gl_h_)
2201#error "don't include gl/gl.h in this file"
2202#endif
2203
2204#if defined(__gl2_h_)
2205#error "don't include gl2/gl2.h in this file"
2206#endif