blob: 4efff753515da783168d7a8f1f37beb0e14cd780 [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()) {
Marissa Wall3ff826c2019-02-07 11:58:25 -080051 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080052}
Marissa Wall61c58622018-07-18 10:12:20 -070053
Alec Mouri4545a8a2019-08-08 20:05:32 -070054BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070055 // The original layer and the clone layer share the same texture and buffer. Therefore, only
56 // one of the layers, in this case the original layer, needs to handle the deletion. The
57 // original layer and the clone should be removed at the same time so there shouldn't be any
58 // issue with the clone layer trying to use the texture.
59 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070060 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070061 // RenderEngine may have been using the buffer as an external texture
62 // after the client uncached the buffer.
63 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070064 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070065 }
66}
67
Marissa Wall61c58622018-07-18 10:12:20 -070068// -----------------------------------------------------------------------
69// Interface implementation for Layer
70// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070071void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080072 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
73 // buffer that was presented on this layer. The first transaction that came in this frame that
74 // replaced the previous buffer on this layer needs this release fence, because the fence will
75 // let the client know when that previous buffer is removed from the screen.
76 //
77 // Every other transaction on this layer does not need a release fence because no other
78 // Transactions that were set on this layer this frame are going to have their preceeding buffer
79 // removed from the display this frame.
80 //
81 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
82 // buffer so it doesn't need a previous release fence because the layer still needs the previous
83 // buffer. The second transaction contains a buffer so it needs a previous release fence because
84 // the previous buffer will be released this frame. The third transaction also contains a
85 // buffer. It replaces the buffer in the second transaction. The buffer in the second
86 // transaction will now no longer be presented so it is released immediately and the third
87 // transaction doesn't need a previous release fence.
88 for (auto& handle : mDrawingState.callbackHandles) {
89 if (handle->releasePreviousBuffer) {
90 handle->previousReleaseFence = releaseFence;
91 break;
92 }
93 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070094
Valerie Haubf784642020-01-29 07:25:23 -080095 mPreviousReleaseFence = releaseFence;
96
Mikael Pessa2e1608f2019-07-19 11:25:35 -070097 // Prevent tracing the same release multiple times.
98 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -070099 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
100 }
Marissa Wall61c58622018-07-18 10:12:20 -0700101}
102
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100103void BufferStateLayer::onSurfaceFrameCreated(
104 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
105 mPendingJankClassifications.emplace_back(surfaceFrame);
106}
107
Valerie Haubf784642020-01-29 07:25:23 -0800108void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700109 for (const auto& handle : mDrawingState.callbackHandles) {
110 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800111 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700112 }
113
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100114 std::vector<JankData> jankData;
115 jankData.reserve(mPendingJankClassifications.size());
116 while (!mPendingJankClassifications.empty()
117 && mPendingJankClassifications.front()->getJankType()) {
118 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
119 mPendingJankClassifications.front();
120 mPendingJankClassifications.pop_front();
121 jankData.emplace_back(
122 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
123 }
124
Marissa Wallefb71af2019-06-27 14:45:53 -0700125 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100126 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800127
128 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800129
130 const sp<Fence>& releaseFence(mPreviousReleaseFence);
131 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
132 {
133 Mutex::Autolock lock(mFrameEventHistoryMutex);
134 if (mPreviousFrameNumber != 0) {
135 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
136 std::move(releaseFenceTime));
137 }
138 }
Marissa Wall61c58622018-07-18 10:12:20 -0700139}
140
Valerie Hau871d6352020-01-29 08:44:02 -0800141void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
142 const CompositorTiming& compositorTiming) {
143 for (const auto& handle : mDrawingState.callbackHandles) {
144 handle->gpuCompositionDoneFence = glDoneFence;
145 handle->compositorTiming = compositorTiming;
146 }
147}
148
Ana Krulec010d2192018-10-08 06:29:54 -0700149bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700150 if (getSidebandStreamChanged() || getAutoRefresh()) {
151 return true;
152 }
153
Marissa Wall024a1912018-08-13 13:55:35 -0700154 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700155}
156
Marissa Walle2ffb422018-10-12 11:33:52 -0700157bool BufferStateLayer::willPresentCurrentTransaction() const {
158 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700159 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800160 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700161 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
162 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700163}
164
Valerie Hau3282b3c2020-02-03 15:37:27 -0800165/* TODO: vhau uncomment once deferred transaction migration complete in
166 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800167void BufferStateLayer::pushPendingState() {
168 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700169 return;
170 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800171 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700172 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700173}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800174*/
Marissa Wall61c58622018-07-18 10:12:20 -0700175
176bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800177 mCurrentStateModified = mCurrentState.modified;
178 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700179 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700180 // Update the acquire fence time if we have a buffer
181 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700182 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800183 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800184 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700185 return stateUpdateAvailable;
186}
187
Marissa Wall861616d2018-10-22 12:52:23 -0700188// Crop that applies to the window
189Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
190 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700191}
192
193bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800194 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800195 mCurrentState.transform = transform;
196 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700197 setTransactionFlags(eTransactionNeeded);
198 return true;
199}
200
201bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800202 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
203 mCurrentState.sequence++;
204 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
205 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700206 setTransactionFlags(eTransactionNeeded);
207 return true;
208}
209
210bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800211 Rect c = crop;
212 if (c.left < 0) {
213 c.left = 0;
214 }
215 if (c.top < 0) {
216 c.top = 0;
217 }
218 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
219 // treats all invalid rectangles the same.
220 if (!c.isValid()) {
221 c.makeInvalid();
222 }
223
224 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800225 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700227 setTransactionFlags(eTransactionNeeded);
228 return true;
229}
230
Marissa Wall861616d2018-10-22 12:52:23 -0700231bool BufferStateLayer::setFrame(const Rect& frame) {
232 int x = frame.left;
233 int y = frame.top;
234 int w = frame.getWidth();
235 int h = frame.getHeight();
236
Marissa Wall0f3242d2018-12-20 15:10:22 -0800237 if (x < 0) {
238 x = 0;
239 w = frame.right;
240 }
241
242 if (y < 0) {
243 y = 0;
244 h = frame.bottom;
245 }
246
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800247 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
248 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700249 return false;
250 }
251
252 if (!frame.isValid()) {
253 x = y = w = h = 0;
254 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800255 mCurrentState.active.transform.set(x, y);
256 mCurrentState.active.w = w;
257 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700258
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800259 mCurrentState.sequence++;
260 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700261 setTransactionFlags(eTransactionNeeded);
262 return true;
263}
264
Valerie Hau871d6352020-01-29 08:44:02 -0800265bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
266 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800267 Mutex::Autolock lock(mFrameEventHistoryMutex);
268 mAcquireTimeline.updateSignalTimes();
269 std::shared_ptr<FenceTime> acquireFenceTime =
270 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
271 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
272 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800273 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800274 mFrameEventHistory.addQueue(newTimestamps);
275 return true;
276}
277
278bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
279 nsecs_t postTime, nsecs_t desiredPresentTime,
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700280 const client_cache_t& clientCacheId, uint64_t frameNumber) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700281 ATRACE_CALL();
282
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700284 mReleasePreviousBuffer = true;
285 }
286
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700287 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800288
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800289 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700290 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700292 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700293
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800294 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800295 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
Alec Mouri9a29e672020-09-14 12:39:14 -0700296 mOwnerUid, postTime);
Valerie Hau871d6352020-01-29 08:44:02 -0800297 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700298 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700299
Ady Abraham5def7332020-05-29 16:13:47 -0700300 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
301 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700302
Valerie Hau871d6352020-01-29 08:44:02 -0800303 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700304 return true;
305}
306
307bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700308 // The acquire fences of BufferStateLayers have already signaled before they are set
309 mCallbackHandleAcquireTime = fence->getSignalTime();
310
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800311 mCurrentState.acquireFence = fence;
312 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700313 setTransactionFlags(eTransactionNeeded);
314 return true;
315}
316
317bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800318 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800319 mCurrentState.dataspace = dataspace;
320 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700321 setTransactionFlags(eTransactionNeeded);
322 return true;
323}
324
325bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800326 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800327 mCurrentState.hdrMetadata = hdrMetadata;
328 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700329 setTransactionFlags(eTransactionNeeded);
330 return true;
331}
332
333bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800334 mCurrentState.surfaceDamageRegion = surfaceDamage;
335 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700336 setTransactionFlags(eTransactionNeeded);
337 return true;
338}
339
340bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800341 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800342 mCurrentState.api = api;
343 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700344 setTransactionFlags(eTransactionNeeded);
345 return true;
346}
347
348bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800349 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800350 mCurrentState.sidebandStream = sidebandStream;
351 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700352 setTransactionFlags(eTransactionNeeded);
353
354 if (!mSidebandStreamChanged.exchange(true)) {
355 // mSidebandStreamChanged was false
356 mFlinger->signalLayerUpdate();
357 }
358 return true;
359}
360
Marissa Walle2ffb422018-10-12 11:33:52 -0700361bool BufferStateLayer::setTransactionCompletedListeners(
362 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700363 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700364 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700365 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700366 return false;
367 }
368
369 const bool willPresent = willPresentCurrentTransaction();
370
371 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700372 // If this transaction set a buffer on this layer, release its previous buffer
373 handle->releasePreviousBuffer = mReleasePreviousBuffer;
374
Marissa Walle2ffb422018-10-12 11:33:52 -0700375 // If this layer will be presented in this frame
376 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700377 // If this transaction set an acquire fence on this layer, set its acquire time
378 handle->acquireTime = mCallbackHandleAcquireTime;
379
Marissa Walle2ffb422018-10-12 11:33:52 -0700380 // Notify the transaction completed thread that there is a pending latched callback
381 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800382 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700383
384 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800385 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700386
387 } else { // If this layer will NOT need to be relatched and presented this frame
388 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700389 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700390 }
391 }
392
Marissa Wallfda30bb2018-10-12 11:34:28 -0700393 mReleasePreviousBuffer = false;
394 mCallbackHandleAcquireTime = -1;
395
Marissa Walle2ffb422018-10-12 11:33:52 -0700396 return willPresent;
397}
398
Valerie Hau7618b232020-01-09 16:03:08 -0800399void BufferStateLayer::forceSendCallbacks() {
400 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100401 mCurrentState.callbackHandles, std::vector<JankData>());
Valerie Hau7618b232020-01-09 16:03:08 -0800402}
403
Marissa Wall61c58622018-07-18 10:12:20 -0700404bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800405 mCurrentState.transparentRegionHint = transparent;
406 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700407 setTransactionFlags(eTransactionNeeded);
408 return true;
409}
410
Marissa Wall861616d2018-10-22 12:52:23 -0700411Rect BufferStateLayer::getBufferSize(const State& s) const {
412 // for buffer state layers we use the display frame size as the buffer size.
413 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
414 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700415 }
416
Marissa Wall861616d2018-10-22 12:52:23 -0700417 // if the display frame is not defined, use the parent bounds as the buffer size.
418 const auto& p = mDrawingParent.promote();
419 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800420 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700421 if (!parentBounds.isEmpty()) {
422 return parentBounds;
423 }
424 }
425
Marissa Wall861616d2018-10-22 12:52:23 -0700426 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700427}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800428
429FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
430 const State& s(getDrawingState());
431 // for buffer state layers we use the display frame size as the buffer size.
432 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
433 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
434 }
435
436 // if the display frame is not defined, use the parent bounds as the buffer size.
437 return parentBounds;
438}
439
Marissa Wall61c58622018-07-18 10:12:20 -0700440// -----------------------------------------------------------------------
441
442// -----------------------------------------------------------------------
443// Interface implementation for BufferLayer
444// -----------------------------------------------------------------------
445bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800446 const bool fenceSignaled =
447 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
448 if (!fenceSignaled) {
449 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
450 TimeStats::LatchSkipReason::LateAcquire);
451 }
452
453 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700454}
455
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700456bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700457 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
458 return true;
459 }
460
chaviwfa67b552019-08-12 16:51:55 -0700461 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700462}
463
Valerie Hau871d6352020-01-29 08:44:02 -0800464bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
465 for (const auto& handle : mDrawingState.callbackHandles) {
466 handle->refreshStartTime = refreshStartTime;
467 }
468 return BufferLayer::onPreComposition(refreshStartTime);
469}
470
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700471uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800472 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700473}
474
Robert Carrfe1209c2020-02-11 12:25:35 -0800475/**
476 * This is the frameNumber used for deferred transaction signalling. We need to use this because
477 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
478 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
479 * deferred transaction?) but this is an important legacy use case, for example moving
480 * a window at the same time it draws makes use of this kind of technique. So anyway
481 * imagine we have something like this:
482 *
483 * Transaction { // containing
484 * Buffer -> frameNumber = 2
485 * DeferTransactionUntil -> frameNumber = 2
486 * Random other stuff
487 * }
488 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
489 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
490 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
491 * is not ready. So we will return false from applyPendingState and not swap
492 * current state to drawing state. But because we don't swap current state
493 * to drawing state the number will never update and we will be stuck. This way
494 * we can see we need to return the frame number for the buffer we are about
495 * to apply.
496 */
497uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
498 return mCurrentState.frameNumber;
499}
500
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800501void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
502 if (!mAutoRefresh.exchange(autoRefresh)) {
503 mFlinger->signalLayerUpdate();
504 }
Marissa Wall61c58622018-07-18 10:12:20 -0700505}
506
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800507bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700508 if (mSidebandStreamChanged.exchange(false)) {
509 const State& s(getDrawingState());
510 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800511 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800512 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800513 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700514 setTransactionFlags(eTransactionNeeded);
515 mFlinger->setTransactionFlags(eTraversalNeeded);
516 }
517 recomputeVisibleRegions = true;
518
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800519 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700520 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800521 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700522}
523
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800524bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800525 const State& c(getCurrentState());
526 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700527}
528
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700529status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
530 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700531 const State& s(getDrawingState());
532
533 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800534 if (s.bgColorLayer) {
535 for (auto& handle : mDrawingState.callbackHandles) {
536 handle->latchTime = latchTime;
537 }
538 }
Marissa Wall61c58622018-07-18 10:12:20 -0700539 return NO_ERROR;
540 }
541
Marissa Wall5a68a772018-12-22 17:43:42 -0800542 for (auto& handle : mDrawingState.callbackHandles) {
543 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800544 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800545 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700546
Vishnu Nairea0de002020-11-17 17:42:37 -0800547 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800548 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700549 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800550 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700551
Marissa Wall16c112d2019-03-20 13:21:13 -0700552 mCurrentStateModified = false;
553
Marissa Wall61c58622018-07-18 10:12:20 -0700554 return NO_ERROR;
555}
556
557status_t BufferStateLayer::updateActiveBuffer() {
558 const State& s(getDrawingState());
559
560 if (s.buffer == nullptr) {
561 return BAD_VALUE;
562 }
563
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700564 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700565 mBufferInfo.mBuffer = s.buffer;
566 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700567
568 return NO_ERROR;
569}
570
Valerie Haubf784642020-01-29 07:25:23 -0800571status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700572 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700573 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800574 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800575 {
576 Mutex::Autolock lock(mFrameEventHistoryMutex);
577 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
578 }
Marissa Wall61c58622018-07-18 10:12:20 -0700579 return NO_ERROR;
580}
581
Marissa Wall947d34e2019-03-29 14:03:53 -0700582void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
583 std::lock_guard lock(mMutex);
584 if (!clientCacheId.isValid()) {
585 ALOGE("invalid process, failed to erase buffer");
586 return;
587 }
588 eraseBufferLocked(clientCacheId);
589}
590
591uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
592 std::lock_guard<std::mutex> lock(mMutex);
593 auto itr = mCachedBuffers.find(clientCacheId);
594 if (itr == mCachedBuffers.end()) {
595 return addCachedBuffer(clientCacheId);
596 }
597 auto& [hwcCacheSlot, counter] = itr->second;
598 counter = mCounter++;
599 return hwcCacheSlot;
600}
601
602uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
603 REQUIRES(mMutex) {
604 if (!clientCacheId.isValid()) {
605 ALOGE("invalid process, returning invalid slot");
606 return BufferQueue::INVALID_BUFFER_SLOT;
607 }
608
609 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
610
611 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
612 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
613 return hwcCacheSlot;
614}
615
616uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
617 if (mFreeHwcCacheSlots.empty()) {
618 evictLeastRecentlyUsed();
619 }
620
621 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
622 mFreeHwcCacheSlots.pop();
623 return hwcCacheSlot;
624}
625
626void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
627 uint64_t minCounter = UINT_MAX;
628 client_cache_t minClientCacheId = {};
629 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
630 const auto& [hwcCacheSlot, counter] = slotCounter;
631 if (counter < minCounter) {
632 minCounter = counter;
633 minClientCacheId = clientCacheId;
634 }
635 }
636 eraseBufferLocked(minClientCacheId);
637
638 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
639}
640
641void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
642 REQUIRES(mMutex) {
643 auto itr = mCachedBuffers.find(clientCacheId);
644 if (itr == mCachedBuffers.end()) {
645 return;
646 }
647 auto& [hwcCacheSlot, counter] = itr->second;
648
649 // TODO send to hwc cache and resources
650
651 mFreeHwcCacheSlots.push(hwcCacheSlot);
652 mCachedBuffers.erase(clientCacheId);
653}
chaviw4244e032019-09-04 11:27:49 -0700654
655void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700656 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700657
chaviwdebadb82020-03-26 14:57:24 -0700658 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700659 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
660 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
661 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700662 mBufferInfo.mTransform = s.transform;
663 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
664 mBufferInfo.mCrop = computeCrop(s);
665 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
666 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
667 mBufferInfo.mHdrMetadata = s.hdrMetadata;
668 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700669 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700670 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700671}
672
Robert Carr916b0362020-10-06 13:53:03 -0700673uint32_t BufferStateLayer::getEffectiveScalingMode() const {
674 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
675}
676
chaviw4244e032019-09-04 11:27:49 -0700677Rect BufferStateLayer::computeCrop(const State& s) {
678 if (s.crop.isEmpty() && s.buffer) {
679 return s.buffer->getBounds();
680 } else if (s.buffer) {
681 Rect crop = s.crop;
682 crop.left = std::max(crop.left, 0);
683 crop.top = std::max(crop.top, 0);
684 uint32_t bufferWidth = s.buffer->getWidth();
685 uint32_t bufferHeight = s.buffer->getHeight();
686 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
687 bufferWidth <= std::numeric_limits<int32_t>::max()) {
688 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
689 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
690 }
691 if (!crop.isValid()) {
692 // Crop rect is out of bounds, return whole buffer
693 return s.buffer->getBounds();
694 }
695 return crop;
696 }
697 return s.crop;
698}
699
chaviwb4c6e582019-08-16 14:35:07 -0700700sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700701 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700702 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700703 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700704 layer->mHwcSlotGenerator = mHwcSlotGenerator;
705 layer->setInitialValuesForClone(this);
706 return layer;
707}
Valerie Hau92bf5482020-02-10 09:49:08 -0800708
709Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
710 const auto& p = mDrawingParent.promote();
711 if (p != nullptr) {
712 RoundedCornerState parentState = p->getRoundedCornerState();
713 if (parentState.radius > 0) {
714 ui::Transform t = getActiveTransform(getDrawingState());
715 t = t.inverse();
716 parentState.cropRect = t.transform(parentState.cropRect);
717 // The rounded corners shader only accepts 1 corner radius for performance reasons,
718 // but a transform matrix can define horizontal and vertical scales.
719 // Let's take the average between both of them and pass into the shader, practically we
720 // never do this type of transformation on windows anyway.
721 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
722 return parentState;
723 }
724 }
725 const float radius = getDrawingState().cornerRadius;
726 const State& s(getDrawingState());
727 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
728 return RoundedCornerState();
729 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
730 static_cast<float>(s.active.transform.ty()),
731 static_cast<float>(s.active.transform.tx() + s.active.w),
732 static_cast<float>(s.active.transform.ty() + s.active.h)),
733 radius);
734}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700735
736bool BufferStateLayer::bufferNeedsFiltering() const {
737 const State& s(getDrawingState());
738 if (!s.buffer) {
739 return false;
740 }
741
742 uint32_t bufferWidth = s.buffer->width;
743 uint32_t bufferHeight = s.buffer->height;
744
745 // Undo any transformations on the buffer and return the result.
746 if (s.transform & ui::Transform::ROT_90) {
747 std::swap(bufferWidth, bufferHeight);
748 }
749
750 if (s.transformToDisplayInverse) {
751 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
752 if (invTransform & ui::Transform::ROT_90) {
753 std::swap(bufferWidth, bufferHeight);
754 }
755 }
756
757 const Rect layerSize{getBounds()};
758 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
759}
Marissa Wall61c58622018-07-18 10:12:20 -0700760} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800761
762// TODO(b/129481165): remove the #pragma below and fix conversion issues
763#pragma clang diagnostic pop // ignored "-Wconversion"