blob: 9a4571805d2ed8d5e626461c547f03858e7cbb21 [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) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700266 mReleasePreviousBuffer = true;
267 }
268
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700269 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800270
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800271 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700272 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800273 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700274 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700275
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800276 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800277 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
278 postTime);
Valerie Hau871d6352020-01-29 08:44:02 -0800279 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700280 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700281
Ady Abraham5def7332020-05-29 16:13:47 -0700282 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
283 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700284
Valerie Hau871d6352020-01-29 08:44:02 -0800285 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700286 return true;
287}
288
289bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700290 // The acquire fences of BufferStateLayers have already signaled before they are set
291 mCallbackHandleAcquireTime = fence->getSignalTime();
292
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800293 mCurrentState.acquireFence = fence;
294 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700295 setTransactionFlags(eTransactionNeeded);
296 return true;
297}
298
299bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800300 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 mCurrentState.dataspace = dataspace;
302 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700303 setTransactionFlags(eTransactionNeeded);
304 return true;
305}
306
307bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800308 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800309 mCurrentState.hdrMetadata = hdrMetadata;
310 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700311 setTransactionFlags(eTransactionNeeded);
312 return true;
313}
314
315bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800316 mCurrentState.surfaceDamageRegion = surfaceDamage;
317 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700318 setTransactionFlags(eTransactionNeeded);
319 return true;
320}
321
322bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800323 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800324 mCurrentState.api = api;
325 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700326 setTransactionFlags(eTransactionNeeded);
327 return true;
328}
329
330bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800331 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800332 mCurrentState.sidebandStream = sidebandStream;
333 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700334 setTransactionFlags(eTransactionNeeded);
335
336 if (!mSidebandStreamChanged.exchange(true)) {
337 // mSidebandStreamChanged was false
338 mFlinger->signalLayerUpdate();
339 }
340 return true;
341}
342
Marissa Walle2ffb422018-10-12 11:33:52 -0700343bool BufferStateLayer::setTransactionCompletedListeners(
344 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700345 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700346 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700347 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700348 return false;
349 }
350
351 const bool willPresent = willPresentCurrentTransaction();
352
353 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700354 // If this transaction set a buffer on this layer, release its previous buffer
355 handle->releasePreviousBuffer = mReleasePreviousBuffer;
356
Marissa Walle2ffb422018-10-12 11:33:52 -0700357 // If this layer will be presented in this frame
358 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700359 // If this transaction set an acquire fence on this layer, set its acquire time
360 handle->acquireTime = mCallbackHandleAcquireTime;
361
Marissa Walle2ffb422018-10-12 11:33:52 -0700362 // Notify the transaction completed thread that there is a pending latched callback
363 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800364 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700365
366 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800367 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700368
369 } else { // If this layer will NOT need to be relatched and presented this frame
370 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700371 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700372 }
373 }
374
Marissa Wallfda30bb2018-10-12 11:34:28 -0700375 mReleasePreviousBuffer = false;
376 mCallbackHandleAcquireTime = -1;
377
Marissa Walle2ffb422018-10-12 11:33:52 -0700378 return willPresent;
379}
380
Valerie Hau7618b232020-01-09 16:03:08 -0800381void BufferStateLayer::forceSendCallbacks() {
382 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
383 mCurrentState.callbackHandles);
384}
385
Marissa Wall61c58622018-07-18 10:12:20 -0700386bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800387 mCurrentState.transparentRegionHint = transparent;
388 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700389 setTransactionFlags(eTransactionNeeded);
390 return true;
391}
392
Marissa Wall861616d2018-10-22 12:52:23 -0700393Rect BufferStateLayer::getBufferSize(const State& s) const {
394 // for buffer state layers we use the display frame size as the buffer size.
395 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
396 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700397 }
398
Marissa Wall861616d2018-10-22 12:52:23 -0700399 // if the display frame is not defined, use the parent bounds as the buffer size.
400 const auto& p = mDrawingParent.promote();
401 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800402 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700403 if (!parentBounds.isEmpty()) {
404 return parentBounds;
405 }
406 }
407
Marissa Wall861616d2018-10-22 12:52:23 -0700408 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700409}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800410
411FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
412 const State& s(getDrawingState());
413 // for buffer state layers we use the display frame size as the buffer size.
414 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
415 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
416 }
417
418 // if the display frame is not defined, use the parent bounds as the buffer size.
419 return parentBounds;
420}
421
Marissa Wall61c58622018-07-18 10:12:20 -0700422// -----------------------------------------------------------------------
423
424// -----------------------------------------------------------------------
425// Interface implementation for BufferLayer
426// -----------------------------------------------------------------------
427bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800428 const bool fenceSignaled =
429 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
430 if (!fenceSignaled) {
431 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
432 TimeStats::LatchSkipReason::LateAcquire);
433 }
434
435 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700436}
437
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700438bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700439 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
440 return true;
441 }
442
chaviwfa67b552019-08-12 16:51:55 -0700443 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700444}
445
Valerie Hau871d6352020-01-29 08:44:02 -0800446bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
447 for (const auto& handle : mDrawingState.callbackHandles) {
448 handle->refreshStartTime = refreshStartTime;
449 }
450 return BufferLayer::onPreComposition(refreshStartTime);
451}
452
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700453uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800454 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700455}
456
Robert Carrfe1209c2020-02-11 12:25:35 -0800457/**
458 * This is the frameNumber used for deferred transaction signalling. We need to use this because
459 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
460 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
461 * deferred transaction?) but this is an important legacy use case, for example moving
462 * a window at the same time it draws makes use of this kind of technique. So anyway
463 * imagine we have something like this:
464 *
465 * Transaction { // containing
466 * Buffer -> frameNumber = 2
467 * DeferTransactionUntil -> frameNumber = 2
468 * Random other stuff
469 * }
470 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
471 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
472 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
473 * is not ready. So we will return false from applyPendingState and not swap
474 * current state to drawing state. But because we don't swap current state
475 * to drawing state the number will never update and we will be stuck. This way
476 * we can see we need to return the frame number for the buffer we are about
477 * to apply.
478 */
479uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
480 return mCurrentState.frameNumber;
481}
482
Marissa Wall61c58622018-07-18 10:12:20 -0700483bool BufferStateLayer::getAutoRefresh() const {
484 // TODO(marissaw): support shared buffer mode
485 return false;
486}
487
488bool BufferStateLayer::getSidebandStreamChanged() const {
489 return mSidebandStreamChanged.load();
490}
491
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800492bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700493 if (mSidebandStreamChanged.exchange(false)) {
494 const State& s(getDrawingState());
495 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800496 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800497 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800498 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700499 setTransactionFlags(eTransactionNeeded);
500 mFlinger->setTransactionFlags(eTraversalNeeded);
501 }
502 recomputeVisibleRegions = true;
503
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800504 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700505 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800506 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700507}
508
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800509bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800510 const State& c(getCurrentState());
511 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700512}
513
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700514status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
515 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700516 const State& s(getDrawingState());
517
518 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800519 if (s.bgColorLayer) {
520 for (auto& handle : mDrawingState.callbackHandles) {
521 handle->latchTime = latchTime;
522 }
523 }
Marissa Wall61c58622018-07-18 10:12:20 -0700524 return NO_ERROR;
525 }
526
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800527 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700528
Marissa Wall61c58622018-07-18 10:12:20 -0700529 // Reject if the layer is invalid
530 uint32_t bufferWidth = s.buffer->width;
531 uint32_t bufferHeight = s.buffer->height;
532
Peiyong Linefefaac2018-08-17 12:27:51 -0700533 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700534 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700535 }
536
537 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800538 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700539 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700540 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700541 }
542 }
543
Vishnu Nair60356342018-11-13 13:00:45 -0800544 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700545 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
546 ALOGE("[%s] rejecting buffer: "
547 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700548 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800549 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700550 return BAD_VALUE;
551 }
552
Marissa Wall5a68a772018-12-22 17:43:42 -0800553 for (auto& handle : mDrawingState.callbackHandles) {
554 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800555 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800556 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700557
Valerie Hau134651a2020-01-28 16:21:22 -0800558 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700559 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800560 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700561
Marissa Wall16c112d2019-03-20 13:21:13 -0700562 mCurrentStateModified = false;
563
Marissa Wall61c58622018-07-18 10:12:20 -0700564 return NO_ERROR;
565}
566
567status_t BufferStateLayer::updateActiveBuffer() {
568 const State& s(getDrawingState());
569
570 if (s.buffer == nullptr) {
571 return BAD_VALUE;
572 }
573
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700574 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700575 mBufferInfo.mBuffer = s.buffer;
576 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700577
578 return NO_ERROR;
579}
580
Valerie Haubf784642020-01-29 07:25:23 -0800581status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700582 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700583 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800584 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800585 {
586 Mutex::Autolock lock(mFrameEventHistoryMutex);
587 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
588 }
Marissa Wall61c58622018-07-18 10:12:20 -0700589 return NO_ERROR;
590}
591
Marissa Wall947d34e2019-03-29 14:03:53 -0700592void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
593 std::lock_guard lock(mMutex);
594 if (!clientCacheId.isValid()) {
595 ALOGE("invalid process, failed to erase buffer");
596 return;
597 }
598 eraseBufferLocked(clientCacheId);
599}
600
601uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
602 std::lock_guard<std::mutex> lock(mMutex);
603 auto itr = mCachedBuffers.find(clientCacheId);
604 if (itr == mCachedBuffers.end()) {
605 return addCachedBuffer(clientCacheId);
606 }
607 auto& [hwcCacheSlot, counter] = itr->second;
608 counter = mCounter++;
609 return hwcCacheSlot;
610}
611
612uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
613 REQUIRES(mMutex) {
614 if (!clientCacheId.isValid()) {
615 ALOGE("invalid process, returning invalid slot");
616 return BufferQueue::INVALID_BUFFER_SLOT;
617 }
618
619 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
620
621 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
622 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
623 return hwcCacheSlot;
624}
625
626uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
627 if (mFreeHwcCacheSlots.empty()) {
628 evictLeastRecentlyUsed();
629 }
630
631 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
632 mFreeHwcCacheSlots.pop();
633 return hwcCacheSlot;
634}
635
636void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
637 uint64_t minCounter = UINT_MAX;
638 client_cache_t minClientCacheId = {};
639 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
640 const auto& [hwcCacheSlot, counter] = slotCounter;
641 if (counter < minCounter) {
642 minCounter = counter;
643 minClientCacheId = clientCacheId;
644 }
645 }
646 eraseBufferLocked(minClientCacheId);
647
648 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
649}
650
651void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
652 REQUIRES(mMutex) {
653 auto itr = mCachedBuffers.find(clientCacheId);
654 if (itr == mCachedBuffers.end()) {
655 return;
656 }
657 auto& [hwcCacheSlot, counter] = itr->second;
658
659 // TODO send to hwc cache and resources
660
661 mFreeHwcCacheSlots.push(hwcCacheSlot);
662 mCachedBuffers.erase(clientCacheId);
663}
chaviw4244e032019-09-04 11:27:49 -0700664
665void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700666 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700667
chaviwdebadb82020-03-26 14:57:24 -0700668 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700669 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
670 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
671 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700672 mBufferInfo.mTransform = s.transform;
673 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
674 mBufferInfo.mCrop = computeCrop(s);
675 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
676 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
677 mBufferInfo.mHdrMetadata = s.hdrMetadata;
678 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700679 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700680 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700681}
682
Robert Carr916b0362020-10-06 13:53:03 -0700683uint32_t BufferStateLayer::getEffectiveScalingMode() const {
684 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
685}
686
chaviw4244e032019-09-04 11:27:49 -0700687Rect BufferStateLayer::computeCrop(const State& s) {
688 if (s.crop.isEmpty() && s.buffer) {
689 return s.buffer->getBounds();
690 } else if (s.buffer) {
691 Rect crop = s.crop;
692 crop.left = std::max(crop.left, 0);
693 crop.top = std::max(crop.top, 0);
694 uint32_t bufferWidth = s.buffer->getWidth();
695 uint32_t bufferHeight = s.buffer->getHeight();
696 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
697 bufferWidth <= std::numeric_limits<int32_t>::max()) {
698 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
699 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
700 }
701 if (!crop.isValid()) {
702 // Crop rect is out of bounds, return whole buffer
703 return s.buffer->getBounds();
704 }
705 return crop;
706 }
707 return s.crop;
708}
709
chaviwb4c6e582019-08-16 14:35:07 -0700710sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700711 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700712 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700713 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700714 layer->mHwcSlotGenerator = mHwcSlotGenerator;
715 layer->setInitialValuesForClone(this);
716 return layer;
717}
Valerie Hau92bf5482020-02-10 09:49:08 -0800718
719Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
720 const auto& p = mDrawingParent.promote();
721 if (p != nullptr) {
722 RoundedCornerState parentState = p->getRoundedCornerState();
723 if (parentState.radius > 0) {
724 ui::Transform t = getActiveTransform(getDrawingState());
725 t = t.inverse();
726 parentState.cropRect = t.transform(parentState.cropRect);
727 // The rounded corners shader only accepts 1 corner radius for performance reasons,
728 // but a transform matrix can define horizontal and vertical scales.
729 // Let's take the average between both of them and pass into the shader, practically we
730 // never do this type of transformation on windows anyway.
731 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
732 return parentState;
733 }
734 }
735 const float radius = getDrawingState().cornerRadius;
736 const State& s(getDrawingState());
737 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
738 return RoundedCornerState();
739 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
740 static_cast<float>(s.active.transform.ty()),
741 static_cast<float>(s.active.transform.tx() + s.active.w),
742 static_cast<float>(s.active.transform.ty() + s.active.h)),
743 radius);
744}
Marissa Wall61c58622018-07-18 10:12:20 -0700745} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800746
747// TODO(b/129481165): remove the #pragma below and fix conversion issues
748#pragma clang diagnostic pop // ignored "-Wconversion"