blob: 4f8fc41469627be649c7fbc0eb088c9e68d65942 [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"
Mikael Pessa90092f42019-08-26 17:22:04 -070036#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080037#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080038
Marissa Wall61c58622018-07-18 10:12:20 -070039namespace android {
40
Lloyd Pique42ab75e2018-09-12 20:46:03 -070041// clang-format off
42const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1
47};
48// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070049
Marissa Wall947d34e2019-03-29 14:03:53 -070050BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
51 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080052 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080053 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080054}
Marissa Wall61c58622018-07-18 10:12:20 -070055
Alec Mouri4545a8a2019-08-08 20:05:32 -070056BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070057 // The original layer and the clone layer share the same texture and buffer. Therefore, only
58 // one of the layers, in this case the original layer, needs to handle the deletion. The
59 // original layer and the clone should be removed at the same time so there shouldn't be any
60 // issue with the clone layer trying to use the texture.
61 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070062 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070063 // RenderEngine may have been using the buffer as an external texture
64 // after the client uncached the buffer.
65 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070066 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070067 }
68}
69
Marissa Wall61c58622018-07-18 10:12:20 -070070// -----------------------------------------------------------------------
71// Interface implementation for Layer
72// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070073void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080074 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
75 // buffer that was presented on this layer. The first transaction that came in this frame that
76 // replaced the previous buffer on this layer needs this release fence, because the fence will
77 // let the client know when that previous buffer is removed from the screen.
78 //
79 // Every other transaction on this layer does not need a release fence because no other
80 // Transactions that were set on this layer this frame are going to have their preceeding buffer
81 // removed from the display this frame.
82 //
83 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
84 // buffer so it doesn't need a previous release fence because the layer still needs the previous
85 // buffer. The second transaction contains a buffer so it needs a previous release fence because
86 // the previous buffer will be released this frame. The third transaction also contains a
87 // buffer. It replaces the buffer in the second transaction. The buffer in the second
88 // transaction will now no longer be presented so it is released immediately and the third
89 // transaction doesn't need a previous release fence.
90 for (auto& handle : mDrawingState.callbackHandles) {
91 if (handle->releasePreviousBuffer) {
92 handle->previousReleaseFence = releaseFence;
93 break;
94 }
95 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070096
Valerie Haubf784642020-01-29 07:25:23 -080097 mPreviousReleaseFence = releaseFence;
98
Mikael Pessa2e1608f2019-07-19 11:25:35 -070099 // Prevent tracing the same release multiple times.
100 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -0700101 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
102 std::make_shared<FenceTime>(releaseFence),
103 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700104 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
105 }
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
Valerie Haubf784642020-01-29 07:25:23 -0800108void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700109 for (const auto& handle : mDrawingState.callbackHandles) {
110 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800111 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700112 }
113
Marissa Wallefb71af2019-06-27 14:45:53 -0700114 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800115 mDrawingState.callbackHandles);
116
117 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800118
119 const sp<Fence>& releaseFence(mPreviousReleaseFence);
120 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
121 {
122 Mutex::Autolock lock(mFrameEventHistoryMutex);
123 if (mPreviousFrameNumber != 0) {
124 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
125 std::move(releaseFenceTime));
126 }
127 }
Marissa Wall61c58622018-07-18 10:12:20 -0700128}
129
Valerie Hau871d6352020-01-29 08:44:02 -0800130void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
131 const CompositorTiming& compositorTiming) {
132 for (const auto& handle : mDrawingState.callbackHandles) {
133 handle->gpuCompositionDoneFence = glDoneFence;
134 handle->compositorTiming = compositorTiming;
135 }
136}
137
Ana Krulec010d2192018-10-08 06:29:54 -0700138bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700139 if (getSidebandStreamChanged() || getAutoRefresh()) {
140 return true;
141 }
142
Marissa Wall024a1912018-08-13 13:55:35 -0700143 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700144}
145
Marissa Walle2ffb422018-10-12 11:33:52 -0700146bool BufferStateLayer::willPresentCurrentTransaction() const {
147 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700148 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800149 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700150 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
151 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700152}
153
Valerie Hau3282b3c2020-02-03 15:37:27 -0800154/* TODO: vhau uncomment once deferred transaction migration complete in
155 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800156void BufferStateLayer::pushPendingState() {
157 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700158 return;
159 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800160 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700161 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700162}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800163*/
Marissa Wall61c58622018-07-18 10:12:20 -0700164
165bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800166 mCurrentStateModified = mCurrentState.modified;
167 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
168 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,
265 const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800266 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700267 mReleasePreviousBuffer = true;
268 }
269
Valerie Hau134651a2020-01-28 16:21:22 -0800270 mCurrentState.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);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800280 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
Valerie Hau134651a2020-01-28 16:21:22 -0800281 mFlinger->mFrameTracer->traceTimestamp(layerId, buffer->getId(), mCurrentState.frameNumber,
282 postTime, FrameTracer::FrameEvent::POST);
Valerie Hau871d6352020-01-29 08:44:02 -0800283 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700284 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700285
Ady Abraham5def7332020-05-29 16:13:47 -0700286 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
287 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700288
Valerie Hau871d6352020-01-29 08:44:02 -0800289 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700290 return true;
291}
292
293bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700294 // The acquire fences of BufferStateLayers have already signaled before they are set
295 mCallbackHandleAcquireTime = fence->getSignalTime();
296
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800297 mCurrentState.acquireFence = fence;
298 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700299 setTransactionFlags(eTransactionNeeded);
300 return true;
301}
302
303bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800304 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800305 mCurrentState.dataspace = dataspace;
306 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700307 setTransactionFlags(eTransactionNeeded);
308 return true;
309}
310
311bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800312 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800313 mCurrentState.hdrMetadata = hdrMetadata;
314 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700315 setTransactionFlags(eTransactionNeeded);
316 return true;
317}
318
319bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800320 mCurrentState.surfaceDamageRegion = surfaceDamage;
321 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700322 setTransactionFlags(eTransactionNeeded);
323 return true;
324}
325
326bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800327 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800328 mCurrentState.api = api;
329 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700330 setTransactionFlags(eTransactionNeeded);
331 return true;
332}
333
334bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800335 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800336 mCurrentState.sidebandStream = sidebandStream;
337 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700338 setTransactionFlags(eTransactionNeeded);
339
340 if (!mSidebandStreamChanged.exchange(true)) {
341 // mSidebandStreamChanged was false
342 mFlinger->signalLayerUpdate();
343 }
344 return true;
345}
346
Marissa Walle2ffb422018-10-12 11:33:52 -0700347bool BufferStateLayer::setTransactionCompletedListeners(
348 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700349 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700350 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700351 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700352 return false;
353 }
354
355 const bool willPresent = willPresentCurrentTransaction();
356
357 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700358 // If this transaction set a buffer on this layer, release its previous buffer
359 handle->releasePreviousBuffer = mReleasePreviousBuffer;
360
Marissa Walle2ffb422018-10-12 11:33:52 -0700361 // If this layer will be presented in this frame
362 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700363 // If this transaction set an acquire fence on this layer, set its acquire time
364 handle->acquireTime = mCallbackHandleAcquireTime;
365
Marissa Walle2ffb422018-10-12 11:33:52 -0700366 // Notify the transaction completed thread that there is a pending latched callback
367 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800368 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700369
370 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800371 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700372
373 } else { // If this layer will NOT need to be relatched and presented this frame
374 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700375 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700376 }
377 }
378
Marissa Wallfda30bb2018-10-12 11:34:28 -0700379 mReleasePreviousBuffer = false;
380 mCallbackHandleAcquireTime = -1;
381
Marissa Walle2ffb422018-10-12 11:33:52 -0700382 return willPresent;
383}
384
Valerie Hau7618b232020-01-09 16:03:08 -0800385void BufferStateLayer::forceSendCallbacks() {
386 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
387 mCurrentState.callbackHandles);
388}
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800391 mCurrentState.transparentRegionHint = transparent;
392 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700393 setTransactionFlags(eTransactionNeeded);
394 return true;
395}
396
Marissa Wall861616d2018-10-22 12:52:23 -0700397Rect BufferStateLayer::getBufferSize(const State& s) const {
398 // for buffer state layers we use the display frame size as the buffer size.
399 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
400 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700401 }
402
Marissa Wall861616d2018-10-22 12:52:23 -0700403 // if the display frame is not defined, use the parent bounds as the buffer size.
404 const auto& p = mDrawingParent.promote();
405 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800406 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700407 if (!parentBounds.isEmpty()) {
408 return parentBounds;
409 }
410 }
411
Marissa Wall861616d2018-10-22 12:52:23 -0700412 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700413}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800414
415FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
416 const State& s(getDrawingState());
417 // for buffer state layers we use the display frame size as the buffer size.
418 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
419 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
420 }
421
422 // if the display frame is not defined, use the parent bounds as the buffer size.
423 return parentBounds;
424}
425
Marissa Wall61c58622018-07-18 10:12:20 -0700426// -----------------------------------------------------------------------
427
428// -----------------------------------------------------------------------
429// Interface implementation for BufferLayer
430// -----------------------------------------------------------------------
431bool BufferStateLayer::fenceHasSignaled() const {
432 if (latchUnsignaledBuffers()) {
433 return true;
434 }
435
Alec Mouri91f6df32020-01-30 08:48:58 -0800436 const bool fenceSignaled =
437 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
438 if (!fenceSignaled) {
439 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
440 TimeStats::LatchSkipReason::LateAcquire);
441 }
442
443 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700444}
445
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700446bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700447 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
448 return true;
449 }
450
chaviwfa67b552019-08-12 16:51:55 -0700451 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700452}
453
Valerie Hau871d6352020-01-29 08:44:02 -0800454bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
455 for (const auto& handle : mDrawingState.callbackHandles) {
456 handle->refreshStartTime = refreshStartTime;
457 }
458 return BufferLayer::onPreComposition(refreshStartTime);
459}
460
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700461uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800462 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700463}
464
Robert Carrfe1209c2020-02-11 12:25:35 -0800465/**
466 * This is the frameNumber used for deferred transaction signalling. We need to use this because
467 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
468 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
469 * deferred transaction?) but this is an important legacy use case, for example moving
470 * a window at the same time it draws makes use of this kind of technique. So anyway
471 * imagine we have something like this:
472 *
473 * Transaction { // containing
474 * Buffer -> frameNumber = 2
475 * DeferTransactionUntil -> frameNumber = 2
476 * Random other stuff
477 * }
478 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
479 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
480 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
481 * is not ready. So we will return false from applyPendingState and not swap
482 * current state to drawing state. But because we don't swap current state
483 * to drawing state the number will never update and we will be stuck. This way
484 * we can see we need to return the frame number for the buffer we are about
485 * to apply.
486 */
487uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
488 return mCurrentState.frameNumber;
489}
490
Marissa Wall61c58622018-07-18 10:12:20 -0700491bool BufferStateLayer::getAutoRefresh() const {
492 // TODO(marissaw): support shared buffer mode
493 return false;
494}
495
496bool BufferStateLayer::getSidebandStreamChanged() const {
497 return mSidebandStreamChanged.load();
498}
499
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800500bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700501 if (mSidebandStreamChanged.exchange(false)) {
502 const State& s(getDrawingState());
503 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800504 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800505 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800506 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700507 setTransactionFlags(eTransactionNeeded);
508 mFlinger->setTransactionFlags(eTraversalNeeded);
509 }
510 recomputeVisibleRegions = true;
511
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800512 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700513 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800514 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700515}
516
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800517bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800518 const State& c(getCurrentState());
519 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700520}
521
Alec Mouri39801c02018-10-10 10:44:47 -0700522status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700523 const State& s(getDrawingState());
524 auto& engine(mFlinger->getRenderEngine());
525
Alec Mourib5c4f352019-02-19 19:46:38 -0800526 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700527}
528
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700529status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
530 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700531 const State& s(getDrawingState());
532
533 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800534 if (s.bgColorLayer) {
535 for (auto& handle : mDrawingState.callbackHandles) {
536 handle->latchTime = latchTime;
537 }
538 }
Marissa Wall61c58622018-07-18 10:12:20 -0700539 return NO_ERROR;
540 }
541
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800542 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700543
Marissa Wall61c58622018-07-18 10:12:20 -0700544 // Reject if the layer is invalid
545 uint32_t bufferWidth = s.buffer->width;
546 uint32_t bufferHeight = s.buffer->height;
547
Peiyong Linefefaac2018-08-17 12:27:51 -0700548 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700549 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700550 }
551
552 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800553 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700554 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700555 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700556 }
557 }
558
Vishnu Nair60356342018-11-13 13:00:45 -0800559 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700560 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
561 ALOGE("[%s] rejecting buffer: "
562 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700563 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800564 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700565 return BAD_VALUE;
566 }
567
Marissa Wall5a68a772018-12-22 17:43:42 -0800568 for (auto& handle : mDrawingState.callbackHandles) {
569 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800570 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800571 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700572
Alec Mouri56e538f2019-01-14 15:22:01 -0800573 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700574 // Bind the new buffer to the GL texture.
575 //
576 // Older devices require the "implicit" synchronization provided
577 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
578 // devices will either call this in Layer::onDraw, or (if it's not
579 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800580 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700581 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800582 mFlinger->mTimeStats->onDestroy(layerId);
583 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700584 return BAD_VALUE;
585 }
586 }
587
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700588 const uint64_t bufferID = getCurrentBufferId();
Valerie Hau134651a2020-01-28 16:21:22 -0800589 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
590 mBufferInfo.mFenceTime);
591 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mDrawingState.frameNumber,
592 mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700593 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Valerie Hau134651a2020-01-28 16:21:22 -0800594 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
595 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mDrawingState.frameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700596 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700597
Marissa Wall16c112d2019-03-20 13:21:13 -0700598 mCurrentStateModified = false;
599
Marissa Wall61c58622018-07-18 10:12:20 -0700600 return NO_ERROR;
601}
602
603status_t BufferStateLayer::updateActiveBuffer() {
604 const State& s(getDrawingState());
605
606 if (s.buffer == nullptr) {
607 return BAD_VALUE;
608 }
609
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700610 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700611 mBufferInfo.mBuffer = s.buffer;
612 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700613
614 return NO_ERROR;
615}
616
Valerie Haubf784642020-01-29 07:25:23 -0800617status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700618 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700619 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800620 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800621 {
622 Mutex::Autolock lock(mFrameEventHistoryMutex);
623 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
624 }
Marissa Wall61c58622018-07-18 10:12:20 -0700625 return NO_ERROR;
626}
627
Marissa Wall947d34e2019-03-29 14:03:53 -0700628void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
629 std::lock_guard lock(mMutex);
630 if (!clientCacheId.isValid()) {
631 ALOGE("invalid process, failed to erase buffer");
632 return;
633 }
634 eraseBufferLocked(clientCacheId);
635}
636
637uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
638 std::lock_guard<std::mutex> lock(mMutex);
639 auto itr = mCachedBuffers.find(clientCacheId);
640 if (itr == mCachedBuffers.end()) {
641 return addCachedBuffer(clientCacheId);
642 }
643 auto& [hwcCacheSlot, counter] = itr->second;
644 counter = mCounter++;
645 return hwcCacheSlot;
646}
647
648uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
649 REQUIRES(mMutex) {
650 if (!clientCacheId.isValid()) {
651 ALOGE("invalid process, returning invalid slot");
652 return BufferQueue::INVALID_BUFFER_SLOT;
653 }
654
655 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
656
657 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
658 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
659 return hwcCacheSlot;
660}
661
662uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
663 if (mFreeHwcCacheSlots.empty()) {
664 evictLeastRecentlyUsed();
665 }
666
667 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
668 mFreeHwcCacheSlots.pop();
669 return hwcCacheSlot;
670}
671
672void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
673 uint64_t minCounter = UINT_MAX;
674 client_cache_t minClientCacheId = {};
675 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
676 const auto& [hwcCacheSlot, counter] = slotCounter;
677 if (counter < minCounter) {
678 minCounter = counter;
679 minClientCacheId = clientCacheId;
680 }
681 }
682 eraseBufferLocked(minClientCacheId);
683
684 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
685}
686
687void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
688 REQUIRES(mMutex) {
689 auto itr = mCachedBuffers.find(clientCacheId);
690 if (itr == mCachedBuffers.end()) {
691 return;
692 }
693 auto& [hwcCacheSlot, counter] = itr->second;
694
695 // TODO send to hwc cache and resources
696
697 mFreeHwcCacheSlots.push(hwcCacheSlot);
698 mCachedBuffers.erase(clientCacheId);
699}
chaviw4244e032019-09-04 11:27:49 -0700700
701void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700702 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700703
chaviwdebadb82020-03-26 14:57:24 -0700704 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700705 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
706 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
707 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700708 mBufferInfo.mTransform = s.transform;
709 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
710 mBufferInfo.mCrop = computeCrop(s);
711 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
712 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
713 mBufferInfo.mHdrMetadata = s.hdrMetadata;
714 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700715 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700716 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700717}
718
719Rect BufferStateLayer::computeCrop(const State& s) {
720 if (s.crop.isEmpty() && s.buffer) {
721 return s.buffer->getBounds();
722 } else if (s.buffer) {
723 Rect crop = s.crop;
724 crop.left = std::max(crop.left, 0);
725 crop.top = std::max(crop.top, 0);
726 uint32_t bufferWidth = s.buffer->getWidth();
727 uint32_t bufferHeight = s.buffer->getHeight();
728 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
729 bufferWidth <= std::numeric_limits<int32_t>::max()) {
730 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
731 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
732 }
733 if (!crop.isValid()) {
734 // Crop rect is out of bounds, return whole buffer
735 return s.buffer->getBounds();
736 }
737 return crop;
738 }
739 return s.crop;
740}
741
chaviwb4c6e582019-08-16 14:35:07 -0700742sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700743 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700744 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700745 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700746 layer->mHwcSlotGenerator = mHwcSlotGenerator;
747 layer->setInitialValuesForClone(this);
748 return layer;
749}
Valerie Hau92bf5482020-02-10 09:49:08 -0800750
751Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
752 const auto& p = mDrawingParent.promote();
753 if (p != nullptr) {
754 RoundedCornerState parentState = p->getRoundedCornerState();
755 if (parentState.radius > 0) {
756 ui::Transform t = getActiveTransform(getDrawingState());
757 t = t.inverse();
758 parentState.cropRect = t.transform(parentState.cropRect);
759 // The rounded corners shader only accepts 1 corner radius for performance reasons,
760 // but a transform matrix can define horizontal and vertical scales.
761 // Let's take the average between both of them and pass into the shader, practically we
762 // never do this type of transformation on windows anyway.
763 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
764 return parentState;
765 }
766 }
767 const float radius = getDrawingState().cornerRadius;
768 const State& s(getDrawingState());
769 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
770 return RoundedCornerState();
771 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
772 static_cast<float>(s.active.transform.ty()),
773 static_cast<float>(s.active.transform.tx() + s.active.w),
774 static_cast<float>(s.active.transform.ty() + s.active.h)),
775 radius);
776}
Marissa Wall61c58622018-07-18 10:12:20 -0700777} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800778
779// TODO(b/129481165): remove the #pragma below and fix conversion issues
780#pragma clang diagnostic pop // ignored "-Wconversion"