blob: a1ed6d7116971a1ecd690959edc92fd68318ee27 [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
Valerie Hau871d6352020-01-29 08:44:02 -0800286 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime);
Ady Abraham09bd3922019-04-08 10:44:56 -0700287
Valerie Hau871d6352020-01-29 08:44:02 -0800288 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700289 return true;
290}
291
292bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700293 // The acquire fences of BufferStateLayers have already signaled before they are set
294 mCallbackHandleAcquireTime = fence->getSignalTime();
295
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800296 mCurrentState.acquireFence = fence;
297 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700298 setTransactionFlags(eTransactionNeeded);
299 return true;
300}
301
302bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800303 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800304 mCurrentState.dataspace = dataspace;
305 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700306 setTransactionFlags(eTransactionNeeded);
307 return true;
308}
309
310bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800311 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800312 mCurrentState.hdrMetadata = hdrMetadata;
313 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700314 setTransactionFlags(eTransactionNeeded);
315 return true;
316}
317
318bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800319 mCurrentState.surfaceDamageRegion = surfaceDamage;
320 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700321 setTransactionFlags(eTransactionNeeded);
322 return true;
323}
324
325bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800326 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800327 mCurrentState.api = api;
328 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700329 setTransactionFlags(eTransactionNeeded);
330 return true;
331}
332
333bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800334 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800335 mCurrentState.sidebandStream = sidebandStream;
336 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700337 setTransactionFlags(eTransactionNeeded);
338
339 if (!mSidebandStreamChanged.exchange(true)) {
340 // mSidebandStreamChanged was false
341 mFlinger->signalLayerUpdate();
342 }
343 return true;
344}
345
Marissa Walle2ffb422018-10-12 11:33:52 -0700346bool BufferStateLayer::setTransactionCompletedListeners(
347 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700348 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700349 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700350 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700351 return false;
352 }
353
354 const bool willPresent = willPresentCurrentTransaction();
355
356 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700357 // If this transaction set a buffer on this layer, release its previous buffer
358 handle->releasePreviousBuffer = mReleasePreviousBuffer;
359
Marissa Walle2ffb422018-10-12 11:33:52 -0700360 // If this layer will be presented in this frame
361 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700362 // If this transaction set an acquire fence on this layer, set its acquire time
363 handle->acquireTime = mCallbackHandleAcquireTime;
364
Marissa Walle2ffb422018-10-12 11:33:52 -0700365 // Notify the transaction completed thread that there is a pending latched callback
366 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800367 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700368
369 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800370 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700371
372 } else { // If this layer will NOT need to be relatched and presented this frame
373 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700374 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700375 }
376 }
377
Marissa Wallfda30bb2018-10-12 11:34:28 -0700378 mReleasePreviousBuffer = false;
379 mCallbackHandleAcquireTime = -1;
380
Marissa Walle2ffb422018-10-12 11:33:52 -0700381 return willPresent;
382}
383
Valerie Hau7618b232020-01-09 16:03:08 -0800384void BufferStateLayer::forceSendCallbacks() {
385 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
386 mCurrentState.callbackHandles);
387}
388
Marissa Wall61c58622018-07-18 10:12:20 -0700389bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800390 mCurrentState.transparentRegionHint = transparent;
391 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700392 setTransactionFlags(eTransactionNeeded);
393 return true;
394}
395
Marissa Wall861616d2018-10-22 12:52:23 -0700396Rect BufferStateLayer::getBufferSize(const State& s) const {
397 // for buffer state layers we use the display frame size as the buffer size.
398 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
399 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700400 }
401
Marissa Wall861616d2018-10-22 12:52:23 -0700402 // if the display frame is not defined, use the parent bounds as the buffer size.
403 const auto& p = mDrawingParent.promote();
404 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800405 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700406 if (!parentBounds.isEmpty()) {
407 return parentBounds;
408 }
409 }
410
Marissa Wall861616d2018-10-22 12:52:23 -0700411 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700412}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800413
414FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
415 const State& s(getDrawingState());
416 // for buffer state layers we use the display frame size as the buffer size.
417 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
418 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
419 }
420
421 // if the display frame is not defined, use the parent bounds as the buffer size.
422 return parentBounds;
423}
424
Marissa Wall61c58622018-07-18 10:12:20 -0700425// -----------------------------------------------------------------------
426
427// -----------------------------------------------------------------------
428// Interface implementation for BufferLayer
429// -----------------------------------------------------------------------
430bool BufferStateLayer::fenceHasSignaled() const {
431 if (latchUnsignaledBuffers()) {
432 return true;
433 }
434
Alec Mouri91f6df32020-01-30 08:48:58 -0800435 const bool fenceSignaled =
436 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
437 if (!fenceSignaled) {
438 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
439 TimeStats::LatchSkipReason::LateAcquire);
440 }
441
442 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700443}
444
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700445bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700446 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
447 return true;
448 }
449
chaviwfa67b552019-08-12 16:51:55 -0700450 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700451}
452
Valerie Hau871d6352020-01-29 08:44:02 -0800453bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
454 for (const auto& handle : mDrawingState.callbackHandles) {
455 handle->refreshStartTime = refreshStartTime;
456 }
457 return BufferLayer::onPreComposition(refreshStartTime);
458}
459
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700460uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800461 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700462}
463
Robert Carrfe1209c2020-02-11 12:25:35 -0800464/**
465 * This is the frameNumber used for deferred transaction signalling. We need to use this because
466 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
467 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
468 * deferred transaction?) but this is an important legacy use case, for example moving
469 * a window at the same time it draws makes use of this kind of technique. So anyway
470 * imagine we have something like this:
471 *
472 * Transaction { // containing
473 * Buffer -> frameNumber = 2
474 * DeferTransactionUntil -> frameNumber = 2
475 * Random other stuff
476 * }
477 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
478 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
479 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
480 * is not ready. So we will return false from applyPendingState and not swap
481 * current state to drawing state. But because we don't swap current state
482 * to drawing state the number will never update and we will be stuck. This way
483 * we can see we need to return the frame number for the buffer we are about
484 * to apply.
485 */
486uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
487 return mCurrentState.frameNumber;
488}
489
Marissa Wall61c58622018-07-18 10:12:20 -0700490bool BufferStateLayer::getAutoRefresh() const {
491 // TODO(marissaw): support shared buffer mode
492 return false;
493}
494
495bool BufferStateLayer::getSidebandStreamChanged() const {
496 return mSidebandStreamChanged.load();
497}
498
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800499bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700500 if (mSidebandStreamChanged.exchange(false)) {
501 const State& s(getDrawingState());
502 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800503 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800504 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800505 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700506 setTransactionFlags(eTransactionNeeded);
507 mFlinger->setTransactionFlags(eTraversalNeeded);
508 }
509 recomputeVisibleRegions = true;
510
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800511 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700512 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800513 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700514}
515
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800516bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800517 const State& c(getCurrentState());
518 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700519}
520
Alec Mouri39801c02018-10-10 10:44:47 -0700521status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700522 const State& s(getDrawingState());
523 auto& engine(mFlinger->getRenderEngine());
524
Alec Mourib5c4f352019-02-19 19:46:38 -0800525 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700526}
527
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700528status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
529 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700530 const State& s(getDrawingState());
531
532 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800533 if (s.bgColorLayer) {
534 for (auto& handle : mDrawingState.callbackHandles) {
535 handle->latchTime = latchTime;
536 }
537 }
Marissa Wall61c58622018-07-18 10:12:20 -0700538 return NO_ERROR;
539 }
540
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800541 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700542
Marissa Wall61c58622018-07-18 10:12:20 -0700543 // Reject if the layer is invalid
544 uint32_t bufferWidth = s.buffer->width;
545 uint32_t bufferHeight = s.buffer->height;
546
Peiyong Linefefaac2018-08-17 12:27:51 -0700547 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700548 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700549 }
550
551 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800552 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700553 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700554 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 }
556 }
557
Vishnu Nair60356342018-11-13 13:00:45 -0800558 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700559 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
560 ALOGE("[%s] rejecting buffer: "
561 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700562 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800563 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700564 return BAD_VALUE;
565 }
566
Marissa Wall5a68a772018-12-22 17:43:42 -0800567 for (auto& handle : mDrawingState.callbackHandles) {
568 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800569 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800570 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700571
Alec Mouri56e538f2019-01-14 15:22:01 -0800572 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700573 // Bind the new buffer to the GL texture.
574 //
575 // Older devices require the "implicit" synchronization provided
576 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
577 // devices will either call this in Layer::onDraw, or (if it's not
578 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800579 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700580 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800581 mFlinger->mTimeStats->onDestroy(layerId);
582 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700583 return BAD_VALUE;
584 }
585 }
586
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700587 const uint64_t bufferID = getCurrentBufferId();
Valerie Hau134651a2020-01-28 16:21:22 -0800588 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
589 mBufferInfo.mFenceTime);
590 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mDrawingState.frameNumber,
591 mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700592 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Valerie Hau134651a2020-01-28 16:21:22 -0800593 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
594 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mDrawingState.frameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700595 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700596
Marissa Wall16c112d2019-03-20 13:21:13 -0700597 mCurrentStateModified = false;
598
Marissa Wall61c58622018-07-18 10:12:20 -0700599 return NO_ERROR;
600}
601
602status_t BufferStateLayer::updateActiveBuffer() {
603 const State& s(getDrawingState());
604
605 if (s.buffer == nullptr) {
606 return BAD_VALUE;
607 }
608
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700609 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700610 mBufferInfo.mBuffer = s.buffer;
611 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700612
613 return NO_ERROR;
614}
615
Valerie Haubf784642020-01-29 07:25:23 -0800616status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700617 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700618 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800619 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800620 {
621 Mutex::Autolock lock(mFrameEventHistoryMutex);
622 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
623 }
Marissa Wall61c58622018-07-18 10:12:20 -0700624 return NO_ERROR;
625}
626
Marissa Wall947d34e2019-03-29 14:03:53 -0700627void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
628 std::lock_guard lock(mMutex);
629 if (!clientCacheId.isValid()) {
630 ALOGE("invalid process, failed to erase buffer");
631 return;
632 }
633 eraseBufferLocked(clientCacheId);
634}
635
636uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
637 std::lock_guard<std::mutex> lock(mMutex);
638 auto itr = mCachedBuffers.find(clientCacheId);
639 if (itr == mCachedBuffers.end()) {
640 return addCachedBuffer(clientCacheId);
641 }
642 auto& [hwcCacheSlot, counter] = itr->second;
643 counter = mCounter++;
644 return hwcCacheSlot;
645}
646
647uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
648 REQUIRES(mMutex) {
649 if (!clientCacheId.isValid()) {
650 ALOGE("invalid process, returning invalid slot");
651 return BufferQueue::INVALID_BUFFER_SLOT;
652 }
653
654 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
655
656 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
657 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
658 return hwcCacheSlot;
659}
660
661uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
662 if (mFreeHwcCacheSlots.empty()) {
663 evictLeastRecentlyUsed();
664 }
665
666 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
667 mFreeHwcCacheSlots.pop();
668 return hwcCacheSlot;
669}
670
671void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
672 uint64_t minCounter = UINT_MAX;
673 client_cache_t minClientCacheId = {};
674 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
675 const auto& [hwcCacheSlot, counter] = slotCounter;
676 if (counter < minCounter) {
677 minCounter = counter;
678 minClientCacheId = clientCacheId;
679 }
680 }
681 eraseBufferLocked(minClientCacheId);
682
683 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
684}
685
686void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
687 REQUIRES(mMutex) {
688 auto itr = mCachedBuffers.find(clientCacheId);
689 if (itr == mCachedBuffers.end()) {
690 return;
691 }
692 auto& [hwcCacheSlot, counter] = itr->second;
693
694 // TODO send to hwc cache and resources
695
696 mFreeHwcCacheSlots.push(hwcCacheSlot);
697 mCachedBuffers.erase(clientCacheId);
698}
chaviw4244e032019-09-04 11:27:49 -0700699
700void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700701 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700702
chaviwdebadb82020-03-26 14:57:24 -0700703 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700704 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
705 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
706 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700707 mBufferInfo.mTransform = s.transform;
708 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
709 mBufferInfo.mCrop = computeCrop(s);
710 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
711 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
712 mBufferInfo.mHdrMetadata = s.hdrMetadata;
713 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700714 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700715 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700716}
717
718Rect BufferStateLayer::computeCrop(const State& s) {
719 if (s.crop.isEmpty() && s.buffer) {
720 return s.buffer->getBounds();
721 } else if (s.buffer) {
722 Rect crop = s.crop;
723 crop.left = std::max(crop.left, 0);
724 crop.top = std::max(crop.top, 0);
725 uint32_t bufferWidth = s.buffer->getWidth();
726 uint32_t bufferHeight = s.buffer->getHeight();
727 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
728 bufferWidth <= std::numeric_limits<int32_t>::max()) {
729 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
730 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
731 }
732 if (!crop.isValid()) {
733 // Crop rect is out of bounds, return whole buffer
734 return s.buffer->getBounds();
735 }
736 return crop;
737 }
738 return s.crop;
739}
740
chaviwb4c6e582019-08-16 14:35:07 -0700741sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700742 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700743 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700744 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700745 layer->mHwcSlotGenerator = mHwcSlotGenerator;
746 layer->setInitialValuesForClone(this);
747 return layer;
748}
Valerie Hau92bf5482020-02-10 09:49:08 -0800749
750Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
751 const auto& p = mDrawingParent.promote();
752 if (p != nullptr) {
753 RoundedCornerState parentState = p->getRoundedCornerState();
754 if (parentState.radius > 0) {
755 ui::Transform t = getActiveTransform(getDrawingState());
756 t = t.inverse();
757 parentState.cropRect = t.transform(parentState.cropRect);
758 // The rounded corners shader only accepts 1 corner radius for performance reasons,
759 // but a transform matrix can define horizontal and vertical scales.
760 // Let's take the average between both of them and pass into the shader, practically we
761 // never do this type of transformation on windows anyway.
762 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
763 return parentState;
764 }
765 }
766 const float radius = getDrawingState().cornerRadius;
767 const State& s(getDrawingState());
768 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
769 return RoundedCornerState();
770 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
771 static_cast<float>(s.active.transform.ty()),
772 static_cast<float>(s.active.transform.tx() + s.active.w),
773 static_cast<float>(s.active.transform.ty() + s.active.h)),
774 radius);
775}
Marissa Wall61c58622018-07-18 10:12:20 -0700776} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800777
778// TODO(b/129481165): remove the #pragma below and fix conversion issues
779#pragma clang diagnostic pop // ignored "-Wconversion"