blob: ac99cd0d7a2a7f923df219797e391dade3276c52 [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),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080079 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080080 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080081 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080082 mFiltering(false),
83 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070084 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Pablo Ceballos40845df2016-01-25 17:41:15 -080085#ifndef USE_HWC2
86 mIsGlesComposition(false),
87#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
89 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070090 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070091 mPotentialCursor(false),
92 mQueueItemLock(),
93 mQueueItemCondition(),
94 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070095 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080096 mUpdateTexImageFailed(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080097 mAutoRefresh(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098{
Dan Stoza9e56aa02015-11-02 13:00:03 -080099#ifdef USE_HWC2
100 ALOGV("Creating Layer %s", name.string());
101#endif
102
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700104 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700105 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 uint32_t layerFlags = 0;
108 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800109 layerFlags |= layer_state_t::eLayerHidden;
110 if (flags & ISurfaceComposerClient::eOpaque)
111 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700112 if (flags & ISurfaceComposerClient::eSecure)
113 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 if (flags & ISurfaceComposerClient::eNonPremultiplied)
116 mPremultipliedAlpha = false;
117
118 mName = name;
119
120 mCurrentState.active.w = w;
121 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800122 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700123 mCurrentState.crop.makeInvalid();
124 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800126#ifdef USE_HWC2
127 mCurrentState.alpha = 1.0f;
128#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131 mCurrentState.layerStack = 0;
132 mCurrentState.flags = layerFlags;
133 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700134 mCurrentState.requested = mCurrentState.active;
135
136 // drawing state & current state are identical
137 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700138
Dan Stoza9e56aa02015-11-02 13:00:03 -0800139#ifdef USE_HWC2
140 const auto& hwc = flinger->getHwComposer();
141 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
142 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
143#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700144 nsecs_t displayPeriod =
145 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800146#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700147 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800148}
149
Mathias Agopian3f844832013-08-07 21:24:32 -0700150void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800151 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700152 sp<IGraphicBufferProducer> producer;
153 sp<IGraphicBufferConsumer> consumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700154 BufferQueue::createBufferQueue(&producer, &consumer);
155 mProducer = new MonitoredProducer(producer, mFlinger);
156 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800157 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800158 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700159 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800160
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700161#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
162#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700163#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700164 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800165#endif
Andy McFadden69052052012-09-14 16:10:11 -0700166
Mathias Agopian84300952012-11-21 16:02:13 -0800167 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
168 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169}
170
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800172 sp<Client> c(mClientRef.promote());
173 if (c != 0) {
174 c->detachLayer(this);
175 }
176
Dan Stozacac35382016-01-27 12:21:06 -0800177 for (auto& point : mRemoteSyncPoints) {
178 point->setTransactionApplied();
179 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700180 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700181 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700182}
183
Mathias Agopian13127d82013-03-05 17:47:11 -0800184// ---------------------------------------------------------------------------
185// callbacks
186// ---------------------------------------------------------------------------
187
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188#ifdef USE_HWC2
189void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
190 if (mHwcLayers.empty()) {
191 return;
192 }
193 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
194}
195#else
Dan Stozac7014012014-02-14 15:03:43 -0800196void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800197 HWComposer::HWCLayerInterface* layer) {
198 if (layer) {
199 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700200 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 }
202}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800204
Dan Stoza6b9454d2014-11-07 16:00:59 -0800205void Layer::onFrameAvailable(const BufferItem& item) {
206 // Add this buffer from our internal queue tracker
207 { // Autolock scope
208 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700209
210 // Reset the frame number tracker when we receive the first buffer after
211 // a frame number reset
212 if (item.mFrameNumber == 1) {
213 mLastFrameNumberReceived = 0;
214 }
215
216 // Ensure that callbacks are handled in order
217 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
218 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
219 ms2ns(500));
220 if (result != NO_ERROR) {
221 ALOGE("[%s] Timed out waiting on callback", mName.string());
222 }
223 }
224
Dan Stoza6b9454d2014-11-07 16:00:59 -0800225 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700226 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700227
228 // Wake up any pending callbacks
229 mLastFrameNumberReceived = item.mFrameNumber;
230 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800231 }
232
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800233 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700234}
235
Dan Stoza6b9454d2014-11-07 16:00:59 -0800236void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700237 { // Autolock scope
238 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700239
Dan Stoza7dde5992015-05-22 09:51:44 -0700240 // Ensure that callbacks are handled in order
241 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
242 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
243 ms2ns(500));
244 if (result != NO_ERROR) {
245 ALOGE("[%s] Timed out waiting on callback", mName.string());
246 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700247 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700248
249 if (mQueueItems.empty()) {
250 ALOGE("Can't replace a frame on an empty queue");
251 return;
252 }
253 mQueueItems.editItemAt(0) = item;
254
255 // Wake up any pending callbacks
256 mLastFrameNumberReceived = item.mFrameNumber;
257 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700258 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800259}
260
Jesse Hall399184a2014-03-03 15:42:54 -0800261void Layer::onSidebandStreamChanged() {
262 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
263 // mSidebandStreamChanged was false
264 mFlinger->signalLayerUpdate();
265 }
266}
267
Mathias Agopian67106042013-03-14 19:18:13 -0700268// called with SurfaceFlinger::mStateLock from the drawing thread after
269// the layer has been remove from the current state list (and just before
270// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800271void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800272 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700273}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700274
Mathias Agopian13127d82013-03-05 17:47:11 -0800275// ---------------------------------------------------------------------------
276// set-up
277// ---------------------------------------------------------------------------
278
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700279const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800280 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281}
282
Mathias Agopianf9d93272009-06-19 17:00:27 -0700283status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 PixelFormat format, uint32_t flags)
285{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700286 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700288
289 // never allow a surface larger than what our underlying GL implementation
290 // can handle.
291 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800292 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700293 return BAD_VALUE;
294 }
295
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700296 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700297
Riley Andrews03414a12014-07-01 14:22:59 -0700298 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700299 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700300 mCurrentOpacity = getOpacityForFormat(format);
301
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800302 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
303 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
304 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 return NO_ERROR;
307}
308
Dan Stoza7dde5992015-05-22 09:51:44 -0700309/*
310 * The layer handle is just a BBinder object passed to the client
311 * (remote process) -- we don't keep any reference on our side such that
312 * the dtor is called when the remote side let go of its reference.
313 *
314 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
315 * this layer when the handle is destroyed.
316 */
317class Layer::Handle : public BBinder, public LayerCleaner {
318 public:
319 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
320 : LayerCleaner(flinger, layer), owner(layer) {}
321
322 wp<Layer> owner;
323};
324
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700325sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800326 Mutex::Autolock _l(mLock);
327
328 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700329 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800330
331 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700332
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700333 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800334}
335
Dan Stozab9b08832014-03-13 11:55:57 -0700336sp<IGraphicBufferProducer> Layer::getProducer() const {
337 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700338}
339
Mathias Agopian13127d82013-03-05 17:47:11 -0800340// ---------------------------------------------------------------------------
341// h/w composer set-up
342// ---------------------------------------------------------------------------
343
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800344Rect Layer::getContentCrop() const {
345 // this is the crop rectangle that applies to the buffer
346 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700347 Rect crop;
348 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800349 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700350 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800351 } else if (mActiveBuffer != NULL) {
352 // otherwise we use the whole buffer
353 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700354 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800355 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700356 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700357 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700358 return crop;
359}
360
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700361static Rect reduce(const Rect& win, const Region& exclude) {
362 if (CC_LIKELY(exclude.isEmpty())) {
363 return win;
364 }
365 if (exclude.isRect()) {
366 return win.reduce(exclude.getBounds());
367 }
368 return Region(win).subtract(exclude).getBounds();
369}
370
Mathias Agopian13127d82013-03-05 17:47:11 -0800371Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700372 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700373 return computeBounds(s.activeTransparentRegion);
374}
375
376Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
377 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800378 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700379
380 if (!s.crop.isEmpty()) {
381 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800382 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700383 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700384 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800385}
386
Mathias Agopian6b442672013-07-09 21:24:52 -0700387FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800388 // the content crop is the area of the content that gets scaled to the
389 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700390 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800391
Robert Carrb5d3d262016-03-25 15:08:13 -0700392 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800393 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700394 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800395
396 // apply the projection's clipping to the window crop in
397 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700398 // if there are no window scaling involved, this operation will map to full
399 // pixels in the buffer.
400 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
401 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700402
403 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700404 if (!s.crop.isEmpty()) {
405 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700406 }
407
Robert Carr3dcabfa2016-03-01 18:36:58 -0800408 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000409 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
410 activeCrop.clear();
411 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700412 if (!s.finalCrop.isEmpty()) {
413 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 activeCrop.clear();
415 }
416 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800417 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Michael Lentine28ea2172014-11-19 18:32:37 -0800419 // This needs to be here as transform.transform(Rect) computes the
420 // transformed rect and then takes the bounding box of the result before
421 // returning. This means
422 // transform.inverse().transform(transform.transform(Rect)) != Rect
423 // in which case we need to make sure the final rect is clipped to the
424 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
426 activeCrop.clear();
427 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800428
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700429 // subtract the transparent region and snap to the bounds
430 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
431
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 // Transform the window crop to match the buffer coordinate system,
433 // which means using the inverse of the current transform set on the
434 // SurfaceFlingerConsumer.
435 uint32_t invTransform = mCurrentTransform;
436 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
437 /*
438 * the code below applies the display's inverse transform to the buffer
439 */
440 uint32_t invTransformOrient = hw->getOrientationTransform();
441 // calculate the inverse transform
442 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
443 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
444 NATIVE_WINDOW_TRANSFORM_FLIP_H;
445 // If the transform has been rotated the axis of flip has been swapped
446 // so we need to swap which flip operations we are performing
Michael Lentine7b902582014-08-19 18:14:06 -0700447 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
448 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 if (is_h_flipped != is_v_flipped) {
Michael Lentine7b902582014-08-19 18:14:06 -0700450 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
451 NATIVE_WINDOW_TRANSFORM_FLIP_H;
452 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800453 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 // and apply to the current transform
455 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457
458 int winWidth = s.active.w;
459 int winHeight = s.active.h;
460 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
461 // If the activeCrop has been rotate the ends are rotated but not
462 // the space itself so when transforming ends back we can't rely on
463 // a modification of the axes of rotation. To account for this we
464 // need to reorient the inverse rotation in terms of the current
465 // axes of rotation.
466 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
467 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
468 if (is_h_flipped == is_v_flipped) {
469 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
470 NATIVE_WINDOW_TRANSFORM_FLIP_H;
471 }
472 winWidth = s.active.h;
473 winHeight = s.active.w;
474 }
475 const Rect winCrop = activeCrop.transform(
476 invTransform, s.active.w, s.active.h);
477
478 // below, crop is intersected with winCrop expressed in crop's coordinate space
479 float xScale = crop.getWidth() / float(winWidth);
480 float yScale = crop.getHeight() / float(winHeight);
481
482 float insetL = winCrop.left * xScale;
483 float insetT = winCrop.top * yScale;
484 float insetR = (winWidth - winCrop.right ) * xScale;
485 float insetB = (winHeight - winCrop.bottom) * yScale;
486
487 crop.left += insetL;
488 crop.top += insetT;
489 crop.right -= insetR;
490 crop.bottom -= insetB;
491
Mathias Agopian13127d82013-03-05 17:47:11 -0800492 return crop;
493}
494
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495#ifdef USE_HWC2
496void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
497#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700498void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700499 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700500 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700502{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503#ifdef USE_HWC2
504 const auto hwcId = displayDevice->getHwcDisplayId();
505 auto& hwcInfo = mHwcLayers[hwcId];
506#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800507 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700509
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700510 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511#ifdef USE_HWC2
512 hwcInfo.forceClientComposition = false;
513
514 if (isSecure() && !displayDevice->isSecure()) {
515 hwcInfo.forceClientComposition = true;
516 }
517
518 auto& hwcLayer = hwcInfo.layer;
519#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700520 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700521
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700522 if (isSecure() && !hw->isSecure()) {
523 layer.setSkip(true);
524 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800525#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700526
Mathias Agopian13127d82013-03-05 17:47:11 -0800527 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700528 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529#ifdef USE_HWC2
530 if (!isOpaque(s) || s.alpha != 1.0f) {
531 auto blendMode = mPremultipliedAlpha ?
532 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
533 auto error = hwcLayer->setBlendMode(blendMode);
534 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
535 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
536 to_string(error).c_str(), static_cast<int32_t>(error));
537 }
538#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800539 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800540 layer.setBlending(mPremultipliedAlpha ?
541 HWC_BLENDING_PREMULT :
542 HWC_BLENDING_COVERAGE);
543 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800545
546 // apply the layer's transform, followed by the display's global transform
547 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700548 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700549 if (!s.crop.isEmpty()) {
550 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800551 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 activeCrop.clear();
558 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800559 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800560 // This needs to be here as transform.transform(Rect) computes the
561 // transformed rect and then takes the bounding box of the result before
562 // returning. This means
563 // transform.inverse().transform(transform.transform(Rect)) != Rect
564 // in which case we need to make sure the final rect is clipped to the
565 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000566 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
567 activeCrop.clear();
568 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700569 // mark regions outside the crop as transparent
570 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
571 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
572 s.active.w, s.active.h));
573 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
574 activeCrop.left, activeCrop.bottom));
575 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
576 s.active.w, activeCrop.bottom));
577 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800578 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700579 if (!s.finalCrop.isEmpty()) {
580 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000581 frame.clear();
582 }
583 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000585 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
586 frame.clear();
587 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588 const Transform& tr(displayDevice->getTransform());
589 Rect transformedFrame = tr.transform(frame);
590 auto error = hwcLayer->setDisplayFrame(transformedFrame);
591 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
592 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
593 transformedFrame.top, transformedFrame.right,
594 transformedFrame.bottom, to_string(error).c_str(),
595 static_cast<int32_t>(error));
596
597 FloatRect sourceCrop = computeCrop(displayDevice);
598 error = hwcLayer->setSourceCrop(sourceCrop);
599 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
600 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
601 sourceCrop.left, sourceCrop.top, sourceCrop.right,
602 sourceCrop.bottom, to_string(error).c_str(),
603 static_cast<int32_t>(error));
604
605 error = hwcLayer->setPlaneAlpha(s.alpha);
606 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
607 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
608 static_cast<int32_t>(error));
609
610 error = hwcLayer->setZOrder(s.z);
611 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
612 mName.string(), s.z, to_string(error).c_str(),
613 static_cast<int32_t>(error));
614#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000615 if (!frame.intersect(hw->getViewport(), &frame)) {
616 frame.clear();
617 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800618 const Transform& tr(hw->getTransform());
619 layer.setFrame(tr.transform(frame));
620 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800621 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800623
Mathias Agopian29a367b2011-07-12 14:51:45 -0700624 /*
625 * Transformations are applied in this order:
626 * 1) buffer orientation/flip/mirror
627 * 2) state transformation (window manager)
628 * 3) layer orientation (screen orientation)
629 * (NOTE: the matrices are multiplied in reverse order)
630 */
631
632 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800633 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634
635 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
636 /*
637 * the code below applies the display's inverse transform to the buffer
638 */
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639#ifdef USE_HWC2
640 uint32_t invTransform = displayDevice->getOrientationTransform();
641#else
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700642 uint32_t invTransform = hw->getOrientationTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643#endif
Michael Lentine14409632014-08-19 11:27:30 -0700644 uint32_t t_orientation = transform.getOrientation();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 // calculate the inverse transform
646 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
647 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
648 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Michael Lentine14409632014-08-19 11:27:30 -0700649 // If the transform has been rotated the axis of flip has been swapped
650 // so we need to swap which flip operations we are performing
651 bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
652 bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
653 if (is_h_flipped != is_v_flipped) {
654 t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
655 NATIVE_WINDOW_TRANSFORM_FLIP_H;
656 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700657 }
658 // and apply to the current transform
Michael Lentine14409632014-08-19 11:27:30 -0700659 transform = Transform(t_orientation) * Transform(invTransform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700660 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700661
662 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800663 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664#ifdef USE_HWC2
665 if (orientation & Transform::ROT_INVALID) {
666 // we can only handle simple transformation
667 hwcInfo.forceClientComposition = true;
668 } else {
669 auto transform = static_cast<HWC2::Transform>(orientation);
670 auto error = hwcLayer->setTransform(transform);
671 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
672 "%s (%d)", mName.string(), to_string(transform).c_str(),
673 to_string(error).c_str(), static_cast<int32_t>(error));
674 }
675#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800676 if (orientation & Transform::ROT_INVALID) {
677 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700678 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700679 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800680 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700681 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700683}
684
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685#ifdef USE_HWC2
686void Layer::forceClientComposition(int32_t hwcId) {
687 if (mHwcLayers.count(hwcId) == 0) {
688 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
689 return;
690 }
691
692 mHwcLayers[hwcId].forceClientComposition = true;
693}
694#endif
695
696#ifdef USE_HWC2
697void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
698 // Apply this display's projection's viewport to the visible region
699 // before giving it to the HWC HAL.
700 const Transform& tr = displayDevice->getTransform();
701 const auto& viewport = displayDevice->getViewport();
702 Region visible = tr.transform(visibleRegion.intersect(viewport));
703 auto hwcId = displayDevice->getHwcDisplayId();
704 auto& hwcLayer = mHwcLayers[hwcId].layer;
705 auto error = hwcLayer->setVisibleRegion(visible);
706 if (error != HWC2::Error::None) {
707 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
708 to_string(error).c_str(), static_cast<int32_t>(error));
709 visible.dump(LOG_TAG);
710 }
711
712 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
713 if (error != HWC2::Error::None) {
714 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
715 to_string(error).c_str(), static_cast<int32_t>(error));
716 surfaceDamageRegion.dump(LOG_TAG);
717 }
718
719 auto compositionType = HWC2::Composition::Invalid;
720 if (mSidebandStream.get()) {
721 compositionType = HWC2::Composition::Sideband;
722 auto error = hwcLayer->setSidebandStream(mSidebandStream->handle());
723 if (error != HWC2::Error::None) {
724 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
725 mName.string(), mSidebandStream->handle(),
726 to_string(error).c_str(), static_cast<int32_t>(error));
727 return;
728 }
729 } else {
730 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr) {
731 compositionType = HWC2::Composition::Client;
732 auto error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
733 if (error != HWC2::Error::None) {
734 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
735 to_string(error).c_str(), static_cast<int32_t>(error));
736 return;
737 }
738 } else {
739 if (mPotentialCursor) {
740 compositionType = HWC2::Composition::Cursor;
741 }
742 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
743 auto error = hwcLayer->setBuffer(mActiveBuffer->handle,
744 acquireFence);
745 if (error != HWC2::Error::None) {
746 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
747 mActiveBuffer->handle, to_string(error).c_str(),
748 static_cast<int32_t>(error));
749 return;
750 }
751 // If it's not a cursor, default to device composition
752 }
753 }
754
755 if (mHwcLayers[hwcId].forceClientComposition) {
756 ALOGV("[%s] Forcing Client composition", mName.string());
757 setCompositionType(hwcId, HWC2::Composition::Client);
758 } else if (compositionType != HWC2::Composition::Invalid) {
759 ALOGV("[%s] Requesting %s composition", mName.string(),
760 to_string(compositionType).c_str());
761 setCompositionType(hwcId, compositionType);
762 } else {
763 ALOGV("[%s] Requesting Device composition", mName.string());
764 setCompositionType(hwcId, HWC2::Composition::Device);
765 }
766}
767#else
Mathias Agopian42977342012-08-05 00:40:46 -0700768void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700769 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800770 // we have to set the visible region on every frame because
771 // we currently free it during onLayerDisplayed(), which is called
772 // after HWComposer::commit() -- every frame.
773 // Apply this display's projection's viewport to the visible region
774 // before giving it to the HWC HAL.
775 const Transform& tr = hw->getTransform();
776 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
777 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700778 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800779 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700780
Jesse Hall399184a2014-03-03 15:42:54 -0800781 if (mSidebandStream.get()) {
782 layer.setSidebandStream(mSidebandStream);
783 } else {
784 // NOTE: buffer can be NULL if the client never drew into this
785 // layer yet, or if we ran out of memory
786 layer.setBuffer(mActiveBuffer);
787 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700788}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700790
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791#ifdef USE_HWC2
792void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
793 auto hwcId = displayDevice->getHwcDisplayId();
794 if (mHwcLayers.count(hwcId) == 0 ||
795 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
796 return;
797 }
798
799 // This gives us only the "orientation" component of the transform
800 const State& s(getCurrentState());
801
802 // Apply the layer's transform, followed by the display's global transform
803 // Here we're guaranteed that the layer's transform preserves rects
804 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700805 if (!s.crop.isEmpty()) {
806 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807 }
808 // Subtract the transparent region and snap to the bounds
809 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800810 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800811 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700812 if (!s.finalCrop.isEmpty()) {
813 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000814 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800815 auto& displayTransform(displayDevice->getTransform());
816 auto position = displayTransform.transform(frame);
817
818 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
819 position.top);
820 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
821 "to (%d, %d): %s (%d)", mName.string(), position.left,
822 position.top, to_string(error).c_str(),
823 static_cast<int32_t>(error));
824}
825#else
Dan Stozac7014012014-02-14 15:03:43 -0800826void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700827 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700828 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700829
830 // TODO: there is a possible optimization here: we only need to set the
831 // acquire fence the first time a new buffer is acquired on EACH display.
832
Riley Andrews03414a12014-07-01 14:22:59 -0700833 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800834 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800835 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700836 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700837 if (fenceFd == -1) {
838 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
839 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700840 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700841 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700842 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700843}
844
Riley Andrews03414a12014-07-01 14:22:59 -0700845Rect Layer::getPosition(
846 const sp<const DisplayDevice>& hw)
847{
848 // this gives us only the "orientation" component of the transform
849 const State& s(getCurrentState());
850
851 // apply the layer's transform, followed by the display's global transform
852 // here we're guaranteed that the layer's transform preserves rects
853 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700854 if (!s.crop.isEmpty()) {
855 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700856 }
857 // subtract the transparent region and snap to the bounds
858 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800859 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700860 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700861 if (!s.finalCrop.isEmpty()) {
862 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000863 }
Riley Andrews03414a12014-07-01 14:22:59 -0700864 const Transform& tr(hw->getTransform());
865 return Rect(tr.transform(frame));
866}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800867#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700868
Mathias Agopian13127d82013-03-05 17:47:11 -0800869// ---------------------------------------------------------------------------
870// drawing...
871// ---------------------------------------------------------------------------
872
873void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800874 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800875}
876
Dan Stozac7014012014-02-14 15:03:43 -0800877void Layer::draw(const sp<const DisplayDevice>& hw,
878 bool useIdentityTransform) const {
879 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800880}
881
Dan Stozac7014012014-02-14 15:03:43 -0800882void Layer::draw(const sp<const DisplayDevice>& hw) const {
883 onDraw(hw, Region(hw->bounds()), false);
884}
885
886void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
887 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800889 ATRACE_CALL();
890
Mathias Agopiana67932f2011-04-20 14:20:59 -0700891 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700893 // in fact never been drawn into. This happens frequently with
894 // SurfaceView because the WindowManager can't know when the client
895 // has drawn the first time.
896
897 // If there is nothing under us, we paint the screen in black, otherwise
898 // we just skip this update.
899
900 // figure out if there is something below us
901 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700902 const SurfaceFlinger::LayerVector& drawingLayers(
903 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700904 const size_t count = drawingLayers.size();
905 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800906 const sp<Layer>& layer(drawingLayers[i]);
907 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700908 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700909 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700910 }
911 // if not everything below us is covered, we plug the holes!
912 Region holes(clip.subtract(under));
913 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700914 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700915 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 return;
917 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700918
Andy McFadden97eba892012-12-11 15:21:45 -0800919 // Bind the current buffer to the GL texture, and wait for it to be
920 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800921 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
922 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800923 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700924 // Go ahead and draw the buffer anyway; no matter what we do the screen
925 // is probably going to have something visibly wrong.
926 }
927
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700928 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
929
Mathias Agopian875d8e12013-06-07 15:35:48 -0700930 RenderEngine& engine(mFlinger->getRenderEngine());
931
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700932 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700933 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700934 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700935
936 // Query the texture matrix given our current filtering mode.
937 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800938 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
939 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700940
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700941 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
942
943 /*
944 * the code below applies the display's inverse transform to the texture transform
945 */
946
947 // create a 4x4 transform matrix from the display transform flags
948 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
949 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
950 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
951
952 mat4 tr;
953 uint32_t transform = hw->getOrientationTransform();
954 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
955 tr = tr * rot90;
956 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
957 tr = tr * flipH;
958 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
959 tr = tr * flipV;
960
961 // calculate the inverse
962 tr = inverse(tr);
963
964 // and finally apply it to the original texture matrix
965 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
966 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
967 }
968
Jamie Genniscbb1a952012-05-08 17:05:52 -0700969 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700970 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
971 mTexture.setFiltering(useFiltering);
972 mTexture.setMatrix(textureMatrix);
973
974 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700975 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700976 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700977 }
Dan Stozac7014012014-02-14 15:03:43 -0800978 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700979 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980}
981
Mathias Agopian13127d82013-03-05 17:47:11 -0800982
Dan Stozac7014012014-02-14 15:03:43 -0800983void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
984 const Region& /* clip */, float red, float green, float blue,
985 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800986{
Mathias Agopian19733a32013-08-28 18:13:56 -0700987 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800988 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700989 engine.setupFillWithColor(red, green, blue, alpha);
990 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800991}
992
993void Layer::clearWithOpenGL(
994 const sp<const DisplayDevice>& hw, const Region& clip) const {
995 clearWithOpenGL(hw, clip, 0,0,0,0);
996}
997
Dan Stozac7014012014-02-14 15:03:43 -0800998void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
999 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001000 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001001
Dan Stozac7014012014-02-14 15:03:43 -08001002 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001003
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 /*
1005 * NOTE: the way we compute the texture coordinates here produces
1006 * different results than when we take the HWC path -- in the later case
1007 * the "source crop" is rounded to texel boundaries.
1008 * This can produce significantly different results when the texture
1009 * is scaled by a large amount.
1010 *
1011 * The GL code below is more logical (imho), and the difference with
1012 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001013 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 * GL composition when a buffer scaling is applied (maybe with some
1015 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1016 * like more of a hack.
1017 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001018 Rect win(computeBounds());
1019
Robert Carrb5d3d262016-03-25 15:08:13 -07001020 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001021 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001022 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001023 win.clear();
1024 }
1025 win = s.active.transform.inverse().transform(win);
1026 if (!win.intersect(computeBounds(), &win)) {
1027 win.clear();
1028 }
1029 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001030
Mathias Agopian3f844832013-08-07 21:24:32 -07001031 float left = float(win.left) / float(s.active.w);
1032 float top = float(win.top) / float(s.active.h);
1033 float right = float(win.right) / float(s.active.w);
1034 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001035
Mathias Agopian875d8e12013-06-07 15:35:48 -07001036 // TODO: we probably want to generate the texture coords with the mesh
1037 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001038 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1039 texCoords[0] = vec2(left, 1.0f - top);
1040 texCoords[1] = vec2(left, 1.0f - bottom);
1041 texCoords[2] = vec2(right, 1.0f - bottom);
1042 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001043
Mathias Agopian875d8e12013-06-07 15:35:48 -07001044 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001045 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001046 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001047 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001048}
1049
Dan Stoza9e56aa02015-11-02 13:00:03 -08001050#ifdef USE_HWC2
1051void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1052 bool callIntoHwc) {
1053 if (mHwcLayers.count(hwcId) == 0) {
1054 ALOGE("setCompositionType called without a valid HWC layer");
1055 return;
1056 }
1057 auto& hwcInfo = mHwcLayers[hwcId];
1058 auto& hwcLayer = hwcInfo.layer;
1059 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1060 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1061 if (hwcInfo.compositionType != type) {
1062 ALOGV(" actually setting");
1063 hwcInfo.compositionType = type;
1064 if (callIntoHwc) {
1065 auto error = hwcLayer->setCompositionType(type);
1066 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1067 "composition type %s: %s (%d)", mName.string(),
1068 to_string(type).c_str(), to_string(error).c_str(),
1069 static_cast<int32_t>(error));
1070 }
1071 }
1072}
1073
1074HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1075 if (mHwcLayers.count(hwcId) == 0) {
1076 ALOGE("getCompositionType called without a valid HWC layer");
1077 return HWC2::Composition::Invalid;
1078 }
1079 return mHwcLayers.at(hwcId).compositionType;
1080}
1081
1082void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1083 if (mHwcLayers.count(hwcId) == 0) {
1084 ALOGE("setClearClientTarget called without a valid HWC layer");
1085 return;
1086 }
1087 mHwcLayers[hwcId].clearClientTarget = clear;
1088}
1089
1090bool Layer::getClearClientTarget(int32_t hwcId) const {
1091 if (mHwcLayers.count(hwcId) == 0) {
1092 ALOGE("getClearClientTarget called without a valid HWC layer");
1093 return false;
1094 }
1095 return mHwcLayers.at(hwcId).clearClientTarget;
1096}
1097#endif
1098
Ruben Brunk1681d952014-06-27 15:51:55 -07001099uint32_t Layer::getProducerStickyTransform() const {
1100 int producerStickyTransform = 0;
1101 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1102 if (ret != OK) {
1103 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1104 strerror(-ret), ret);
1105 return 0;
1106 }
1107 return static_cast<uint32_t>(producerStickyTransform);
1108}
1109
Dan Stozacac35382016-01-27 12:21:06 -08001110uint64_t Layer::getHeadFrameNumber() const {
1111 Mutex::Autolock lock(mQueueItemLock);
1112 if (!mQueueItems.empty()) {
1113 return mQueueItems[0].mFrameNumber;
1114 } else {
1115 return mCurrentFrameNumber;
1116 }
1117}
1118
1119bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1120 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1121 // Don't bother with a SyncPoint, since we've already latched the
1122 // relevant frame
1123 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001124 }
1125
Dan Stozacac35382016-01-27 12:21:06 -08001126 Mutex::Autolock lock(mLocalSyncPointMutex);
1127 mLocalSyncPoints.push_back(point);
1128 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001129}
1130
Mathias Agopian13127d82013-03-05 17:47:11 -08001131void Layer::setFiltering(bool filtering) {
1132 mFiltering = filtering;
1133}
1134
1135bool Layer::getFiltering() const {
1136 return mFiltering;
1137}
1138
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001139// As documented in libhardware header, formats in the range
1140// 0x100 - 0x1FF are specific to the HAL implementation, and
1141// are known to have no alpha channel
1142// TODO: move definition for device-specific range into
1143// hardware.h, instead of using hard-coded values here.
1144#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1145
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001146bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001147 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1148 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001149 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001150 switch (format) {
1151 case HAL_PIXEL_FORMAT_RGBA_8888:
1152 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001153 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001154 }
1155 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001156 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001157}
1158
Mathias Agopian13127d82013-03-05 17:47:11 -08001159// ----------------------------------------------------------------------------
1160// local state
1161// ----------------------------------------------------------------------------
1162
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001163static void boundPoint(vec2* point, const Rect& crop) {
1164 if (point->x < crop.left) {
1165 point->x = crop.left;
1166 }
1167 if (point->x > crop.right) {
1168 point->x = crop.right;
1169 }
1170 if (point->y < crop.top) {
1171 point->y = crop.top;
1172 }
1173 if (point->y > crop.bottom) {
1174 point->y = crop.bottom;
1175 }
1176}
1177
Dan Stozac7014012014-02-14 15:03:43 -08001178void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1179 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001180{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001181 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001182 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 const uint32_t hw_h = hw->getHeight();
1184 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001185 if (!s.crop.isEmpty()) {
1186 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001188 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001189 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001190
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001191 vec2 lt = vec2(win.left, win.top);
1192 vec2 lb = vec2(win.left, win.bottom);
1193 vec2 rb = vec2(win.right, win.bottom);
1194 vec2 rt = vec2(win.right, win.top);
1195
1196 if (!useIdentityTransform) {
1197 lt = s.active.transform.transform(lt);
1198 lb = s.active.transform.transform(lb);
1199 rb = s.active.transform.transform(rb);
1200 rt = s.active.transform.transform(rt);
1201 }
1202
Robert Carrb5d3d262016-03-25 15:08:13 -07001203 if (!s.finalCrop.isEmpty()) {
1204 boundPoint(&lt, s.finalCrop);
1205 boundPoint(&lb, s.finalCrop);
1206 boundPoint(&rb, s.finalCrop);
1207 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001208 }
1209
Mathias Agopianff2ed702013-09-01 21:36:12 -07001210 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001211 position[0] = tr.transform(lt);
1212 position[1] = tr.transform(lb);
1213 position[2] = tr.transform(rb);
1214 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001215 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001216 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 }
1218}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001219
Andy McFadden4125a4f2014-01-29 17:17:11 -08001220bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001221{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001222 // if we don't have a buffer yet, we're translucent regardless of the
1223 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001224 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001226 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001227
1228 // if the layer has the opaque flag, then we're always opaque,
1229 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001230 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001231}
1232
Dan Stoza23116082015-06-18 14:58:39 -07001233bool Layer::isSecure() const
1234{
1235 const Layer::State& s(mDrawingState);
1236 return (s.flags & layer_state_t::eLayerSecure);
1237}
1238
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001239bool Layer::isProtected() const
1240{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001241 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001242 return (activeBuffer != 0) &&
1243 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1244}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001245
Mathias Agopian13127d82013-03-05 17:47:11 -08001246bool Layer::isFixedSize() const {
1247 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1248}
1249
1250bool Layer::isCropped() const {
1251 return !mCurrentCrop.isEmpty();
1252}
1253
1254bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1255 return mNeedsFiltering || hw->needsFiltering();
1256}
1257
1258void Layer::setVisibleRegion(const Region& visibleRegion) {
1259 // always called from main thread
1260 this->visibleRegion = visibleRegion;
1261}
1262
1263void Layer::setCoveredRegion(const Region& coveredRegion) {
1264 // always called from main thread
1265 this->coveredRegion = coveredRegion;
1266}
1267
1268void Layer::setVisibleNonTransparentRegion(const Region&
1269 setVisibleNonTransparentRegion) {
1270 // always called from main thread
1271 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1272}
1273
1274// ----------------------------------------------------------------------------
1275// transaction
1276// ----------------------------------------------------------------------------
1277
Dan Stoza7dde5992015-05-22 09:51:44 -07001278void Layer::pushPendingState() {
1279 if (!mCurrentState.modified) {
1280 return;
1281 }
1282
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 // If this transaction is waiting on the receipt of a frame, generate a sync
1284 // point and send it to the remote layer.
1285 if (mCurrentState.handle != nullptr) {
1286 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1287 sp<Layer> handleLayer = handle->owner.promote();
1288 if (handleLayer == nullptr) {
1289 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1290 // If we can't promote the layer we are intended to wait on,
1291 // then it is expired or otherwise invalid. Allow this transaction
1292 // to be applied as per normal (no synchronization).
1293 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001294 } else {
1295 auto syncPoint = std::make_shared<SyncPoint>(
1296 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001297 if (handleLayer->addSyncPoint(syncPoint)) {
1298 mRemoteSyncPoints.push_back(std::move(syncPoint));
1299 } else {
1300 // We already missed the frame we're supposed to synchronize
1301 // on, so go ahead and apply the state update
1302 mCurrentState.handle = nullptr;
1303 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001304 }
1305
Dan Stoza7dde5992015-05-22 09:51:44 -07001306 // Wake us up to check if the frame has been received
1307 setTransactionFlags(eTransactionNeeded);
1308 }
1309 mPendingStates.push_back(mCurrentState);
1310}
1311
1312void Layer::popPendingState() {
1313 auto oldFlags = mCurrentState.flags;
1314 mCurrentState = mPendingStates[0];
Dan Stozacac35382016-01-27 12:21:06 -08001315 mCurrentState.flags = (oldFlags & ~mCurrentState.mask) |
Dan Stoza7dde5992015-05-22 09:51:44 -07001316 (mCurrentState.flags & mCurrentState.mask);
1317
1318 mPendingStates.removeAt(0);
1319}
1320
1321bool Layer::applyPendingStates() {
Dan Stoza7dde5992015-05-22 09:51:44 -07001322 bool stateUpdateAvailable = false;
1323 while (!mPendingStates.empty()) {
1324 if (mPendingStates[0].handle != nullptr) {
1325 if (mRemoteSyncPoints.empty()) {
1326 // If we don't have a sync point for this, apply it anyway. It
1327 // will be visually wrong, but it should keep us from getting
1328 // into too much trouble.
1329 ALOGE("[%s] No local sync point found", mName.string());
1330 popPendingState();
1331 stateUpdateAvailable = true;
1332 continue;
1333 }
1334
Dan Stozacac35382016-01-27 12:21:06 -08001335 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1336 mPendingStates[0].frameNumber) {
1337 ALOGE("[%s] Unexpected sync point frame number found",
1338 mName.string());
1339
1340 // Signal our end of the sync point and then dispose of it
1341 mRemoteSyncPoints.front()->setTransactionApplied();
1342 mRemoteSyncPoints.pop_front();
1343 continue;
1344 }
1345
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1347 // Apply the state update
1348 popPendingState();
1349 stateUpdateAvailable = true;
1350
1351 // Signal our end of the sync point and then dispose of it
1352 mRemoteSyncPoints.front()->setTransactionApplied();
1353 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001354 } else {
1355 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001356 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 } else {
1358 popPendingState();
1359 stateUpdateAvailable = true;
1360 }
1361 }
1362
1363 // If we still have pending updates, wake SurfaceFlinger back up and point
1364 // it at this layer so we can process them
1365 if (!mPendingStates.empty()) {
1366 setTransactionFlags(eTransactionNeeded);
1367 mFlinger->setTransactionFlags(eTraversalNeeded);
1368 }
1369
1370 mCurrentState.modified = false;
1371 return stateUpdateAvailable;
1372}
1373
1374void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001375 auto headFrameNumber = getHeadFrameNumber();
1376 Mutex::Autolock lock(mLocalSyncPointMutex);
1377 for (auto& point : mLocalSyncPoints) {
1378 if (headFrameNumber >= point->getFrameNumber()) {
1379 point->setFrameAvailable();
1380 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001381 }
1382}
1383
Mathias Agopian13127d82013-03-05 17:47:11 -08001384uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001385 ATRACE_CALL();
1386
Dan Stoza7dde5992015-05-22 09:51:44 -07001387 pushPendingState();
1388 if (!applyPendingStates()) {
1389 return 0;
1390 }
1391
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001392 const Layer::State& s(getDrawingState());
1393 const Layer::State& c(getCurrentState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001395 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1396 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001397
1398 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001399 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001400 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001401 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001402 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001403 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001404 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001405 " requested={ wh={%4u,%4u} }}\n",
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001406 this, getName().string(), mCurrentTransform, mCurrentScalingMode,
1407 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001408 c.crop.left,
1409 c.crop.top,
1410 c.crop.right,
1411 c.crop.bottom,
1412 c.crop.getWidth(),
1413 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001414 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001416 s.crop.left,
1417 s.crop.top,
1418 s.crop.right,
1419 s.crop.bottom,
1420 s.crop.getWidth(),
1421 s.crop.getHeight(),
1422 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001424 // record the new size, form this point on, when the client request
1425 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001426 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001427 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001429
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001430 if (!isFixedSize()) {
1431
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001432 const bool resizePending = (c.requested.w != c.active.w) ||
1433 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001434
Dan Stoza9e9b0442015-04-22 14:59:08 -07001435 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001436 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001437 // if we have a pending resize, unless we are in fixed-size mode.
1438 // the drawing state will be updated only once we receive a buffer
1439 // with the correct size.
1440 //
1441 // in particular, we want to make sure the clip (which is part
1442 // of the geometry state) is latched together with the size but is
1443 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001444 //
1445 // If a sideband stream is attached, however, we want to skip this
1446 // optimization so that transactions aren't missed when a buffer
1447 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001448
1449 flags |= eDontUpdateGeometryState;
1450 }
1451 }
1452
Mathias Agopian13127d82013-03-05 17:47:11 -08001453 // always set active to requested, unless we're asked not to
1454 // this is used by Layer, which special cases resizes.
1455 if (flags & eDontUpdateGeometryState) {
1456 } else {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001457 Layer::State& editCurrentState(getCurrentState());
1458 editCurrentState.active = c.requested;
Mathias Agopian13127d82013-03-05 17:47:11 -08001459 }
1460
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001461 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001462 // invalidate and recompute the visible regions if needed
1463 flags |= Layer::eVisibleRegion;
1464 }
1465
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001466 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001467 // invalidate and recompute the visible regions if needed
1468 flags |= eVisibleRegion;
1469 this->contentDirty = true;
1470
1471 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001472 const uint8_t type = c.active.transform.getType();
1473 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 (type >= Transform::SCALE));
1475 }
1476
1477 // Commit the transaction
1478 commitTransaction();
1479 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480}
1481
Mathias Agopian13127d82013-03-05 17:47:11 -08001482void Layer::commitTransaction() {
1483 mDrawingState = mCurrentState;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001484}
1485
Mathias Agopian13127d82013-03-05 17:47:11 -08001486uint32_t Layer::getTransactionFlags(uint32_t flags) {
1487 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1488}
1489
1490uint32_t Layer::setTransactionFlags(uint32_t flags) {
1491 return android_atomic_or(flags, &mTransactionFlags);
1492}
1493
1494bool Layer::setPosition(float x, float y) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001495 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001496 return false;
1497 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001498
1499 // We update the requested and active position simultaneously because
1500 // we want to apply the position portion of the transform matrix immediately,
1501 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001502 mCurrentState.requested.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001503 mCurrentState.active.transform.set(x, y);
1504
Dan Stoza7dde5992015-05-22 09:51:44 -07001505 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001506 setTransactionFlags(eTransactionNeeded);
1507 return true;
1508}
1509bool Layer::setLayer(uint32_t z) {
1510 if (mCurrentState.z == z)
1511 return false;
1512 mCurrentState.sequence++;
1513 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001514 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001515 setTransactionFlags(eTransactionNeeded);
1516 return true;
1517}
1518bool Layer::setSize(uint32_t w, uint32_t h) {
1519 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1520 return false;
1521 mCurrentState.requested.w = w;
1522 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001523 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001524 setTransactionFlags(eTransactionNeeded);
1525 return true;
1526}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001527#ifdef USE_HWC2
1528bool Layer::setAlpha(float alpha) {
1529#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001530bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001531#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001532 if (mCurrentState.alpha == alpha)
1533 return false;
1534 mCurrentState.sequence++;
1535 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001536 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001537 setTransactionFlags(eTransactionNeeded);
1538 return true;
1539}
1540bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1541 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001542 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001543 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001544 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001545 setTransactionFlags(eTransactionNeeded);
1546 return true;
1547}
1548bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001549 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001550 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001551 setTransactionFlags(eTransactionNeeded);
1552 return true;
1553}
1554bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1555 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1556 if (mCurrentState.flags == newFlags)
1557 return false;
1558 mCurrentState.sequence++;
1559 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001560 mCurrentState.mask = mask;
1561 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001562 setTransactionFlags(eTransactionNeeded);
1563 return true;
1564}
1565bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001566 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001567 return false;
1568 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001569 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001570 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001571 setTransactionFlags(eTransactionNeeded);
1572 return true;
1573}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001574bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001575 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001576 return false;
1577 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001578 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001579 mCurrentState.modified = true;
1580 setTransactionFlags(eTransactionNeeded);
1581 return true;
1582}
Mathias Agopian13127d82013-03-05 17:47:11 -08001583
1584bool Layer::setLayerStack(uint32_t layerStack) {
1585 if (mCurrentState.layerStack == layerStack)
1586 return false;
1587 mCurrentState.sequence++;
1588 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001589 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001590 setTransactionFlags(eTransactionNeeded);
1591 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001592}
1593
Dan Stoza7dde5992015-05-22 09:51:44 -07001594void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1595 uint64_t frameNumber) {
1596 mCurrentState.handle = handle;
1597 mCurrentState.frameNumber = frameNumber;
1598 // We don't set eTransactionNeeded, because just receiving a deferral
1599 // request without any other state updates shouldn't actually induce a delay
1600 mCurrentState.modified = true;
1601 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001602 mCurrentState.handle = nullptr;
1603 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001604 mCurrentState.modified = false;
1605}
1606
Dan Stozaee44edd2015-03-23 15:50:23 -07001607void Layer::useSurfaceDamage() {
1608 if (mFlinger->mForceFullDamage) {
1609 surfaceDamageRegion = Region::INVALID_REGION;
1610 } else {
1611 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1612 }
1613}
1614
1615void Layer::useEmptyDamage() {
1616 surfaceDamageRegion.clear();
1617}
1618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619// ----------------------------------------------------------------------------
1620// pageflip handling...
1621// ----------------------------------------------------------------------------
1622
Dan Stoza6b9454d2014-11-07 16:00:59 -08001623bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001624 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001625 return true;
1626 }
1627
Dan Stoza6b9454d2014-11-07 16:00:59 -08001628 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001629 if (mQueueItems.empty()) {
1630 return false;
1631 }
1632 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001633 nsecs_t expectedPresent =
1634 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001635
1636 // Ignore timestamps more than a second in the future
1637 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1638 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1639 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1640 expectedPresent);
1641
1642 bool isDue = timestamp < expectedPresent;
1643 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001644}
1645
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001646bool Layer::onPreComposition() {
1647 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001648 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001649}
1650
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001651void Layer::onPostComposition() {
1652 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001653 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001654 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1655
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001656 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001657 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001658 mFrameTracker.setFrameReadyFence(frameReadyFence);
1659 } else {
1660 // There was no fence for this frame, so assume that it was ready
1661 // to be presented at the desired present time.
1662 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1663 }
1664
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001665 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001666#ifdef USE_HWC2
1667 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1668#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001669 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001670#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001671 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001672 mFrameTracker.setActualPresentFence(presentFence);
1673 } else {
1674 // The HWC doesn't support present fences, so use the refresh
1675 // timestamp instead.
1676 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1677 mFrameTracker.setActualPresentTime(presentTime);
1678 }
1679
1680 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001681 mFrameLatencyNeeded = false;
1682 }
1683}
1684
Dan Stoza9e56aa02015-11-02 13:00:03 -08001685#ifdef USE_HWC2
1686void Layer::releasePendingBuffer() {
1687 mSurfaceFlingerConsumer->releasePendingBuffer();
1688}
1689#endif
1690
Mathias Agopianda27af92012-09-13 18:17:13 -07001691bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001692 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001693#ifdef USE_HWC2
1694 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1695 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1696#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001697 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001698 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001699#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001700}
1701
Mathias Agopian4fec8732012-06-29 14:12:52 -07001702Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001704 ATRACE_CALL();
1705
Jesse Hall399184a2014-03-03 15:42:54 -08001706 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1707 // mSidebandStreamChanged was true
1708 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001709 if (mSidebandStream != NULL) {
1710 setTransactionFlags(eTransactionNeeded);
1711 mFlinger->setTransactionFlags(eTraversalNeeded);
1712 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001713 recomputeVisibleRegions = true;
1714
1715 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001716 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001717 }
1718
Mathias Agopian4fec8732012-06-29 14:12:52 -07001719 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001720 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001721
1722 // if we've already called updateTexImage() without going through
1723 // a composition step, we have to skip this layer at this point
1724 // because we cannot call updateTeximage() without a corresponding
1725 // compositionComplete() call.
1726 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001727 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001728 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001729 }
1730
Jamie Gennis351a5132011-09-14 18:23:37 -07001731 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001732 const State& s(getDrawingState());
1733 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001734 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001735
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001736 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001737 Layer::State& front;
1738 Layer::State& current;
1739 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001740 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001741 const char* name;
1742
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001743 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001744 bool& recomputeVisibleRegions, bool stickySet,
1745 const char* name)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001746 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001747 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001748 stickyTransformSet(stickySet),
1749 name(name) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001750 }
1751
1752 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001753 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001754 if (buf == NULL) {
1755 return false;
1756 }
1757
1758 uint32_t bufWidth = buf->getWidth();
1759 uint32_t bufHeight = buf->getHeight();
1760
1761 // check that we received a buffer of the right size
1762 // (Take the buffer's orientation into account)
1763 if (item.mTransform & Transform::ROT_90) {
1764 swap(bufWidth, bufHeight);
1765 }
1766
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001767 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1768 if (front.active != front.requested) {
1769
1770 if (isFixedSize ||
1771 (bufWidth == front.requested.w &&
1772 bufHeight == front.requested.h))
1773 {
1774 // Here we pretend the transaction happened by updating the
1775 // current and drawing states. Drawing state is only accessed
1776 // in this thread, no need to have it locked
1777 front.active = front.requested;
1778
1779 // We also need to update the current state so that
1780 // we don't end-up overwriting the drawing state with
1781 // this stale current state during the next transaction
1782 //
1783 // NOTE: We don't need to hold the transaction lock here
1784 // because State::active is only accessed from this thread.
1785 current.active = front.active;
1786
1787 // recompute visible region
1788 recomputeVisibleRegions = true;
1789 }
1790
1791 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001792 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001793 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001794 " requested={ wh={%4u,%4u} }}\n",
1795 name,
Andy McFadden69052052012-09-14 16:10:11 -07001796 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001797 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001798 front.crop.left,
1799 front.crop.top,
1800 front.crop.right,
1801 front.crop.bottom,
1802 front.crop.getWidth(),
1803 front.crop.getHeight(),
1804 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001805 }
1806
Ruben Brunk1681d952014-06-27 15:51:55 -07001807 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001808 if (front.active.w != bufWidth ||
1809 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001810 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001811 ALOGE("[%s] rejecting buffer: "
1812 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1813 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001814 return true;
1815 }
1816 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001817
1818 // if the transparent region has changed (this test is
1819 // conservative, but that's fine, worst case we're doing
1820 // a bit of extra work), we latch the new one and we
1821 // trigger a visible-region recompute.
1822 if (!front.activeTransparentRegion.isTriviallyEqual(
1823 front.requestedTransparentRegion)) {
1824 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001825
1826 // We also need to update the current state so that
1827 // we don't end-up overwriting the drawing state with
1828 // this stale current state during the next transaction
1829 //
1830 // NOTE: We don't need to hold the transaction lock here
1831 // because State::active is only accessed from this thread.
1832 current.activeTransparentRegion = front.activeTransparentRegion;
1833
1834 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001835 recomputeVisibleRegions = true;
1836 }
1837
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001838 return false;
1839 }
1840 };
1841
Ruben Brunk1681d952014-06-27 15:51:55 -07001842 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrb5d3d262016-03-25 15:08:13 -07001843 getProducerStickyTransform() != 0, mName.string());
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001844
Dan Stozacac35382016-01-27 12:21:06 -08001845
1846 // Check all of our local sync points to ensure that all transactions
1847 // which need to have been applied prior to the frame which is about to
1848 // be latched have signaled
1849
1850 auto headFrameNumber = getHeadFrameNumber();
1851 bool matchingFramesFound = false;
1852 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001853 {
Dan Stozacac35382016-01-27 12:21:06 -08001854 Mutex::Autolock lock(mLocalSyncPointMutex);
1855 for (auto& point : mLocalSyncPoints) {
1856 if (point->getFrameNumber() > headFrameNumber) {
1857 break;
1858 }
1859
1860 matchingFramesFound = true;
1861
1862 if (!point->frameIsAvailable()) {
1863 // We haven't notified the remote layer that the frame for
1864 // this point is available yet. Notify it now, and then
1865 // abort this attempt to latch.
1866 point->setFrameAvailable();
1867 allTransactionsApplied = false;
1868 break;
1869 }
1870
1871 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001872 }
1873 }
1874
Dan Stozacac35382016-01-27 12:21:06 -08001875 if (matchingFramesFound && !allTransactionsApplied) {
1876 mFlinger->signalLayerUpdate();
1877 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001878 }
1879
Pablo Ceballos06312182015-10-07 16:32:12 -07001880 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1881 // of the buffer queue isn't modified when the buffer queue is returning
1882 // BufferItem's that weren't actually queued. This can happen in single
1883 // buffer mode.
1884 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001885 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001886 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001887 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001888 if (updateResult == BufferQueue::PRESENT_LATER) {
1889 // Producer doesn't want buffer to be displayed yet. Signal a
1890 // layer update so we check again at the next opportunity.
1891 mFlinger->signalLayerUpdate();
1892 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001893 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1894 // If the buffer has been rejected, remove it from the shadow queue
1895 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001896 if (queuedBuffer) {
1897 Mutex::Autolock lock(mQueueItemLock);
1898 mQueueItems.removeAt(0);
1899 android_atomic_dec(&mQueuedFrames);
1900 }
Dan Stozaecc50402015-04-28 14:42:06 -07001901 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001902 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1903 // This can occur if something goes wrong when trying to create the
1904 // EGLImage for this buffer. If this happens, the buffer has already
1905 // been released, so we need to clean up the queue and bug out
1906 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001907 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001908 Mutex::Autolock lock(mQueueItemLock);
1909 mQueueItems.clear();
1910 android_atomic_and(0, &mQueuedFrames);
1911 }
1912
1913 // Once we have hit this state, the shadow queue may no longer
1914 // correctly reflect the incoming BufferQueue's contents, so even if
1915 // updateTexImage starts working, the only safe course of action is
1916 // to continue to ignore updates.
1917 mUpdateTexImageFailed = true;
1918
1919 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001920 }
1921
Pablo Ceballos06312182015-10-07 16:32:12 -07001922 if (queuedBuffer) {
1923 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001924 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1925
Dan Stoza6b9454d2014-11-07 16:00:59 -08001926 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001927
1928 // Remove any stale buffers that have been dropped during
1929 // updateTexImage
1930 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1931 mQueueItems.removeAt(0);
1932 android_atomic_dec(&mQueuedFrames);
1933 }
1934
Dan Stoza6b9454d2014-11-07 16:00:59 -08001935 mQueueItems.removeAt(0);
1936 }
1937
Dan Stozaecc50402015-04-28 14:42:06 -07001938
Andy McFadden1585c4d2013-06-28 13:52:40 -07001939 // Decrement the queued-frames count. Signal another event if we
1940 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001941 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001942 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001943 mFlinger->signalLayerUpdate();
1944 }
1945
1946 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001947 // something happened!
1948 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001949 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001950 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001951
Jamie Gennis351a5132011-09-14 18:23:37 -07001952 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001953 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001954 if (mActiveBuffer == NULL) {
1955 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001956 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001957 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001958
Mathias Agopian4824d402012-06-04 18:16:30 -07001959 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001960 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001961 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001962 // the first time we receive a buffer, we need to trigger a
1963 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001964 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001965 }
1966
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001967 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1968 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1969 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07001970 if ((crop != mCurrentCrop) ||
1971 (transform != mCurrentTransform) ||
1972 (scalingMode != mCurrentScalingMode))
1973 {
1974 mCurrentCrop = crop;
1975 mCurrentTransform = transform;
1976 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07001977 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001978 }
1979
1980 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07001981 uint32_t bufWidth = mActiveBuffer->getWidth();
1982 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07001983 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
1984 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07001985 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001986 }
1987 }
1988
1989 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08001990 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07001991 recomputeVisibleRegions = true;
1992 }
1993
Dan Stozacac35382016-01-27 12:21:06 -08001994 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1995
1996 // Remove any sync points corresponding to the buffer which was just
1997 // latched
1998 {
1999 Mutex::Autolock lock(mLocalSyncPointMutex);
2000 auto point = mLocalSyncPoints.begin();
2001 while (point != mLocalSyncPoints.end()) {
2002 if (!(*point)->frameIsAvailable() ||
2003 !(*point)->transactionIsApplied()) {
2004 // This sync point must have been added since we started
2005 // latching. Don't drop it yet.
2006 ++point;
2007 continue;
2008 }
2009
2010 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2011 point = mLocalSyncPoints.erase(point);
2012 } else {
2013 ++point;
2014 }
2015 }
2016 }
2017
Mathias Agopian4fec8732012-06-29 14:12:52 -07002018 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002019 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002020
2021 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002022 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002023 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002024 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025}
2026
Mathias Agopiana67932f2011-04-20 14:20:59 -07002027uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002028{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002029 // TODO: should we do something special if mSecure is set?
2030 if (mProtectedByApp) {
2031 // need a hardware-protected path to external video sink
2032 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002033 }
Riley Andrews03414a12014-07-01 14:22:59 -07002034 if (mPotentialCursor) {
2035 usage |= GraphicBuffer::USAGE_CURSOR;
2036 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002037 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002038 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002039}
2040
Mathias Agopian84300952012-11-21 16:02:13 -08002041void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002042 uint32_t orientation = 0;
2043 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002044 // The transform hint is used to improve performance, but we can
2045 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002046 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002047 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002048 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002049 if (orientation & Transform::ROT_INVALID) {
2050 orientation = 0;
2051 }
2052 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002053 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002054}
2055
Mathias Agopian13127d82013-03-05 17:47:11 -08002056// ----------------------------------------------------------------------------
2057// debugging
2058// ----------------------------------------------------------------------------
2059
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002060void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002061{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002062 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002063
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002064 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002065 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002066 "+ %s %p (%s)\n",
2067 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002068 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002069
Mathias Agopian2ca79392013-04-02 18:30:32 -07002070 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002071 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002072 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002073 sp<Client> client(mClientRef.promote());
2074
Mathias Agopian74d211a2013-04-22 16:55:35 +02002075 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002076 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2077 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002078 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002079#ifdef USE_HWC2
2080 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2081#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002082 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002083#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002084 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002085 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 -07002086 s.crop.left, s.crop.top,
2087 s.crop.right, s.crop.bottom,
2088 s.finalCrop.left, s.finalCrop.top,
2089 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002090 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002091 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002092 s.active.transform[0][0], s.active.transform[0][1],
2093 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002094 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002095
2096 sp<const GraphicBuffer> buf0(mActiveBuffer);
2097 uint32_t w0=0, h0=0, s0=0, f0=0;
2098 if (buf0 != 0) {
2099 w0 = buf0->getWidth();
2100 h0 = buf0->getHeight();
2101 s0 = buf0->getStride();
2102 f0 = buf0->format;
2103 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002104 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002105 " "
2106 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2107 " queued-frames=%d, mRefreshPending=%d\n",
2108 mFormat, w0, h0, s0,f0,
2109 mQueuedFrames, mRefreshPending);
2110
Mathias Agopian13127d82013-03-05 17:47:11 -08002111 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002112 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002113 }
2114}
2115
Svetoslavd85084b2014-03-20 10:28:31 -07002116void Layer::dumpFrameStats(String8& result) const {
2117 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002118}
2119
Svetoslavd85084b2014-03-20 10:28:31 -07002120void Layer::clearFrameStats() {
2121 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002122}
2123
Jamie Gennis6547ff42013-07-16 20:12:42 -07002124void Layer::logFrameStats() {
2125 mFrameTracker.logAndResetStats(mName);
2126}
2127
Svetoslavd85084b2014-03-20 10:28:31 -07002128void Layer::getFrameStats(FrameStats* outStats) const {
2129 mFrameTracker.getStats(outStats);
2130}
2131
Pablo Ceballos40845df2016-01-25 17:41:15 -08002132void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2133 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2134 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2135 *outName = mName;
2136 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2137
2138#ifdef USE_HWC2
2139 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2140 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2141 HWC2::Composition::Client : true;
2142#else
2143 *outIsGlesComposition = mIsGlesComposition;
2144#endif
2145 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2146 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2147 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2148}
Mathias Agopian13127d82013-03-05 17:47:11 -08002149// ---------------------------------------------------------------------------
2150
2151Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2152 const sp<Layer>& layer)
2153 : mFlinger(flinger), mLayer(layer) {
2154}
2155
2156Layer::LayerCleaner::~LayerCleaner() {
2157 // destroy client resources
2158 mFlinger->onLayerDestroyed(mLayer);
2159}
2160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002162}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002163
2164#if defined(__gl_h_)
2165#error "don't include gl/gl.h in this file"
2166#endif
2167
2168#if defined(__gl2_h_)
2169#error "don't include gl2/gl2.h in this file"
2170#endif