blob: dffc5425c5c202fb10c4caa8859beacb2d117da3 [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;
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700453 invTransform = (Transform(invTransformOrient) * Transform(invTransform))
454 .getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456
457 int winWidth = s.active.w;
458 int winHeight = s.active.h;
459 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
460 // If the activeCrop has been rotate the ends are rotated but not
461 // the space itself so when transforming ends back we can't rely on
462 // a modification of the axes of rotation. To account for this we
463 // need to reorient the inverse rotation in terms of the current
464 // axes of rotation.
465 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
466 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
467 if (is_h_flipped == is_v_flipped) {
468 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
469 NATIVE_WINDOW_TRANSFORM_FLIP_H;
470 }
471 winWidth = s.active.h;
472 winHeight = s.active.w;
473 }
474 const Rect winCrop = activeCrop.transform(
475 invTransform, s.active.w, s.active.h);
476
477 // below, crop is intersected with winCrop expressed in crop's coordinate space
478 float xScale = crop.getWidth() / float(winWidth);
479 float yScale = crop.getHeight() / float(winHeight);
480
481 float insetL = winCrop.left * xScale;
482 float insetT = winCrop.top * yScale;
483 float insetR = (winWidth - winCrop.right ) * xScale;
484 float insetB = (winHeight - winCrop.bottom) * yScale;
485
486 crop.left += insetL;
487 crop.top += insetT;
488 crop.right -= insetR;
489 crop.bottom -= insetB;
490
Mathias Agopian13127d82013-03-05 17:47:11 -0800491 return crop;
492}
493
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494#ifdef USE_HWC2
495void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
496#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700497void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700498 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700499 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700501{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502#ifdef USE_HWC2
503 const auto hwcId = displayDevice->getHwcDisplayId();
504 auto& hwcInfo = mHwcLayers[hwcId];
505#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800506 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700508
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700509 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510#ifdef USE_HWC2
511 hwcInfo.forceClientComposition = false;
512
513 if (isSecure() && !displayDevice->isSecure()) {
514 hwcInfo.forceClientComposition = true;
515 }
516
517 auto& hwcLayer = hwcInfo.layer;
518#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700519 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700520
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700521 if (isSecure() && !hw->isSecure()) {
522 layer.setSkip(true);
523 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800524#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700525
Mathias Agopian13127d82013-03-05 17:47:11 -0800526 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700527 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800528#ifdef USE_HWC2
529 if (!isOpaque(s) || s.alpha != 1.0f) {
530 auto blendMode = mPremultipliedAlpha ?
531 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
532 auto error = hwcLayer->setBlendMode(blendMode);
533 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
534 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
535 to_string(error).c_str(), static_cast<int32_t>(error));
536 }
537#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800538 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800539 layer.setBlending(mPremultipliedAlpha ?
540 HWC_BLENDING_PREMULT :
541 HWC_BLENDING_COVERAGE);
542 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800544
545 // apply the layer's transform, followed by the display's global transform
546 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700547 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700548 if (!s.crop.isEmpty()) {
549 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800550 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000552 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000554 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 activeCrop.clear();
557 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800558 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800559 // This needs to be here as transform.transform(Rect) computes the
560 // transformed rect and then takes the bounding box of the result before
561 // returning. This means
562 // transform.inverse().transform(transform.transform(Rect)) != Rect
563 // in which case we need to make sure the final rect is clipped to the
564 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000565 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
566 activeCrop.clear();
567 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700568 // mark regions outside the crop as transparent
569 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
570 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
571 s.active.w, s.active.h));
572 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
573 activeCrop.left, activeCrop.bottom));
574 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
575 s.active.w, activeCrop.bottom));
576 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800577 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700578 if (!s.finalCrop.isEmpty()) {
579 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000580 frame.clear();
581 }
582 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000584 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
585 frame.clear();
586 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 const Transform& tr(displayDevice->getTransform());
588 Rect transformedFrame = tr.transform(frame);
589 auto error = hwcLayer->setDisplayFrame(transformedFrame);
590 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
591 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
592 transformedFrame.top, transformedFrame.right,
593 transformedFrame.bottom, to_string(error).c_str(),
594 static_cast<int32_t>(error));
595
596 FloatRect sourceCrop = computeCrop(displayDevice);
597 error = hwcLayer->setSourceCrop(sourceCrop);
598 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
599 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
600 sourceCrop.left, sourceCrop.top, sourceCrop.right,
601 sourceCrop.bottom, to_string(error).c_str(),
602 static_cast<int32_t>(error));
603
604 error = hwcLayer->setPlaneAlpha(s.alpha);
605 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
606 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
607 static_cast<int32_t>(error));
608
609 error = hwcLayer->setZOrder(s.z);
610 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
611 mName.string(), s.z, to_string(error).c_str(),
612 static_cast<int32_t>(error));
613#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000614 if (!frame.intersect(hw->getViewport(), &frame)) {
615 frame.clear();
616 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800617 const Transform& tr(hw->getTransform());
618 layer.setFrame(tr.transform(frame));
619 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800620 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800622
Mathias Agopian29a367b2011-07-12 14:51:45 -0700623 /*
624 * Transformations are applied in this order:
625 * 1) buffer orientation/flip/mirror
626 * 2) state transformation (window manager)
627 * 3) layer orientation (screen orientation)
628 * (NOTE: the matrices are multiplied in reverse order)
629 */
630
631 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800632 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633
634 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
635 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700636 * the code below applies the primary display's inverse transform to the
637 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700638 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700639 uint32_t invTransform =
640 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700641 // calculate the inverse transform
642 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
643 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
644 NATIVE_WINDOW_TRANSFORM_FLIP_H;
645 }
646 // and apply to the current transform
Pablo Ceballos0f5131f2016-05-17 13:34:45 -0700647 transform = Transform(invTransform) * transform;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700648 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700649
650 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800651 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652#ifdef USE_HWC2
653 if (orientation & Transform::ROT_INVALID) {
654 // we can only handle simple transformation
655 hwcInfo.forceClientComposition = true;
656 } else {
657 auto transform = static_cast<HWC2::Transform>(orientation);
658 auto error = hwcLayer->setTransform(transform);
659 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
660 "%s (%d)", mName.string(), to_string(transform).c_str(),
661 to_string(error).c_str(), static_cast<int32_t>(error));
662 }
663#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800664 if (orientation & Transform::ROT_INVALID) {
665 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700666 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700667 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800668 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700669 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700671}
672
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673#ifdef USE_HWC2
674void Layer::forceClientComposition(int32_t hwcId) {
675 if (mHwcLayers.count(hwcId) == 0) {
676 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
677 return;
678 }
679
680 mHwcLayers[hwcId].forceClientComposition = true;
681}
682#endif
683
684#ifdef USE_HWC2
685void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
686 // Apply this display's projection's viewport to the visible region
687 // before giving it to the HWC HAL.
688 const Transform& tr = displayDevice->getTransform();
689 const auto& viewport = displayDevice->getViewport();
690 Region visible = tr.transform(visibleRegion.intersect(viewport));
691 auto hwcId = displayDevice->getHwcDisplayId();
692 auto& hwcLayer = mHwcLayers[hwcId].layer;
693 auto error = hwcLayer->setVisibleRegion(visible);
694 if (error != HWC2::Error::None) {
695 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
696 to_string(error).c_str(), static_cast<int32_t>(error));
697 visible.dump(LOG_TAG);
698 }
699
700 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
701 if (error != HWC2::Error::None) {
702 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
703 to_string(error).c_str(), static_cast<int32_t>(error));
704 surfaceDamageRegion.dump(LOG_TAG);
705 }
706
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700707 // Sideband layers
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 if (mSidebandStream.get()) {
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700709 setCompositionType(hwcId, HWC2::Composition::Sideband);
710 ALOGV("[%s] Requesting Sideband composition", mName.string());
711 error = hwcLayer->setSidebandStream(mSidebandStream->handle());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 if (error != HWC2::Error::None) {
713 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
714 mName.string(), mSidebandStream->handle(),
715 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700717 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 }
719
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700720 // Client or SolidColor layers
721 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr ||
722 mHwcLayers[hwcId].forceClientComposition) {
723 // TODO: This also includes solid color layers, but no API exists to
724 // setup a solid color layer yet
725 ALOGV("[%s] Requesting Client composition", mName.string());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726 setCompositionType(hwcId, HWC2::Composition::Client);
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700727 error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
728 if (error != HWC2::Error::None) {
729 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
730 to_string(error).c_str(), static_cast<int32_t>(error));
731 }
732 return;
733 }
734
735 // Device or Cursor layers
736 if (mPotentialCursor) {
737 ALOGV("[%s] Requesting Cursor composition", mName.string());
738 setCompositionType(hwcId, HWC2::Composition::Cursor);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739 } else {
740 ALOGV("[%s] Requesting Device composition", mName.string());
741 setCompositionType(hwcId, HWC2::Composition::Device);
742 }
Dan Stoza0f67b3f2016-05-17 10:48:38 -0700743
744 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
745 error = hwcLayer->setBuffer(mActiveBuffer->handle, acquireFence);
746 if (error != HWC2::Error::None) {
747 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
748 mActiveBuffer->handle, to_string(error).c_str(),
749 static_cast<int32_t>(error));
750 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800751}
752#else
Mathias Agopian42977342012-08-05 00:40:46 -0700753void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700754 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800755 // we have to set the visible region on every frame because
756 // we currently free it during onLayerDisplayed(), which is called
757 // after HWComposer::commit() -- every frame.
758 // Apply this display's projection's viewport to the visible region
759 // before giving it to the HWC HAL.
760 const Transform& tr = hw->getTransform();
761 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
762 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700763 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800764 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700765
Jesse Hall399184a2014-03-03 15:42:54 -0800766 if (mSidebandStream.get()) {
767 layer.setSidebandStream(mSidebandStream);
768 } else {
769 // NOTE: buffer can be NULL if the client never drew into this
770 // layer yet, or if we ran out of memory
771 layer.setBuffer(mActiveBuffer);
772 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700773}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700775
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776#ifdef USE_HWC2
777void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
778 auto hwcId = displayDevice->getHwcDisplayId();
779 if (mHwcLayers.count(hwcId) == 0 ||
780 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
781 return;
782 }
783
784 // This gives us only the "orientation" component of the transform
785 const State& s(getCurrentState());
786
787 // Apply the layer's transform, followed by the display's global transform
788 // Here we're guaranteed that the layer's transform preserves rects
789 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700790 if (!s.crop.isEmpty()) {
791 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800792 }
793 // Subtract the transparent region and snap to the bounds
794 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800795 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700797 if (!s.finalCrop.isEmpty()) {
798 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000799 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800800 auto& displayTransform(displayDevice->getTransform());
801 auto position = displayTransform.transform(frame);
802
803 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
804 position.top);
805 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
806 "to (%d, %d): %s (%d)", mName.string(), position.left,
807 position.top, to_string(error).c_str(),
808 static_cast<int32_t>(error));
809}
810#else
Dan Stozac7014012014-02-14 15:03:43 -0800811void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700812 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700813 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700814
815 // TODO: there is a possible optimization here: we only need to set the
816 // acquire fence the first time a new buffer is acquired on EACH display.
817
Riley Andrews03414a12014-07-01 14:22:59 -0700818 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800819 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800820 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700821 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700822 if (fenceFd == -1) {
823 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
824 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700825 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700826 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700827 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700828}
829
Riley Andrews03414a12014-07-01 14:22:59 -0700830Rect Layer::getPosition(
831 const sp<const DisplayDevice>& hw)
832{
833 // this gives us only the "orientation" component of the transform
834 const State& s(getCurrentState());
835
836 // apply the layer's transform, followed by the display's global transform
837 // here we're guaranteed that the layer's transform preserves rects
838 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700839 if (!s.crop.isEmpty()) {
840 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700841 }
842 // subtract the transparent region and snap to the bounds
843 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800844 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700845 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700846 if (!s.finalCrop.isEmpty()) {
847 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000848 }
Riley Andrews03414a12014-07-01 14:22:59 -0700849 const Transform& tr(hw->getTransform());
850 return Rect(tr.transform(frame));
851}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800852#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700853
Mathias Agopian13127d82013-03-05 17:47:11 -0800854// ---------------------------------------------------------------------------
855// drawing...
856// ---------------------------------------------------------------------------
857
858void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800859 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800860}
861
Dan Stozac7014012014-02-14 15:03:43 -0800862void Layer::draw(const sp<const DisplayDevice>& hw,
863 bool useIdentityTransform) const {
864 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800865}
866
Dan Stozac7014012014-02-14 15:03:43 -0800867void Layer::draw(const sp<const DisplayDevice>& hw) const {
868 onDraw(hw, Region(hw->bounds()), false);
869}
870
871void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
872 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800874 ATRACE_CALL();
875
Mathias Agopiana67932f2011-04-20 14:20:59 -0700876 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700878 // in fact never been drawn into. This happens frequently with
879 // SurfaceView because the WindowManager can't know when the client
880 // has drawn the first time.
881
882 // If there is nothing under us, we paint the screen in black, otherwise
883 // we just skip this update.
884
885 // figure out if there is something below us
886 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700887 const SurfaceFlinger::LayerVector& drawingLayers(
888 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700889 const size_t count = drawingLayers.size();
890 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 const sp<Layer>& layer(drawingLayers[i]);
892 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700893 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700894 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700895 }
896 // if not everything below us is covered, we plug the holes!
897 Region holes(clip.subtract(under));
898 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700899 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700900 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 return;
902 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700903
Andy McFadden97eba892012-12-11 15:21:45 -0800904 // Bind the current buffer to the GL texture, and wait for it to be
905 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800906 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
907 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800908 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700909 // Go ahead and draw the buffer anyway; no matter what we do the screen
910 // is probably going to have something visibly wrong.
911 }
912
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700913 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
914
Mathias Agopian875d8e12013-06-07 15:35:48 -0700915 RenderEngine& engine(mFlinger->getRenderEngine());
916
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700917 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700918 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700919 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700920
921 // Query the texture matrix given our current filtering mode.
922 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800923 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
924 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700925
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700926 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
927
928 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700929 * the code below applies the primary display's inverse transform to
930 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700931 */
932
933 // create a 4x4 transform matrix from the display transform flags
934 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
935 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
936 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
937
938 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700939 uint32_t transform =
940 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700941 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
942 tr = tr * rot90;
943 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
944 tr = tr * flipH;
945 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
946 tr = tr * flipV;
947
948 // calculate the inverse
949 tr = inverse(tr);
950
951 // and finally apply it to the original texture matrix
952 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
953 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
954 }
955
Jamie Genniscbb1a952012-05-08 17:05:52 -0700956 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700957 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
958 mTexture.setFiltering(useFiltering);
959 mTexture.setMatrix(textureMatrix);
960
961 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700962 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700963 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700964 }
Dan Stozac7014012014-02-14 15:03:43 -0800965 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700966 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967}
968
Mathias Agopian13127d82013-03-05 17:47:11 -0800969
Dan Stozac7014012014-02-14 15:03:43 -0800970void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
971 const Region& /* clip */, float red, float green, float blue,
972 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800973{
Mathias Agopian19733a32013-08-28 18:13:56 -0700974 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800975 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700976 engine.setupFillWithColor(red, green, blue, alpha);
977 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800978}
979
980void Layer::clearWithOpenGL(
981 const sp<const DisplayDevice>& hw, const Region& clip) const {
982 clearWithOpenGL(hw, clip, 0,0,0,0);
983}
984
Dan Stozac7014012014-02-14 15:03:43 -0800985void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
986 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700987 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800988
Dan Stozac7014012014-02-14 15:03:43 -0800989 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800990
Mathias Agopian13127d82013-03-05 17:47:11 -0800991 /*
992 * NOTE: the way we compute the texture coordinates here produces
993 * different results than when we take the HWC path -- in the later case
994 * the "source crop" is rounded to texel boundaries.
995 * This can produce significantly different results when the texture
996 * is scaled by a large amount.
997 *
998 * The GL code below is more logical (imho), and the difference with
999 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001000 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001001 * GL composition when a buffer scaling is applied (maybe with some
1002 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1003 * like more of a hack.
1004 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001005 Rect win(computeBounds());
1006
Robert Carrb5d3d262016-03-25 15:08:13 -07001007 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001008 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001009 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001010 win.clear();
1011 }
1012 win = s.active.transform.inverse().transform(win);
1013 if (!win.intersect(computeBounds(), &win)) {
1014 win.clear();
1015 }
1016 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001017
Mathias Agopian3f844832013-08-07 21:24:32 -07001018 float left = float(win.left) / float(s.active.w);
1019 float top = float(win.top) / float(s.active.h);
1020 float right = float(win.right) / float(s.active.w);
1021 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001022
Mathias Agopian875d8e12013-06-07 15:35:48 -07001023 // TODO: we probably want to generate the texture coords with the mesh
1024 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001025 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1026 texCoords[0] = vec2(left, 1.0f - top);
1027 texCoords[1] = vec2(left, 1.0f - bottom);
1028 texCoords[2] = vec2(right, 1.0f - bottom);
1029 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001030
Mathias Agopian875d8e12013-06-07 15:35:48 -07001031 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001032 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001033 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001034 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001035}
1036
Dan Stoza9e56aa02015-11-02 13:00:03 -08001037#ifdef USE_HWC2
1038void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1039 bool callIntoHwc) {
1040 if (mHwcLayers.count(hwcId) == 0) {
1041 ALOGE("setCompositionType called without a valid HWC layer");
1042 return;
1043 }
1044 auto& hwcInfo = mHwcLayers[hwcId];
1045 auto& hwcLayer = hwcInfo.layer;
1046 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1047 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1048 if (hwcInfo.compositionType != type) {
1049 ALOGV(" actually setting");
1050 hwcInfo.compositionType = type;
1051 if (callIntoHwc) {
1052 auto error = hwcLayer->setCompositionType(type);
1053 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1054 "composition type %s: %s (%d)", mName.string(),
1055 to_string(type).c_str(), to_string(error).c_str(),
1056 static_cast<int32_t>(error));
1057 }
1058 }
1059}
1060
1061HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1062 if (mHwcLayers.count(hwcId) == 0) {
1063 ALOGE("getCompositionType called without a valid HWC layer");
1064 return HWC2::Composition::Invalid;
1065 }
1066 return mHwcLayers.at(hwcId).compositionType;
1067}
1068
1069void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1070 if (mHwcLayers.count(hwcId) == 0) {
1071 ALOGE("setClearClientTarget called without a valid HWC layer");
1072 return;
1073 }
1074 mHwcLayers[hwcId].clearClientTarget = clear;
1075}
1076
1077bool Layer::getClearClientTarget(int32_t hwcId) const {
1078 if (mHwcLayers.count(hwcId) == 0) {
1079 ALOGE("getClearClientTarget called without a valid HWC layer");
1080 return false;
1081 }
1082 return mHwcLayers.at(hwcId).clearClientTarget;
1083}
1084#endif
1085
Ruben Brunk1681d952014-06-27 15:51:55 -07001086uint32_t Layer::getProducerStickyTransform() const {
1087 int producerStickyTransform = 0;
1088 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1089 if (ret != OK) {
1090 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1091 strerror(-ret), ret);
1092 return 0;
1093 }
1094 return static_cast<uint32_t>(producerStickyTransform);
1095}
1096
Dan Stozacac35382016-01-27 12:21:06 -08001097uint64_t Layer::getHeadFrameNumber() const {
1098 Mutex::Autolock lock(mQueueItemLock);
1099 if (!mQueueItems.empty()) {
1100 return mQueueItems[0].mFrameNumber;
1101 } else {
1102 return mCurrentFrameNumber;
1103 }
1104}
1105
1106bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1107 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1108 // Don't bother with a SyncPoint, since we've already latched the
1109 // relevant frame
1110 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001111 }
1112
Dan Stozacac35382016-01-27 12:21:06 -08001113 Mutex::Autolock lock(mLocalSyncPointMutex);
1114 mLocalSyncPoints.push_back(point);
1115 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001116}
1117
Mathias Agopian13127d82013-03-05 17:47:11 -08001118void Layer::setFiltering(bool filtering) {
1119 mFiltering = filtering;
1120}
1121
1122bool Layer::getFiltering() const {
1123 return mFiltering;
1124}
1125
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001126// As documented in libhardware header, formats in the range
1127// 0x100 - 0x1FF are specific to the HAL implementation, and
1128// are known to have no alpha channel
1129// TODO: move definition for device-specific range into
1130// hardware.h, instead of using hard-coded values here.
1131#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1132
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001133bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001134 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1135 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001136 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001137 switch (format) {
1138 case HAL_PIXEL_FORMAT_RGBA_8888:
1139 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001140 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001141 }
1142 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001143 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001144}
1145
Mathias Agopian13127d82013-03-05 17:47:11 -08001146// ----------------------------------------------------------------------------
1147// local state
1148// ----------------------------------------------------------------------------
1149
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001150static void boundPoint(vec2* point, const Rect& crop) {
1151 if (point->x < crop.left) {
1152 point->x = crop.left;
1153 }
1154 if (point->x > crop.right) {
1155 point->x = crop.right;
1156 }
1157 if (point->y < crop.top) {
1158 point->y = crop.top;
1159 }
1160 if (point->y > crop.bottom) {
1161 point->y = crop.bottom;
1162 }
1163}
1164
Dan Stozac7014012014-02-14 15:03:43 -08001165void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1166 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001167{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001168 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001169 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 const uint32_t hw_h = hw->getHeight();
1171 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001172 if (!s.crop.isEmpty()) {
1173 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001175 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001176 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001177
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001178 vec2 lt = vec2(win.left, win.top);
1179 vec2 lb = vec2(win.left, win.bottom);
1180 vec2 rb = vec2(win.right, win.bottom);
1181 vec2 rt = vec2(win.right, win.top);
1182
1183 if (!useIdentityTransform) {
1184 lt = s.active.transform.transform(lt);
1185 lb = s.active.transform.transform(lb);
1186 rb = s.active.transform.transform(rb);
1187 rt = s.active.transform.transform(rt);
1188 }
1189
Robert Carrb5d3d262016-03-25 15:08:13 -07001190 if (!s.finalCrop.isEmpty()) {
1191 boundPoint(&lt, s.finalCrop);
1192 boundPoint(&lb, s.finalCrop);
1193 boundPoint(&rb, s.finalCrop);
1194 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001195 }
1196
Mathias Agopianff2ed702013-09-01 21:36:12 -07001197 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001198 position[0] = tr.transform(lt);
1199 position[1] = tr.transform(lb);
1200 position[2] = tr.transform(rb);
1201 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001202 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001203 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001204 }
1205}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001206
Andy McFadden4125a4f2014-01-29 17:17:11 -08001207bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001208{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001209 // if we don't have a buffer yet, we're translucent regardless of the
1210 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001211 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001212 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001213 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001214
1215 // if the layer has the opaque flag, then we're always opaque,
1216 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001217 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001218}
1219
Dan Stoza23116082015-06-18 14:58:39 -07001220bool Layer::isSecure() const
1221{
1222 const Layer::State& s(mDrawingState);
1223 return (s.flags & layer_state_t::eLayerSecure);
1224}
1225
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001226bool Layer::isProtected() const
1227{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001229 return (activeBuffer != 0) &&
1230 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1231}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001232
Mathias Agopian13127d82013-03-05 17:47:11 -08001233bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001234 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235}
1236
1237bool Layer::isCropped() const {
1238 return !mCurrentCrop.isEmpty();
1239}
1240
1241bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1242 return mNeedsFiltering || hw->needsFiltering();
1243}
1244
1245void Layer::setVisibleRegion(const Region& visibleRegion) {
1246 // always called from main thread
1247 this->visibleRegion = visibleRegion;
1248}
1249
1250void Layer::setCoveredRegion(const Region& coveredRegion) {
1251 // always called from main thread
1252 this->coveredRegion = coveredRegion;
1253}
1254
1255void Layer::setVisibleNonTransparentRegion(const Region&
1256 setVisibleNonTransparentRegion) {
1257 // always called from main thread
1258 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1259}
1260
1261// ----------------------------------------------------------------------------
1262// transaction
1263// ----------------------------------------------------------------------------
1264
Dan Stoza7dde5992015-05-22 09:51:44 -07001265void Layer::pushPendingState() {
1266 if (!mCurrentState.modified) {
1267 return;
1268 }
1269
Dan Stoza7dde5992015-05-22 09:51:44 -07001270 // If this transaction is waiting on the receipt of a frame, generate a sync
1271 // point and send it to the remote layer.
1272 if (mCurrentState.handle != nullptr) {
1273 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1274 sp<Layer> handleLayer = handle->owner.promote();
1275 if (handleLayer == nullptr) {
1276 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1277 // If we can't promote the layer we are intended to wait on,
1278 // then it is expired or otherwise invalid. Allow this transaction
1279 // to be applied as per normal (no synchronization).
1280 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001281 } else {
1282 auto syncPoint = std::make_shared<SyncPoint>(
1283 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001284 if (handleLayer->addSyncPoint(syncPoint)) {
1285 mRemoteSyncPoints.push_back(std::move(syncPoint));
1286 } else {
1287 // We already missed the frame we're supposed to synchronize
1288 // on, so go ahead and apply the state update
1289 mCurrentState.handle = nullptr;
1290 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001291 }
1292
Dan Stoza7dde5992015-05-22 09:51:44 -07001293 // Wake us up to check if the frame has been received
1294 setTransactionFlags(eTransactionNeeded);
1295 }
1296 mPendingStates.push_back(mCurrentState);
1297}
1298
Pablo Ceballos05289c22016-04-14 15:49:55 -07001299void Layer::popPendingState(State* stateToCommit) {
1300 auto oldFlags = stateToCommit->flags;
1301 *stateToCommit = mPendingStates[0];
1302 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1303 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001304
1305 mPendingStates.removeAt(0);
1306}
1307
Pablo Ceballos05289c22016-04-14 15:49:55 -07001308bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 bool stateUpdateAvailable = false;
1310 while (!mPendingStates.empty()) {
1311 if (mPendingStates[0].handle != nullptr) {
1312 if (mRemoteSyncPoints.empty()) {
1313 // If we don't have a sync point for this, apply it anyway. It
1314 // will be visually wrong, but it should keep us from getting
1315 // into too much trouble.
1316 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001317 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001318 stateUpdateAvailable = true;
1319 continue;
1320 }
1321
Dan Stozacac35382016-01-27 12:21:06 -08001322 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1323 mPendingStates[0].frameNumber) {
1324 ALOGE("[%s] Unexpected sync point frame number found",
1325 mName.string());
1326
1327 // Signal our end of the sync point and then dispose of it
1328 mRemoteSyncPoints.front()->setTransactionApplied();
1329 mRemoteSyncPoints.pop_front();
1330 continue;
1331 }
1332
Dan Stoza7dde5992015-05-22 09:51:44 -07001333 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1334 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001335 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001336 stateUpdateAvailable = true;
1337
1338 // Signal our end of the sync point and then dispose of it
1339 mRemoteSyncPoints.front()->setTransactionApplied();
1340 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001341 } else {
1342 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001343 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001344 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001345 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 stateUpdateAvailable = true;
1347 }
1348 }
1349
1350 // If we still have pending updates, wake SurfaceFlinger back up and point
1351 // it at this layer so we can process them
1352 if (!mPendingStates.empty()) {
1353 setTransactionFlags(eTransactionNeeded);
1354 mFlinger->setTransactionFlags(eTraversalNeeded);
1355 }
1356
1357 mCurrentState.modified = false;
1358 return stateUpdateAvailable;
1359}
1360
1361void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001362 auto headFrameNumber = getHeadFrameNumber();
1363 Mutex::Autolock lock(mLocalSyncPointMutex);
1364 for (auto& point : mLocalSyncPoints) {
1365 if (headFrameNumber >= point->getFrameNumber()) {
1366 point->setFrameAvailable();
1367 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 }
1369}
1370
Mathias Agopian13127d82013-03-05 17:47:11 -08001371uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001372 ATRACE_CALL();
1373
Dan Stoza7dde5992015-05-22 09:51:44 -07001374 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001375 Layer::State c = getCurrentState();
1376 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001377 return 0;
1378 }
1379
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001380 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001382 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1383 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001384
1385 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001386 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001387 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001388 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001389 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001390 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001391 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001392 " requested={ wh={%4u,%4u} }}\n",
Robert Carrc3574f72016-03-24 12:19:32 -07001393 this, getName().string(), mCurrentTransform,
1394 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001395 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001396 c.crop.left,
1397 c.crop.top,
1398 c.crop.right,
1399 c.crop.bottom,
1400 c.crop.getWidth(),
1401 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001402 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001403 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001404 s.crop.left,
1405 s.crop.top,
1406 s.crop.right,
1407 s.crop.bottom,
1408 s.crop.getWidth(),
1409 s.crop.getHeight(),
1410 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001412 // record the new size, form this point on, when the client request
1413 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001414 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001417
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001418 if (!isFixedSize()) {
1419
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 const bool resizePending = (c.requested.w != c.active.w) ||
1421 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001422
Dan Stoza9e9b0442015-04-22 14:59:08 -07001423 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001424 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001425 // if we have a pending resize, unless we are in fixed-size mode.
1426 // the drawing state will be updated only once we receive a buffer
1427 // with the correct size.
1428 //
1429 // in particular, we want to make sure the clip (which is part
1430 // of the geometry state) is latched together with the size but is
1431 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001432 //
1433 // If a sideband stream is attached, however, we want to skip this
1434 // optimization so that transactions aren't missed when a buffer
1435 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001436
1437 flags |= eDontUpdateGeometryState;
1438 }
1439 }
1440
Mathias Agopian13127d82013-03-05 17:47:11 -08001441 // always set active to requested, unless we're asked not to
1442 // this is used by Layer, which special cases resizes.
1443 if (flags & eDontUpdateGeometryState) {
1444 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001445 c.active = c.requested;
Mathias Agopian13127d82013-03-05 17:47:11 -08001446 }
1447
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001448 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001449 // invalidate and recompute the visible regions if needed
1450 flags |= Layer::eVisibleRegion;
1451 }
1452
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001453 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001454 // invalidate and recompute the visible regions if needed
1455 flags |= eVisibleRegion;
1456 this->contentDirty = true;
1457
1458 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001459 const uint8_t type = c.active.transform.getType();
1460 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001461 (type >= Transform::SCALE));
1462 }
1463
Dan Stozac8145172016-04-28 16:29:06 -07001464 // If the layer is hidden, signal and clear out all local sync points so
1465 // that transactions for layers depending on this layer's frames becoming
1466 // visible are not blocked
1467 if (c.flags & layer_state_t::eLayerHidden) {
1468 Mutex::Autolock lock(mLocalSyncPointMutex);
1469 for (auto& point : mLocalSyncPoints) {
1470 point->setFrameAvailable();
1471 }
1472 mLocalSyncPoints.clear();
1473 }
1474
Mathias Agopian13127d82013-03-05 17:47:11 -08001475 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001476 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478}
1479
Pablo Ceballos05289c22016-04-14 15:49:55 -07001480void Layer::commitTransaction(const State& stateToCommit) {
1481 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001482}
1483
Mathias Agopian13127d82013-03-05 17:47:11 -08001484uint32_t Layer::getTransactionFlags(uint32_t flags) {
1485 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1486}
1487
1488uint32_t Layer::setTransactionFlags(uint32_t flags) {
1489 return android_atomic_or(flags, &mTransactionFlags);
1490}
1491
1492bool Layer::setPosition(float x, float y) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001493 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001494 return false;
1495 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001496
1497 // We update the requested and active position simultaneously because
1498 // we want to apply the position portion of the transform matrix immediately,
1499 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001500 mCurrentState.requested.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001501 mCurrentState.active.transform.set(x, y);
1502
Dan Stoza7dde5992015-05-22 09:51:44 -07001503 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001504 setTransactionFlags(eTransactionNeeded);
1505 return true;
1506}
1507bool Layer::setLayer(uint32_t z) {
1508 if (mCurrentState.z == z)
1509 return false;
1510 mCurrentState.sequence++;
1511 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001512 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001513 setTransactionFlags(eTransactionNeeded);
1514 return true;
1515}
1516bool Layer::setSize(uint32_t w, uint32_t h) {
1517 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1518 return false;
1519 mCurrentState.requested.w = w;
1520 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001521 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001522 setTransactionFlags(eTransactionNeeded);
1523 return true;
1524}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001525#ifdef USE_HWC2
1526bool Layer::setAlpha(float alpha) {
1527#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001528bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001529#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001530 if (mCurrentState.alpha == alpha)
1531 return false;
1532 mCurrentState.sequence++;
1533 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001534 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001535 setTransactionFlags(eTransactionNeeded);
1536 return true;
1537}
1538bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1539 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001540 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001541 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001542 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001543 setTransactionFlags(eTransactionNeeded);
1544 return true;
1545}
1546bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001547 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001548 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001549 setTransactionFlags(eTransactionNeeded);
1550 return true;
1551}
1552bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1553 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1554 if (mCurrentState.flags == newFlags)
1555 return false;
1556 mCurrentState.sequence++;
1557 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001558 mCurrentState.mask = mask;
1559 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001560 setTransactionFlags(eTransactionNeeded);
1561 return true;
1562}
1563bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001564 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001565 return false;
1566 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001567 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001568 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001569 setTransactionFlags(eTransactionNeeded);
1570 return true;
1571}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001572bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001573 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001574 return false;
1575 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001576 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001577 mCurrentState.modified = true;
1578 setTransactionFlags(eTransactionNeeded);
1579 return true;
1580}
Mathias Agopian13127d82013-03-05 17:47:11 -08001581
Robert Carrc3574f72016-03-24 12:19:32 -07001582bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1583 if (scalingMode == mOverrideScalingMode)
1584 return false;
1585 mOverrideScalingMode = scalingMode;
1586 return true;
1587}
1588
1589uint32_t Layer::getEffectiveScalingMode() const {
1590 if (mOverrideScalingMode >= 0) {
1591 return mOverrideScalingMode;
1592 }
1593 return mCurrentScalingMode;
1594}
1595
Mathias Agopian13127d82013-03-05 17:47:11 -08001596bool Layer::setLayerStack(uint32_t layerStack) {
1597 if (mCurrentState.layerStack == layerStack)
1598 return false;
1599 mCurrentState.sequence++;
1600 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001601 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 setTransactionFlags(eTransactionNeeded);
1603 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001604}
1605
Dan Stoza7dde5992015-05-22 09:51:44 -07001606void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1607 uint64_t frameNumber) {
1608 mCurrentState.handle = handle;
1609 mCurrentState.frameNumber = frameNumber;
1610 // We don't set eTransactionNeeded, because just receiving a deferral
1611 // request without any other state updates shouldn't actually induce a delay
1612 mCurrentState.modified = true;
1613 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001614 mCurrentState.handle = nullptr;
1615 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001616 mCurrentState.modified = false;
1617}
1618
Dan Stozaee44edd2015-03-23 15:50:23 -07001619void Layer::useSurfaceDamage() {
1620 if (mFlinger->mForceFullDamage) {
1621 surfaceDamageRegion = Region::INVALID_REGION;
1622 } else {
1623 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1624 }
1625}
1626
1627void Layer::useEmptyDamage() {
1628 surfaceDamageRegion.clear();
1629}
1630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631// ----------------------------------------------------------------------------
1632// pageflip handling...
1633// ----------------------------------------------------------------------------
1634
Dan Stoza6b9454d2014-11-07 16:00:59 -08001635bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001636 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001637 return true;
1638 }
1639
Dan Stoza6b9454d2014-11-07 16:00:59 -08001640 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001641 if (mQueueItems.empty()) {
1642 return false;
1643 }
1644 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001645 nsecs_t expectedPresent =
1646 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001647
1648 // Ignore timestamps more than a second in the future
1649 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1650 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1651 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1652 expectedPresent);
1653
1654 bool isDue = timestamp < expectedPresent;
1655 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001656}
1657
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001658bool Layer::onPreComposition() {
1659 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001660 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001661}
1662
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001663void Layer::onPostComposition() {
1664 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001665 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001666 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1667
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001668 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001669 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001670 mFrameTracker.setFrameReadyFence(frameReadyFence);
1671 } else {
1672 // There was no fence for this frame, so assume that it was ready
1673 // to be presented at the desired present time.
1674 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1675 }
1676
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001677 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001678#ifdef USE_HWC2
1679 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1680#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001681 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001682#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001683 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001684 mFrameTracker.setActualPresentFence(presentFence);
1685 } else {
1686 // The HWC doesn't support present fences, so use the refresh
1687 // timestamp instead.
1688 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1689 mFrameTracker.setActualPresentTime(presentTime);
1690 }
1691
1692 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001693 mFrameLatencyNeeded = false;
1694 }
1695}
1696
Dan Stoza9e56aa02015-11-02 13:00:03 -08001697#ifdef USE_HWC2
1698void Layer::releasePendingBuffer() {
1699 mSurfaceFlingerConsumer->releasePendingBuffer();
1700}
1701#endif
1702
Mathias Agopianda27af92012-09-13 18:17:13 -07001703bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001704 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001705#ifdef USE_HWC2
1706 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1707 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1708#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001709 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001710 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001711#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001712}
1713
Mathias Agopian4fec8732012-06-29 14:12:52 -07001714Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001716 ATRACE_CALL();
1717
Jesse Hall399184a2014-03-03 15:42:54 -08001718 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1719 // mSidebandStreamChanged was true
1720 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001721 if (mSidebandStream != NULL) {
1722 setTransactionFlags(eTransactionNeeded);
1723 mFlinger->setTransactionFlags(eTraversalNeeded);
1724 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001725 recomputeVisibleRegions = true;
1726
1727 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001728 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001729 }
1730
Mathias Agopian4fec8732012-06-29 14:12:52 -07001731 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001732 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001733
1734 // if we've already called updateTexImage() without going through
1735 // a composition step, we have to skip this layer at this point
1736 // because we cannot call updateTeximage() without a corresponding
1737 // compositionComplete() call.
1738 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001739 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001740 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001741 }
1742
Jamie Gennis351a5132011-09-14 18:23:37 -07001743 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001744 const State& s(getDrawingState());
1745 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001746 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001747
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001748 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001749 Layer::State& front;
1750 Layer::State& current;
1751 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001752 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001753 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001754 int32_t overrideScalingMode;
Robert Carrb5d3d262016-03-25 15:08:13 -07001755
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001756 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001757 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001758 const char* name,
1759 int32_t overrideScalingMode)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001760 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001761 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001762 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001763 name(name),
1764 overrideScalingMode(overrideScalingMode) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001765 }
1766
1767 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001768 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001769 if (buf == NULL) {
1770 return false;
1771 }
1772
1773 uint32_t bufWidth = buf->getWidth();
1774 uint32_t bufHeight = buf->getHeight();
1775
1776 // check that we received a buffer of the right size
1777 // (Take the buffer's orientation into account)
1778 if (item.mTransform & Transform::ROT_90) {
1779 swap(bufWidth, bufHeight);
1780 }
1781
Robert Carrc3574f72016-03-24 12:19:32 -07001782 int actualScalingMode = overrideScalingMode >= 0 ?
1783 overrideScalingMode : item.mScalingMode;
1784 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001785 if (front.active != front.requested) {
1786
1787 if (isFixedSize ||
1788 (bufWidth == front.requested.w &&
1789 bufHeight == front.requested.h))
1790 {
1791 // Here we pretend the transaction happened by updating the
1792 // current and drawing states. Drawing state is only accessed
1793 // in this thread, no need to have it locked
1794 front.active = front.requested;
1795
1796 // We also need to update the current state so that
1797 // we don't end-up overwriting the drawing state with
1798 // this stale current state during the next transaction
1799 //
1800 // NOTE: We don't need to hold the transaction lock here
1801 // because State::active is only accessed from this thread.
1802 current.active = front.active;
Pablo Ceballos39c88e82016-05-10 17:15:24 -07001803 current.modified = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001804
1805 // recompute visible region
1806 recomputeVisibleRegions = true;
1807 }
1808
1809 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001810 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001811 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001812 " requested={ wh={%4u,%4u} }}\n",
1813 name,
Andy McFadden69052052012-09-14 16:10:11 -07001814 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001815 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001816 front.crop.left,
1817 front.crop.top,
1818 front.crop.right,
1819 front.crop.bottom,
1820 front.crop.getWidth(),
1821 front.crop.getHeight(),
1822 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001823 }
1824
Ruben Brunk1681d952014-06-27 15:51:55 -07001825 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001826 if (front.active.w != bufWidth ||
1827 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001828 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001829 ALOGE("[%s] rejecting buffer: "
1830 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1831 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001832 return true;
1833 }
1834 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001835
1836 // if the transparent region has changed (this test is
1837 // conservative, but that's fine, worst case we're doing
1838 // a bit of extra work), we latch the new one and we
1839 // trigger a visible-region recompute.
1840 if (!front.activeTransparentRegion.isTriviallyEqual(
1841 front.requestedTransparentRegion)) {
1842 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001843
1844 // We also need to update the current state so that
1845 // we don't end-up overwriting the drawing state with
1846 // this stale current state during the next transaction
1847 //
1848 // NOTE: We don't need to hold the transaction lock here
1849 // because State::active is only accessed from this thread.
1850 current.activeTransparentRegion = front.activeTransparentRegion;
1851
1852 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001853 recomputeVisibleRegions = true;
1854 }
1855
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001856 return false;
1857 }
1858 };
1859
Ruben Brunk1681d952014-06-27 15:51:55 -07001860 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001861 getProducerStickyTransform() != 0, mName.string(),
1862 mOverrideScalingMode);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001863
Dan Stozacac35382016-01-27 12:21:06 -08001864
1865 // Check all of our local sync points to ensure that all transactions
1866 // which need to have been applied prior to the frame which is about to
1867 // be latched have signaled
1868
1869 auto headFrameNumber = getHeadFrameNumber();
1870 bool matchingFramesFound = false;
1871 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001872 {
Dan Stozacac35382016-01-27 12:21:06 -08001873 Mutex::Autolock lock(mLocalSyncPointMutex);
1874 for (auto& point : mLocalSyncPoints) {
1875 if (point->getFrameNumber() > headFrameNumber) {
1876 break;
1877 }
1878
1879 matchingFramesFound = true;
1880
1881 if (!point->frameIsAvailable()) {
1882 // We haven't notified the remote layer that the frame for
1883 // this point is available yet. Notify it now, and then
1884 // abort this attempt to latch.
1885 point->setFrameAvailable();
1886 allTransactionsApplied = false;
1887 break;
1888 }
1889
1890 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001891 }
1892 }
1893
Dan Stozacac35382016-01-27 12:21:06 -08001894 if (matchingFramesFound && !allTransactionsApplied) {
1895 mFlinger->signalLayerUpdate();
1896 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001897 }
1898
Pablo Ceballos06312182015-10-07 16:32:12 -07001899 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1900 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001901 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001902 // buffer mode.
1903 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001904 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001905 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001906 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001907 if (updateResult == BufferQueue::PRESENT_LATER) {
1908 // Producer doesn't want buffer to be displayed yet. Signal a
1909 // layer update so we check again at the next opportunity.
1910 mFlinger->signalLayerUpdate();
1911 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001912 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1913 // If the buffer has been rejected, remove it from the shadow queue
1914 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001915 if (queuedBuffer) {
1916 Mutex::Autolock lock(mQueueItemLock);
1917 mQueueItems.removeAt(0);
1918 android_atomic_dec(&mQueuedFrames);
1919 }
Dan Stozaecc50402015-04-28 14:42:06 -07001920 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001921 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1922 // This can occur if something goes wrong when trying to create the
1923 // EGLImage for this buffer. If this happens, the buffer has already
1924 // been released, so we need to clean up the queue and bug out
1925 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001926 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001927 Mutex::Autolock lock(mQueueItemLock);
1928 mQueueItems.clear();
1929 android_atomic_and(0, &mQueuedFrames);
1930 }
1931
1932 // Once we have hit this state, the shadow queue may no longer
1933 // correctly reflect the incoming BufferQueue's contents, so even if
1934 // updateTexImage starts working, the only safe course of action is
1935 // to continue to ignore updates.
1936 mUpdateTexImageFailed = true;
1937
1938 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001939 }
1940
Pablo Ceballos06312182015-10-07 16:32:12 -07001941 if (queuedBuffer) {
1942 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001943 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1944
Dan Stoza6b9454d2014-11-07 16:00:59 -08001945 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001946
1947 // Remove any stale buffers that have been dropped during
1948 // updateTexImage
1949 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1950 mQueueItems.removeAt(0);
1951 android_atomic_dec(&mQueuedFrames);
1952 }
1953
Dan Stoza6b9454d2014-11-07 16:00:59 -08001954 mQueueItems.removeAt(0);
1955 }
1956
Dan Stozaecc50402015-04-28 14:42:06 -07001957
Andy McFadden1585c4d2013-06-28 13:52:40 -07001958 // Decrement the queued-frames count. Signal another event if we
1959 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001960 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001961 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001962 mFlinger->signalLayerUpdate();
1963 }
1964
1965 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001966 // something happened!
1967 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001968 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001969 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001970
Jamie Gennis351a5132011-09-14 18:23:37 -07001971 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001972 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001973 if (mActiveBuffer == NULL) {
1974 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001975 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001976 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001977
Mathias Agopian4824d402012-06-04 18:16:30 -07001978 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001979 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001980 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001981 // the first time we receive a buffer, we need to trigger a
1982 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001983 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001984 }
1985
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001986 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1987 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1988 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07001989 if ((crop != mCurrentCrop) ||
1990 (transform != mCurrentTransform) ||
1991 (scalingMode != mCurrentScalingMode))
1992 {
1993 mCurrentCrop = crop;
1994 mCurrentTransform = transform;
1995 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07001996 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001997 }
1998
1999 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002000 uint32_t bufWidth = mActiveBuffer->getWidth();
2001 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002002 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2003 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002004 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002005 }
2006 }
2007
2008 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002009 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002010 recomputeVisibleRegions = true;
2011 }
2012
Dan Stozacac35382016-01-27 12:21:06 -08002013 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2014
2015 // Remove any sync points corresponding to the buffer which was just
2016 // latched
2017 {
2018 Mutex::Autolock lock(mLocalSyncPointMutex);
2019 auto point = mLocalSyncPoints.begin();
2020 while (point != mLocalSyncPoints.end()) {
2021 if (!(*point)->frameIsAvailable() ||
2022 !(*point)->transactionIsApplied()) {
2023 // This sync point must have been added since we started
2024 // latching. Don't drop it yet.
2025 ++point;
2026 continue;
2027 }
2028
2029 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2030 point = mLocalSyncPoints.erase(point);
2031 } else {
2032 ++point;
2033 }
2034 }
2035 }
2036
Mathias Agopian4fec8732012-06-29 14:12:52 -07002037 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002038 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002039
2040 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002041 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002042 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002043 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044}
2045
Mathias Agopiana67932f2011-04-20 14:20:59 -07002046uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002047{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002048 // TODO: should we do something special if mSecure is set?
2049 if (mProtectedByApp) {
2050 // need a hardware-protected path to external video sink
2051 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002052 }
Riley Andrews03414a12014-07-01 14:22:59 -07002053 if (mPotentialCursor) {
2054 usage |= GraphicBuffer::USAGE_CURSOR;
2055 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002056 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002057 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002058}
2059
Mathias Agopian84300952012-11-21 16:02:13 -08002060void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002061 uint32_t orientation = 0;
2062 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002063 // The transform hint is used to improve performance, but we can
2064 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002065 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002066 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002067 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002068 if (orientation & Transform::ROT_INVALID) {
2069 orientation = 0;
2070 }
2071 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002072 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002073}
2074
Mathias Agopian13127d82013-03-05 17:47:11 -08002075// ----------------------------------------------------------------------------
2076// debugging
2077// ----------------------------------------------------------------------------
2078
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002079void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002080{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002081 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002082
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002083 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002084 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002085 "+ %s %p (%s)\n",
2086 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002087 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002088
Mathias Agopian2ca79392013-04-02 18:30:32 -07002089 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002090 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002091 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002092 sp<Client> client(mClientRef.promote());
2093
Mathias Agopian74d211a2013-04-22 16:55:35 +02002094 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002095 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2096 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002097 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002098#ifdef USE_HWC2
2099 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2100#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002101 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002102#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002103 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002104 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 -07002105 s.crop.left, s.crop.top,
2106 s.crop.right, s.crop.bottom,
2107 s.finalCrop.left, s.finalCrop.top,
2108 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002109 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002110 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002111 s.active.transform[0][0], s.active.transform[0][1],
2112 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002113 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002114
2115 sp<const GraphicBuffer> buf0(mActiveBuffer);
2116 uint32_t w0=0, h0=0, s0=0, f0=0;
2117 if (buf0 != 0) {
2118 w0 = buf0->getWidth();
2119 h0 = buf0->getHeight();
2120 s0 = buf0->getStride();
2121 f0 = buf0->format;
2122 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002123 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002124 " "
2125 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2126 " queued-frames=%d, mRefreshPending=%d\n",
2127 mFormat, w0, h0, s0,f0,
2128 mQueuedFrames, mRefreshPending);
2129
Mathias Agopian13127d82013-03-05 17:47:11 -08002130 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002131 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002132 }
2133}
2134
Svetoslavd85084b2014-03-20 10:28:31 -07002135void Layer::dumpFrameStats(String8& result) const {
2136 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002137}
2138
Svetoslavd85084b2014-03-20 10:28:31 -07002139void Layer::clearFrameStats() {
2140 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002141}
2142
Jamie Gennis6547ff42013-07-16 20:12:42 -07002143void Layer::logFrameStats() {
2144 mFrameTracker.logAndResetStats(mName);
2145}
2146
Svetoslavd85084b2014-03-20 10:28:31 -07002147void Layer::getFrameStats(FrameStats* outStats) const {
2148 mFrameTracker.getStats(outStats);
2149}
2150
Pablo Ceballos40845df2016-01-25 17:41:15 -08002151void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2152 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2153 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2154 *outName = mName;
2155 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2156
2157#ifdef USE_HWC2
2158 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2159 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2160 HWC2::Composition::Client : true;
2161#else
2162 *outIsGlesComposition = mIsGlesComposition;
2163#endif
2164 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2165 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2166 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2167}
Mathias Agopian13127d82013-03-05 17:47:11 -08002168// ---------------------------------------------------------------------------
2169
2170Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2171 const sp<Layer>& layer)
2172 : mFlinger(flinger), mLayer(layer) {
2173}
2174
2175Layer::LayerCleaner::~LayerCleaner() {
2176 // destroy client resources
2177 mFlinger->onLayerDestroyed(mLayer);
2178}
2179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002180// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002181}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002182
2183#if defined(__gl_h_)
2184#error "don't include gl/gl.h in this file"
2185#endif
2186
2187#if defined(__gl2_h_)
2188#error "don't include gl2/gl2.h in this file"
2189#endif