blob: 361c1f3f984c0a92abb674b2ccc47d615fd3db7a [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);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700164 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700165 // Update the acquire fence time if we have a buffer
166 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700167 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800168 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800169 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700170 return stateUpdateAvailable;
171}
172
Marissa Wall861616d2018-10-22 12:52:23 -0700173// Crop that applies to the window
174Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
175 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700176}
177
178bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800179 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800180 mCurrentState.transform = transform;
181 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700182 setTransactionFlags(eTransactionNeeded);
183 return true;
184}
185
186bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800187 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
188 mCurrentState.sequence++;
189 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
190 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700191 setTransactionFlags(eTransactionNeeded);
192 return true;
193}
194
195bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800196 Rect c = crop;
197 if (c.left < 0) {
198 c.left = 0;
199 }
200 if (c.top < 0) {
201 c.top = 0;
202 }
203 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
204 // treats all invalid rectangles the same.
205 if (!c.isValid()) {
206 c.makeInvalid();
207 }
208
209 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800210 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800211 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700212 setTransactionFlags(eTransactionNeeded);
213 return true;
214}
215
Marissa Wall861616d2018-10-22 12:52:23 -0700216bool BufferStateLayer::setFrame(const Rect& frame) {
217 int x = frame.left;
218 int y = frame.top;
219 int w = frame.getWidth();
220 int h = frame.getHeight();
221
Marissa Wall0f3242d2018-12-20 15:10:22 -0800222 if (x < 0) {
223 x = 0;
224 w = frame.right;
225 }
226
227 if (y < 0) {
228 y = 0;
229 h = frame.bottom;
230 }
231
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800232 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
233 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700234 return false;
235 }
236
237 if (!frame.isValid()) {
238 x = y = w = h = 0;
239 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 mCurrentState.active.transform.set(x, y);
241 mCurrentState.active.w = w;
242 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700243
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800244 mCurrentState.sequence++;
245 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700246 setTransactionFlags(eTransactionNeeded);
247 return true;
248}
249
Valerie Hau871d6352020-01-29 08:44:02 -0800250bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
251 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800252 Mutex::Autolock lock(mFrameEventHistoryMutex);
253 mAcquireTimeline.updateSignalTimes();
254 std::shared_ptr<FenceTime> acquireFenceTime =
255 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
256 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
257 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800258 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800259 mFrameEventHistory.addQueue(newTimestamps);
260 return true;
261}
262
263bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
264 nsecs_t postTime, nsecs_t desiredPresentTime,
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700265 const client_cache_t& clientCacheId, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800266 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700267 mReleasePreviousBuffer = true;
268 }
269
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700270 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800271
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800272 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700273 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700275 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700276
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800277 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800278 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
279 postTime);
Valerie Hau871d6352020-01-29 08:44:02 -0800280 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700281 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700282
Ady Abraham5def7332020-05-29 16:13:47 -0700283 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
284 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700285
Valerie Hau871d6352020-01-29 08:44:02 -0800286 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700287 return true;
288}
289
290bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700291 // The acquire fences of BufferStateLayers have already signaled before they are set
292 mCallbackHandleAcquireTime = fence->getSignalTime();
293
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800294 mCurrentState.acquireFence = fence;
295 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700296 setTransactionFlags(eTransactionNeeded);
297 return true;
298}
299
300bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 mCurrentState.dataspace = dataspace;
303 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700304 setTransactionFlags(eTransactionNeeded);
305 return true;
306}
307
308bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800309 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800310 mCurrentState.hdrMetadata = hdrMetadata;
311 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700312 setTransactionFlags(eTransactionNeeded);
313 return true;
314}
315
316bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800317 mCurrentState.surfaceDamageRegion = surfaceDamage;
318 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700319 setTransactionFlags(eTransactionNeeded);
320 return true;
321}
322
323bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800324 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800325 mCurrentState.api = api;
326 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700327 setTransactionFlags(eTransactionNeeded);
328 return true;
329}
330
331bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800332 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800333 mCurrentState.sidebandStream = sidebandStream;
334 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700335 setTransactionFlags(eTransactionNeeded);
336
337 if (!mSidebandStreamChanged.exchange(true)) {
338 // mSidebandStreamChanged was false
339 mFlinger->signalLayerUpdate();
340 }
341 return true;
342}
343
Marissa Walle2ffb422018-10-12 11:33:52 -0700344bool BufferStateLayer::setTransactionCompletedListeners(
345 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700346 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700347 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700348 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700349 return false;
350 }
351
352 const bool willPresent = willPresentCurrentTransaction();
353
354 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700355 // If this transaction set a buffer on this layer, release its previous buffer
356 handle->releasePreviousBuffer = mReleasePreviousBuffer;
357
Marissa Walle2ffb422018-10-12 11:33:52 -0700358 // If this layer will be presented in this frame
359 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700360 // If this transaction set an acquire fence on this layer, set its acquire time
361 handle->acquireTime = mCallbackHandleAcquireTime;
362
Marissa Walle2ffb422018-10-12 11:33:52 -0700363 // Notify the transaction completed thread that there is a pending latched callback
364 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800365 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700366
367 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800368 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700369
370 } else { // If this layer will NOT need to be relatched and presented this frame
371 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700372 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700373 }
374 }
375
Marissa Wallfda30bb2018-10-12 11:34:28 -0700376 mReleasePreviousBuffer = false;
377 mCallbackHandleAcquireTime = -1;
378
Marissa Walle2ffb422018-10-12 11:33:52 -0700379 return willPresent;
380}
381
Valerie Hau7618b232020-01-09 16:03:08 -0800382void BufferStateLayer::forceSendCallbacks() {
383 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
384 mCurrentState.callbackHandles);
385}
386
Marissa Wall61c58622018-07-18 10:12:20 -0700387bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800388 mCurrentState.transparentRegionHint = transparent;
389 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700390 setTransactionFlags(eTransactionNeeded);
391 return true;
392}
393
Marissa Wall861616d2018-10-22 12:52:23 -0700394Rect BufferStateLayer::getBufferSize(const State& s) const {
395 // for buffer state layers we use the display frame size as the buffer size.
396 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
397 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700398 }
399
Marissa Wall861616d2018-10-22 12:52:23 -0700400 // if the display frame is not defined, use the parent bounds as the buffer size.
401 const auto& p = mDrawingParent.promote();
402 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800403 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700404 if (!parentBounds.isEmpty()) {
405 return parentBounds;
406 }
407 }
408
Marissa Wall861616d2018-10-22 12:52:23 -0700409 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700410}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800411
412FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
413 const State& s(getDrawingState());
414 // for buffer state layers we use the display frame size as the buffer size.
415 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
416 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
417 }
418
419 // if the display frame is not defined, use the parent bounds as the buffer size.
420 return parentBounds;
421}
422
Marissa Wall61c58622018-07-18 10:12:20 -0700423// -----------------------------------------------------------------------
424
425// -----------------------------------------------------------------------
426// Interface implementation for BufferLayer
427// -----------------------------------------------------------------------
428bool BufferStateLayer::fenceHasSignaled() const {
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
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700515status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
516 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700517 const State& s(getDrawingState());
518
519 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800520 if (s.bgColorLayer) {
521 for (auto& handle : mDrawingState.callbackHandles) {
522 handle->latchTime = latchTime;
523 }
524 }
Marissa Wall61c58622018-07-18 10:12:20 -0700525 return NO_ERROR;
526 }
527
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800528 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700529
Marissa Wall61c58622018-07-18 10:12:20 -0700530 // Reject if the layer is invalid
531 uint32_t bufferWidth = s.buffer->width;
532 uint32_t bufferHeight = s.buffer->height;
533
Peiyong Linefefaac2018-08-17 12:27:51 -0700534 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700535 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700536 }
537
538 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800539 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700540 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700541 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700542 }
543 }
544
Vishnu Nair60356342018-11-13 13:00:45 -0800545 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700546 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
547 ALOGE("[%s] rejecting buffer: "
548 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700549 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800550 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700551 return BAD_VALUE;
552 }
553
Marissa Wall5a68a772018-12-22 17:43:42 -0800554 for (auto& handle : mDrawingState.callbackHandles) {
555 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800556 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800557 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700558
Valerie Hau134651a2020-01-28 16:21:22 -0800559 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700560 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800561 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700562
Marissa Wall16c112d2019-03-20 13:21:13 -0700563 mCurrentStateModified = false;
564
Marissa Wall61c58622018-07-18 10:12:20 -0700565 return NO_ERROR;
566}
567
568status_t BufferStateLayer::updateActiveBuffer() {
569 const State& s(getDrawingState());
570
571 if (s.buffer == nullptr) {
572 return BAD_VALUE;
573 }
574
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700575 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700576 mBufferInfo.mBuffer = s.buffer;
577 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700578
579 return NO_ERROR;
580}
581
Valerie Haubf784642020-01-29 07:25:23 -0800582status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700583 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700584 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800585 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800586 {
587 Mutex::Autolock lock(mFrameEventHistoryMutex);
588 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
589 }
Marissa Wall61c58622018-07-18 10:12:20 -0700590 return NO_ERROR;
591}
592
Marissa Wall947d34e2019-03-29 14:03:53 -0700593void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
594 std::lock_guard lock(mMutex);
595 if (!clientCacheId.isValid()) {
596 ALOGE("invalid process, failed to erase buffer");
597 return;
598 }
599 eraseBufferLocked(clientCacheId);
600}
601
602uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
603 std::lock_guard<std::mutex> lock(mMutex);
604 auto itr = mCachedBuffers.find(clientCacheId);
605 if (itr == mCachedBuffers.end()) {
606 return addCachedBuffer(clientCacheId);
607 }
608 auto& [hwcCacheSlot, counter] = itr->second;
609 counter = mCounter++;
610 return hwcCacheSlot;
611}
612
613uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
614 REQUIRES(mMutex) {
615 if (!clientCacheId.isValid()) {
616 ALOGE("invalid process, returning invalid slot");
617 return BufferQueue::INVALID_BUFFER_SLOT;
618 }
619
620 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
621
622 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
623 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
624 return hwcCacheSlot;
625}
626
627uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
628 if (mFreeHwcCacheSlots.empty()) {
629 evictLeastRecentlyUsed();
630 }
631
632 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
633 mFreeHwcCacheSlots.pop();
634 return hwcCacheSlot;
635}
636
637void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
638 uint64_t minCounter = UINT_MAX;
639 client_cache_t minClientCacheId = {};
640 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
641 const auto& [hwcCacheSlot, counter] = slotCounter;
642 if (counter < minCounter) {
643 minCounter = counter;
644 minClientCacheId = clientCacheId;
645 }
646 }
647 eraseBufferLocked(minClientCacheId);
648
649 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
650}
651
652void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
653 REQUIRES(mMutex) {
654 auto itr = mCachedBuffers.find(clientCacheId);
655 if (itr == mCachedBuffers.end()) {
656 return;
657 }
658 auto& [hwcCacheSlot, counter] = itr->second;
659
660 // TODO send to hwc cache and resources
661
662 mFreeHwcCacheSlots.push(hwcCacheSlot);
663 mCachedBuffers.erase(clientCacheId);
664}
chaviw4244e032019-09-04 11:27:49 -0700665
666void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700667 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700668
chaviwdebadb82020-03-26 14:57:24 -0700669 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700670 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
671 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
672 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700673 mBufferInfo.mTransform = s.transform;
674 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
675 mBufferInfo.mCrop = computeCrop(s);
676 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
677 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
678 mBufferInfo.mHdrMetadata = s.hdrMetadata;
679 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700680 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700681 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700682}
683
684Rect BufferStateLayer::computeCrop(const State& s) {
685 if (s.crop.isEmpty() && s.buffer) {
686 return s.buffer->getBounds();
687 } else if (s.buffer) {
688 Rect crop = s.crop;
689 crop.left = std::max(crop.left, 0);
690 crop.top = std::max(crop.top, 0);
691 uint32_t bufferWidth = s.buffer->getWidth();
692 uint32_t bufferHeight = s.buffer->getHeight();
693 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
694 bufferWidth <= std::numeric_limits<int32_t>::max()) {
695 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
696 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
697 }
698 if (!crop.isValid()) {
699 // Crop rect is out of bounds, return whole buffer
700 return s.buffer->getBounds();
701 }
702 return crop;
703 }
704 return s.crop;
705}
706
chaviwb4c6e582019-08-16 14:35:07 -0700707sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700708 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700709 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700710 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700711 layer->mHwcSlotGenerator = mHwcSlotGenerator;
712 layer->setInitialValuesForClone(this);
713 return layer;
714}
Valerie Hau92bf5482020-02-10 09:49:08 -0800715
716Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
717 const auto& p = mDrawingParent.promote();
718 if (p != nullptr) {
719 RoundedCornerState parentState = p->getRoundedCornerState();
720 if (parentState.radius > 0) {
721 ui::Transform t = getActiveTransform(getDrawingState());
722 t = t.inverse();
723 parentState.cropRect = t.transform(parentState.cropRect);
724 // The rounded corners shader only accepts 1 corner radius for performance reasons,
725 // but a transform matrix can define horizontal and vertical scales.
726 // Let's take the average between both of them and pass into the shader, practically we
727 // never do this type of transformation on windows anyway.
728 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
729 return parentState;
730 }
731 }
732 const float radius = getDrawingState().cornerRadius;
733 const State& s(getDrawingState());
734 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
735 return RoundedCornerState();
736 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
737 static_cast<float>(s.active.transform.ty()),
738 static_cast<float>(s.active.transform.tx() + s.active.w),
739 static_cast<float>(s.active.transform.ty() + s.active.h)),
740 radius);
741}
Marissa Wall61c58622018-07-18 10:12:20 -0700742} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800743
744// TODO(b/129481165): remove the #pragma below and fix conversion issues
745#pragma clang diagnostic pop // ignored "-Wconversion"