blob: 2e25233270a6d607b57bf4a3ee4772c5c4099ce8 [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 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700438 * the code below applies the primary display's inverse transform to the
439 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700441 uint32_t invTransformOrient =
442 DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // calculate the inverse transform
444 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
445 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
446 NATIVE_WINDOW_TRANSFORM_FLIP_H;
447 // If the transform has been rotated the axis of flip has been swapped
448 // so we need to swap which flip operations we are performing
Michael Lentine7b902582014-08-19 18:14:06 -0700449 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
450 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 if (is_h_flipped != is_v_flipped) {
Michael Lentine7b902582014-08-19 18:14:06 -0700452 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
453 NATIVE_WINDOW_TRANSFORM_FLIP_H;
454 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800455 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456 // and apply to the current transform
457 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800458 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459
460 int winWidth = s.active.w;
461 int winHeight = s.active.h;
462 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
463 // If the activeCrop has been rotate the ends are rotated but not
464 // the space itself so when transforming ends back we can't rely on
465 // a modification of the axes of rotation. To account for this we
466 // need to reorient the inverse rotation in terms of the current
467 // axes of rotation.
468 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
469 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
470 if (is_h_flipped == is_v_flipped) {
471 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
472 NATIVE_WINDOW_TRANSFORM_FLIP_H;
473 }
474 winWidth = s.active.h;
475 winHeight = s.active.w;
476 }
477 const Rect winCrop = activeCrop.transform(
478 invTransform, s.active.w, s.active.h);
479
480 // below, crop is intersected with winCrop expressed in crop's coordinate space
481 float xScale = crop.getWidth() / float(winWidth);
482 float yScale = crop.getHeight() / float(winHeight);
483
484 float insetL = winCrop.left * xScale;
485 float insetT = winCrop.top * yScale;
486 float insetR = (winWidth - winCrop.right ) * xScale;
487 float insetB = (winHeight - winCrop.bottom) * yScale;
488
489 crop.left += insetL;
490 crop.top += insetT;
491 crop.right -= insetR;
492 crop.bottom -= insetB;
493
Mathias Agopian13127d82013-03-05 17:47:11 -0800494 return crop;
495}
496
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497#ifdef USE_HWC2
498void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
499#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700500void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700501 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700502 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700504{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505#ifdef USE_HWC2
506 const auto hwcId = displayDevice->getHwcDisplayId();
507 auto& hwcInfo = mHwcLayers[hwcId];
508#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800509 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700511
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700512 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800513#ifdef USE_HWC2
514 hwcInfo.forceClientComposition = false;
515
516 if (isSecure() && !displayDevice->isSecure()) {
517 hwcInfo.forceClientComposition = true;
518 }
519
520 auto& hwcLayer = hwcInfo.layer;
521#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700522 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700523
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700524 if (isSecure() && !hw->isSecure()) {
525 layer.setSkip(true);
526 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800527#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700528
Mathias Agopian13127d82013-03-05 17:47:11 -0800529 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700530 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531#ifdef USE_HWC2
532 if (!isOpaque(s) || s.alpha != 1.0f) {
533 auto blendMode = mPremultipliedAlpha ?
534 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
535 auto error = hwcLayer->setBlendMode(blendMode);
536 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
537 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
538 to_string(error).c_str(), static_cast<int32_t>(error));
539 }
540#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800541 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800542 layer.setBlending(mPremultipliedAlpha ?
543 HWC_BLENDING_PREMULT :
544 HWC_BLENDING_COVERAGE);
545 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800547
548 // apply the layer's transform, followed by the display's global transform
549 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700550 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700551 if (!s.crop.isEmpty()) {
552 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800553 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000559 activeCrop.clear();
560 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800561 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800562 // This needs to be here as transform.transform(Rect) computes the
563 // transformed rect and then takes the bounding box of the result before
564 // returning. This means
565 // transform.inverse().transform(transform.transform(Rect)) != Rect
566 // in which case we need to make sure the final rect is clipped to the
567 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000568 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
569 activeCrop.clear();
570 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700571 // mark regions outside the crop as transparent
572 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
573 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
574 s.active.w, s.active.h));
575 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
576 activeCrop.left, activeCrop.bottom));
577 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
578 s.active.w, activeCrop.bottom));
579 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800580 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700581 if (!s.finalCrop.isEmpty()) {
582 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000583 frame.clear();
584 }
585 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000587 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
588 frame.clear();
589 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590 const Transform& tr(displayDevice->getTransform());
591 Rect transformedFrame = tr.transform(frame);
592 auto error = hwcLayer->setDisplayFrame(transformedFrame);
593 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
594 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
595 transformedFrame.top, transformedFrame.right,
596 transformedFrame.bottom, to_string(error).c_str(),
597 static_cast<int32_t>(error));
598
599 FloatRect sourceCrop = computeCrop(displayDevice);
600 error = hwcLayer->setSourceCrop(sourceCrop);
601 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
602 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
603 sourceCrop.left, sourceCrop.top, sourceCrop.right,
604 sourceCrop.bottom, to_string(error).c_str(),
605 static_cast<int32_t>(error));
606
607 error = hwcLayer->setPlaneAlpha(s.alpha);
608 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
609 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
610 static_cast<int32_t>(error));
611
612 error = hwcLayer->setZOrder(s.z);
613 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
614 mName.string(), s.z, to_string(error).c_str(),
615 static_cast<int32_t>(error));
616#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000617 if (!frame.intersect(hw->getViewport(), &frame)) {
618 frame.clear();
619 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800620 const Transform& tr(hw->getTransform());
621 layer.setFrame(tr.transform(frame));
622 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800623 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800625
Mathias Agopian29a367b2011-07-12 14:51:45 -0700626 /*
627 * Transformations are applied in this order:
628 * 1) buffer orientation/flip/mirror
629 * 2) state transformation (window manager)
630 * 3) layer orientation (screen orientation)
631 * (NOTE: the matrices are multiplied in reverse order)
632 */
633
634 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800635 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636
637 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
638 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700639 * the code below applies the primary display's inverse transform to the
640 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700641 */
Pablo Ceballos021623b2016-04-15 17:31:51 -0700642 uint32_t invTransform =
643 DisplayDevice::getPrimaryDisplayOrientationTransform();
644
Michael Lentine14409632014-08-19 11:27:30 -0700645 uint32_t t_orientation = transform.getOrientation();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700646 // calculate the inverse transform
647 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
648 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
649 NATIVE_WINDOW_TRANSFORM_FLIP_H;
Michael Lentine14409632014-08-19 11:27:30 -0700650 // If the transform has been rotated the axis of flip has been swapped
651 // so we need to swap which flip operations we are performing
652 bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
653 bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
654 if (is_h_flipped != is_v_flipped) {
655 t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
656 NATIVE_WINDOW_TRANSFORM_FLIP_H;
657 }
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700658 }
659 // and apply to the current transform
Michael Lentine14409632014-08-19 11:27:30 -0700660 transform = Transform(t_orientation) * Transform(invTransform);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700661 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700662
663 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800664 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665#ifdef USE_HWC2
666 if (orientation & Transform::ROT_INVALID) {
667 // we can only handle simple transformation
668 hwcInfo.forceClientComposition = true;
669 } else {
670 auto transform = static_cast<HWC2::Transform>(orientation);
671 auto error = hwcLayer->setTransform(transform);
672 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: "
673 "%s (%d)", mName.string(), to_string(transform).c_str(),
674 to_string(error).c_str(), static_cast<int32_t>(error));
675 }
676#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800677 if (orientation & Transform::ROT_INVALID) {
678 // we can only handle simple transformation
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700679 layer.setSkip(true);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700680 } else {
Mathias Agopian13127d82013-03-05 17:47:11 -0800681 layer.setTransform(orientation);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700682 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700684}
685
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686#ifdef USE_HWC2
687void Layer::forceClientComposition(int32_t hwcId) {
688 if (mHwcLayers.count(hwcId) == 0) {
689 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
690 return;
691 }
692
693 mHwcLayers[hwcId].forceClientComposition = true;
694}
695#endif
696
697#ifdef USE_HWC2
698void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
699 // Apply this display's projection's viewport to the visible region
700 // before giving it to the HWC HAL.
701 const Transform& tr = displayDevice->getTransform();
702 const auto& viewport = displayDevice->getViewport();
703 Region visible = tr.transform(visibleRegion.intersect(viewport));
704 auto hwcId = displayDevice->getHwcDisplayId();
705 auto& hwcLayer = mHwcLayers[hwcId].layer;
706 auto error = hwcLayer->setVisibleRegion(visible);
707 if (error != HWC2::Error::None) {
708 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
709 to_string(error).c_str(), static_cast<int32_t>(error));
710 visible.dump(LOG_TAG);
711 }
712
713 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
714 if (error != HWC2::Error::None) {
715 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
716 to_string(error).c_str(), static_cast<int32_t>(error));
717 surfaceDamageRegion.dump(LOG_TAG);
718 }
719
720 auto compositionType = HWC2::Composition::Invalid;
721 if (mSidebandStream.get()) {
722 compositionType = HWC2::Composition::Sideband;
723 auto error = hwcLayer->setSidebandStream(mSidebandStream->handle());
724 if (error != HWC2::Error::None) {
725 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)",
726 mName.string(), mSidebandStream->handle(),
727 to_string(error).c_str(), static_cast<int32_t>(error));
728 return;
729 }
730 } else {
731 if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr) {
732 compositionType = HWC2::Composition::Client;
733 auto error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE);
734 if (error != HWC2::Error::None) {
735 ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(),
736 to_string(error).c_str(), static_cast<int32_t>(error));
737 return;
738 }
739 } else {
740 if (mPotentialCursor) {
741 compositionType = HWC2::Composition::Cursor;
742 }
743 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
744 auto error = hwcLayer->setBuffer(mActiveBuffer->handle,
745 acquireFence);
746 if (error != HWC2::Error::None) {
747 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
748 mActiveBuffer->handle, to_string(error).c_str(),
749 static_cast<int32_t>(error));
750 return;
751 }
752 // If it's not a cursor, default to device composition
753 }
754 }
755
756 if (mHwcLayers[hwcId].forceClientComposition) {
757 ALOGV("[%s] Forcing Client composition", mName.string());
758 setCompositionType(hwcId, HWC2::Composition::Client);
759 } else if (compositionType != HWC2::Composition::Invalid) {
760 ALOGV("[%s] Requesting %s composition", mName.string(),
761 to_string(compositionType).c_str());
762 setCompositionType(hwcId, compositionType);
763 } else {
764 ALOGV("[%s] Requesting Device composition", mName.string());
765 setCompositionType(hwcId, HWC2::Composition::Device);
766 }
767}
768#else
Mathias Agopian42977342012-08-05 00:40:46 -0700769void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700770 HWComposer::HWCLayerInterface& layer) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800771 // we have to set the visible region on every frame because
772 // we currently free it during onLayerDisplayed(), which is called
773 // after HWComposer::commit() -- every frame.
774 // Apply this display's projection's viewport to the visible region
775 // before giving it to the HWC HAL.
776 const Transform& tr = hw->getTransform();
777 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
778 layer.setVisibleRegionScreen(visible);
Dan Stozadb4850c2015-06-25 16:10:18 -0700779 layer.setSurfaceDamage(surfaceDamageRegion);
Pablo Ceballos40845df2016-01-25 17:41:15 -0800780 mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER);
Dan Stozaee44edd2015-03-23 15:50:23 -0700781
Jesse Hall399184a2014-03-03 15:42:54 -0800782 if (mSidebandStream.get()) {
783 layer.setSidebandStream(mSidebandStream);
784 } else {
785 // NOTE: buffer can be NULL if the client never drew into this
786 // layer yet, or if we ran out of memory
787 layer.setBuffer(mActiveBuffer);
788 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700789}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790#endif
Jesse Halldc5b4852012-06-29 15:21:18 -0700791
Dan Stoza9e56aa02015-11-02 13:00:03 -0800792#ifdef USE_HWC2
793void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
794 auto hwcId = displayDevice->getHwcDisplayId();
795 if (mHwcLayers.count(hwcId) == 0 ||
796 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
797 return;
798 }
799
800 // This gives us only the "orientation" component of the transform
801 const State& s(getCurrentState());
802
803 // Apply the layer's transform, followed by the display's global transform
804 // Here we're guaranteed that the layer's transform preserves rects
805 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700806 if (!s.crop.isEmpty()) {
807 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800808 }
809 // Subtract the transparent region and snap to the bounds
810 Rect bounds = reduce(win, s.activeTransparentRegion);
Dan Stozabaf416d2016-03-10 11:57:08 -0800811 Rect frame(s.active.transform.transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800812 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700813 if (!s.finalCrop.isEmpty()) {
814 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000815 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800816 auto& displayTransform(displayDevice->getTransform());
817 auto position = displayTransform.transform(frame);
818
819 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left,
820 position.top);
821 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position "
822 "to (%d, %d): %s (%d)", mName.string(), position.left,
823 position.top, to_string(error).c_str(),
824 static_cast<int32_t>(error));
825}
826#else
Dan Stozac7014012014-02-14 15:03:43 -0800827void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700828 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700829 int fenceFd = -1;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700830
831 // TODO: there is a possible optimization here: we only need to set the
832 // acquire fence the first time a new buffer is acquired on EACH display.
833
Riley Andrews03414a12014-07-01 14:22:59 -0700834 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800835 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800836 if (fence->isValid()) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700837 fenceFd = fence->dup();
Jesse Halldc5b4852012-06-29 15:21:18 -0700838 if (fenceFd == -1) {
839 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
840 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700841 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700842 }
Jesse Hallc5c5a142012-07-02 16:49:28 -0700843 layer.setAcquireFenceFd(fenceFd);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700844}
845
Riley Andrews03414a12014-07-01 14:22:59 -0700846Rect Layer::getPosition(
847 const sp<const DisplayDevice>& hw)
848{
849 // this gives us only the "orientation" component of the transform
850 const State& s(getCurrentState());
851
852 // apply the layer's transform, followed by the display's global transform
853 // here we're guaranteed that the layer's transform preserves rects
854 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700855 if (!s.crop.isEmpty()) {
856 win.intersect(s.crop, &win);
Riley Andrews03414a12014-07-01 14:22:59 -0700857 }
858 // subtract the transparent region and snap to the bounds
859 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800860 Rect frame(s.active.transform.transform(bounds));
Riley Andrews03414a12014-07-01 14:22:59 -0700861 frame.intersect(hw->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700862 if (!s.finalCrop.isEmpty()) {
863 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000864 }
Riley Andrews03414a12014-07-01 14:22:59 -0700865 const Transform& tr(hw->getTransform());
866 return Rect(tr.transform(frame));
867}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800868#endif
Riley Andrews03414a12014-07-01 14:22:59 -0700869
Mathias Agopian13127d82013-03-05 17:47:11 -0800870// ---------------------------------------------------------------------------
871// drawing...
872// ---------------------------------------------------------------------------
873
874void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
Dan Stozac7014012014-02-14 15:03:43 -0800875 onDraw(hw, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800876}
877
Dan Stozac7014012014-02-14 15:03:43 -0800878void Layer::draw(const sp<const DisplayDevice>& hw,
879 bool useIdentityTransform) const {
880 onDraw(hw, Region(hw->bounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800881}
882
Dan Stozac7014012014-02-14 15:03:43 -0800883void Layer::draw(const sp<const DisplayDevice>& hw) const {
884 onDraw(hw, Region(hw->bounds()), false);
885}
886
887void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
888 bool useIdentityTransform) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800890 ATRACE_CALL();
891
Mathias Agopiana67932f2011-04-20 14:20:59 -0700892 if (CC_UNLIKELY(mActiveBuffer == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700894 // in fact never been drawn into. This happens frequently with
895 // SurfaceView because the WindowManager can't know when the client
896 // has drawn the first time.
897
898 // If there is nothing under us, we paint the screen in black, otherwise
899 // we just skip this update.
900
901 // figure out if there is something below us
902 Region under;
Mathias Agopianf7ae69d2011-08-23 12:34:29 -0700903 const SurfaceFlinger::LayerVector& drawingLayers(
904 mFlinger->mDrawingState.layersSortedByZ);
Mathias Agopian179169e2010-05-06 20:21:45 -0700905 const size_t count = drawingLayers.size();
906 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 const sp<Layer>& layer(drawingLayers[i]);
908 if (layer.get() == static_cast<Layer const*>(this))
Mathias Agopian179169e2010-05-06 20:21:45 -0700909 break;
Mathias Agopian42977342012-08-05 00:40:46 -0700910 under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
Mathias Agopian179169e2010-05-06 20:21:45 -0700911 }
912 // if not everything below us is covered, we plug the holes!
913 Region holes(clip.subtract(under));
914 if (!holes.isEmpty()) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700915 clearWithOpenGL(hw, holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 return;
918 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700919
Andy McFadden97eba892012-12-11 15:21:45 -0800920 // Bind the current buffer to the GL texture, and wait for it to be
921 // ready for us to draw into.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800922 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
923 if (err != NO_ERROR) {
Andy McFadden97eba892012-12-11 15:21:45 -0800924 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
Jesse Halldc5b4852012-06-29 15:21:18 -0700925 // Go ahead and draw the buffer anyway; no matter what we do the screen
926 // is probably going to have something visibly wrong.
927 }
928
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700929 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
930
Mathias Agopian875d8e12013-06-07 15:35:48 -0700931 RenderEngine& engine(mFlinger->getRenderEngine());
932
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700933 if (!blackOutLayer) {
Jamie Genniscbb1a952012-05-08 17:05:52 -0700934 // TODO: we could be more subtle with isFixedSize()
Mathias Agopianeba8c682012-09-19 23:14:45 -0700935 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
Jamie Genniscbb1a952012-05-08 17:05:52 -0700936
937 // Query the texture matrix given our current filtering mode.
938 float textureMatrix[16];
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800939 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
940 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
Jamie Genniscbb1a952012-05-08 17:05:52 -0700941
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700942 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
943
944 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700945 * the code below applies the primary display's inverse transform to
946 * the texture transform
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700947 */
948
949 // create a 4x4 transform matrix from the display transform flags
950 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
951 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
952 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
953
954 mat4 tr;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700955 uint32_t transform =
956 DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700957 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
958 tr = tr * rot90;
959 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
960 tr = tr * flipH;
961 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
962 tr = tr * flipV;
963
964 // calculate the inverse
965 tr = inverse(tr);
966
967 // and finally apply it to the original texture matrix
968 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
969 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
970 }
971
Jamie Genniscbb1a952012-05-08 17:05:52 -0700972 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700973 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
974 mTexture.setFiltering(useFiltering);
975 mTexture.setMatrix(textureMatrix);
976
977 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700978 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700979 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700980 }
Dan Stozac7014012014-02-14 15:03:43 -0800981 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700982 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983}
984
Mathias Agopian13127d82013-03-05 17:47:11 -0800985
Dan Stozac7014012014-02-14 15:03:43 -0800986void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
987 const Region& /* clip */, float red, float green, float blue,
988 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800989{
Mathias Agopian19733a32013-08-28 18:13:56 -0700990 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800991 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700992 engine.setupFillWithColor(red, green, blue, alpha);
993 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800994}
995
996void Layer::clearWithOpenGL(
997 const sp<const DisplayDevice>& hw, const Region& clip) const {
998 clearWithOpenGL(hw, clip, 0,0,0,0);
999}
1000
Dan Stozac7014012014-02-14 15:03:43 -08001001void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
1002 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001003 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001004
Dan Stozac7014012014-02-14 15:03:43 -08001005 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001006
Mathias Agopian13127d82013-03-05 17:47:11 -08001007 /*
1008 * NOTE: the way we compute the texture coordinates here produces
1009 * different results than when we take the HWC path -- in the later case
1010 * the "source crop" is rounded to texel boundaries.
1011 * This can produce significantly different results when the texture
1012 * is scaled by a large amount.
1013 *
1014 * The GL code below is more logical (imho), and the difference with
1015 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001016 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001017 * GL composition when a buffer scaling is applied (maybe with some
1018 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1019 * like more of a hack.
1020 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001021 Rect win(computeBounds());
1022
Robert Carrb5d3d262016-03-25 15:08:13 -07001023 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001024 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001025 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001026 win.clear();
1027 }
1028 win = s.active.transform.inverse().transform(win);
1029 if (!win.intersect(computeBounds(), &win)) {
1030 win.clear();
1031 }
1032 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001033
Mathias Agopian3f844832013-08-07 21:24:32 -07001034 float left = float(win.left) / float(s.active.w);
1035 float top = float(win.top) / float(s.active.h);
1036 float right = float(win.right) / float(s.active.w);
1037 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001038
Mathias Agopian875d8e12013-06-07 15:35:48 -07001039 // TODO: we probably want to generate the texture coords with the mesh
1040 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001041 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1042 texCoords[0] = vec2(left, 1.0f - top);
1043 texCoords[1] = vec2(left, 1.0f - bottom);
1044 texCoords[2] = vec2(right, 1.0f - bottom);
1045 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001046
Mathias Agopian875d8e12013-06-07 15:35:48 -07001047 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001048 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001049 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001050 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001051}
1052
Dan Stoza9e56aa02015-11-02 13:00:03 -08001053#ifdef USE_HWC2
1054void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1055 bool callIntoHwc) {
1056 if (mHwcLayers.count(hwcId) == 0) {
1057 ALOGE("setCompositionType called without a valid HWC layer");
1058 return;
1059 }
1060 auto& hwcInfo = mHwcLayers[hwcId];
1061 auto& hwcLayer = hwcInfo.layer;
1062 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1063 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1064 if (hwcInfo.compositionType != type) {
1065 ALOGV(" actually setting");
1066 hwcInfo.compositionType = type;
1067 if (callIntoHwc) {
1068 auto error = hwcLayer->setCompositionType(type);
1069 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1070 "composition type %s: %s (%d)", mName.string(),
1071 to_string(type).c_str(), to_string(error).c_str(),
1072 static_cast<int32_t>(error));
1073 }
1074 }
1075}
1076
1077HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1078 if (mHwcLayers.count(hwcId) == 0) {
1079 ALOGE("getCompositionType called without a valid HWC layer");
1080 return HWC2::Composition::Invalid;
1081 }
1082 return mHwcLayers.at(hwcId).compositionType;
1083}
1084
1085void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1086 if (mHwcLayers.count(hwcId) == 0) {
1087 ALOGE("setClearClientTarget called without a valid HWC layer");
1088 return;
1089 }
1090 mHwcLayers[hwcId].clearClientTarget = clear;
1091}
1092
1093bool Layer::getClearClientTarget(int32_t hwcId) const {
1094 if (mHwcLayers.count(hwcId) == 0) {
1095 ALOGE("getClearClientTarget called without a valid HWC layer");
1096 return false;
1097 }
1098 return mHwcLayers.at(hwcId).clearClientTarget;
1099}
1100#endif
1101
Ruben Brunk1681d952014-06-27 15:51:55 -07001102uint32_t Layer::getProducerStickyTransform() const {
1103 int producerStickyTransform = 0;
1104 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1105 if (ret != OK) {
1106 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1107 strerror(-ret), ret);
1108 return 0;
1109 }
1110 return static_cast<uint32_t>(producerStickyTransform);
1111}
1112
Dan Stozacac35382016-01-27 12:21:06 -08001113uint64_t Layer::getHeadFrameNumber() const {
1114 Mutex::Autolock lock(mQueueItemLock);
1115 if (!mQueueItems.empty()) {
1116 return mQueueItems[0].mFrameNumber;
1117 } else {
1118 return mCurrentFrameNumber;
1119 }
1120}
1121
1122bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1123 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1124 // Don't bother with a SyncPoint, since we've already latched the
1125 // relevant frame
1126 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001127 }
1128
Dan Stozacac35382016-01-27 12:21:06 -08001129 Mutex::Autolock lock(mLocalSyncPointMutex);
1130 mLocalSyncPoints.push_back(point);
1131 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001132}
1133
Mathias Agopian13127d82013-03-05 17:47:11 -08001134void Layer::setFiltering(bool filtering) {
1135 mFiltering = filtering;
1136}
1137
1138bool Layer::getFiltering() const {
1139 return mFiltering;
1140}
1141
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001142// As documented in libhardware header, formats in the range
1143// 0x100 - 0x1FF are specific to the HAL implementation, and
1144// are known to have no alpha channel
1145// TODO: move definition for device-specific range into
1146// hardware.h, instead of using hard-coded values here.
1147#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1148
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001149bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001150 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1151 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001152 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001153 switch (format) {
1154 case HAL_PIXEL_FORMAT_RGBA_8888:
1155 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001156 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001157 }
1158 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001159 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001160}
1161
Mathias Agopian13127d82013-03-05 17:47:11 -08001162// ----------------------------------------------------------------------------
1163// local state
1164// ----------------------------------------------------------------------------
1165
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001166static void boundPoint(vec2* point, const Rect& crop) {
1167 if (point->x < crop.left) {
1168 point->x = crop.left;
1169 }
1170 if (point->x > crop.right) {
1171 point->x = crop.right;
1172 }
1173 if (point->y < crop.top) {
1174 point->y = crop.top;
1175 }
1176 if (point->y > crop.bottom) {
1177 point->y = crop.bottom;
1178 }
1179}
1180
Dan Stozac7014012014-02-14 15:03:43 -08001181void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1182 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001183{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001184 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001185 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001186 const uint32_t hw_h = hw->getHeight();
1187 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001188 if (!s.crop.isEmpty()) {
1189 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001190 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001191 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001192 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001193
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001194 vec2 lt = vec2(win.left, win.top);
1195 vec2 lb = vec2(win.left, win.bottom);
1196 vec2 rb = vec2(win.right, win.bottom);
1197 vec2 rt = vec2(win.right, win.top);
1198
1199 if (!useIdentityTransform) {
1200 lt = s.active.transform.transform(lt);
1201 lb = s.active.transform.transform(lb);
1202 rb = s.active.transform.transform(rb);
1203 rt = s.active.transform.transform(rt);
1204 }
1205
Robert Carrb5d3d262016-03-25 15:08:13 -07001206 if (!s.finalCrop.isEmpty()) {
1207 boundPoint(&lt, s.finalCrop);
1208 boundPoint(&lb, s.finalCrop);
1209 boundPoint(&rb, s.finalCrop);
1210 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001211 }
1212
Mathias Agopianff2ed702013-09-01 21:36:12 -07001213 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001214 position[0] = tr.transform(lt);
1215 position[1] = tr.transform(lb);
1216 position[2] = tr.transform(rb);
1217 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001218 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001219 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 }
1221}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001222
Andy McFadden4125a4f2014-01-29 17:17:11 -08001223bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001224{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225 // if we don't have a buffer yet, we're translucent regardless of the
1226 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001227 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001229 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001230
1231 // if the layer has the opaque flag, then we're always opaque,
1232 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001233 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001234}
1235
Dan Stoza23116082015-06-18 14:58:39 -07001236bool Layer::isSecure() const
1237{
1238 const Layer::State& s(mDrawingState);
1239 return (s.flags & layer_state_t::eLayerSecure);
1240}
1241
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001242bool Layer::isProtected() const
1243{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001244 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001245 return (activeBuffer != 0) &&
1246 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1247}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001248
Mathias Agopian13127d82013-03-05 17:47:11 -08001249bool Layer::isFixedSize() const {
1250 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1251}
1252
1253bool Layer::isCropped() const {
1254 return !mCurrentCrop.isEmpty();
1255}
1256
1257bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1258 return mNeedsFiltering || hw->needsFiltering();
1259}
1260
1261void Layer::setVisibleRegion(const Region& visibleRegion) {
1262 // always called from main thread
1263 this->visibleRegion = visibleRegion;
1264}
1265
1266void Layer::setCoveredRegion(const Region& coveredRegion) {
1267 // always called from main thread
1268 this->coveredRegion = coveredRegion;
1269}
1270
1271void Layer::setVisibleNonTransparentRegion(const Region&
1272 setVisibleNonTransparentRegion) {
1273 // always called from main thread
1274 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1275}
1276
1277// ----------------------------------------------------------------------------
1278// transaction
1279// ----------------------------------------------------------------------------
1280
Dan Stoza7dde5992015-05-22 09:51:44 -07001281void Layer::pushPendingState() {
1282 if (!mCurrentState.modified) {
1283 return;
1284 }
1285
Dan Stoza7dde5992015-05-22 09:51:44 -07001286 // If this transaction is waiting on the receipt of a frame, generate a sync
1287 // point and send it to the remote layer.
1288 if (mCurrentState.handle != nullptr) {
1289 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1290 sp<Layer> handleLayer = handle->owner.promote();
1291 if (handleLayer == nullptr) {
1292 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1293 // If we can't promote the layer we are intended to wait on,
1294 // then it is expired or otherwise invalid. Allow this transaction
1295 // to be applied as per normal (no synchronization).
1296 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001297 } else {
1298 auto syncPoint = std::make_shared<SyncPoint>(
1299 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001300 if (handleLayer->addSyncPoint(syncPoint)) {
1301 mRemoteSyncPoints.push_back(std::move(syncPoint));
1302 } else {
1303 // We already missed the frame we're supposed to synchronize
1304 // on, so go ahead and apply the state update
1305 mCurrentState.handle = nullptr;
1306 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 }
1308
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 // Wake us up to check if the frame has been received
1310 setTransactionFlags(eTransactionNeeded);
1311 }
1312 mPendingStates.push_back(mCurrentState);
1313}
1314
1315void Layer::popPendingState() {
1316 auto oldFlags = mCurrentState.flags;
1317 mCurrentState = mPendingStates[0];
Dan Stozacac35382016-01-27 12:21:06 -08001318 mCurrentState.flags = (oldFlags & ~mCurrentState.mask) |
Dan Stoza7dde5992015-05-22 09:51:44 -07001319 (mCurrentState.flags & mCurrentState.mask);
1320
1321 mPendingStates.removeAt(0);
1322}
1323
1324bool Layer::applyPendingStates() {
Dan Stoza7dde5992015-05-22 09:51:44 -07001325 bool stateUpdateAvailable = false;
1326 while (!mPendingStates.empty()) {
1327 if (mPendingStates[0].handle != nullptr) {
1328 if (mRemoteSyncPoints.empty()) {
1329 // If we don't have a sync point for this, apply it anyway. It
1330 // will be visually wrong, but it should keep us from getting
1331 // into too much trouble.
1332 ALOGE("[%s] No local sync point found", mName.string());
1333 popPendingState();
1334 stateUpdateAvailable = true;
1335 continue;
1336 }
1337
Dan Stozacac35382016-01-27 12:21:06 -08001338 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1339 mPendingStates[0].frameNumber) {
1340 ALOGE("[%s] Unexpected sync point frame number found",
1341 mName.string());
1342
1343 // Signal our end of the sync point and then dispose of it
1344 mRemoteSyncPoints.front()->setTransactionApplied();
1345 mRemoteSyncPoints.pop_front();
1346 continue;
1347 }
1348
Dan Stoza7dde5992015-05-22 09:51:44 -07001349 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1350 // Apply the state update
1351 popPendingState();
1352 stateUpdateAvailable = true;
1353
1354 // Signal our end of the sync point and then dispose of it
1355 mRemoteSyncPoints.front()->setTransactionApplied();
1356 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001357 } else {
1358 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 } else {
1361 popPendingState();
1362 stateUpdateAvailable = true;
1363 }
1364 }
1365
1366 // If we still have pending updates, wake SurfaceFlinger back up and point
1367 // it at this layer so we can process them
1368 if (!mPendingStates.empty()) {
1369 setTransactionFlags(eTransactionNeeded);
1370 mFlinger->setTransactionFlags(eTraversalNeeded);
1371 }
1372
1373 mCurrentState.modified = false;
1374 return stateUpdateAvailable;
1375}
1376
1377void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001378 auto headFrameNumber = getHeadFrameNumber();
1379 Mutex::Autolock lock(mLocalSyncPointMutex);
1380 for (auto& point : mLocalSyncPoints) {
1381 if (headFrameNumber >= point->getFrameNumber()) {
1382 point->setFrameAvailable();
1383 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001384 }
1385}
1386
Mathias Agopian13127d82013-03-05 17:47:11 -08001387uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001388 ATRACE_CALL();
1389
Dan Stoza7dde5992015-05-22 09:51:44 -07001390 pushPendingState();
1391 if (!applyPendingStates()) {
1392 return 0;
1393 }
1394
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001395 const Layer::State& s(getDrawingState());
1396 const Layer::State& c(getCurrentState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001398 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1399 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001400
1401 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001402 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001403 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001404 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001405 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001406 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001407 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001408 " requested={ wh={%4u,%4u} }}\n",
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001409 this, getName().string(), mCurrentTransform, mCurrentScalingMode,
1410 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001411 c.crop.left,
1412 c.crop.top,
1413 c.crop.right,
1414 c.crop.bottom,
1415 c.crop.getWidth(),
1416 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001417 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001418 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001419 s.crop.left,
1420 s.crop.top,
1421 s.crop.right,
1422 s.crop.bottom,
1423 s.crop.getWidth(),
1424 s.crop.getHeight(),
1425 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001427 // record the new size, form this point on, when the client request
1428 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001429 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001430 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001432
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001433 if (!isFixedSize()) {
1434
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001435 const bool resizePending = (c.requested.w != c.active.w) ||
1436 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001437
Dan Stoza9e9b0442015-04-22 14:59:08 -07001438 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001439 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001440 // if we have a pending resize, unless we are in fixed-size mode.
1441 // the drawing state will be updated only once we receive a buffer
1442 // with the correct size.
1443 //
1444 // in particular, we want to make sure the clip (which is part
1445 // of the geometry state) is latched together with the size but is
1446 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001447 //
1448 // If a sideband stream is attached, however, we want to skip this
1449 // optimization so that transactions aren't missed when a buffer
1450 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001451
1452 flags |= eDontUpdateGeometryState;
1453 }
1454 }
1455
Mathias Agopian13127d82013-03-05 17:47:11 -08001456 // always set active to requested, unless we're asked not to
1457 // this is used by Layer, which special cases resizes.
1458 if (flags & eDontUpdateGeometryState) {
1459 } else {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001460 Layer::State& editCurrentState(getCurrentState());
1461 editCurrentState.active = c.requested;
Mathias Agopian13127d82013-03-05 17:47:11 -08001462 }
1463
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001464 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001465 // invalidate and recompute the visible regions if needed
1466 flags |= Layer::eVisibleRegion;
1467 }
1468
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001469 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001470 // invalidate and recompute the visible regions if needed
1471 flags |= eVisibleRegion;
1472 this->contentDirty = true;
1473
1474 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001475 const uint8_t type = c.active.transform.getType();
1476 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 (type >= Transform::SCALE));
1478 }
1479
1480 // Commit the transaction
1481 commitTransaction();
1482 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483}
1484
Mathias Agopian13127d82013-03-05 17:47:11 -08001485void Layer::commitTransaction() {
1486 mDrawingState = mCurrentState;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001487}
1488
Mathias Agopian13127d82013-03-05 17:47:11 -08001489uint32_t Layer::getTransactionFlags(uint32_t flags) {
1490 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1491}
1492
1493uint32_t Layer::setTransactionFlags(uint32_t flags) {
1494 return android_atomic_or(flags, &mTransactionFlags);
1495}
1496
1497bool Layer::setPosition(float x, float y) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001498 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001499 return false;
1500 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001501
1502 // We update the requested and active position simultaneously because
1503 // we want to apply the position portion of the transform matrix immediately,
1504 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001505 mCurrentState.requested.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001506 mCurrentState.active.transform.set(x, y);
1507
Dan Stoza7dde5992015-05-22 09:51:44 -07001508 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001509 setTransactionFlags(eTransactionNeeded);
1510 return true;
1511}
1512bool Layer::setLayer(uint32_t z) {
1513 if (mCurrentState.z == z)
1514 return false;
1515 mCurrentState.sequence++;
1516 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001517 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001518 setTransactionFlags(eTransactionNeeded);
1519 return true;
1520}
1521bool Layer::setSize(uint32_t w, uint32_t h) {
1522 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1523 return false;
1524 mCurrentState.requested.w = w;
1525 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001526 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001527 setTransactionFlags(eTransactionNeeded);
1528 return true;
1529}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001530#ifdef USE_HWC2
1531bool Layer::setAlpha(float alpha) {
1532#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001533bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001534#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001535 if (mCurrentState.alpha == alpha)
1536 return false;
1537 mCurrentState.sequence++;
1538 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001539 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001540 setTransactionFlags(eTransactionNeeded);
1541 return true;
1542}
1543bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1544 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001545 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001546 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001547 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001548 setTransactionFlags(eTransactionNeeded);
1549 return true;
1550}
1551bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001552 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001553 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001554 setTransactionFlags(eTransactionNeeded);
1555 return true;
1556}
1557bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1558 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1559 if (mCurrentState.flags == newFlags)
1560 return false;
1561 mCurrentState.sequence++;
1562 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001563 mCurrentState.mask = mask;
1564 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001565 setTransactionFlags(eTransactionNeeded);
1566 return true;
1567}
1568bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001569 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 return false;
1571 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001572 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001573 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001574 setTransactionFlags(eTransactionNeeded);
1575 return true;
1576}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001577bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001578 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001579 return false;
1580 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001581 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001582 mCurrentState.modified = true;
1583 setTransactionFlags(eTransactionNeeded);
1584 return true;
1585}
Mathias Agopian13127d82013-03-05 17:47:11 -08001586
1587bool Layer::setLayerStack(uint32_t layerStack) {
1588 if (mCurrentState.layerStack == layerStack)
1589 return false;
1590 mCurrentState.sequence++;
1591 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001592 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001593 setTransactionFlags(eTransactionNeeded);
1594 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001595}
1596
Dan Stoza7dde5992015-05-22 09:51:44 -07001597void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1598 uint64_t frameNumber) {
1599 mCurrentState.handle = handle;
1600 mCurrentState.frameNumber = frameNumber;
1601 // We don't set eTransactionNeeded, because just receiving a deferral
1602 // request without any other state updates shouldn't actually induce a delay
1603 mCurrentState.modified = true;
1604 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001605 mCurrentState.handle = nullptr;
1606 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001607 mCurrentState.modified = false;
1608}
1609
Dan Stozaee44edd2015-03-23 15:50:23 -07001610void Layer::useSurfaceDamage() {
1611 if (mFlinger->mForceFullDamage) {
1612 surfaceDamageRegion = Region::INVALID_REGION;
1613 } else {
1614 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1615 }
1616}
1617
1618void Layer::useEmptyDamage() {
1619 surfaceDamageRegion.clear();
1620}
1621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622// ----------------------------------------------------------------------------
1623// pageflip handling...
1624// ----------------------------------------------------------------------------
1625
Dan Stoza6b9454d2014-11-07 16:00:59 -08001626bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001627 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001628 return true;
1629 }
1630
Dan Stoza6b9454d2014-11-07 16:00:59 -08001631 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001632 if (mQueueItems.empty()) {
1633 return false;
1634 }
1635 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001636 nsecs_t expectedPresent =
1637 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001638
1639 // Ignore timestamps more than a second in the future
1640 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1641 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1642 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1643 expectedPresent);
1644
1645 bool isDue = timestamp < expectedPresent;
1646 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001647}
1648
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001649bool Layer::onPreComposition() {
1650 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001651 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001652}
1653
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001654void Layer::onPostComposition() {
1655 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001656 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001657 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1658
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001659 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001660 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001661 mFrameTracker.setFrameReadyFence(frameReadyFence);
1662 } else {
1663 // There was no fence for this frame, so assume that it was ready
1664 // to be presented at the desired present time.
1665 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1666 }
1667
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001668 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001669#ifdef USE_HWC2
1670 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1671#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001672 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001673#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001674 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001675 mFrameTracker.setActualPresentFence(presentFence);
1676 } else {
1677 // The HWC doesn't support present fences, so use the refresh
1678 // timestamp instead.
1679 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1680 mFrameTracker.setActualPresentTime(presentTime);
1681 }
1682
1683 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001684 mFrameLatencyNeeded = false;
1685 }
1686}
1687
Dan Stoza9e56aa02015-11-02 13:00:03 -08001688#ifdef USE_HWC2
1689void Layer::releasePendingBuffer() {
1690 mSurfaceFlingerConsumer->releasePendingBuffer();
1691}
1692#endif
1693
Mathias Agopianda27af92012-09-13 18:17:13 -07001694bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001695 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001696#ifdef USE_HWC2
1697 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1698 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1699#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001700 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001701 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001702#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001703}
1704
Mathias Agopian4fec8732012-06-29 14:12:52 -07001705Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001707 ATRACE_CALL();
1708
Jesse Hall399184a2014-03-03 15:42:54 -08001709 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1710 // mSidebandStreamChanged was true
1711 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001712 if (mSidebandStream != NULL) {
1713 setTransactionFlags(eTransactionNeeded);
1714 mFlinger->setTransactionFlags(eTraversalNeeded);
1715 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001716 recomputeVisibleRegions = true;
1717
1718 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001719 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001720 }
1721
Mathias Agopian4fec8732012-06-29 14:12:52 -07001722 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001723 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001724
1725 // if we've already called updateTexImage() without going through
1726 // a composition step, we have to skip this layer at this point
1727 // because we cannot call updateTeximage() without a corresponding
1728 // compositionComplete() call.
1729 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001730 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001731 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001732 }
1733
Jamie Gennis351a5132011-09-14 18:23:37 -07001734 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001735 const State& s(getDrawingState());
1736 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001737 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001738
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001739 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001740 Layer::State& front;
1741 Layer::State& current;
1742 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001743 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001744 const char* name;
1745
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001746 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001747 bool& recomputeVisibleRegions, bool stickySet,
1748 const char* name)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001749 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001750 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001751 stickyTransformSet(stickySet),
1752 name(name) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001753 }
1754
1755 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001756 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001757 if (buf == NULL) {
1758 return false;
1759 }
1760
1761 uint32_t bufWidth = buf->getWidth();
1762 uint32_t bufHeight = buf->getHeight();
1763
1764 // check that we received a buffer of the right size
1765 // (Take the buffer's orientation into account)
1766 if (item.mTransform & Transform::ROT_90) {
1767 swap(bufWidth, bufHeight);
1768 }
1769
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001770 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
1771 if (front.active != front.requested) {
1772
1773 if (isFixedSize ||
1774 (bufWidth == front.requested.w &&
1775 bufHeight == front.requested.h))
1776 {
1777 // Here we pretend the transaction happened by updating the
1778 // current and drawing states. Drawing state is only accessed
1779 // in this thread, no need to have it locked
1780 front.active = front.requested;
1781
1782 // We also need to update the current state so that
1783 // we don't end-up overwriting the drawing state with
1784 // this stale current state during the next transaction
1785 //
1786 // NOTE: We don't need to hold the transaction lock here
1787 // because State::active is only accessed from this thread.
1788 current.active = front.active;
1789
1790 // recompute visible region
1791 recomputeVisibleRegions = true;
1792 }
1793
1794 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001795 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001796 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001797 " requested={ wh={%4u,%4u} }}\n",
1798 name,
Andy McFadden69052052012-09-14 16:10:11 -07001799 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001800 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001801 front.crop.left,
1802 front.crop.top,
1803 front.crop.right,
1804 front.crop.bottom,
1805 front.crop.getWidth(),
1806 front.crop.getHeight(),
1807 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001808 }
1809
Ruben Brunk1681d952014-06-27 15:51:55 -07001810 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001811 if (front.active.w != bufWidth ||
1812 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001813 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001814 ALOGE("[%s] rejecting buffer: "
1815 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1816 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001817 return true;
1818 }
1819 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001820
1821 // if the transparent region has changed (this test is
1822 // conservative, but that's fine, worst case we're doing
1823 // a bit of extra work), we latch the new one and we
1824 // trigger a visible-region recompute.
1825 if (!front.activeTransparentRegion.isTriviallyEqual(
1826 front.requestedTransparentRegion)) {
1827 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001828
1829 // We also need to update the current state so that
1830 // we don't end-up overwriting the drawing state with
1831 // this stale current state during the next transaction
1832 //
1833 // NOTE: We don't need to hold the transaction lock here
1834 // because State::active is only accessed from this thread.
1835 current.activeTransparentRegion = front.activeTransparentRegion;
1836
1837 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001838 recomputeVisibleRegions = true;
1839 }
1840
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001841 return false;
1842 }
1843 };
1844
Ruben Brunk1681d952014-06-27 15:51:55 -07001845 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrb5d3d262016-03-25 15:08:13 -07001846 getProducerStickyTransform() != 0, mName.string());
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001847
Dan Stozacac35382016-01-27 12:21:06 -08001848
1849 // Check all of our local sync points to ensure that all transactions
1850 // which need to have been applied prior to the frame which is about to
1851 // be latched have signaled
1852
1853 auto headFrameNumber = getHeadFrameNumber();
1854 bool matchingFramesFound = false;
1855 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001856 {
Dan Stozacac35382016-01-27 12:21:06 -08001857 Mutex::Autolock lock(mLocalSyncPointMutex);
1858 for (auto& point : mLocalSyncPoints) {
1859 if (point->getFrameNumber() > headFrameNumber) {
1860 break;
1861 }
1862
1863 matchingFramesFound = true;
1864
1865 if (!point->frameIsAvailable()) {
1866 // We haven't notified the remote layer that the frame for
1867 // this point is available yet. Notify it now, and then
1868 // abort this attempt to latch.
1869 point->setFrameAvailable();
1870 allTransactionsApplied = false;
1871 break;
1872 }
1873
1874 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001875 }
1876 }
1877
Dan Stozacac35382016-01-27 12:21:06 -08001878 if (matchingFramesFound && !allTransactionsApplied) {
1879 mFlinger->signalLayerUpdate();
1880 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001881 }
1882
Pablo Ceballos06312182015-10-07 16:32:12 -07001883 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1884 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001885 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001886 // buffer mode.
1887 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001888 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001889 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001890 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001891 if (updateResult == BufferQueue::PRESENT_LATER) {
1892 // Producer doesn't want buffer to be displayed yet. Signal a
1893 // layer update so we check again at the next opportunity.
1894 mFlinger->signalLayerUpdate();
1895 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001896 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1897 // If the buffer has been rejected, remove it from the shadow queue
1898 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001899 if (queuedBuffer) {
1900 Mutex::Autolock lock(mQueueItemLock);
1901 mQueueItems.removeAt(0);
1902 android_atomic_dec(&mQueuedFrames);
1903 }
Dan Stozaecc50402015-04-28 14:42:06 -07001904 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001905 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1906 // This can occur if something goes wrong when trying to create the
1907 // EGLImage for this buffer. If this happens, the buffer has already
1908 // been released, so we need to clean up the queue and bug out
1909 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001910 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001911 Mutex::Autolock lock(mQueueItemLock);
1912 mQueueItems.clear();
1913 android_atomic_and(0, &mQueuedFrames);
1914 }
1915
1916 // Once we have hit this state, the shadow queue may no longer
1917 // correctly reflect the incoming BufferQueue's contents, so even if
1918 // updateTexImage starts working, the only safe course of action is
1919 // to continue to ignore updates.
1920 mUpdateTexImageFailed = true;
1921
1922 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001923 }
1924
Pablo Ceballos06312182015-10-07 16:32:12 -07001925 if (queuedBuffer) {
1926 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001927 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1928
Dan Stoza6b9454d2014-11-07 16:00:59 -08001929 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001930
1931 // Remove any stale buffers that have been dropped during
1932 // updateTexImage
1933 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1934 mQueueItems.removeAt(0);
1935 android_atomic_dec(&mQueuedFrames);
1936 }
1937
Dan Stoza6b9454d2014-11-07 16:00:59 -08001938 mQueueItems.removeAt(0);
1939 }
1940
Dan Stozaecc50402015-04-28 14:42:06 -07001941
Andy McFadden1585c4d2013-06-28 13:52:40 -07001942 // Decrement the queued-frames count. Signal another event if we
1943 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001944 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001945 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001946 mFlinger->signalLayerUpdate();
1947 }
1948
1949 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001950 // something happened!
1951 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001952 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001953 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001954
Jamie Gennis351a5132011-09-14 18:23:37 -07001955 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001956 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001957 if (mActiveBuffer == NULL) {
1958 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001959 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001960 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001961
Mathias Agopian4824d402012-06-04 18:16:30 -07001962 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001963 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001964 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001965 // the first time we receive a buffer, we need to trigger a
1966 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001967 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001968 }
1969
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001970 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1971 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1972 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07001973 if ((crop != mCurrentCrop) ||
1974 (transform != mCurrentTransform) ||
1975 (scalingMode != mCurrentScalingMode))
1976 {
1977 mCurrentCrop = crop;
1978 mCurrentTransform = transform;
1979 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07001980 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001981 }
1982
1983 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07001984 uint32_t bufWidth = mActiveBuffer->getWidth();
1985 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07001986 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
1987 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07001988 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001989 }
1990 }
1991
1992 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08001993 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07001994 recomputeVisibleRegions = true;
1995 }
1996
Dan Stozacac35382016-01-27 12:21:06 -08001997 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1998
1999 // Remove any sync points corresponding to the buffer which was just
2000 // latched
2001 {
2002 Mutex::Autolock lock(mLocalSyncPointMutex);
2003 auto point = mLocalSyncPoints.begin();
2004 while (point != mLocalSyncPoints.end()) {
2005 if (!(*point)->frameIsAvailable() ||
2006 !(*point)->transactionIsApplied()) {
2007 // This sync point must have been added since we started
2008 // latching. Don't drop it yet.
2009 ++point;
2010 continue;
2011 }
2012
2013 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2014 point = mLocalSyncPoints.erase(point);
2015 } else {
2016 ++point;
2017 }
2018 }
2019 }
2020
Mathias Agopian4fec8732012-06-29 14:12:52 -07002021 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002022 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002023
2024 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002025 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002026 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002027 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028}
2029
Mathias Agopiana67932f2011-04-20 14:20:59 -07002030uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002031{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002032 // TODO: should we do something special if mSecure is set?
2033 if (mProtectedByApp) {
2034 // need a hardware-protected path to external video sink
2035 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002036 }
Riley Andrews03414a12014-07-01 14:22:59 -07002037 if (mPotentialCursor) {
2038 usage |= GraphicBuffer::USAGE_CURSOR;
2039 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002040 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002041 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002042}
2043
Mathias Agopian84300952012-11-21 16:02:13 -08002044void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002045 uint32_t orientation = 0;
2046 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002047 // The transform hint is used to improve performance, but we can
2048 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002049 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002050 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002051 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002052 if (orientation & Transform::ROT_INVALID) {
2053 orientation = 0;
2054 }
2055 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002056 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002057}
2058
Mathias Agopian13127d82013-03-05 17:47:11 -08002059// ----------------------------------------------------------------------------
2060// debugging
2061// ----------------------------------------------------------------------------
2062
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002063void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002064{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002065 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002066
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002067 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002068 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002069 "+ %s %p (%s)\n",
2070 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002071 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002072
Mathias Agopian2ca79392013-04-02 18:30:32 -07002073 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002074 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002075 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002076 sp<Client> client(mClientRef.promote());
2077
Mathias Agopian74d211a2013-04-22 16:55:35 +02002078 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002079 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2080 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002081 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002082#ifdef USE_HWC2
2083 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2084#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002085 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002086#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002087 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002088 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 -07002089 s.crop.left, s.crop.top,
2090 s.crop.right, s.crop.bottom,
2091 s.finalCrop.left, s.finalCrop.top,
2092 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002093 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002094 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002095 s.active.transform[0][0], s.active.transform[0][1],
2096 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002097 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002098
2099 sp<const GraphicBuffer> buf0(mActiveBuffer);
2100 uint32_t w0=0, h0=0, s0=0, f0=0;
2101 if (buf0 != 0) {
2102 w0 = buf0->getWidth();
2103 h0 = buf0->getHeight();
2104 s0 = buf0->getStride();
2105 f0 = buf0->format;
2106 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002107 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002108 " "
2109 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2110 " queued-frames=%d, mRefreshPending=%d\n",
2111 mFormat, w0, h0, s0,f0,
2112 mQueuedFrames, mRefreshPending);
2113
Mathias Agopian13127d82013-03-05 17:47:11 -08002114 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002115 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002116 }
2117}
2118
Svetoslavd85084b2014-03-20 10:28:31 -07002119void Layer::dumpFrameStats(String8& result) const {
2120 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002121}
2122
Svetoslavd85084b2014-03-20 10:28:31 -07002123void Layer::clearFrameStats() {
2124 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002125}
2126
Jamie Gennis6547ff42013-07-16 20:12:42 -07002127void Layer::logFrameStats() {
2128 mFrameTracker.logAndResetStats(mName);
2129}
2130
Svetoslavd85084b2014-03-20 10:28:31 -07002131void Layer::getFrameStats(FrameStats* outStats) const {
2132 mFrameTracker.getStats(outStats);
2133}
2134
Pablo Ceballos40845df2016-01-25 17:41:15 -08002135void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2136 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2137 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2138 *outName = mName;
2139 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2140
2141#ifdef USE_HWC2
2142 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2143 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2144 HWC2::Composition::Client : true;
2145#else
2146 *outIsGlesComposition = mIsGlesComposition;
2147#endif
2148 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2149 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2150 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2151}
Mathias Agopian13127d82013-03-05 17:47:11 -08002152// ---------------------------------------------------------------------------
2153
2154Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2155 const sp<Layer>& layer)
2156 : mFlinger(flinger), mLayer(layer) {
2157}
2158
2159Layer::LayerCleaner::~LayerCleaner() {
2160 // destroy client resources
2161 mFlinger->onLayerDestroyed(mLayer);
2162}
2163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002164// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002165}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002166
2167#if defined(__gl_h_)
2168#error "don't include gl/gl.h in this file"
2169#endif
2170
2171#if defined(__gl2_h_)
2172#error "don't include gl2/gl2.h in this file"
2173#endif