blob: 790f2ece77a3740c6d401fa91ca28b510cce0b02 [file] [log] [blame]
Marissa Wall61c58622018-07-18 10:12:20 -07001/*
2 * Copyright (C) 2017 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Marissa Wall61c58622018-07-18 10:12:20 -070021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "BufferStateLayer"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Lloyd Pique9755fb72019-03-26 14:44:40 -070026#include "BufferStateLayer.h"
27
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070029
Lloyd Pique9755fb72019-03-26 14:44:40 -070030#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070031#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070032#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070033#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070034
Vishnu Nairfa247b12020-02-11 08:58:26 -080035#include "EffectLayer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080037
Marissa Wall61c58622018-07-18 10:12:20 -070038namespace android {
39
Lloyd Pique42ab75e2018-09-12 20:46:03 -070040// clang-format off
41const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1
46};
47// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070048
Marissa Wall947d34e2019-03-29 14:03:53 -070049BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
50 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080051 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080052 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080053}
Marissa Wall61c58622018-07-18 10:12:20 -070054
Alec Mouri4545a8a2019-08-08 20:05:32 -070055BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070056 // The original layer and the clone layer share the same texture and buffer. Therefore, only
57 // one of the layers, in this case the original layer, needs to handle the deletion. The
58 // original layer and the clone should be removed at the same time so there shouldn't be any
59 // issue with the clone layer trying to use the texture.
60 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070061 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070062 // RenderEngine may have been using the buffer as an external texture
63 // after the client uncached the buffer.
64 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070065 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070066 }
67}
68
Marissa Wall61c58622018-07-18 10:12:20 -070069// -----------------------------------------------------------------------
70// Interface implementation for Layer
71// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070072void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080073 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
74 // buffer that was presented on this layer. The first transaction that came in this frame that
75 // replaced the previous buffer on this layer needs this release fence, because the fence will
76 // let the client know when that previous buffer is removed from the screen.
77 //
78 // Every other transaction on this layer does not need a release fence because no other
79 // Transactions that were set on this layer this frame are going to have their preceeding buffer
80 // removed from the display this frame.
81 //
82 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
83 // buffer so it doesn't need a previous release fence because the layer still needs the previous
84 // buffer. The second transaction contains a buffer so it needs a previous release fence because
85 // the previous buffer will be released this frame. The third transaction also contains a
86 // buffer. It replaces the buffer in the second transaction. The buffer in the second
87 // transaction will now no longer be presented so it is released immediately and the third
88 // transaction doesn't need a previous release fence.
89 for (auto& handle : mDrawingState.callbackHandles) {
90 if (handle->releasePreviousBuffer) {
91 handle->previousReleaseFence = releaseFence;
92 break;
93 }
94 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070095
Valerie Haubf784642020-01-29 07:25:23 -080096 mPreviousReleaseFence = releaseFence;
97
Mikael Pessa2e1608f2019-07-19 11:25:35 -070098 // Prevent tracing the same release multiple times.
99 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700100 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
101 }
Marissa Wall61c58622018-07-18 10:12:20 -0700102}
103
Valerie Haubf784642020-01-29 07:25:23 -0800104void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700105 for (const auto& handle : mDrawingState.callbackHandles) {
106 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800107 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700108 }
109
Marissa Wallefb71af2019-06-27 14:45:53 -0700110 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800111 mDrawingState.callbackHandles);
112
113 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800114
115 const sp<Fence>& releaseFence(mPreviousReleaseFence);
116 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
117 {
118 Mutex::Autolock lock(mFrameEventHistoryMutex);
119 if (mPreviousFrameNumber != 0) {
120 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
121 std::move(releaseFenceTime));
122 }
123 }
Marissa Wall61c58622018-07-18 10:12:20 -0700124}
125
Valerie Hau871d6352020-01-29 08:44:02 -0800126void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
127 const CompositorTiming& compositorTiming) {
128 for (const auto& handle : mDrawingState.callbackHandles) {
129 handle->gpuCompositionDoneFence = glDoneFence;
130 handle->compositorTiming = compositorTiming;
131 }
132}
133
Ana Krulec010d2192018-10-08 06:29:54 -0700134bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700135 if (getSidebandStreamChanged() || getAutoRefresh()) {
136 return true;
137 }
138
Marissa Wall024a1912018-08-13 13:55:35 -0700139 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700140}
141
Marissa Walle2ffb422018-10-12 11:33:52 -0700142bool BufferStateLayer::willPresentCurrentTransaction() const {
143 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700144 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800145 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700146 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
147 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700148}
149
Valerie Hau3282b3c2020-02-03 15:37:27 -0800150/* TODO: vhau uncomment once deferred transaction migration complete in
151 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800152void BufferStateLayer::pushPendingState() {
153 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700154 return;
155 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800156 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700157 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700158}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800159*/
Marissa Wall61c58622018-07-18 10:12:20 -0700160
161bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800162 mCurrentStateModified = mCurrentState.modified;
163 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
164 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800165 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700166 return stateUpdateAvailable;
167}
168
Marissa Wall861616d2018-10-22 12:52:23 -0700169// Crop that applies to the window
170Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
171 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700172}
173
174bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800175 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800176 mCurrentState.transform = transform;
177 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700178 setTransactionFlags(eTransactionNeeded);
179 return true;
180}
181
182bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800183 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
184 mCurrentState.sequence++;
185 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
186 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700187 setTransactionFlags(eTransactionNeeded);
188 return true;
189}
190
191bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800192 Rect c = crop;
193 if (c.left < 0) {
194 c.left = 0;
195 }
196 if (c.top < 0) {
197 c.top = 0;
198 }
199 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
200 // treats all invalid rectangles the same.
201 if (!c.isValid()) {
202 c.makeInvalid();
203 }
204
205 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800206 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800207 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700208 setTransactionFlags(eTransactionNeeded);
209 return true;
210}
211
Marissa Wall861616d2018-10-22 12:52:23 -0700212bool BufferStateLayer::setFrame(const Rect& frame) {
213 int x = frame.left;
214 int y = frame.top;
215 int w = frame.getWidth();
216 int h = frame.getHeight();
217
Marissa Wall0f3242d2018-12-20 15:10:22 -0800218 if (x < 0) {
219 x = 0;
220 w = frame.right;
221 }
222
223 if (y < 0) {
224 y = 0;
225 h = frame.bottom;
226 }
227
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800228 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
229 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700230 return false;
231 }
232
233 if (!frame.isValid()) {
234 x = y = w = h = 0;
235 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800236 mCurrentState.active.transform.set(x, y);
237 mCurrentState.active.w = w;
238 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700239
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 mCurrentState.sequence++;
241 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700242 setTransactionFlags(eTransactionNeeded);
243 return true;
244}
245
Valerie Hau871d6352020-01-29 08:44:02 -0800246bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
247 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800248 Mutex::Autolock lock(mFrameEventHistoryMutex);
249 mAcquireTimeline.updateSignalTimes();
250 std::shared_ptr<FenceTime> acquireFenceTime =
251 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
252 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
253 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800254 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800255 mFrameEventHistory.addQueue(newTimestamps);
256 return true;
257}
258
259bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
260 nsecs_t postTime, nsecs_t desiredPresentTime,
261 const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800262 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700263 mReleasePreviousBuffer = true;
264 }
265
Valerie Hau134651a2020-01-28 16:21:22 -0800266 mCurrentState.frameNumber++;
Valerie Hau2f54d642020-01-22 09:37:03 -0800267
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700269 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700271 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700272
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800273 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800274 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
275 postTime);
Valerie Hau871d6352020-01-29 08:44:02 -0800276 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700277 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700278
Ady Abraham5def7332020-05-29 16:13:47 -0700279 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
280 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700281
Valerie Hau871d6352020-01-29 08:44:02 -0800282 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700283 return true;
284}
285
286bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700287 // The acquire fences of BufferStateLayers have already signaled before they are set
288 mCallbackHandleAcquireTime = fence->getSignalTime();
289
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800290 mCurrentState.acquireFence = fence;
291 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700292 setTransactionFlags(eTransactionNeeded);
293 return true;
294}
295
296bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800297 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 mCurrentState.dataspace = dataspace;
299 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700300 setTransactionFlags(eTransactionNeeded);
301 return true;
302}
303
304bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800305 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800306 mCurrentState.hdrMetadata = hdrMetadata;
307 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700308 setTransactionFlags(eTransactionNeeded);
309 return true;
310}
311
312bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800313 mCurrentState.surfaceDamageRegion = surfaceDamage;
314 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700315 setTransactionFlags(eTransactionNeeded);
316 return true;
317}
318
319bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800320 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800321 mCurrentState.api = api;
322 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700323 setTransactionFlags(eTransactionNeeded);
324 return true;
325}
326
327bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800328 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800329 mCurrentState.sidebandStream = sidebandStream;
330 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700331 setTransactionFlags(eTransactionNeeded);
332
333 if (!mSidebandStreamChanged.exchange(true)) {
334 // mSidebandStreamChanged was false
335 mFlinger->signalLayerUpdate();
336 }
337 return true;
338}
339
Marissa Walle2ffb422018-10-12 11:33:52 -0700340bool BufferStateLayer::setTransactionCompletedListeners(
341 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700342 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700343 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700344 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700345 return false;
346 }
347
348 const bool willPresent = willPresentCurrentTransaction();
349
350 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700351 // If this transaction set a buffer on this layer, release its previous buffer
352 handle->releasePreviousBuffer = mReleasePreviousBuffer;
353
Marissa Walle2ffb422018-10-12 11:33:52 -0700354 // If this layer will be presented in this frame
355 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700356 // If this transaction set an acquire fence on this layer, set its acquire time
357 handle->acquireTime = mCallbackHandleAcquireTime;
358
Marissa Walle2ffb422018-10-12 11:33:52 -0700359 // Notify the transaction completed thread that there is a pending latched callback
360 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800361 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700362
363 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800364 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700365
366 } else { // If this layer will NOT need to be relatched and presented this frame
367 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700368 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700369 }
370 }
371
Marissa Wallfda30bb2018-10-12 11:34:28 -0700372 mReleasePreviousBuffer = false;
373 mCallbackHandleAcquireTime = -1;
374
Marissa Walle2ffb422018-10-12 11:33:52 -0700375 return willPresent;
376}
377
Valerie Hau7618b232020-01-09 16:03:08 -0800378void BufferStateLayer::forceSendCallbacks() {
379 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
380 mCurrentState.callbackHandles);
381}
382
Marissa Wall61c58622018-07-18 10:12:20 -0700383bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800384 mCurrentState.transparentRegionHint = transparent;
385 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700386 setTransactionFlags(eTransactionNeeded);
387 return true;
388}
389
Marissa Wall861616d2018-10-22 12:52:23 -0700390Rect BufferStateLayer::getBufferSize(const State& s) const {
391 // for buffer state layers we use the display frame size as the buffer size.
392 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
393 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700394 }
395
Marissa Wall861616d2018-10-22 12:52:23 -0700396 // if the display frame is not defined, use the parent bounds as the buffer size.
397 const auto& p = mDrawingParent.promote();
398 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800399 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700400 if (!parentBounds.isEmpty()) {
401 return parentBounds;
402 }
403 }
404
Marissa Wall861616d2018-10-22 12:52:23 -0700405 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700406}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800407
408FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
409 const State& s(getDrawingState());
410 // for buffer state layers we use the display frame size as the buffer size.
411 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
412 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
413 }
414
415 // if the display frame is not defined, use the parent bounds as the buffer size.
416 return parentBounds;
417}
418
Marissa Wall61c58622018-07-18 10:12:20 -0700419// -----------------------------------------------------------------------
420
421// -----------------------------------------------------------------------
422// Interface implementation for BufferLayer
423// -----------------------------------------------------------------------
424bool BufferStateLayer::fenceHasSignaled() const {
425 if (latchUnsignaledBuffers()) {
426 return true;
427 }
428
Alec Mouri91f6df32020-01-30 08:48:58 -0800429 const bool fenceSignaled =
430 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
431 if (!fenceSignaled) {
432 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
433 TimeStats::LatchSkipReason::LateAcquire);
434 }
435
436 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700437}
438
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700439bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700440 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
441 return true;
442 }
443
chaviwfa67b552019-08-12 16:51:55 -0700444 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700445}
446
Valerie Hau871d6352020-01-29 08:44:02 -0800447bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
448 for (const auto& handle : mDrawingState.callbackHandles) {
449 handle->refreshStartTime = refreshStartTime;
450 }
451 return BufferLayer::onPreComposition(refreshStartTime);
452}
453
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700454uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800455 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700456}
457
Robert Carrfe1209c2020-02-11 12:25:35 -0800458/**
459 * This is the frameNumber used for deferred transaction signalling. We need to use this because
460 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
461 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
462 * deferred transaction?) but this is an important legacy use case, for example moving
463 * a window at the same time it draws makes use of this kind of technique. So anyway
464 * imagine we have something like this:
465 *
466 * Transaction { // containing
467 * Buffer -> frameNumber = 2
468 * DeferTransactionUntil -> frameNumber = 2
469 * Random other stuff
470 * }
471 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
472 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
473 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
474 * is not ready. So we will return false from applyPendingState and not swap
475 * current state to drawing state. But because we don't swap current state
476 * to drawing state the number will never update and we will be stuck. This way
477 * we can see we need to return the frame number for the buffer we are about
478 * to apply.
479 */
480uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
481 return mCurrentState.frameNumber;
482}
483
Marissa Wall61c58622018-07-18 10:12:20 -0700484bool BufferStateLayer::getAutoRefresh() const {
485 // TODO(marissaw): support shared buffer mode
486 return false;
487}
488
489bool BufferStateLayer::getSidebandStreamChanged() const {
490 return mSidebandStreamChanged.load();
491}
492
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800493bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700494 if (mSidebandStreamChanged.exchange(false)) {
495 const State& s(getDrawingState());
496 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800497 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800498 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800499 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700500 setTransactionFlags(eTransactionNeeded);
501 mFlinger->setTransactionFlags(eTraversalNeeded);
502 }
503 recomputeVisibleRegions = true;
504
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800505 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700506 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800507 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700508}
509
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800510bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800511 const State& c(getCurrentState());
512 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700513}
514
Alec Mouri39801c02018-10-10 10:44:47 -0700515status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700516 const State& s(getDrawingState());
517 auto& engine(mFlinger->getRenderEngine());
518
Alec Mourib5c4f352019-02-19 19:46:38 -0800519 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700520}
521
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700522status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
523 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700524 const State& s(getDrawingState());
525
526 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800527 if (s.bgColorLayer) {
528 for (auto& handle : mDrawingState.callbackHandles) {
529 handle->latchTime = latchTime;
530 }
531 }
Marissa Wall61c58622018-07-18 10:12:20 -0700532 return NO_ERROR;
533 }
534
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800535 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700536
Marissa Wall61c58622018-07-18 10:12:20 -0700537 // Reject if the layer is invalid
538 uint32_t bufferWidth = s.buffer->width;
539 uint32_t bufferHeight = s.buffer->height;
540
Peiyong Linefefaac2018-08-17 12:27:51 -0700541 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700542 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700543 }
544
545 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800546 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700547 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700548 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700549 }
550 }
551
Vishnu Nair60356342018-11-13 13:00:45 -0800552 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700553 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
554 ALOGE("[%s] rejecting buffer: "
555 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700556 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800557 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700558 return BAD_VALUE;
559 }
560
Marissa Wall5a68a772018-12-22 17:43:42 -0800561 for (auto& handle : mDrawingState.callbackHandles) {
562 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800563 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800564 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700565
Alec Mouri56e538f2019-01-14 15:22:01 -0800566 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700567 // Bind the new buffer to the GL texture.
568 //
569 // Older devices require the "implicit" synchronization provided
570 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
571 // devices will either call this in Layer::onDraw, or (if it's not
572 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800573 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700574 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800575 mFlinger->mTimeStats->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700576 return BAD_VALUE;
577 }
578 }
579
Valerie Hau134651a2020-01-28 16:21:22 -0800580 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700581 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800582 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700583
Marissa Wall16c112d2019-03-20 13:21:13 -0700584 mCurrentStateModified = false;
585
Marissa Wall61c58622018-07-18 10:12:20 -0700586 return NO_ERROR;
587}
588
589status_t BufferStateLayer::updateActiveBuffer() {
590 const State& s(getDrawingState());
591
592 if (s.buffer == nullptr) {
593 return BAD_VALUE;
594 }
595
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700596 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700597 mBufferInfo.mBuffer = s.buffer;
598 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700599
600 return NO_ERROR;
601}
602
Valerie Haubf784642020-01-29 07:25:23 -0800603status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700604 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700605 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800606 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800607 {
608 Mutex::Autolock lock(mFrameEventHistoryMutex);
609 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
610 }
Marissa Wall61c58622018-07-18 10:12:20 -0700611 return NO_ERROR;
612}
613
Marissa Wall947d34e2019-03-29 14:03:53 -0700614void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
615 std::lock_guard lock(mMutex);
616 if (!clientCacheId.isValid()) {
617 ALOGE("invalid process, failed to erase buffer");
618 return;
619 }
620 eraseBufferLocked(clientCacheId);
621}
622
623uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
624 std::lock_guard<std::mutex> lock(mMutex);
625 auto itr = mCachedBuffers.find(clientCacheId);
626 if (itr == mCachedBuffers.end()) {
627 return addCachedBuffer(clientCacheId);
628 }
629 auto& [hwcCacheSlot, counter] = itr->second;
630 counter = mCounter++;
631 return hwcCacheSlot;
632}
633
634uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
635 REQUIRES(mMutex) {
636 if (!clientCacheId.isValid()) {
637 ALOGE("invalid process, returning invalid slot");
638 return BufferQueue::INVALID_BUFFER_SLOT;
639 }
640
641 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
642
643 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
644 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
645 return hwcCacheSlot;
646}
647
648uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
649 if (mFreeHwcCacheSlots.empty()) {
650 evictLeastRecentlyUsed();
651 }
652
653 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
654 mFreeHwcCacheSlots.pop();
655 return hwcCacheSlot;
656}
657
658void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
659 uint64_t minCounter = UINT_MAX;
660 client_cache_t minClientCacheId = {};
661 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
662 const auto& [hwcCacheSlot, counter] = slotCounter;
663 if (counter < minCounter) {
664 minCounter = counter;
665 minClientCacheId = clientCacheId;
666 }
667 }
668 eraseBufferLocked(minClientCacheId);
669
670 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
671}
672
673void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
674 REQUIRES(mMutex) {
675 auto itr = mCachedBuffers.find(clientCacheId);
676 if (itr == mCachedBuffers.end()) {
677 return;
678 }
679 auto& [hwcCacheSlot, counter] = itr->second;
680
681 // TODO send to hwc cache and resources
682
683 mFreeHwcCacheSlots.push(hwcCacheSlot);
684 mCachedBuffers.erase(clientCacheId);
685}
chaviw4244e032019-09-04 11:27:49 -0700686
687void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700688 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700689
chaviwdebadb82020-03-26 14:57:24 -0700690 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700691 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
692 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
693 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700694 mBufferInfo.mTransform = s.transform;
695 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
696 mBufferInfo.mCrop = computeCrop(s);
697 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
698 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
699 mBufferInfo.mHdrMetadata = s.hdrMetadata;
700 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700701 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700702 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700703}
704
705Rect BufferStateLayer::computeCrop(const State& s) {
706 if (s.crop.isEmpty() && s.buffer) {
707 return s.buffer->getBounds();
708 } else if (s.buffer) {
709 Rect crop = s.crop;
710 crop.left = std::max(crop.left, 0);
711 crop.top = std::max(crop.top, 0);
712 uint32_t bufferWidth = s.buffer->getWidth();
713 uint32_t bufferHeight = s.buffer->getHeight();
714 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
715 bufferWidth <= std::numeric_limits<int32_t>::max()) {
716 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
717 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
718 }
719 if (!crop.isValid()) {
720 // Crop rect is out of bounds, return whole buffer
721 return s.buffer->getBounds();
722 }
723 return crop;
724 }
725 return s.crop;
726}
727
chaviwb4c6e582019-08-16 14:35:07 -0700728sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700729 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700730 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700731 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700732 layer->mHwcSlotGenerator = mHwcSlotGenerator;
733 layer->setInitialValuesForClone(this);
734 return layer;
735}
Valerie Hau92bf5482020-02-10 09:49:08 -0800736
737Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
738 const auto& p = mDrawingParent.promote();
739 if (p != nullptr) {
740 RoundedCornerState parentState = p->getRoundedCornerState();
741 if (parentState.radius > 0) {
742 ui::Transform t = getActiveTransform(getDrawingState());
743 t = t.inverse();
744 parentState.cropRect = t.transform(parentState.cropRect);
745 // The rounded corners shader only accepts 1 corner radius for performance reasons,
746 // but a transform matrix can define horizontal and vertical scales.
747 // Let's take the average between both of them and pass into the shader, practically we
748 // never do this type of transformation on windows anyway.
749 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
750 return parentState;
751 }
752 }
753 const float radius = getDrawingState().cornerRadius;
754 const State& s(getDrawingState());
755 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
756 return RoundedCornerState();
757 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
758 static_cast<float>(s.active.transform.ty()),
759 static_cast<float>(s.active.transform.tx() + s.active.w),
760 static_cast<float>(s.active.transform.ty() + s.active.h)),
761 radius);
762}
Marissa Wall61c58622018-07-18 10:12:20 -0700763} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800764
765// TODO(b/129481165): remove the #pragma below and fix conversion issues
766#pragma clang diagnostic pop // ignored "-Wconversion"