blob: 1803ee5d29ffdafca106ced86952b812da8a05c4 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <stdlib.h>
23#include <stdint.h>
24#include <sys/types.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080025#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Dan Stoza6b9454d2014-11-07 16:00:59 -080040#include <gui/BufferItem.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020044#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070045#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include "Layer.h"
Dan Stozab9b08832014-03-13 11:55:57 -070047#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian1b031492012-06-20 17:51:20 -070050#include "DisplayHardware/HWComposer.h"
51
Mathias Agopian875d8e12013-06-07 15:35:48 -070052#include "RenderEngine/RenderEngine.h"
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#define DEBUG_RESIZE 0
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056namespace android {
57
58// ---------------------------------------------------------------------------
59
Mathias Agopian13127d82013-03-05 17:47:11 -080060int32_t Layer::sSequence = 1;
61
Mathias Agopian4d9b8222013-03-12 17:11:48 -070062Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
63 const String8& name, uint32_t w, uint32_t h, uint32_t flags)
Mathias Agopian13127d82013-03-05 17:47:11 -080064 : contentDirty(false),
65 sequence(uint32_t(android_atomic_inc(&sSequence))),
66 mFlinger(flinger),
Mathias Agopiana67932f2011-04-20 14:20:59 -070067 mTextureName(-1U),
Mathias Agopian13127d82013-03-05 17:47:11 -080068 mPremultipliedAlpha(true),
69 mName("unnamed"),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070072 mPendingStateMutex(),
73 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080075 mSidebandStreamChanged(false),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentTransform(0),
Mathias Agopian933389f2011-07-18 16:15:08 -070077 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Robert Carrc3574f72016-03-24 12:19:32 -070078 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080080 mCurrentFrameNumber(0),
Mathias Agopian4d143ee2012-02-23 20:05:39 -080081 mRefreshPending(false),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080082 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080083 mFiltering(false),
84 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070085 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Pablo Ceballos40845df2016-01-25 17:41:15 -080086#ifndef USE_HWC2
87 mIsGlesComposition(false),
88#endif
Mathias Agopian13127d82013-03-05 17:47:11 -080089 mProtectedByApp(false),
90 mHasSurface(false),
Riley Andrews03414a12014-07-01 14:22:59 -070091 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070092 mPotentialCursor(false),
93 mQueueItemLock(),
94 mQueueItemCondition(),
95 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070096 mLastFrameNumberReceived(0),
Pablo Ceballos04839ab2015-11-13 13:39:23 -080097 mUpdateTexImageFailed(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080098 mAutoRefresh(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100#ifdef USE_HWC2
101 ALOGV("Creating Layer %s", name.string());
102#endif
103
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104 mCurrentCrop.makeInvalid();
Mathias Agopian3f844832013-08-07 21:24:32 -0700105 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700106 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 uint32_t layerFlags = 0;
109 if (flags & ISurfaceComposerClient::eHidden)
Andy McFadden4125a4f2014-01-29 17:17:11 -0800110 layerFlags |= layer_state_t::eLayerHidden;
111 if (flags & ISurfaceComposerClient::eOpaque)
112 layerFlags |= layer_state_t::eLayerOpaque;
Dan Stoza23116082015-06-18 14:58:39 -0700113 if (flags & ISurfaceComposerClient::eSecure)
114 layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 if (flags & ISurfaceComposerClient::eNonPremultiplied)
117 mPremultipliedAlpha = false;
118
119 mName = name;
120
121 mCurrentState.active.w = w;
122 mCurrentState.active.h = h;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800123 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700124 mCurrentState.crop.makeInvalid();
125 mCurrentState.finalCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.z = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127#ifdef USE_HWC2
128 mCurrentState.alpha = 1.0f;
129#else
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130 mCurrentState.alpha = 0xFF;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800131#endif
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132 mCurrentState.layerStack = 0;
133 mCurrentState.flags = layerFlags;
134 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135 mCurrentState.requested = mCurrentState.active;
136
137 // drawing state & current state are identical
138 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700139
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140#ifdef USE_HWC2
141 const auto& hwc = flinger->getHwComposer();
142 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
143 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
144#else
Jamie Gennis6547ff42013-07-16 20:12:42 -0700145 nsecs_t displayPeriod =
146 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800147#endif
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Jamie Gennise8696a42012-01-15 18:54:57 -0800149}
150
Mathias Agopian3f844832013-08-07 21:24:32 -0700151void Layer::onFirstRef() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800152 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
Dan Stozab3d0bdf2014-04-07 16:33:59 -0700153 sp<IGraphicBufferProducer> producer;
154 sp<IGraphicBufferConsumer> consumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700155 BufferQueue::createBufferQueue(&producer, &consumer);
156 mProducer = new MonitoredProducer(producer, mFlinger);
157 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800158 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Jesse Hall399184a2014-03-03 15:42:54 -0800159 mSurfaceFlingerConsumer->setContentsChangedListener(this);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700160 mSurfaceFlingerConsumer->setName(mName);
Daniel Lamb2675792012-02-23 14:35:13 -0800161
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700162#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
163#warning "disabling triple buffering"
Mathias Agopian7f42a9c2012-04-23 20:00:16 -0700164#else
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700165 mProducer->setMaxDequeuedBufferCount(2);
Mathias Agopian303d5382012-02-05 01:49:16 -0800166#endif
Andy McFadden69052052012-09-14 16:10:11 -0700167
Mathias Agopian84300952012-11-21 16:02:13 -0800168 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
169 updateTransformHint(hw);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700170}
171
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700172Layer::~Layer() {
Pablo Ceballos8ea4e7b2016-03-03 15:20:02 -0800173 sp<Client> c(mClientRef.promote());
174 if (c != 0) {
175 c->detachLayer(this);
176 }
177
Dan Stozacac35382016-01-27 12:21:06 -0800178 for (auto& point : mRemoteSyncPoints) {
179 point->setTransactionApplied();
180 }
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700181 mFlinger->deleteTextureAsync(mTextureName);
Jamie Gennis6547ff42013-07-16 20:12:42 -0700182 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700183}
184
Mathias Agopian13127d82013-03-05 17:47:11 -0800185// ---------------------------------------------------------------------------
186// callbacks
187// ---------------------------------------------------------------------------
188
Dan Stoza9e56aa02015-11-02 13:00:03 -0800189#ifdef USE_HWC2
190void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) {
191 if (mHwcLayers.empty()) {
192 return;
193 }
194 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
195}
196#else
Dan Stozac7014012014-02-14 15:03:43 -0800197void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */,
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 HWComposer::HWCLayerInterface* layer) {
199 if (layer) {
200 layer->onDisplayed();
Jesse Hall13f01cb2013-03-20 11:37:21 -0700201 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 }
203}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800205
Dan Stoza6b9454d2014-11-07 16:00:59 -0800206void Layer::onFrameAvailable(const BufferItem& item) {
207 // Add this buffer from our internal queue tracker
208 { // Autolock scope
209 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700210
211 // Reset the frame number tracker when we receive the first buffer after
212 // a frame number reset
213 if (item.mFrameNumber == 1) {
214 mLastFrameNumberReceived = 0;
215 }
216
217 // Ensure that callbacks are handled in order
218 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
219 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
220 ms2ns(500));
221 if (result != NO_ERROR) {
222 ALOGE("[%s] Timed out waiting on callback", mName.string());
223 }
224 }
225
Dan Stoza6b9454d2014-11-07 16:00:59 -0800226 mQueueItems.push_back(item);
Dan Stozaecc50402015-04-28 14:42:06 -0700227 android_atomic_inc(&mQueuedFrames);
Dan Stozaa4650a52015-05-12 12:56:16 -0700228
229 // Wake up any pending callbacks
230 mLastFrameNumberReceived = item.mFrameNumber;
231 mQueueItemCondition.broadcast();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800232 }
233
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800234 mFlinger->signalLayerUpdate();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700235}
236
Dan Stoza6b9454d2014-11-07 16:00:59 -0800237void Layer::onFrameReplaced(const BufferItem& item) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700238 { // Autolock scope
239 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaa4650a52015-05-12 12:56:16 -0700240
Dan Stoza7dde5992015-05-22 09:51:44 -0700241 // Ensure that callbacks are handled in order
242 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
243 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
244 ms2ns(500));
245 if (result != NO_ERROR) {
246 ALOGE("[%s] Timed out waiting on callback", mName.string());
247 }
Dan Stozaa4650a52015-05-12 12:56:16 -0700248 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700249
250 if (mQueueItems.empty()) {
251 ALOGE("Can't replace a frame on an empty queue");
252 return;
253 }
254 mQueueItems.editItemAt(0) = item;
255
256 // Wake up any pending callbacks
257 mLastFrameNumberReceived = item.mFrameNumber;
258 mQueueItemCondition.broadcast();
Dan Stozaa4650a52015-05-12 12:56:16 -0700259 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800260}
261
Jesse Hall399184a2014-03-03 15:42:54 -0800262void Layer::onSidebandStreamChanged() {
263 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
264 // mSidebandStreamChanged was false
265 mFlinger->signalLayerUpdate();
266 }
267}
268
Mathias Agopian67106042013-03-14 19:18:13 -0700269// called with SurfaceFlinger::mStateLock from the drawing thread after
270// the layer has been remove from the current state list (and just before
271// it's removed from the drawing state list)
Mathias Agopian13127d82013-03-05 17:47:11 -0800272void Layer::onRemoved() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800273 mSurfaceFlingerConsumer->abandon();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700274}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700275
Mathias Agopian13127d82013-03-05 17:47:11 -0800276// ---------------------------------------------------------------------------
277// set-up
278// ---------------------------------------------------------------------------
279
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700280const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800281 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282}
283
Mathias Agopianf9d93272009-06-19 17:00:27 -0700284status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 PixelFormat format, uint32_t flags)
286{
Mathias Agopianca99fb82010-04-14 16:43:44 -0700287 uint32_t const maxSurfaceDims = min(
Mathias Agopiana4912602012-07-12 14:25:33 -0700288 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700289
290 // never allow a surface larger than what our underlying GL implementation
291 // can handle.
292 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
Mathias Agopianff615cc2012-02-24 14:58:36 -0800293 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700294 return BAD_VALUE;
295 }
296
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700297 mFormat = format;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700298
Riley Andrews03414a12014-07-01 14:22:59 -0700299 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700300 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700301 mCurrentOpacity = getOpacityForFormat(format);
302
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800303 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
304 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
305 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
Mathias Agopianca99fb82010-04-14 16:43:44 -0700306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 return NO_ERROR;
308}
309
Dan Stoza7dde5992015-05-22 09:51:44 -0700310/*
311 * The layer handle is just a BBinder object passed to the client
312 * (remote process) -- we don't keep any reference on our side such that
313 * the dtor is called when the remote side let go of its reference.
314 *
315 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
316 * this layer when the handle is destroyed.
317 */
318class Layer::Handle : public BBinder, public LayerCleaner {
319 public:
320 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
321 : LayerCleaner(flinger, layer), owner(layer) {}
322
323 wp<Layer> owner;
324};
325
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700326sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800327 Mutex::Autolock _l(mLock);
328
329 LOG_ALWAYS_FATAL_IF(mHasSurface,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700330 "Layer::getHandle() has already been called");
Mathias Agopian13127d82013-03-05 17:47:11 -0800331
332 mHasSurface = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700333
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700334 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800335}
336
Dan Stozab9b08832014-03-13 11:55:57 -0700337sp<IGraphicBufferProducer> Layer::getProducer() const {
338 return mProducer;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700339}
340
Mathias Agopian13127d82013-03-05 17:47:11 -0800341// ---------------------------------------------------------------------------
342// h/w composer set-up
343// ---------------------------------------------------------------------------
344
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800345Rect Layer::getContentCrop() const {
346 // this is the crop rectangle that applies to the buffer
347 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700348 Rect crop;
349 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800350 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700351 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800352 } else if (mActiveBuffer != NULL) {
353 // otherwise we use the whole buffer
354 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700355 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800356 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700357 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700358 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700359 return crop;
360}
361
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700362static Rect reduce(const Rect& win, const Region& exclude) {
363 if (CC_LIKELY(exclude.isEmpty())) {
364 return win;
365 }
366 if (exclude.isRect()) {
367 return win.reduce(exclude.getBounds());
368 }
369 return Region(win).subtract(exclude).getBounds();
370}
371
Mathias Agopian13127d82013-03-05 17:47:11 -0800372Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700373 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700374 return computeBounds(s.activeTransparentRegion);
375}
376
377Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
378 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800379 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700380
381 if (!s.crop.isEmpty()) {
382 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700384 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700385 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800386}
387
Mathias Agopian6b442672013-07-09 21:24:52 -0700388FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800389 // the content crop is the area of the content that gets scaled to the
390 // layer's size.
Mathias Agopian6b442672013-07-09 21:24:52 -0700391 FloatRect crop(getContentCrop());
Mathias Agopian13127d82013-03-05 17:47:11 -0800392
Robert Carrb5d3d262016-03-25 15:08:13 -0700393 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800394 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700395 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800396
397 // apply the projection's clipping to the window crop in
398 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700399 // if there are no window scaling involved, this operation will map to full
400 // pixels in the buffer.
401 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
402 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700403
404 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700405 if (!s.crop.isEmpty()) {
406 activeCrop = s.crop;
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700407 }
408
Robert Carr3dcabfa2016-03-01 18:36:58 -0800409 activeCrop = s.active.transform.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000410 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
411 activeCrop.clear();
412 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700413 if (!s.finalCrop.isEmpty()) {
414 if(!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 activeCrop.clear();
416 }
417 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800418 activeCrop = s.active.transform.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800419
Michael Lentine28ea2172014-11-19 18:32:37 -0800420 // This needs to be here as transform.transform(Rect) computes the
421 // transformed rect and then takes the bounding box of the result before
422 // returning. This means
423 // transform.inverse().transform(transform.transform(Rect)) != Rect
424 // in which case we need to make sure the final rect is clipped to the
425 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
427 activeCrop.clear();
428 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800429
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700430 // subtract the transparent region and snap to the bounds
431 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
432
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // Transform the window crop to match the buffer coordinate system,
434 // which means using the inverse of the current transform set on the
435 // SurfaceFlingerConsumer.
436 uint32_t invTransform = mCurrentTransform;
437 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
438 /*
439 * the code below applies the display's inverse transform to the buffer
440 */
441 uint32_t invTransformOrient = hw->getOrientationTransform();
442 // calculate the inverse transform
443 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
444 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
445 NATIVE_WINDOW_TRANSFORM_FLIP_H;
446 // If the transform has been rotated the axis of flip has been swapped
447 // so we need to swap which flip operations we are performing
Michael Lentine7b902582014-08-19 18:14:06 -0700448 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
449 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 if (is_h_flipped != is_v_flipped) {
Michael Lentine7b902582014-08-19 18:14:06 -0700451 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
452 NATIVE_WINDOW_TRANSFORM_FLIP_H;
453 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 // and apply to the current transform
456 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800457 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458
459 int winWidth = s.active.w;
460 int winHeight = s.active.h;
461 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
462 // If the activeCrop has been rotate the ends are rotated but not
463 // the space itself so when transforming ends back we can't rely on
464 // a modification of the axes of rotation. To account for this we
465 // need to reorient the inverse rotation in terms of the current
466 // axes of rotation.
467 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
468 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
469 if (is_h_flipped == is_v_flipped) {
470 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
471 NATIVE_WINDOW_TRANSFORM_FLIP_H;
472 }
473 winWidth = s.active.h;
474 winHeight = s.active.w;
475 }
476 const Rect winCrop = activeCrop.transform(
477 invTransform, s.active.w, s.active.h);
478
479 // below, crop is intersected with winCrop expressed in crop's coordinate space
480 float xScale = crop.getWidth() / float(winWidth);
481 float yScale = crop.getHeight() / float(winHeight);
482
483 float insetL = winCrop.left * xScale;
484 float insetT = winCrop.top * yScale;
485 float insetR = (winWidth - winCrop.right ) * xScale;
486 float insetB = (winHeight - winCrop.bottom) * yScale;
487
488 crop.left += insetL;
489 crop.top += insetT;
490 crop.right -= insetR;
491 crop.bottom -= insetB;
492
Mathias Agopian13127d82013-03-05 17:47:11 -0800493 return crop;
494}
495
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496#ifdef USE_HWC2
497void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice)
498#else
Mathias Agopian4fec8732012-06-29 14:12:52 -0700499void Layer::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700500 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700501 HWComposer::HWCLayerInterface& layer)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502#endif
Mathias Agopiana350ff92010-08-10 17:14:02 -0700503{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504#ifdef USE_HWC2
505 const auto hwcId = displayDevice->getHwcDisplayId();
506 auto& hwcInfo = mHwcLayers[hwcId];
507#else
Mathias Agopian13127d82013-03-05 17:47:11 -0800508 layer.setDefaultState();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509#endif
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700510
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700511 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512#ifdef USE_HWC2
513 hwcInfo.forceClientComposition = false;
514
515 if (isSecure() && !displayDevice->isSecure()) {
516 hwcInfo.forceClientComposition = true;
517 }
518
519 auto& hwcLayer = hwcInfo.layer;
520#else
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700521 layer.setSkip(false);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700522
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700523 if (isSecure() && !hw->isSecure()) {
524 layer.setSkip(true);
525 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800526#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700527
Mathias Agopian13127d82013-03-05 17:47:11 -0800528 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700529 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530#ifdef USE_HWC2
531 if (!isOpaque(s) || s.alpha != 1.0f) {
532 auto blendMode = mPremultipliedAlpha ?
533 HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
534 auto error = hwcLayer->setBlendMode(blendMode);
535 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:"
536 " %s (%d)", mName.string(), to_string(blendMode).c_str(),
537 to_string(error).c_str(), static_cast<int32_t>(error));
538 }
539#else
Andy McFadden4125a4f2014-01-29 17:17:11 -0800540 if (!isOpaque(s) || s.alpha != 0xFF) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800541 layer.setBlending(mPremultipliedAlpha ?
542 HWC_BLENDING_PREMULT :
543 HWC_BLENDING_COVERAGE);
544 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545#endif
Mathias Agopian13127d82013-03-05 17:47:11 -0800546
547 // apply the layer's transform, followed by the display's global transform
548 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700549 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carrb5d3d262016-03-25 15:08:13 -0700550 if (!s.crop.isEmpty()) {
551 Rect activeCrop(s.crop);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800552 activeCrop = s.active.transform.transform(activeCrop);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000554 if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557#endif
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000558 activeCrop.clear();
559 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800560 activeCrop = s.active.transform.inverse().transform(activeCrop);
Michael Lentine28ea2172014-11-19 18:32:37 -0800561 // This needs to be here as transform.transform(Rect) computes the
562 // transformed rect and then takes the bounding box of the result before
563 // returning. This means
564 // transform.inverse().transform(transform.transform(Rect)) != Rect
565 // in which case we need to make sure the final rect is clipped to the
566 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000567 if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
568 activeCrop.clear();
569 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700570 // mark regions outside the crop as transparent
571 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
572 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom,
573 s.active.w, s.active.h));
574 activeTransparentRegion.orSelf(Rect(0, activeCrop.top,
575 activeCrop.left, activeCrop.bottom));
576 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top,
577 s.active.w, activeCrop.bottom));
578 }
Robert Carr3dcabfa2016-03-01 18:36:58 -0800579 Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700580 if (!s.finalCrop.isEmpty()) {
581 if(!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000582 frame.clear();
583 }
584 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585#ifdef USE_HWC2
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000586 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
587 frame.clear();
588 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 const Transform& tr(displayDevice->getTransform());
590 Rect transformedFrame = tr.transform(frame);
591 auto error = hwcLayer->setDisplayFrame(transformedFrame);
592 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame "
593 "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left,
594 transformedFrame.top, transformedFrame.right,
595 transformedFrame.bottom, to_string(error).c_str(),
596 static_cast<int32_t>(error));
597
598 FloatRect sourceCrop = computeCrop(displayDevice);
599 error = hwcLayer->setSourceCrop(sourceCrop);
600 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop "
601 "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(),
602 sourceCrop.left, sourceCrop.top, sourceCrop.right,
603 sourceCrop.bottom, to_string(error).c_str(),
604 static_cast<int32_t>(error));
605
606 error = hwcLayer->setPlaneAlpha(s.alpha);
607 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: "
608 "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(),
609 static_cast<int32_t>(error));
610
611 error = hwcLayer->setZOrder(s.z);
612 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)",
613 mName.string(), s.z, to_string(error).c_str(),
614 static_cast<int32_t>(error));
615#else
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000616 if (!frame.intersect(hw->getViewport(), &frame)) {
617 frame.clear();
618 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800619 const Transform& tr(hw->getTransform());
620 layer.setFrame(tr.transform(frame));
621 layer.setCrop(computeCrop(hw));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800622 layer.setPlaneAlpha(s.alpha);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623#endif
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800624
Mathias Agopian29a367b2011-07-12 14:51:45 -0700625 /*
626 * Transformations are applied in this order:
627 * 1) buffer orientation/flip/mirror
628 * 2) state transformation (window manager)
629 * 3) layer orientation (screen orientation)
630 * (NOTE: the matrices are multiplied in reverse order)
631 */
632
633 const Transform bufferOrientation(mCurrentTransform);
Robert Carr3dcabfa2016-03-01 18:36:58 -0800634 Transform transform(tr * s.active.transform * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635
636 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) {
637 /*
638 * the code below applies the display's inverse transform to the buffer
639 */
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640#ifdef USE_HWC2
641 uint32_t invTransform = displayDevice->getOrientationTransform();
642#else
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700643 uint32_t invTransform = hw->getOrientationTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644#endif
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 /*
945 * the code below applies the display's inverse transform to the texture transform
946 */
947
948 // create a 4x4 transform matrix from the display transform flags
949 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1);
950 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1);
951 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1);
952
953 mat4 tr;
954 uint32_t transform = hw->getOrientationTransform();
955 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90)
956 tr = tr * rot90;
957 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H)
958 tr = tr * flipH;
959 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V)
960 tr = tr * flipV;
961
962 // calculate the inverse
963 tr = inverse(tr);
964
965 // and finally apply it to the original texture matrix
966 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
967 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
968 }
969
Jamie Genniscbb1a952012-05-08 17:05:52 -0700970 // Set things up for texturing.
Mathias Agopian49457ac2013-08-14 18:20:17 -0700971 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
972 mTexture.setFiltering(useFiltering);
973 mTexture.setMatrix(textureMatrix);
974
975 engine.setupLayerTexturing(mTexture);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700976 } else {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700977 engine.setupLayerBlackedOut();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700978 }
Dan Stozac7014012014-02-14 15:03:43 -0800979 drawWithOpenGL(hw, clip, useIdentityTransform);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700980 engine.disableTexturing();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983
Dan Stozac7014012014-02-14 15:03:43 -0800984void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw,
985 const Region& /* clip */, float red, float green, float blue,
986 float alpha) const
Mathias Agopian13127d82013-03-05 17:47:11 -0800987{
Mathias Agopian19733a32013-08-28 18:13:56 -0700988 RenderEngine& engine(mFlinger->getRenderEngine());
Dan Stozac7014012014-02-14 15:03:43 -0800989 computeGeometry(hw, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700990 engine.setupFillWithColor(red, green, blue, alpha);
991 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800992}
993
994void Layer::clearWithOpenGL(
995 const sp<const DisplayDevice>& hw, const Region& clip) const {
996 clearWithOpenGL(hw, clip, 0,0,0,0);
997}
998
Dan Stozac7014012014-02-14 15:03:43 -0800999void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw,
1000 const Region& /* clip */, bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001001 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08001002
Dan Stozac7014012014-02-14 15:03:43 -08001003 computeGeometry(hw, mMesh, useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -08001004
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 /*
1006 * NOTE: the way we compute the texture coordinates here produces
1007 * different results than when we take the HWC path -- in the later case
1008 * the "source crop" is rounded to texel boundaries.
1009 * This can produce significantly different results when the texture
1010 * is scaled by a large amount.
1011 *
1012 * The GL code below is more logical (imho), and the difference with
1013 * HWC is due to a limitation of the HWC API to integers -- a question
Mathias Agopianc1c05de2013-09-17 23:45:22 -07001014 * is suspend is whether we should ignore this problem or revert to
Mathias Agopian13127d82013-03-05 17:47:11 -08001015 * GL composition when a buffer scaling is applied (maybe with some
1016 * minimal value)? Or, we could make GL behave like HWC -- but this feel
1017 * like more of a hack.
1018 */
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001019 Rect win(computeBounds());
1020
Robert Carrb5d3d262016-03-25 15:08:13 -07001021 if (!s.finalCrop.isEmpty()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001022 win = s.active.transform.transform(win);
Robert Carrb5d3d262016-03-25 15:08:13 -07001023 if (!win.intersect(s.finalCrop, &win)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001024 win.clear();
1025 }
1026 win = s.active.transform.inverse().transform(win);
1027 if (!win.intersect(computeBounds(), &win)) {
1028 win.clear();
1029 }
1030 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001031
Mathias Agopian3f844832013-08-07 21:24:32 -07001032 float left = float(win.left) / float(s.active.w);
1033 float top = float(win.top) / float(s.active.h);
1034 float right = float(win.right) / float(s.active.w);
1035 float bottom = float(win.bottom) / float(s.active.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001036
Mathias Agopian875d8e12013-06-07 15:35:48 -07001037 // TODO: we probably want to generate the texture coords with the mesh
1038 // here we assume that we only have 4 vertices
Mathias Agopianff2ed702013-09-01 21:36:12 -07001039 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
1040 texCoords[0] = vec2(left, 1.0f - top);
1041 texCoords[1] = vec2(left, 1.0f - bottom);
1042 texCoords[2] = vec2(right, 1.0f - bottom);
1043 texCoords[3] = vec2(right, 1.0f - top);
Mathias Agopian13127d82013-03-05 17:47:11 -08001044
Mathias Agopian875d8e12013-06-07 15:35:48 -07001045 RenderEngine& engine(mFlinger->getRenderEngine());
Andy McFadden4125a4f2014-01-29 17:17:11 -08001046 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha);
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001047 engine.drawMesh(mMesh);
Mathias Agopian875d8e12013-06-07 15:35:48 -07001048 engine.disableBlending();
Mathias Agopian13127d82013-03-05 17:47:11 -08001049}
1050
Dan Stoza9e56aa02015-11-02 13:00:03 -08001051#ifdef USE_HWC2
1052void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type,
1053 bool callIntoHwc) {
1054 if (mHwcLayers.count(hwcId) == 0) {
1055 ALOGE("setCompositionType called without a valid HWC layer");
1056 return;
1057 }
1058 auto& hwcInfo = mHwcLayers[hwcId];
1059 auto& hwcLayer = hwcInfo.layer;
1060 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(),
1061 to_string(type).c_str(), static_cast<int>(callIntoHwc));
1062 if (hwcInfo.compositionType != type) {
1063 ALOGV(" actually setting");
1064 hwcInfo.compositionType = type;
1065 if (callIntoHwc) {
1066 auto error = hwcLayer->setCompositionType(type);
1067 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set "
1068 "composition type %s: %s (%d)", mName.string(),
1069 to_string(type).c_str(), to_string(error).c_str(),
1070 static_cast<int32_t>(error));
1071 }
1072 }
1073}
1074
1075HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
1076 if (mHwcLayers.count(hwcId) == 0) {
1077 ALOGE("getCompositionType called without a valid HWC layer");
1078 return HWC2::Composition::Invalid;
1079 }
1080 return mHwcLayers.at(hwcId).compositionType;
1081}
1082
1083void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
1084 if (mHwcLayers.count(hwcId) == 0) {
1085 ALOGE("setClearClientTarget called without a valid HWC layer");
1086 return;
1087 }
1088 mHwcLayers[hwcId].clearClientTarget = clear;
1089}
1090
1091bool Layer::getClearClientTarget(int32_t hwcId) const {
1092 if (mHwcLayers.count(hwcId) == 0) {
1093 ALOGE("getClearClientTarget called without a valid HWC layer");
1094 return false;
1095 }
1096 return mHwcLayers.at(hwcId).clearClientTarget;
1097}
1098#endif
1099
Ruben Brunk1681d952014-06-27 15:51:55 -07001100uint32_t Layer::getProducerStickyTransform() const {
1101 int producerStickyTransform = 0;
1102 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
1103 if (ret != OK) {
1104 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
1105 strerror(-ret), ret);
1106 return 0;
1107 }
1108 return static_cast<uint32_t>(producerStickyTransform);
1109}
1110
Dan Stozacac35382016-01-27 12:21:06 -08001111uint64_t Layer::getHeadFrameNumber() const {
1112 Mutex::Autolock lock(mQueueItemLock);
1113 if (!mQueueItems.empty()) {
1114 return mQueueItems[0].mFrameNumber;
1115 } else {
1116 return mCurrentFrameNumber;
1117 }
1118}
1119
1120bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
1121 if (point->getFrameNumber() <= mCurrentFrameNumber) {
1122 // Don't bother with a SyncPoint, since we've already latched the
1123 // relevant frame
1124 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001125 }
1126
Dan Stozacac35382016-01-27 12:21:06 -08001127 Mutex::Autolock lock(mLocalSyncPointMutex);
1128 mLocalSyncPoints.push_back(point);
1129 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -07001130}
1131
Mathias Agopian13127d82013-03-05 17:47:11 -08001132void Layer::setFiltering(bool filtering) {
1133 mFiltering = filtering;
1134}
1135
1136bool Layer::getFiltering() const {
1137 return mFiltering;
1138}
1139
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001140// As documented in libhardware header, formats in the range
1141// 0x100 - 0x1FF are specific to the HAL implementation, and
1142// are known to have no alpha channel
1143// TODO: move definition for device-specific range into
1144// hardware.h, instead of using hard-coded values here.
1145#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1146
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001147bool Layer::getOpacityForFormat(uint32_t format) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001148 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1149 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001150 }
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001151 switch (format) {
1152 case HAL_PIXEL_FORMAT_RGBA_8888:
1153 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopiandd533712013-07-26 15:31:39 -07001154 return false;
Mathias Agopian5773d3f2013-07-25 19:24:31 -07001155 }
1156 // in all other case, we have no blending (also for unknown formats)
Mathias Agopiandd533712013-07-26 15:31:39 -07001157 return true;
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001158}
1159
Mathias Agopian13127d82013-03-05 17:47:11 -08001160// ----------------------------------------------------------------------------
1161// local state
1162// ----------------------------------------------------------------------------
1163
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001164static void boundPoint(vec2* point, const Rect& crop) {
1165 if (point->x < crop.left) {
1166 point->x = crop.left;
1167 }
1168 if (point->x > crop.right) {
1169 point->x = crop.right;
1170 }
1171 if (point->y < crop.top) {
1172 point->y = crop.top;
1173 }
1174 if (point->y > crop.bottom) {
1175 point->y = crop.bottom;
1176 }
1177}
1178
Dan Stozac7014012014-02-14 15:03:43 -08001179void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
1180 bool useIdentityTransform) const
Mathias Agopian13127d82013-03-05 17:47:11 -08001181{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001182 const Layer::State& s(getDrawingState());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001183 const Transform tr(hw->getTransform());
Mathias Agopian13127d82013-03-05 17:47:11 -08001184 const uint32_t hw_h = hw->getHeight();
1185 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -07001186 if (!s.crop.isEmpty()) {
1187 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -08001188 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -07001189 // subtract the transparent region and snap to the bounds
Mathias Agopianf3e85d42013-05-10 18:01:12 -07001190 win = reduce(win, s.activeTransparentRegion);
Mathias Agopian3f844832013-08-07 21:24:32 -07001191
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001192 vec2 lt = vec2(win.left, win.top);
1193 vec2 lb = vec2(win.left, win.bottom);
1194 vec2 rb = vec2(win.right, win.bottom);
1195 vec2 rt = vec2(win.right, win.top);
1196
1197 if (!useIdentityTransform) {
1198 lt = s.active.transform.transform(lt);
1199 lb = s.active.transform.transform(lb);
1200 rb = s.active.transform.transform(rb);
1201 rt = s.active.transform.transform(rt);
1202 }
1203
Robert Carrb5d3d262016-03-25 15:08:13 -07001204 if (!s.finalCrop.isEmpty()) {
1205 boundPoint(&lt, s.finalCrop);
1206 boundPoint(&lb, s.finalCrop);
1207 boundPoint(&rb, s.finalCrop);
1208 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001209 }
1210
Mathias Agopianff2ed702013-09-01 21:36:12 -07001211 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001212 position[0] = tr.transform(lt);
1213 position[1] = tr.transform(lb);
1214 position[2] = tr.transform(rb);
1215 position[3] = tr.transform(rt);
Mathias Agopian3f844832013-08-07 21:24:32 -07001216 for (size_t i=0 ; i<4 ; i++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -07001217 position[i].y = hw_h - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 }
1219}
Eric Hassoldac45e6b2011-02-10 14:41:26 -08001220
Andy McFadden4125a4f2014-01-29 17:17:11 -08001221bool Layer::isOpaque(const Layer::State& s) const
Mathias Agopiana7f66922010-05-26 22:08:52 -07001222{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001223 // if we don't have a buffer yet, we're translucent regardless of the
1224 // layer's opaque flag.
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001225 if (mActiveBuffer == 0) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001226 return false;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001227 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228
1229 // if the layer has the opaque flag, then we're always opaque,
1230 // otherwise we use the current buffer's format.
Andy McFadden4125a4f2014-01-29 17:17:11 -08001231 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
Mathias Agopiana7f66922010-05-26 22:08:52 -07001232}
1233
Dan Stoza23116082015-06-18 14:58:39 -07001234bool Layer::isSecure() const
1235{
1236 const Layer::State& s(mDrawingState);
1237 return (s.flags & layer_state_t::eLayerSecure);
1238}
1239
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001240bool Layer::isProtected() const
1241{
Mathias Agopiana67932f2011-04-20 14:20:59 -07001242 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08001243 return (activeBuffer != 0) &&
1244 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1245}
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001246
Mathias Agopian13127d82013-03-05 17:47:11 -08001247bool Layer::isFixedSize() const {
Robert Carrc3574f72016-03-24 12:19:32 -07001248 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian13127d82013-03-05 17:47:11 -08001249}
1250
1251bool Layer::isCropped() const {
1252 return !mCurrentCrop.isEmpty();
1253}
1254
1255bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
1256 return mNeedsFiltering || hw->needsFiltering();
1257}
1258
1259void Layer::setVisibleRegion(const Region& visibleRegion) {
1260 // always called from main thread
1261 this->visibleRegion = visibleRegion;
1262}
1263
1264void Layer::setCoveredRegion(const Region& coveredRegion) {
1265 // always called from main thread
1266 this->coveredRegion = coveredRegion;
1267}
1268
1269void Layer::setVisibleNonTransparentRegion(const Region&
1270 setVisibleNonTransparentRegion) {
1271 // always called from main thread
1272 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
1273}
1274
1275// ----------------------------------------------------------------------------
1276// transaction
1277// ----------------------------------------------------------------------------
1278
Dan Stoza7dde5992015-05-22 09:51:44 -07001279void Layer::pushPendingState() {
1280 if (!mCurrentState.modified) {
1281 return;
1282 }
1283
Dan Stoza7dde5992015-05-22 09:51:44 -07001284 // If this transaction is waiting on the receipt of a frame, generate a sync
1285 // point and send it to the remote layer.
1286 if (mCurrentState.handle != nullptr) {
1287 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get());
1288 sp<Layer> handleLayer = handle->owner.promote();
1289 if (handleLayer == nullptr) {
1290 ALOGE("[%s] Unable to promote Layer handle", mName.string());
1291 // If we can't promote the layer we are intended to wait on,
1292 // then it is expired or otherwise invalid. Allow this transaction
1293 // to be applied as per normal (no synchronization).
1294 mCurrentState.handle = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -08001295 } else {
1296 auto syncPoint = std::make_shared<SyncPoint>(
1297 mCurrentState.frameNumber);
Dan Stozacac35382016-01-27 12:21:06 -08001298 if (handleLayer->addSyncPoint(syncPoint)) {
1299 mRemoteSyncPoints.push_back(std::move(syncPoint));
1300 } else {
1301 // We already missed the frame we're supposed to synchronize
1302 // on, so go ahead and apply the state update
1303 mCurrentState.handle = nullptr;
1304 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001305 }
1306
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 // Wake us up to check if the frame has been received
1308 setTransactionFlags(eTransactionNeeded);
1309 }
1310 mPendingStates.push_back(mCurrentState);
1311}
1312
Pablo Ceballos05289c22016-04-14 15:49:55 -07001313void Layer::popPendingState(State* stateToCommit) {
1314 auto oldFlags = stateToCommit->flags;
1315 *stateToCommit = mPendingStates[0];
1316 stateToCommit->flags = (oldFlags & ~stateToCommit->mask) |
1317 (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -07001318
1319 mPendingStates.removeAt(0);
1320}
1321
Pablo Ceballos05289c22016-04-14 15:49:55 -07001322bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 bool stateUpdateAvailable = false;
1324 while (!mPendingStates.empty()) {
1325 if (mPendingStates[0].handle != nullptr) {
1326 if (mRemoteSyncPoints.empty()) {
1327 // If we don't have a sync point for this, apply it anyway. It
1328 // will be visually wrong, but it should keep us from getting
1329 // into too much trouble.
1330 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001331 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001332 stateUpdateAvailable = true;
1333 continue;
1334 }
1335
Dan Stozacac35382016-01-27 12:21:06 -08001336 if (mRemoteSyncPoints.front()->getFrameNumber() !=
1337 mPendingStates[0].frameNumber) {
1338 ALOGE("[%s] Unexpected sync point frame number found",
1339 mName.string());
1340
1341 // Signal our end of the sync point and then dispose of it
1342 mRemoteSyncPoints.front()->setTransactionApplied();
1343 mRemoteSyncPoints.pop_front();
1344 continue;
1345 }
1346
Dan Stoza7dde5992015-05-22 09:51:44 -07001347 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1348 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001349 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001350 stateUpdateAvailable = true;
1351
1352 // Signal our end of the sync point and then dispose of it
1353 mRemoteSyncPoints.front()->setTransactionApplied();
1354 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001355 } else {
1356 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001358 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001359 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 stateUpdateAvailable = true;
1361 }
1362 }
1363
1364 // If we still have pending updates, wake SurfaceFlinger back up and point
1365 // it at this layer so we can process them
1366 if (!mPendingStates.empty()) {
1367 setTransactionFlags(eTransactionNeeded);
1368 mFlinger->setTransactionFlags(eTraversalNeeded);
1369 }
1370
1371 mCurrentState.modified = false;
1372 return stateUpdateAvailable;
1373}
1374
1375void Layer::notifyAvailableFrames() {
Dan Stozacac35382016-01-27 12:21:06 -08001376 auto headFrameNumber = getHeadFrameNumber();
1377 Mutex::Autolock lock(mLocalSyncPointMutex);
1378 for (auto& point : mLocalSyncPoints) {
1379 if (headFrameNumber >= point->getFrameNumber()) {
1380 point->setFrameAvailable();
1381 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001382 }
1383}
1384
Mathias Agopian13127d82013-03-05 17:47:11 -08001385uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001386 ATRACE_CALL();
1387
Dan Stoza7dde5992015-05-22 09:51:44 -07001388 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001389 Layer::State c = getCurrentState();
1390 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001391 return 0;
1392 }
1393
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001394 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001396 const bool sizeChanged = (c.requested.w != s.requested.w) ||
1397 (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001398
1399 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001400 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001401 ALOGD_IF(DEBUG_RESIZE,
Andy McFadden69052052012-09-14 16:10:11 -07001402 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001403 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001404 " requested={ wh={%4u,%4u} }}\n"
Mathias Agopian419e1962012-05-23 14:34:07 -07001405 " drawing={ 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",
Robert Carrc3574f72016-03-24 12:19:32 -07001407 this, getName().string(), mCurrentTransform,
1408 getEffectiveScalingMode(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001409 c.active.w, c.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001410 c.crop.left,
1411 c.crop.top,
1412 c.crop.right,
1413 c.crop.bottom,
1414 c.crop.getWidth(),
1415 c.crop.getHeight(),
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001416 c.requested.w, c.requested.h,
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001417 s.active.w, s.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001418 s.crop.left,
1419 s.crop.top,
1420 s.crop.right,
1421 s.crop.bottom,
1422 s.crop.getWidth(),
1423 s.crop.getHeight(),
1424 s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001426 // record the new size, form this point on, when the client request
1427 // a buffer, it'll get the new size.
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001428 mSurfaceFlingerConsumer->setDefaultBufferSize(
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001429 c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001431
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001432 if (!isFixedSize()) {
1433
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001434 const bool resizePending = (c.requested.w != c.active.w) ||
1435 (c.requested.h != c.active.h);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001436
Dan Stoza9e9b0442015-04-22 14:59:08 -07001437 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001438 // don't let Layer::doTransaction update the drawing state
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001439 // if we have a pending resize, unless we are in fixed-size mode.
1440 // the drawing state will be updated only once we receive a buffer
1441 // with the correct size.
1442 //
1443 // in particular, we want to make sure the clip (which is part
1444 // of the geometry state) is latched together with the size but is
1445 // latched immediately when no resizing is involved.
Dan Stoza9e9b0442015-04-22 14:59:08 -07001446 //
1447 // If a sideband stream is attached, however, we want to skip this
1448 // optimization so that transactions aren't missed when a buffer
1449 // never arrives
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001450
1451 flags |= eDontUpdateGeometryState;
1452 }
1453 }
1454
Mathias Agopian13127d82013-03-05 17:47:11 -08001455 // always set active to requested, unless we're asked not to
1456 // this is used by Layer, which special cases resizes.
1457 if (flags & eDontUpdateGeometryState) {
1458 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001459 c.active = c.requested;
Mathias Agopian13127d82013-03-05 17:47:11 -08001460 }
1461
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001462 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 // invalidate and recompute the visible regions if needed
1464 flags |= Layer::eVisibleRegion;
1465 }
1466
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001467 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001468 // invalidate and recompute the visible regions if needed
1469 flags |= eVisibleRegion;
1470 this->contentDirty = true;
1471
1472 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001473 const uint8_t type = c.active.transform.getType();
1474 mNeedsFiltering = (!c.active.transform.preserveRects() ||
Mathias Agopian13127d82013-03-05 17:47:11 -08001475 (type >= Transform::SCALE));
1476 }
1477
1478 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001479 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001480 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481}
1482
Pablo Ceballos05289c22016-04-14 15:49:55 -07001483void Layer::commitTransaction(const State& stateToCommit) {
1484 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001485}
1486
Mathias Agopian13127d82013-03-05 17:47:11 -08001487uint32_t Layer::getTransactionFlags(uint32_t flags) {
1488 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1489}
1490
1491uint32_t Layer::setTransactionFlags(uint32_t flags) {
1492 return android_atomic_or(flags, &mTransactionFlags);
1493}
1494
1495bool Layer::setPosition(float x, float y) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001496 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001497 return false;
1498 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001499
1500 // We update the requested and active position simultaneously because
1501 // we want to apply the position portion of the transform matrix immediately,
1502 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001503 mCurrentState.requested.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001504 mCurrentState.active.transform.set(x, y);
1505
Dan Stoza7dde5992015-05-22 09:51:44 -07001506 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001507 setTransactionFlags(eTransactionNeeded);
1508 return true;
1509}
1510bool Layer::setLayer(uint32_t z) {
1511 if (mCurrentState.z == z)
1512 return false;
1513 mCurrentState.sequence++;
1514 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001515 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001516 setTransactionFlags(eTransactionNeeded);
1517 return true;
1518}
1519bool Layer::setSize(uint32_t w, uint32_t h) {
1520 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1521 return false;
1522 mCurrentState.requested.w = w;
1523 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001524 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001525 setTransactionFlags(eTransactionNeeded);
1526 return true;
1527}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001528#ifdef USE_HWC2
1529bool Layer::setAlpha(float alpha) {
1530#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001531bool Layer::setAlpha(uint8_t alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001532#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001533 if (mCurrentState.alpha == alpha)
1534 return false;
1535 mCurrentState.sequence++;
1536 mCurrentState.alpha = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001537 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001538 setTransactionFlags(eTransactionNeeded);
1539 return true;
1540}
1541bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1542 mCurrentState.sequence++;
Robert Carr3dcabfa2016-03-01 18:36:58 -08001543 mCurrentState.requested.transform.set(
Mathias Agopian13127d82013-03-05 17:47:11 -08001544 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001545 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001546 setTransactionFlags(eTransactionNeeded);
1547 return true;
1548}
1549bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001550 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001551 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001552 setTransactionFlags(eTransactionNeeded);
1553 return true;
1554}
1555bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1556 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1557 if (mCurrentState.flags == newFlags)
1558 return false;
1559 mCurrentState.sequence++;
1560 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001561 mCurrentState.mask = mask;
1562 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001563 setTransactionFlags(eTransactionNeeded);
1564 return true;
1565}
1566bool Layer::setCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001567 if (mCurrentState.crop == crop)
Mathias Agopian13127d82013-03-05 17:47:11 -08001568 return false;
1569 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001570 mCurrentState.crop = crop;
Dan Stoza7dde5992015-05-22 09:51:44 -07001571 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001572 setTransactionFlags(eTransactionNeeded);
1573 return true;
1574}
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001575bool Layer::setFinalCrop(const Rect& crop) {
Robert Carrb5d3d262016-03-25 15:08:13 -07001576 if (mCurrentState.finalCrop == crop)
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001577 return false;
1578 mCurrentState.sequence++;
Robert Carrb5d3d262016-03-25 15:08:13 -07001579 mCurrentState.finalCrop = crop;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001580 mCurrentState.modified = true;
1581 setTransactionFlags(eTransactionNeeded);
1582 return true;
1583}
Mathias Agopian13127d82013-03-05 17:47:11 -08001584
Robert Carrc3574f72016-03-24 12:19:32 -07001585bool Layer::setOverrideScalingMode(int32_t scalingMode) {
1586 if (scalingMode == mOverrideScalingMode)
1587 return false;
1588 mOverrideScalingMode = scalingMode;
1589 return true;
1590}
1591
1592uint32_t Layer::getEffectiveScalingMode() const {
1593 if (mOverrideScalingMode >= 0) {
1594 return mOverrideScalingMode;
1595 }
1596 return mCurrentScalingMode;
1597}
1598
Mathias Agopian13127d82013-03-05 17:47:11 -08001599bool Layer::setLayerStack(uint32_t layerStack) {
1600 if (mCurrentState.layerStack == layerStack)
1601 return false;
1602 mCurrentState.sequence++;
1603 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001604 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001605 setTransactionFlags(eTransactionNeeded);
1606 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001607}
1608
Dan Stoza7dde5992015-05-22 09:51:44 -07001609void Layer::deferTransactionUntil(const sp<IBinder>& handle,
1610 uint64_t frameNumber) {
1611 mCurrentState.handle = handle;
1612 mCurrentState.frameNumber = frameNumber;
1613 // We don't set eTransactionNeeded, because just receiving a deferral
1614 // request without any other state updates shouldn't actually induce a delay
1615 mCurrentState.modified = true;
1616 pushPendingState();
Dan Stoza792e5292016-02-11 11:43:58 -08001617 mCurrentState.handle = nullptr;
1618 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001619 mCurrentState.modified = false;
1620}
1621
Dan Stozaee44edd2015-03-23 15:50:23 -07001622void Layer::useSurfaceDamage() {
1623 if (mFlinger->mForceFullDamage) {
1624 surfaceDamageRegion = Region::INVALID_REGION;
1625 } else {
1626 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
1627 }
1628}
1629
1630void Layer::useEmptyDamage() {
1631 surfaceDamageRegion.clear();
1632}
1633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634// ----------------------------------------------------------------------------
1635// pageflip handling...
1636// ----------------------------------------------------------------------------
1637
Dan Stoza6b9454d2014-11-07 16:00:59 -08001638bool Layer::shouldPresentNow(const DispSync& dispSync) const {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001639 if (mSidebandStreamChanged || mAutoRefresh) {
Dan Stozad87defa2015-07-29 16:15:50 -07001640 return true;
1641 }
1642
Dan Stoza6b9454d2014-11-07 16:00:59 -08001643 Mutex::Autolock lock(mQueueItemLock);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001644 if (mQueueItems.empty()) {
1645 return false;
1646 }
1647 auto timestamp = mQueueItems[0].mTimestamp;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001648 nsecs_t expectedPresent =
1649 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
Dan Stoza0eb2d392015-07-06 12:56:50 -07001650
1651 // Ignore timestamps more than a second in the future
1652 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
1653 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible "
1654 "relative to expectedPresent %" PRId64, mName.string(), timestamp,
1655 expectedPresent);
1656
1657 bool isDue = timestamp < expectedPresent;
1658 return isDue || !isPlausible;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001659}
1660
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001661bool Layer::onPreComposition() {
1662 mRefreshPending = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001663 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001664}
1665
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001666void Layer::onPostComposition() {
1667 if (mFrameLatencyNeeded) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001668 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
Jamie Gennis82dbc742012-11-08 19:23:28 -08001669 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1670
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001671 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
Jamie Gennis789a6c32013-02-25 13:37:54 -08001672 if (frameReadyFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001673 mFrameTracker.setFrameReadyFence(frameReadyFence);
1674 } else {
1675 // There was no fence for this frame, so assume that it was ready
1676 // to be presented at the desired present time.
1677 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1678 }
1679
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001680 const HWComposer& hwc = mFlinger->getHwComposer();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001681#ifdef USE_HWC2
1682 sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY);
1683#else
Jamie Gennis82dbc742012-11-08 19:23:28 -08001684 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001685#endif
Jamie Gennis789a6c32013-02-25 13:37:54 -08001686 if (presentFence->isValid()) {
Jamie Gennis82dbc742012-11-08 19:23:28 -08001687 mFrameTracker.setActualPresentFence(presentFence);
1688 } else {
1689 // The HWC doesn't support present fences, so use the refresh
1690 // timestamp instead.
1691 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1692 mFrameTracker.setActualPresentTime(presentTime);
1693 }
1694
1695 mFrameTracker.advanceFrame();
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001696 mFrameLatencyNeeded = false;
1697 }
1698}
1699
Dan Stoza9e56aa02015-11-02 13:00:03 -08001700#ifdef USE_HWC2
1701void Layer::releasePendingBuffer() {
1702 mSurfaceFlingerConsumer->releasePendingBuffer();
1703}
1704#endif
1705
Mathias Agopianda27af92012-09-13 18:17:13 -07001706bool Layer::isVisible() const {
Mathias Agopian13127d82013-03-05 17:47:11 -08001707 const Layer::State& s(mDrawingState);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001708#ifdef USE_HWC2
1709 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f
1710 && (mActiveBuffer != NULL || mSidebandStream != NULL);
1711#else
Mathias Agopian13127d82013-03-05 17:47:11 -08001712 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
Wonsik Kimafe30812014-03-31 23:16:08 +09001713 && (mActiveBuffer != NULL || mSidebandStream != NULL);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001714#endif
Mathias Agopianda27af92012-09-13 18:17:13 -07001715}
1716
Mathias Agopian4fec8732012-06-29 14:12:52 -07001717Region Layer::latchBuffer(bool& recomputeVisibleRegions)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001719 ATRACE_CALL();
1720
Jesse Hall399184a2014-03-03 15:42:54 -08001721 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
1722 // mSidebandStreamChanged was true
1723 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
Dan Stoza12e0a272015-05-05 14:00:52 -07001724 if (mSidebandStream != NULL) {
1725 setTransactionFlags(eTransactionNeeded);
1726 mFlinger->setTransactionFlags(eTraversalNeeded);
1727 }
Jesse Hall5bf786d2014-09-30 10:35:11 -07001728 recomputeVisibleRegions = true;
1729
1730 const State& s(getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001731 return s.active.transform.transform(Region(Rect(s.active.w, s.active.h)));
Jesse Hall399184a2014-03-03 15:42:54 -08001732 }
1733
Mathias Agopian4fec8732012-06-29 14:12:52 -07001734 Region outDirtyRegion;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001735 if (mQueuedFrames > 0 || mAutoRefresh) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001736
1737 // if we've already called updateTexImage() without going through
1738 // a composition step, we have to skip this layer at this point
1739 // because we cannot call updateTeximage() without a corresponding
1740 // compositionComplete() call.
1741 // we'll trigger an update in onPreComposition().
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001742 if (mRefreshPending) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001743 return outDirtyRegion;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001744 }
1745
Jamie Gennis351a5132011-09-14 18:23:37 -07001746 // Capture the old state of the layer for comparisons later
Andy McFadden4125a4f2014-01-29 17:17:11 -08001747 const State& s(getDrawingState());
1748 const bool oldOpacity = isOpaque(s);
Jamie Gennis351a5132011-09-14 18:23:37 -07001749 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
Jamie Gennisdb5230f2011-07-28 14:54:07 -07001750
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001751 struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001752 Layer::State& front;
1753 Layer::State& current;
1754 bool& recomputeVisibleRegions;
Ruben Brunk1681d952014-06-27 15:51:55 -07001755 bool stickyTransformSet;
Robert Carrb5d3d262016-03-25 15:08:13 -07001756 const char* name;
Robert Carrc3574f72016-03-24 12:19:32 -07001757 int32_t overrideScalingMode;
Robert Carrb5d3d262016-03-25 15:08:13 -07001758
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001759 Reject(Layer::State& front, Layer::State& current,
Robert Carrb5d3d262016-03-25 15:08:13 -07001760 bool& recomputeVisibleRegions, bool stickySet,
Robert Carrc3574f72016-03-24 12:19:32 -07001761 const char* name,
1762 int32_t overrideScalingMode)
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001763 : front(front), current(current),
Ruben Brunk1681d952014-06-27 15:51:55 -07001764 recomputeVisibleRegions(recomputeVisibleRegions),
Robert Carrb5d3d262016-03-25 15:08:13 -07001765 stickyTransformSet(stickySet),
Robert Carrc3574f72016-03-24 12:19:32 -07001766 name(name),
1767 overrideScalingMode(overrideScalingMode) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001768 }
1769
1770 virtual bool reject(const sp<GraphicBuffer>& buf,
Dan Stoza11611f92015-03-12 15:12:44 -07001771 const BufferItem& item) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001772 if (buf == NULL) {
1773 return false;
1774 }
1775
1776 uint32_t bufWidth = buf->getWidth();
1777 uint32_t bufHeight = buf->getHeight();
1778
1779 // check that we received a buffer of the right size
1780 // (Take the buffer's orientation into account)
1781 if (item.mTransform & Transform::ROT_90) {
1782 swap(bufWidth, bufHeight);
1783 }
1784
Robert Carrc3574f72016-03-24 12:19:32 -07001785 int actualScalingMode = overrideScalingMode >= 0 ?
1786 overrideScalingMode : item.mScalingMode;
1787 bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001788 if (front.active != front.requested) {
1789
1790 if (isFixedSize ||
1791 (bufWidth == front.requested.w &&
1792 bufHeight == front.requested.h))
1793 {
1794 // Here we pretend the transaction happened by updating the
1795 // current and drawing states. Drawing state is only accessed
1796 // in this thread, no need to have it locked
1797 front.active = front.requested;
1798
1799 // We also need to update the current state so that
1800 // we don't end-up overwriting the drawing state with
1801 // this stale current state during the next transaction
1802 //
1803 // NOTE: We don't need to hold the transaction lock here
1804 // because State::active is only accessed from this thread.
1805 current.active = front.active;
1806
1807 // recompute visible region
1808 recomputeVisibleRegions = true;
1809 }
1810
1811 ALOGD_IF(DEBUG_RESIZE,
Robert Carrb5d3d262016-03-25 15:08:13 -07001812 "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001813 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
Robert Carrb5d3d262016-03-25 15:08:13 -07001814 " requested={ wh={%4u,%4u} }}\n",
1815 name,
Andy McFadden69052052012-09-14 16:10:11 -07001816 bufWidth, bufHeight, item.mTransform, item.mScalingMode,
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001817 front.active.w, front.active.h,
Robert Carrb5d3d262016-03-25 15:08:13 -07001818 front.crop.left,
1819 front.crop.top,
1820 front.crop.right,
1821 front.crop.bottom,
1822 front.crop.getWidth(),
1823 front.crop.getHeight(),
1824 front.requested.w, front.requested.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001825 }
1826
Ruben Brunk1681d952014-06-27 15:51:55 -07001827 if (!isFixedSize && !stickyTransformSet) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001828 if (front.active.w != bufWidth ||
1829 front.active.h != bufHeight) {
Mathias Agopian4824d402012-06-04 18:16:30 -07001830 // reject this buffer
Robert Carrb5d3d262016-03-25 15:08:13 -07001831 ALOGE("[%s] rejecting buffer: "
1832 "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
1833 name, bufWidth, bufHeight, front.active.w, front.active.h);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001834 return true;
1835 }
1836 }
Mathias Agopian2ca79392013-04-02 18:30:32 -07001837
1838 // if the transparent region has changed (this test is
1839 // conservative, but that's fine, worst case we're doing
1840 // a bit of extra work), we latch the new one and we
1841 // trigger a visible-region recompute.
1842 if (!front.activeTransparentRegion.isTriviallyEqual(
1843 front.requestedTransparentRegion)) {
1844 front.activeTransparentRegion = front.requestedTransparentRegion;
Mathias Agopian6c67f0f2013-04-12 16:58:11 -07001845
1846 // We also need to update the current state so that
1847 // we don't end-up overwriting the drawing state with
1848 // this stale current state during the next transaction
1849 //
1850 // NOTE: We don't need to hold the transaction lock here
1851 // because State::active is only accessed from this thread.
1852 current.activeTransparentRegion = front.activeTransparentRegion;
1853
1854 // recompute visible region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001855 recomputeVisibleRegions = true;
1856 }
1857
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001858 return false;
1859 }
1860 };
1861
Ruben Brunk1681d952014-06-27 15:51:55 -07001862 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
Robert Carrc3574f72016-03-24 12:19:32 -07001863 getProducerStickyTransform() != 0, mName.string(),
1864 mOverrideScalingMode);
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001865
Dan Stozacac35382016-01-27 12:21:06 -08001866
1867 // Check all of our local sync points to ensure that all transactions
1868 // which need to have been applied prior to the frame which is about to
1869 // be latched have signaled
1870
1871 auto headFrameNumber = getHeadFrameNumber();
1872 bool matchingFramesFound = false;
1873 bool allTransactionsApplied = true;
Dan Stozaa4650a52015-05-12 12:56:16 -07001874 {
Dan Stozacac35382016-01-27 12:21:06 -08001875 Mutex::Autolock lock(mLocalSyncPointMutex);
1876 for (auto& point : mLocalSyncPoints) {
1877 if (point->getFrameNumber() > headFrameNumber) {
1878 break;
1879 }
1880
1881 matchingFramesFound = true;
1882
1883 if (!point->frameIsAvailable()) {
1884 // We haven't notified the remote layer that the frame for
1885 // this point is available yet. Notify it now, and then
1886 // abort this attempt to latch.
1887 point->setFrameAvailable();
1888 allTransactionsApplied = false;
1889 break;
1890 }
1891
1892 allTransactionsApplied &= point->transactionIsApplied();
Dan Stoza7dde5992015-05-22 09:51:44 -07001893 }
1894 }
1895
Dan Stozacac35382016-01-27 12:21:06 -08001896 if (matchingFramesFound && !allTransactionsApplied) {
1897 mFlinger->signalLayerUpdate();
1898 return outDirtyRegion;
Dan Stozaa4650a52015-05-12 12:56:16 -07001899 }
1900
Pablo Ceballos06312182015-10-07 16:32:12 -07001901 // This boolean is used to make sure that SurfaceFlinger's shadow copy
1902 // of the buffer queue isn't modified when the buffer queue is returning
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001903 // BufferItem's that weren't actually queued. This can happen in shared
Pablo Ceballos06312182015-10-07 16:32:12 -07001904 // buffer mode.
1905 bool queuedBuffer = false;
Andy McFadden41d67d72014-04-25 16:58:34 -07001906 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r,
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001907 mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer,
Dan Stozacac35382016-01-27 12:21:06 -08001908 mLastFrameNumberReceived);
Andy McFadden1585c4d2013-06-28 13:52:40 -07001909 if (updateResult == BufferQueue::PRESENT_LATER) {
1910 // Producer doesn't want buffer to be displayed yet. Signal a
1911 // layer update so we check again at the next opportunity.
1912 mFlinger->signalLayerUpdate();
1913 return outDirtyRegion;
Dan Stozaecc50402015-04-28 14:42:06 -07001914 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
1915 // If the buffer has been rejected, remove it from the shadow queue
1916 // and return early
Pablo Ceballos06312182015-10-07 16:32:12 -07001917 if (queuedBuffer) {
1918 Mutex::Autolock lock(mQueueItemLock);
1919 mQueueItems.removeAt(0);
1920 android_atomic_dec(&mQueuedFrames);
1921 }
Dan Stozaecc50402015-04-28 14:42:06 -07001922 return outDirtyRegion;
Dan Stoza65476f32015-05-14 09:27:25 -07001923 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
1924 // This can occur if something goes wrong when trying to create the
1925 // EGLImage for this buffer. If this happens, the buffer has already
1926 // been released, so we need to clean up the queue and bug out
1927 // early.
Pablo Ceballos06312182015-10-07 16:32:12 -07001928 if (queuedBuffer) {
Dan Stoza65476f32015-05-14 09:27:25 -07001929 Mutex::Autolock lock(mQueueItemLock);
1930 mQueueItems.clear();
1931 android_atomic_and(0, &mQueuedFrames);
1932 }
1933
1934 // Once we have hit this state, the shadow queue may no longer
1935 // correctly reflect the incoming BufferQueue's contents, so even if
1936 // updateTexImage starts working, the only safe course of action is
1937 // to continue to ignore updates.
1938 mUpdateTexImageFailed = true;
1939
1940 return outDirtyRegion;
Andy McFadden1585c4d2013-06-28 13:52:40 -07001941 }
1942
Pablo Ceballos06312182015-10-07 16:32:12 -07001943 if (queuedBuffer) {
1944 // Autolock scope
Dan Stozaecc50402015-04-28 14:42:06 -07001945 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
1946
Dan Stoza6b9454d2014-11-07 16:00:59 -08001947 Mutex::Autolock lock(mQueueItemLock);
Dan Stozaecc50402015-04-28 14:42:06 -07001948
1949 // Remove any stale buffers that have been dropped during
1950 // updateTexImage
1951 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
1952 mQueueItems.removeAt(0);
1953 android_atomic_dec(&mQueuedFrames);
1954 }
1955
Dan Stoza6b9454d2014-11-07 16:00:59 -08001956 mQueueItems.removeAt(0);
1957 }
1958
Dan Stozaecc50402015-04-28 14:42:06 -07001959
Andy McFadden1585c4d2013-06-28 13:52:40 -07001960 // Decrement the queued-frames count. Signal another event if we
1961 // have more frames pending.
Pablo Ceballos06312182015-10-07 16:32:12 -07001962 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1)
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001963 || mAutoRefresh) {
Andy McFadden1585c4d2013-06-28 13:52:40 -07001964 mFlinger->signalLayerUpdate();
1965 }
1966
1967 if (updateResult != NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001968 // something happened!
1969 recomputeVisibleRegions = true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001970 return outDirtyRegion;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001971 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001972
Jamie Gennis351a5132011-09-14 18:23:37 -07001973 // update the active buffer
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001974 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
Mathias Agopiane31564d2012-05-29 20:41:03 -07001975 if (mActiveBuffer == NULL) {
1976 // this can only happen if the very first buffer was rejected.
Mathias Agopian4fec8732012-06-29 14:12:52 -07001977 return outDirtyRegion;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001978 }
Mathias Agopianda9584d2010-12-13 18:51:59 -08001979
Mathias Agopian4824d402012-06-04 18:16:30 -07001980 mRefreshPending = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001981 mFrameLatencyNeeded = true;
Mathias Agopiane31564d2012-05-29 20:41:03 -07001982 if (oldActiveBuffer == NULL) {
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001983 // the first time we receive a buffer, we need to trigger a
1984 // geometry invalidation.
Andy McFaddenab10c582012-09-26 16:19:12 -07001985 recomputeVisibleRegions = true;
Mathias Agopian2c8207e2012-05-23 17:56:42 -07001986 }
1987
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001988 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1989 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1990 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
Mathias Agopian702634a2012-05-23 17:50:31 -07001991 if ((crop != mCurrentCrop) ||
1992 (transform != mCurrentTransform) ||
1993 (scalingMode != mCurrentScalingMode))
1994 {
1995 mCurrentCrop = crop;
1996 mCurrentTransform = transform;
1997 mCurrentScalingMode = scalingMode;
Andy McFaddenab10c582012-09-26 16:19:12 -07001998 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07001999 }
2000
2001 if (oldActiveBuffer != NULL) {
Mathias Agopiane31564d2012-05-29 20:41:03 -07002002 uint32_t bufWidth = mActiveBuffer->getWidth();
2003 uint32_t bufHeight = mActiveBuffer->getHeight();
Mathias Agopian702634a2012-05-23 17:50:31 -07002004 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
2005 bufHeight != uint32_t(oldActiveBuffer->height)) {
Andy McFaddenab10c582012-09-26 16:19:12 -07002006 recomputeVisibleRegions = true;
Mathias Agopian702634a2012-05-23 17:50:31 -07002007 }
2008 }
2009
2010 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
Andy McFadden4125a4f2014-01-29 17:17:11 -08002011 if (oldOpacity != isOpaque(s)) {
Mathias Agopian702634a2012-05-23 17:50:31 -07002012 recomputeVisibleRegions = true;
2013 }
2014
Dan Stozacac35382016-01-27 12:21:06 -08002015 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2016
2017 // Remove any sync points corresponding to the buffer which was just
2018 // latched
2019 {
2020 Mutex::Autolock lock(mLocalSyncPointMutex);
2021 auto point = mLocalSyncPoints.begin();
2022 while (point != mLocalSyncPoints.end()) {
2023 if (!(*point)->frameIsAvailable() ||
2024 !(*point)->transactionIsApplied()) {
2025 // This sync point must have been added since we started
2026 // latching. Don't drop it yet.
2027 ++point;
2028 continue;
2029 }
2030
2031 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
2032 point = mLocalSyncPoints.erase(point);
2033 } else {
2034 ++point;
2035 }
2036 }
2037 }
2038
Mathias Agopian4fec8732012-06-29 14:12:52 -07002039 // FIXME: postedRegion should be dirty & bounds
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002040 Region dirtyRegion(Rect(s.active.w, s.active.h));
Mathias Agopian4fec8732012-06-29 14:12:52 -07002041
2042 // transform the dirty region to window-manager space
Robert Carr3dcabfa2016-03-01 18:36:58 -08002043 outDirtyRegion = (s.active.transform.transform(dirtyRegion));
Mathias Agopiancaa600c2009-09-16 18:27:24 -07002044 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07002045 return outDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046}
2047
Mathias Agopiana67932f2011-04-20 14:20:59 -07002048uint32_t Layer::getEffectiveUsage(uint32_t usage) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002049{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002050 // TODO: should we do something special if mSecure is set?
2051 if (mProtectedByApp) {
2052 // need a hardware-protected path to external video sink
2053 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07002054 }
Riley Andrews03414a12014-07-01 14:22:59 -07002055 if (mPotentialCursor) {
2056 usage |= GraphicBuffer::USAGE_CURSOR;
2057 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07002058 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002059 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07002060}
2061
Mathias Agopian84300952012-11-21 16:02:13 -08002062void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07002063 uint32_t orientation = 0;
2064 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08002065 // The transform hint is used to improve performance, but we can
2066 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07002067 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07002068 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07002069 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07002070 if (orientation & Transform::ROT_INVALID) {
2071 orientation = 0;
2072 }
2073 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -08002074 mSurfaceFlingerConsumer->setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07002075}
2076
Mathias Agopian13127d82013-03-05 17:47:11 -08002077// ----------------------------------------------------------------------------
2078// debugging
2079// ----------------------------------------------------------------------------
2080
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002081void Layer::dump(String8& result, Colorizer& colorizer) const
Mathias Agopian13127d82013-03-05 17:47:11 -08002082{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002083 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -08002084
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002085 colorizer.colorize(result, Colorizer::GREEN);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002086 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002087 "+ %s %p (%s)\n",
2088 getTypeId(), this, getName().string());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002089 colorizer.reset(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002090
Mathias Agopian2ca79392013-04-02 18:30:32 -07002091 s.activeTransparentRegion.dump(result, "transparentRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002092 visibleRegion.dump(result, "visibleRegion");
Dan Stozaee44edd2015-03-23 15:50:23 -07002093 surfaceDamageRegion.dump(result, "surfaceDamageRegion");
Mathias Agopian13127d82013-03-05 17:47:11 -08002094 sp<Client> client(mClientRef.promote());
2095
Mathias Agopian74d211a2013-04-22 16:55:35 +02002096 result.appendFormat( " "
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002097 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), "
2098 "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), "
Mathias Agopian13127d82013-03-05 17:47:11 -08002099 "isOpaque=%1d, invalidate=%1d, "
Dan Stoza9e56aa02015-11-02 13:00:03 -08002100#ifdef USE_HWC2
2101 "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
2102#else
Mathias Agopian13127d82013-03-05 17:47:11 -08002103 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
Dan Stoza9e56aa02015-11-02 13:00:03 -08002104#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08002105 " client=%p\n",
Robert Carr3dcabfa2016-03-01 18:36:58 -08002106 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 -07002107 s.crop.left, s.crop.top,
2108 s.crop.right, s.crop.bottom,
2109 s.finalCrop.left, s.finalCrop.top,
2110 s.finalCrop.right, s.finalCrop.bottom,
Andy McFadden4125a4f2014-01-29 17:17:11 -08002111 isOpaque(s), contentDirty,
Mathias Agopian13127d82013-03-05 17:47:11 -08002112 s.alpha, s.flags,
Robert Carr3dcabfa2016-03-01 18:36:58 -08002113 s.active.transform[0][0], s.active.transform[0][1],
2114 s.active.transform[1][0], s.active.transform[1][1],
Mathias Agopian13127d82013-03-05 17:47:11 -08002115 client.get());
Mathias Agopian13127d82013-03-05 17:47:11 -08002116
2117 sp<const GraphicBuffer> buf0(mActiveBuffer);
2118 uint32_t w0=0, h0=0, s0=0, f0=0;
2119 if (buf0 != 0) {
2120 w0 = buf0->getWidth();
2121 h0 = buf0->getHeight();
2122 s0 = buf0->getStride();
2123 f0 = buf0->format;
2124 }
Mathias Agopian74d211a2013-04-22 16:55:35 +02002125 result.appendFormat(
Mathias Agopian13127d82013-03-05 17:47:11 -08002126 " "
2127 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
2128 " queued-frames=%d, mRefreshPending=%d\n",
2129 mFormat, w0, h0, s0,f0,
2130 mQueuedFrames, mRefreshPending);
2131
Mathias Agopian13127d82013-03-05 17:47:11 -08002132 if (mSurfaceFlingerConsumer != 0) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002133 mSurfaceFlingerConsumer->dump(result, " ");
Mathias Agopian13127d82013-03-05 17:47:11 -08002134 }
2135}
2136
Svetoslavd85084b2014-03-20 10:28:31 -07002137void Layer::dumpFrameStats(String8& result) const {
2138 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08002139}
2140
Svetoslavd85084b2014-03-20 10:28:31 -07002141void Layer::clearFrameStats() {
2142 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08002143}
2144
Jamie Gennis6547ff42013-07-16 20:12:42 -07002145void Layer::logFrameStats() {
2146 mFrameTracker.logAndResetStats(mName);
2147}
2148
Svetoslavd85084b2014-03-20 10:28:31 -07002149void Layer::getFrameStats(FrameStats* outStats) const {
2150 mFrameTracker.getStats(outStats);
2151}
2152
Pablo Ceballos40845df2016-01-25 17:41:15 -08002153void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber,
2154 bool* outIsGlesComposition, nsecs_t* outPostedTime,
2155 sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const {
2156 *outName = mName;
2157 *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
2158
2159#ifdef USE_HWC2
2160 *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ?
2161 mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType ==
2162 HWC2::Composition::Client : true;
2163#else
2164 *outIsGlesComposition = mIsGlesComposition;
2165#endif
2166 *outPostedTime = mSurfaceFlingerConsumer->getTimestamp();
2167 *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence();
2168 *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence();
2169}
Mathias Agopian13127d82013-03-05 17:47:11 -08002170// ---------------------------------------------------------------------------
2171
2172Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
2173 const sp<Layer>& layer)
2174 : mFlinger(flinger), mLayer(layer) {
2175}
2176
2177Layer::LayerCleaner::~LayerCleaner() {
2178 // destroy client resources
2179 mFlinger->onLayerDestroyed(mLayer);
2180}
2181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002182// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002184
2185#if defined(__gl_h_)
2186#error "don't include gl/gl.h in this file"
2187#endif
2188
2189#if defined(__gl2_h_)
2190#error "don't include gl2/gl2.h in this file"
2191#endif