blob: f85151449b3b4d284498b5c234d454cfc89de4fc [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
Valerie Haubf784642020-01-29 07:25:23 -0800103void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700104 for (const auto& handle : mDrawingState.callbackHandles) {
105 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800106 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700107 }
108
Marissa Wallefb71af2019-06-27 14:45:53 -0700109 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800110 mDrawingState.callbackHandles);
111
112 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800113
114 const sp<Fence>& releaseFence(mPreviousReleaseFence);
115 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
116 {
117 Mutex::Autolock lock(mFrameEventHistoryMutex);
118 if (mPreviousFrameNumber != 0) {
119 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
120 std::move(releaseFenceTime));
121 }
122 }
Marissa Wall61c58622018-07-18 10:12:20 -0700123}
124
Valerie Hau871d6352020-01-29 08:44:02 -0800125void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
126 const CompositorTiming& compositorTiming) {
127 for (const auto& handle : mDrawingState.callbackHandles) {
128 handle->gpuCompositionDoneFence = glDoneFence;
129 handle->compositorTiming = compositorTiming;
130 }
131}
132
Ana Krulec010d2192018-10-08 06:29:54 -0700133bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700134 if (getSidebandStreamChanged() || getAutoRefresh()) {
135 return true;
136 }
137
Marissa Wall024a1912018-08-13 13:55:35 -0700138 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700139}
140
Marissa Walle2ffb422018-10-12 11:33:52 -0700141bool BufferStateLayer::willPresentCurrentTransaction() const {
142 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700143 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800144 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700145 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
146 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700147}
148
Valerie Hau3282b3c2020-02-03 15:37:27 -0800149/* TODO: vhau uncomment once deferred transaction migration complete in
150 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151void BufferStateLayer::pushPendingState() {
152 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700153 return;
154 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800155 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700156 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700157}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800158*/
Marissa Wall61c58622018-07-18 10:12:20 -0700159
160bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800161 mCurrentStateModified = mCurrentState.modified;
162 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700163 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700164 // Update the acquire fence time if we have a buffer
165 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700166 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800167 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800168 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700169 return stateUpdateAvailable;
170}
171
Marissa Wall861616d2018-10-22 12:52:23 -0700172// Crop that applies to the window
173Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
174 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700175}
176
177bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800178 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800179 mCurrentState.transform = transform;
180 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700181 setTransactionFlags(eTransactionNeeded);
182 return true;
183}
184
185bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800186 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
187 mCurrentState.sequence++;
188 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
189 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700190 setTransactionFlags(eTransactionNeeded);
191 return true;
192}
193
194bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800195 Rect c = crop;
196 if (c.left < 0) {
197 c.left = 0;
198 }
199 if (c.top < 0) {
200 c.top = 0;
201 }
202 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
203 // treats all invalid rectangles the same.
204 if (!c.isValid()) {
205 c.makeInvalid();
206 }
207
208 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800209 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800210 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700211 setTransactionFlags(eTransactionNeeded);
212 return true;
213}
214
Marissa Wall861616d2018-10-22 12:52:23 -0700215bool BufferStateLayer::setFrame(const Rect& frame) {
216 int x = frame.left;
217 int y = frame.top;
218 int w = frame.getWidth();
219 int h = frame.getHeight();
220
Marissa Wall0f3242d2018-12-20 15:10:22 -0800221 if (x < 0) {
222 x = 0;
223 w = frame.right;
224 }
225
226 if (y < 0) {
227 y = 0;
228 h = frame.bottom;
229 }
230
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
232 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700233 return false;
234 }
235
236 if (!frame.isValid()) {
237 x = y = w = h = 0;
238 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800239 mCurrentState.active.transform.set(x, y);
240 mCurrentState.active.w = w;
241 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700242
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 mCurrentState.sequence++;
244 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700245 setTransactionFlags(eTransactionNeeded);
246 return true;
247}
248
Valerie Hau871d6352020-01-29 08:44:02 -0800249bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
250 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800251 Mutex::Autolock lock(mFrameEventHistoryMutex);
252 mAcquireTimeline.updateSignalTimes();
253 std::shared_ptr<FenceTime> acquireFenceTime =
254 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
255 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
256 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800257 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800258 mFrameEventHistory.addQueue(newTimestamps);
259 return true;
260}
261
262bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
263 nsecs_t postTime, nsecs_t desiredPresentTime,
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700264 const client_cache_t& clientCacheId, uint64_t frameNumber) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700265 ATRACE_CALL();
266
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700268 mReleasePreviousBuffer = true;
269 }
270
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700271 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800272
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800273 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700274 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700276 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700277
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800278 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800279 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
280 postTime);
Valerie Hau871d6352020-01-29 08:44:02 -0800281 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700282 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700283
Ady Abraham5def7332020-05-29 16:13:47 -0700284 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
285 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700286
Valerie Hau871d6352020-01-29 08:44:02 -0800287 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700288 return true;
289}
290
291bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700292 // The acquire fences of BufferStateLayers have already signaled before they are set
293 mCallbackHandleAcquireTime = fence->getSignalTime();
294
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800295 mCurrentState.acquireFence = fence;
296 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700297 setTransactionFlags(eTransactionNeeded);
298 return true;
299}
300
301bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800303 mCurrentState.dataspace = dataspace;
304 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700305 setTransactionFlags(eTransactionNeeded);
306 return true;
307}
308
309bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800310 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800311 mCurrentState.hdrMetadata = hdrMetadata;
312 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700313 setTransactionFlags(eTransactionNeeded);
314 return true;
315}
316
317bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800318 mCurrentState.surfaceDamageRegion = surfaceDamage;
319 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700320 setTransactionFlags(eTransactionNeeded);
321 return true;
322}
323
324bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800325 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800326 mCurrentState.api = api;
327 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700328 setTransactionFlags(eTransactionNeeded);
329 return true;
330}
331
332bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800333 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800334 mCurrentState.sidebandStream = sidebandStream;
335 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700336 setTransactionFlags(eTransactionNeeded);
337
338 if (!mSidebandStreamChanged.exchange(true)) {
339 // mSidebandStreamChanged was false
340 mFlinger->signalLayerUpdate();
341 }
342 return true;
343}
344
Marissa Walle2ffb422018-10-12 11:33:52 -0700345bool BufferStateLayer::setTransactionCompletedListeners(
346 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700347 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700348 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700349 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700350 return false;
351 }
352
353 const bool willPresent = willPresentCurrentTransaction();
354
355 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700356 // If this transaction set a buffer on this layer, release its previous buffer
357 handle->releasePreviousBuffer = mReleasePreviousBuffer;
358
Marissa Walle2ffb422018-10-12 11:33:52 -0700359 // If this layer will be presented in this frame
360 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700361 // If this transaction set an acquire fence on this layer, set its acquire time
362 handle->acquireTime = mCallbackHandleAcquireTime;
363
Marissa Walle2ffb422018-10-12 11:33:52 -0700364 // Notify the transaction completed thread that there is a pending latched callback
365 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800366 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700367
368 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800369 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700370
371 } else { // If this layer will NOT need to be relatched and presented this frame
372 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700373 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700374 }
375 }
376
Marissa Wallfda30bb2018-10-12 11:34:28 -0700377 mReleasePreviousBuffer = false;
378 mCallbackHandleAcquireTime = -1;
379
Marissa Walle2ffb422018-10-12 11:33:52 -0700380 return willPresent;
381}
382
Valerie Hau7618b232020-01-09 16:03:08 -0800383void BufferStateLayer::forceSendCallbacks() {
384 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
385 mCurrentState.callbackHandles);
386}
387
Marissa Wall61c58622018-07-18 10:12:20 -0700388bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800389 mCurrentState.transparentRegionHint = transparent;
390 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700391 setTransactionFlags(eTransactionNeeded);
392 return true;
393}
394
Marissa Wall861616d2018-10-22 12:52:23 -0700395Rect BufferStateLayer::getBufferSize(const State& s) const {
396 // for buffer state layers we use the display frame size as the buffer size.
397 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
398 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700399 }
400
Marissa Wall861616d2018-10-22 12:52:23 -0700401 // if the display frame is not defined, use the parent bounds as the buffer size.
402 const auto& p = mDrawingParent.promote();
403 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800404 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700405 if (!parentBounds.isEmpty()) {
406 return parentBounds;
407 }
408 }
409
Marissa Wall861616d2018-10-22 12:52:23 -0700410 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700411}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800412
413FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
414 const State& s(getDrawingState());
415 // for buffer state layers we use the display frame size as the buffer size.
416 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
417 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
418 }
419
420 // if the display frame is not defined, use the parent bounds as the buffer size.
421 return parentBounds;
422}
423
Marissa Wall61c58622018-07-18 10:12:20 -0700424// -----------------------------------------------------------------------
425
426// -----------------------------------------------------------------------
427// Interface implementation for BufferLayer
428// -----------------------------------------------------------------------
429bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800430 const bool fenceSignaled =
431 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
432 if (!fenceSignaled) {
433 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
434 TimeStats::LatchSkipReason::LateAcquire);
435 }
436
437 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700440bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700441 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
442 return true;
443 }
444
chaviwfa67b552019-08-12 16:51:55 -0700445 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700446}
447
Valerie Hau871d6352020-01-29 08:44:02 -0800448bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
449 for (const auto& handle : mDrawingState.callbackHandles) {
450 handle->refreshStartTime = refreshStartTime;
451 }
452 return BufferLayer::onPreComposition(refreshStartTime);
453}
454
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700455uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800456 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700457}
458
Robert Carrfe1209c2020-02-11 12:25:35 -0800459/**
460 * This is the frameNumber used for deferred transaction signalling. We need to use this because
461 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
462 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
463 * deferred transaction?) but this is an important legacy use case, for example moving
464 * a window at the same time it draws makes use of this kind of technique. So anyway
465 * imagine we have something like this:
466 *
467 * Transaction { // containing
468 * Buffer -> frameNumber = 2
469 * DeferTransactionUntil -> frameNumber = 2
470 * Random other stuff
471 * }
472 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
473 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
474 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
475 * is not ready. So we will return false from applyPendingState and not swap
476 * current state to drawing state. But because we don't swap current state
477 * to drawing state the number will never update and we will be stuck. This way
478 * we can see we need to return the frame number for the buffer we are about
479 * to apply.
480 */
481uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
482 return mCurrentState.frameNumber;
483}
484
Marissa Wall61c58622018-07-18 10:12:20 -0700485bool BufferStateLayer::getAutoRefresh() const {
486 // TODO(marissaw): support shared buffer mode
487 return false;
488}
489
490bool BufferStateLayer::getSidebandStreamChanged() const {
491 return mSidebandStreamChanged.load();
492}
493
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800494bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700495 if (mSidebandStreamChanged.exchange(false)) {
496 const State& s(getDrawingState());
497 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800498 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800499 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800500 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700501 setTransactionFlags(eTransactionNeeded);
502 mFlinger->setTransactionFlags(eTraversalNeeded);
503 }
504 recomputeVisibleRegions = true;
505
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800506 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700507 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800508 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700509}
510
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800511bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800512 const State& c(getCurrentState());
513 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700514}
515
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700516status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
517 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700518 const State& s(getDrawingState());
519
520 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800521 if (s.bgColorLayer) {
522 for (auto& handle : mDrawingState.callbackHandles) {
523 handle->latchTime = latchTime;
524 }
525 }
Marissa Wall61c58622018-07-18 10:12:20 -0700526 return NO_ERROR;
527 }
528
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800529 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700530
Marissa Wall61c58622018-07-18 10:12:20 -0700531 // Reject if the layer is invalid
532 uint32_t bufferWidth = s.buffer->width;
533 uint32_t bufferHeight = s.buffer->height;
534
Peiyong Linefefaac2018-08-17 12:27:51 -0700535 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700536 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700537 }
538
539 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800540 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700541 if (invTransform & 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
Vishnu Nair60356342018-11-13 13:00:45 -0800546 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700547 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
548 ALOGE("[%s] rejecting buffer: "
549 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700550 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800551 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700552 return BAD_VALUE;
553 }
554
Marissa Wall5a68a772018-12-22 17:43:42 -0800555 for (auto& handle : mDrawingState.callbackHandles) {
556 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800557 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800558 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700559
Valerie Hau134651a2020-01-28 16:21:22 -0800560 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700561 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800562 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700563
Marissa Wall16c112d2019-03-20 13:21:13 -0700564 mCurrentStateModified = false;
565
Marissa Wall61c58622018-07-18 10:12:20 -0700566 return NO_ERROR;
567}
568
569status_t BufferStateLayer::updateActiveBuffer() {
570 const State& s(getDrawingState());
571
572 if (s.buffer == nullptr) {
573 return BAD_VALUE;
574 }
575
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700576 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700577 mBufferInfo.mBuffer = s.buffer;
578 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700579
580 return NO_ERROR;
581}
582
Valerie Haubf784642020-01-29 07:25:23 -0800583status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700584 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700585 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800586 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800587 {
588 Mutex::Autolock lock(mFrameEventHistoryMutex);
589 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
590 }
Marissa Wall61c58622018-07-18 10:12:20 -0700591 return NO_ERROR;
592}
593
Marissa Wall947d34e2019-03-29 14:03:53 -0700594void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
595 std::lock_guard lock(mMutex);
596 if (!clientCacheId.isValid()) {
597 ALOGE("invalid process, failed to erase buffer");
598 return;
599 }
600 eraseBufferLocked(clientCacheId);
601}
602
603uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
604 std::lock_guard<std::mutex> lock(mMutex);
605 auto itr = mCachedBuffers.find(clientCacheId);
606 if (itr == mCachedBuffers.end()) {
607 return addCachedBuffer(clientCacheId);
608 }
609 auto& [hwcCacheSlot, counter] = itr->second;
610 counter = mCounter++;
611 return hwcCacheSlot;
612}
613
614uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
615 REQUIRES(mMutex) {
616 if (!clientCacheId.isValid()) {
617 ALOGE("invalid process, returning invalid slot");
618 return BufferQueue::INVALID_BUFFER_SLOT;
619 }
620
621 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
622
623 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
624 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
625 return hwcCacheSlot;
626}
627
628uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
629 if (mFreeHwcCacheSlots.empty()) {
630 evictLeastRecentlyUsed();
631 }
632
633 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
634 mFreeHwcCacheSlots.pop();
635 return hwcCacheSlot;
636}
637
638void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
639 uint64_t minCounter = UINT_MAX;
640 client_cache_t minClientCacheId = {};
641 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
642 const auto& [hwcCacheSlot, counter] = slotCounter;
643 if (counter < minCounter) {
644 minCounter = counter;
645 minClientCacheId = clientCacheId;
646 }
647 }
648 eraseBufferLocked(minClientCacheId);
649
650 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
651}
652
653void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
654 REQUIRES(mMutex) {
655 auto itr = mCachedBuffers.find(clientCacheId);
656 if (itr == mCachedBuffers.end()) {
657 return;
658 }
659 auto& [hwcCacheSlot, counter] = itr->second;
660
661 // TODO send to hwc cache and resources
662
663 mFreeHwcCacheSlots.push(hwcCacheSlot);
664 mCachedBuffers.erase(clientCacheId);
665}
chaviw4244e032019-09-04 11:27:49 -0700666
667void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700668 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700669
chaviwdebadb82020-03-26 14:57:24 -0700670 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700671 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
672 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
673 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700674 mBufferInfo.mTransform = s.transform;
675 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
676 mBufferInfo.mCrop = computeCrop(s);
677 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
678 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
679 mBufferInfo.mHdrMetadata = s.hdrMetadata;
680 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700681 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700682 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700683}
684
Robert Carr916b0362020-10-06 13:53:03 -0700685uint32_t BufferStateLayer::getEffectiveScalingMode() const {
686 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
687}
688
chaviw4244e032019-09-04 11:27:49 -0700689Rect BufferStateLayer::computeCrop(const State& s) {
690 if (s.crop.isEmpty() && s.buffer) {
691 return s.buffer->getBounds();
692 } else if (s.buffer) {
693 Rect crop = s.crop;
694 crop.left = std::max(crop.left, 0);
695 crop.top = std::max(crop.top, 0);
696 uint32_t bufferWidth = s.buffer->getWidth();
697 uint32_t bufferHeight = s.buffer->getHeight();
698 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
699 bufferWidth <= std::numeric_limits<int32_t>::max()) {
700 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
701 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
702 }
703 if (!crop.isValid()) {
704 // Crop rect is out of bounds, return whole buffer
705 return s.buffer->getBounds();
706 }
707 return crop;
708 }
709 return s.crop;
710}
711
chaviwb4c6e582019-08-16 14:35:07 -0700712sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700713 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700714 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700715 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700716 layer->mHwcSlotGenerator = mHwcSlotGenerator;
717 layer->setInitialValuesForClone(this);
718 return layer;
719}
Valerie Hau92bf5482020-02-10 09:49:08 -0800720
721Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
722 const auto& p = mDrawingParent.promote();
723 if (p != nullptr) {
724 RoundedCornerState parentState = p->getRoundedCornerState();
725 if (parentState.radius > 0) {
726 ui::Transform t = getActiveTransform(getDrawingState());
727 t = t.inverse();
728 parentState.cropRect = t.transform(parentState.cropRect);
729 // The rounded corners shader only accepts 1 corner radius for performance reasons,
730 // but a transform matrix can define horizontal and vertical scales.
731 // Let's take the average between both of them and pass into the shader, practically we
732 // never do this type of transformation on windows anyway.
733 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
734 return parentState;
735 }
736 }
737 const float radius = getDrawingState().cornerRadius;
738 const State& s(getDrawingState());
739 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
740 return RoundedCornerState();
741 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
742 static_cast<float>(s.active.transform.ty()),
743 static_cast<float>(s.active.transform.tx() + s.active.w),
744 static_cast<float>(s.active.transform.ty() + s.active.h)),
745 radius);
746}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700747
748bool BufferStateLayer::bufferNeedsFiltering() const {
749 const State& s(getDrawingState());
750 if (!s.buffer) {
751 return false;
752 }
753
754 uint32_t bufferWidth = s.buffer->width;
755 uint32_t bufferHeight = s.buffer->height;
756
757 // Undo any transformations on the buffer and return the result.
758 if (s.transform & ui::Transform::ROT_90) {
759 std::swap(bufferWidth, bufferHeight);
760 }
761
762 if (s.transformToDisplayInverse) {
763 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
764 if (invTransform & ui::Transform::ROT_90) {
765 std::swap(bufferWidth, bufferHeight);
766 }
767 }
768
769 const Rect layerSize{getBounds()};
770 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
771}
Marissa Wall61c58622018-07-18 10:12:20 -0700772} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800773
774// TODO(b/129481165): remove the #pragma below and fix conversion issues
775#pragma clang diagnostic pop // ignored "-Wconversion"