blob: 278ad5292744b6d11c5a86978cdede5353a0f1e4 [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 Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/Layer.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070031#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070032#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070033#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070034#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070035
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "ColorLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070037#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080039
Marissa Wall61c58622018-07-18 10:12:20 -070040namespace android {
41
Lloyd Pique42ab75e2018-09-12 20:46:03 -070042// clang-format off
43const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
44 1, 0, 0, 0,
45 0, 1, 0, 0,
46 0, 0, 1, 0,
47 0, 0, 0, 1
48};
49// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070050
Marissa Wall947d34e2019-03-29 14:03:53 -070051BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
52 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080053 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080054 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
chaviwb4c6e582019-08-16 14:35:07 -070055 if (const auto display = args.displayDevice) {
56 updateTransformHint(display);
57 }
Vishnu Nair60356342018-11-13 13:00:45 -080058}
Marissa Wall61c58622018-07-18 10:12:20 -070059
Alec Mouri4545a8a2019-08-08 20:05:32 -070060BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070061 // The original layer and the clone layer share the same texture and buffer. Therefore, only
62 // one of the layers, in this case the original layer, needs to handle the deletion. The
63 // original layer and the clone should be removed at the same time so there shouldn't be any
64 // issue with the clone layer trying to use the texture.
65 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070066 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070067 // RenderEngine may have been using the buffer as an external texture
68 // after the client uncached the buffer.
69 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070070 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070071 }
72}
73
Marissa Wall61c58622018-07-18 10:12:20 -070074// -----------------------------------------------------------------------
75// Interface implementation for Layer
76// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070077void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080078 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
79 // buffer that was presented on this layer. The first transaction that came in this frame that
80 // replaced the previous buffer on this layer needs this release fence, because the fence will
81 // let the client know when that previous buffer is removed from the screen.
82 //
83 // Every other transaction on this layer does not need a release fence because no other
84 // Transactions that were set on this layer this frame are going to have their preceeding buffer
85 // removed from the display this frame.
86 //
87 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
88 // buffer so it doesn't need a previous release fence because the layer still needs the previous
89 // buffer. The second transaction contains a buffer so it needs a previous release fence because
90 // the previous buffer will be released this frame. The third transaction also contains a
91 // buffer. It replaces the buffer in the second transaction. The buffer in the second
92 // transaction will now no longer be presented so it is released immediately and the third
93 // transaction doesn't need a previous release fence.
94 for (auto& handle : mDrawingState.callbackHandles) {
95 if (handle->releasePreviousBuffer) {
96 handle->previousReleaseFence = releaseFence;
97 break;
98 }
99 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700100
101 // Prevent tracing the same release multiple times.
102 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -0700103 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
104 std::make_shared<FenceTime>(releaseFence),
105 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700106 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
107 }
Marissa Wall61c58622018-07-18 10:12:20 -0700108}
109
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700110void BufferStateLayer::setTransformHint(uint32_t orientation) const {
111 mTransformHint = orientation;
Marissa Wall61c58622018-07-18 10:12:20 -0700112}
113
114void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700115 for (const auto& handle : mDrawingState.callbackHandles) {
116 handle->transformHint = mTransformHint;
117 }
118
Marissa Wallefb71af2019-06-27 14:45:53 -0700119 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800120 mDrawingState.callbackHandles);
121
122 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700123}
124
Ana Krulec010d2192018-10-08 06:29:54 -0700125bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700126 if (getSidebandStreamChanged() || getAutoRefresh()) {
127 return true;
128 }
129
Marissa Wall024a1912018-08-13 13:55:35 -0700130 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700131}
132
Marissa Walle2ffb422018-10-12 11:33:52 -0700133bool BufferStateLayer::willPresentCurrentTransaction() const {
134 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700135 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800136 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700137 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
138 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700139}
140
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141void BufferStateLayer::pushPendingState() {
142 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700143 return;
144 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700146 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700147}
148
149bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 const bool stateUpdateAvailable = !mPendingStates.empty();
151 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700152 popPendingState(stateToCommit);
153 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800154 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
155 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700156 return stateUpdateAvailable;
157}
158
Marissa Wall861616d2018-10-22 12:52:23 -0700159// Crop that applies to the window
160Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
161 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700162}
163
164bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800165 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800166 mCurrentState.transform = transform;
167 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700168 setTransactionFlags(eTransactionNeeded);
169 return true;
170}
171
172bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800173 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
174 mCurrentState.sequence++;
175 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
176 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700177 setTransactionFlags(eTransactionNeeded);
178 return true;
179}
180
181bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800182 Rect c = crop;
183 if (c.left < 0) {
184 c.left = 0;
185 }
186 if (c.top < 0) {
187 c.top = 0;
188 }
189 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
190 // treats all invalid rectangles the same.
191 if (!c.isValid()) {
192 c.makeInvalid();
193 }
194
195 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800196 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800197 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700198 setTransactionFlags(eTransactionNeeded);
199 return true;
200}
201
Marissa Wall861616d2018-10-22 12:52:23 -0700202bool BufferStateLayer::setFrame(const Rect& frame) {
203 int x = frame.left;
204 int y = frame.top;
205 int w = frame.getWidth();
206 int h = frame.getHeight();
207
Marissa Wall0f3242d2018-12-20 15:10:22 -0800208 if (x < 0) {
209 x = 0;
210 w = frame.right;
211 }
212
213 if (y < 0) {
214 y = 0;
215 h = frame.bottom;
216 }
217
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800218 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
219 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700220 return false;
221 }
222
223 if (!frame.isValid()) {
224 x = y = w = h = 0;
225 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226 mCurrentState.active.transform.set(x, y);
227 mCurrentState.active.w = w;
228 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700229
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800230 mCurrentState.sequence++;
231 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700232 setTransactionFlags(eTransactionNeeded);
233 return true;
234}
235
Ady Abraham09bd3922019-04-08 10:44:56 -0700236bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700237 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800238 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700239 mReleasePreviousBuffer = true;
240 }
241
Valerie Hau134651a2020-01-28 16:21:22 -0800242 mCurrentState.frameNumber++;
Valerie Hau2f54d642020-01-22 09:37:03 -0800243
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800244 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700245 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800246 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700247 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700248
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800249 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800250 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
251 postTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800252 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
Valerie Hau134651a2020-01-28 16:21:22 -0800253 mFlinger->mFrameTracer->traceTimestamp(layerId, buffer->getId(), mCurrentState.frameNumber,
254 postTime, FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700255 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700256
Ady Abraham8a82ba62020-01-17 12:43:17 -0800257 mFlinger->mScheduler->recordLayerHistory(this,
258 desiredPresentTime <= 0 ? 0 : desiredPresentTime);
Ady Abraham09bd3922019-04-08 10:44:56 -0700259
Marissa Wall61c58622018-07-18 10:12:20 -0700260 return true;
261}
262
263bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700264 // The acquire fences of BufferStateLayers have already signaled before they are set
265 mCallbackHandleAcquireTime = fence->getSignalTime();
266
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 mCurrentState.acquireFence = fence;
268 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700269 setTransactionFlags(eTransactionNeeded);
270 return true;
271}
272
273bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 mCurrentState.dataspace = dataspace;
276 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700277 setTransactionFlags(eTransactionNeeded);
278 return true;
279}
280
281bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800282 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 mCurrentState.hdrMetadata = hdrMetadata;
284 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700285 setTransactionFlags(eTransactionNeeded);
286 return true;
287}
288
289bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800290 mCurrentState.surfaceDamageRegion = surfaceDamage;
291 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700292 setTransactionFlags(eTransactionNeeded);
293 return true;
294}
295
296bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800297 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 mCurrentState.api = api;
299 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700300 setTransactionFlags(eTransactionNeeded);
301 return true;
302}
303
304bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800305 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800306 mCurrentState.sidebandStream = sidebandStream;
307 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700308 setTransactionFlags(eTransactionNeeded);
309
310 if (!mSidebandStreamChanged.exchange(true)) {
311 // mSidebandStreamChanged was false
312 mFlinger->signalLayerUpdate();
313 }
314 return true;
315}
316
Marissa Walle2ffb422018-10-12 11:33:52 -0700317bool BufferStateLayer::setTransactionCompletedListeners(
318 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700319 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700320 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700321 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700322 return false;
323 }
324
325 const bool willPresent = willPresentCurrentTransaction();
326
327 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700328 // If this transaction set a buffer on this layer, release its previous buffer
329 handle->releasePreviousBuffer = mReleasePreviousBuffer;
330
Marissa Walle2ffb422018-10-12 11:33:52 -0700331 // If this layer will be presented in this frame
332 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700333 // If this transaction set an acquire fence on this layer, set its acquire time
334 handle->acquireTime = mCallbackHandleAcquireTime;
335
Marissa Walle2ffb422018-10-12 11:33:52 -0700336 // Notify the transaction completed thread that there is a pending latched callback
337 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800338 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700339
340 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800341 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700342
343 } else { // If this layer will NOT need to be relatched and presented this frame
344 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700345 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700346 }
347 }
348
Marissa Wallfda30bb2018-10-12 11:34:28 -0700349 mReleasePreviousBuffer = false;
350 mCallbackHandleAcquireTime = -1;
351
Marissa Walle2ffb422018-10-12 11:33:52 -0700352 return willPresent;
353}
354
Valerie Hau7618b232020-01-09 16:03:08 -0800355void BufferStateLayer::forceSendCallbacks() {
356 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
357 mCurrentState.callbackHandles);
358}
359
Marissa Wall61c58622018-07-18 10:12:20 -0700360bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800361 mCurrentState.transparentRegionHint = transparent;
362 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700363 setTransactionFlags(eTransactionNeeded);
364 return true;
365}
366
Marissa Wall861616d2018-10-22 12:52:23 -0700367Rect BufferStateLayer::getBufferSize(const State& s) const {
368 // for buffer state layers we use the display frame size as the buffer size.
369 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
370 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700371 }
372
Marissa Wall861616d2018-10-22 12:52:23 -0700373 // if the display frame is not defined, use the parent bounds as the buffer size.
374 const auto& p = mDrawingParent.promote();
375 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800376 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700377 if (!parentBounds.isEmpty()) {
378 return parentBounds;
379 }
380 }
381
Marissa Wall861616d2018-10-22 12:52:23 -0700382 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700383}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800384
385FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
386 const State& s(getDrawingState());
387 // for buffer state layers we use the display frame size as the buffer size.
388 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
389 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
390 }
391
392 // if the display frame is not defined, use the parent bounds as the buffer size.
393 return parentBounds;
394}
395
Marissa Wall61c58622018-07-18 10:12:20 -0700396// -----------------------------------------------------------------------
397
398// -----------------------------------------------------------------------
399// Interface implementation for BufferLayer
400// -----------------------------------------------------------------------
401bool BufferStateLayer::fenceHasSignaled() const {
402 if (latchUnsignaledBuffers()) {
403 return true;
404 }
405
Alec Mouri91f6df32020-01-30 08:48:58 -0800406 const bool fenceSignaled =
407 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
408 if (!fenceSignaled) {
409 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
410 TimeStats::LatchSkipReason::LateAcquire);
411 }
412
413 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700414}
415
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700416bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700417 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
418 return true;
419 }
420
chaviwfa67b552019-08-12 16:51:55 -0700421 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700422}
423
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700424uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800425 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700426}
427
428bool BufferStateLayer::getAutoRefresh() const {
429 // TODO(marissaw): support shared buffer mode
430 return false;
431}
432
433bool BufferStateLayer::getSidebandStreamChanged() const {
434 return mSidebandStreamChanged.load();
435}
436
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800437bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700438 if (mSidebandStreamChanged.exchange(false)) {
439 const State& s(getDrawingState());
440 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800441 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
442 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700443 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800444 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700445 setTransactionFlags(eTransactionNeeded);
446 mFlinger->setTransactionFlags(eTraversalNeeded);
447 }
448 recomputeVisibleRegions = true;
449
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800450 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700451 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800452 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700453}
454
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800455bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800456 const State& c(getCurrentState());
457 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700458}
459
Alec Mouri39801c02018-10-10 10:44:47 -0700460status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700461 const State& s(getDrawingState());
462 auto& engine(mFlinger->getRenderEngine());
463
Alec Mourib5c4f352019-02-19 19:46:38 -0800464 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700465}
466
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700467status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
468 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700469 const State& s(getDrawingState());
470
471 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800472 if (s.bgColorLayer) {
473 for (auto& handle : mDrawingState.callbackHandles) {
474 handle->latchTime = latchTime;
475 }
476 }
Marissa Wall61c58622018-07-18 10:12:20 -0700477 return NO_ERROR;
478 }
479
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800480 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700481
Marissa Wall61c58622018-07-18 10:12:20 -0700482 // Reject if the layer is invalid
483 uint32_t bufferWidth = s.buffer->width;
484 uint32_t bufferHeight = s.buffer->height;
485
Peiyong Linefefaac2018-08-17 12:27:51 -0700486 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700487 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700488 }
489
490 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800491 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700492 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700493 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700494 }
495 }
496
Vishnu Nair60356342018-11-13 13:00:45 -0800497 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700498 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
499 ALOGE("[%s] rejecting buffer: "
500 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700501 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800502 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700503 return BAD_VALUE;
504 }
505
Marissa Wall5a68a772018-12-22 17:43:42 -0800506 for (auto& handle : mDrawingState.callbackHandles) {
507 handle->latchTime = latchTime;
508 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700509
Alec Mouri56e538f2019-01-14 15:22:01 -0800510 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700511 // Bind the new buffer to the GL texture.
512 //
513 // Older devices require the "implicit" synchronization provided
514 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
515 // devices will either call this in Layer::onDraw, or (if it's not
516 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800517 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700518 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800519 mFlinger->mTimeStats->onDestroy(layerId);
520 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700521 return BAD_VALUE;
522 }
523 }
524
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700525 const uint64_t bufferID = getCurrentBufferId();
Valerie Hau134651a2020-01-28 16:21:22 -0800526 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
527 mBufferInfo.mFenceTime);
528 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mDrawingState.frameNumber,
529 mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700530 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Valerie Hau134651a2020-01-28 16:21:22 -0800531 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
532 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mDrawingState.frameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700533 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700534
Marissa Wall16c112d2019-03-20 13:21:13 -0700535 mCurrentStateModified = false;
536
Marissa Wall61c58622018-07-18 10:12:20 -0700537 return NO_ERROR;
538}
539
540status_t BufferStateLayer::updateActiveBuffer() {
541 const State& s(getDrawingState());
542
543 if (s.buffer == nullptr) {
544 return BAD_VALUE;
545 }
546
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700547 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700548 mBufferInfo.mBuffer = s.buffer;
549 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700550 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700551 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700552
553 return NO_ERROR;
554}
555
556status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
557 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700558 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800559 mCurrentFrameNumber = mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700560 return NO_ERROR;
561}
562
Lloyd Piquef5275482019-01-29 18:42:42 -0800563void BufferStateLayer::latchPerFrameState(
564 compositionengine::LayerFECompositionState& compositionState) const {
565 BufferLayer::latchPerFrameState(compositionState);
566 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
567 return;
568 }
Marissa Wall61c58622018-07-18 10:12:20 -0700569
chaviwf83ce182019-09-12 14:43:08 -0700570 compositionState.buffer = mBufferInfo.mBuffer;
571 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700572 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700573}
574
Marissa Wall947d34e2019-03-29 14:03:53 -0700575void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
576 std::lock_guard lock(mMutex);
577 if (!clientCacheId.isValid()) {
578 ALOGE("invalid process, failed to erase buffer");
579 return;
580 }
581 eraseBufferLocked(clientCacheId);
582}
583
584uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
585 std::lock_guard<std::mutex> lock(mMutex);
586 auto itr = mCachedBuffers.find(clientCacheId);
587 if (itr == mCachedBuffers.end()) {
588 return addCachedBuffer(clientCacheId);
589 }
590 auto& [hwcCacheSlot, counter] = itr->second;
591 counter = mCounter++;
592 return hwcCacheSlot;
593}
594
595uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
596 REQUIRES(mMutex) {
597 if (!clientCacheId.isValid()) {
598 ALOGE("invalid process, returning invalid slot");
599 return BufferQueue::INVALID_BUFFER_SLOT;
600 }
601
602 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
603
604 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
605 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
606 return hwcCacheSlot;
607}
608
609uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
610 if (mFreeHwcCacheSlots.empty()) {
611 evictLeastRecentlyUsed();
612 }
613
614 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
615 mFreeHwcCacheSlots.pop();
616 return hwcCacheSlot;
617}
618
619void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
620 uint64_t minCounter = UINT_MAX;
621 client_cache_t minClientCacheId = {};
622 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
623 const auto& [hwcCacheSlot, counter] = slotCounter;
624 if (counter < minCounter) {
625 minCounter = counter;
626 minClientCacheId = clientCacheId;
627 }
628 }
629 eraseBufferLocked(minClientCacheId);
630
631 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
632}
633
634void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
635 REQUIRES(mMutex) {
636 auto itr = mCachedBuffers.find(clientCacheId);
637 if (itr == mCachedBuffers.end()) {
638 return;
639 }
640 auto& [hwcCacheSlot, counter] = itr->second;
641
642 // TODO send to hwc cache and resources
643
644 mFreeHwcCacheSlots.push(hwcCacheSlot);
645 mCachedBuffers.erase(clientCacheId);
646}
chaviw4244e032019-09-04 11:27:49 -0700647
648void BufferStateLayer::gatherBufferInfo() {
649 const State& s(getDrawingState());
650
651 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
652 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
653 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700654 mBufferInfo.mTransform = s.transform;
655 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
656 mBufferInfo.mCrop = computeCrop(s);
657 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
658 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
659 mBufferInfo.mHdrMetadata = s.hdrMetadata;
660 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700661 mBufferInfo.mPixelFormat =
662 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700663 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700664 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700665}
666
667Rect BufferStateLayer::computeCrop(const State& s) {
668 if (s.crop.isEmpty() && s.buffer) {
669 return s.buffer->getBounds();
670 } else if (s.buffer) {
671 Rect crop = s.crop;
672 crop.left = std::max(crop.left, 0);
673 crop.top = std::max(crop.top, 0);
674 uint32_t bufferWidth = s.buffer->getWidth();
675 uint32_t bufferHeight = s.buffer->getHeight();
676 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
677 bufferWidth <= std::numeric_limits<int32_t>::max()) {
678 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
679 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
680 }
681 if (!crop.isValid()) {
682 // Crop rect is out of bounds, return whole buffer
683 return s.buffer->getBounds();
684 }
685 return crop;
686 }
687 return s.crop;
688}
689
chaviwb4c6e582019-08-16 14:35:07 -0700690sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700691 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700692 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700693 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700694 layer->mHwcSlotGenerator = mHwcSlotGenerator;
695 layer->setInitialValuesForClone(this);
696 return layer;
697}
Marissa Wall61c58622018-07-18 10:12:20 -0700698} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800699
700// TODO(b/129481165): remove the #pragma below and fix conversion issues
701#pragma clang diagnostic pop // ignored "-Wconversion"