blob: 30fdbe14e8347f98a1454a65400b3f7be6c12068 [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
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BufferStateLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Lloyd Pique9755fb72019-03-26 14:44:40 -070022#include "BufferStateLayer.h"
23
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070025
Lloyd Pique0b785d82018-12-04 17:25:27 -080026#include <compositionengine/Layer.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070027#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070028#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070029#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070030#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070031
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include "ColorLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070033#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080035
Marissa Wall61c58622018-07-18 10:12:20 -070036namespace android {
37
Lloyd Pique42ab75e2018-09-12 20:46:03 -070038// clang-format off
39const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1
44};
45// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070046
Marissa Wall947d34e2019-03-29 14:03:53 -070047BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
48 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080049 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080050 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
chaviwb4c6e582019-08-16 14:35:07 -070051 if (const auto display = args.displayDevice) {
52 updateTransformHint(display);
53 }
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
97 // Prevent tracing the same release multiple times.
98 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -070099 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
100 std::make_shared<FenceTime>(releaseFence),
101 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700102 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
103 }
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
106void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
107 // TODO(marissaw): send the transform hint to buffer owner
108 return;
109}
110
111void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700112 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800113 mDrawingState.callbackHandles);
114
115 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700116}
117
Ana Krulec010d2192018-10-08 06:29:54 -0700118bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700119 if (getSidebandStreamChanged() || getAutoRefresh()) {
120 return true;
121 }
122
Marissa Wall024a1912018-08-13 13:55:35 -0700123 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700124}
125
Marissa Walle2ffb422018-10-12 11:33:52 -0700126bool BufferStateLayer::willPresentCurrentTransaction() const {
127 // Returns true if the most recent Transaction applied to CurrentState will be presented.
128 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800129 (mCurrentState.modified &&
130 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700131}
132
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800133void BufferStateLayer::pushPendingState() {
134 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700135 return;
136 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800137 mPendingStates.push_back(mCurrentState);
138 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700139}
140
141bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800142 const bool stateUpdateAvailable = !mPendingStates.empty();
143 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700144 popPendingState(stateToCommit);
145 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
147 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700148 return stateUpdateAvailable;
149}
150
Marissa Wall861616d2018-10-22 12:52:23 -0700151// Crop that applies to the window
152Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
153 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700154}
155
156bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800157 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800158 mCurrentState.transform = transform;
159 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700160 setTransactionFlags(eTransactionNeeded);
161 return true;
162}
163
164bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800165 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
166 mCurrentState.sequence++;
167 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
168 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700169 setTransactionFlags(eTransactionNeeded);
170 return true;
171}
172
173bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800174 Rect c = crop;
175 if (c.left < 0) {
176 c.left = 0;
177 }
178 if (c.top < 0) {
179 c.top = 0;
180 }
181 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
182 // treats all invalid rectangles the same.
183 if (!c.isValid()) {
184 c.makeInvalid();
185 }
186
187 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800188 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800189 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700190 setTransactionFlags(eTransactionNeeded);
191 return true;
192}
193
Marissa Wall861616d2018-10-22 12:52:23 -0700194bool BufferStateLayer::setFrame(const Rect& frame) {
195 int x = frame.left;
196 int y = frame.top;
197 int w = frame.getWidth();
198 int h = frame.getHeight();
199
Marissa Wall0f3242d2018-12-20 15:10:22 -0800200 if (x < 0) {
201 x = 0;
202 w = frame.right;
203 }
204
205 if (y < 0) {
206 y = 0;
207 h = frame.bottom;
208 }
209
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800210 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
211 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700212 return false;
213 }
214
215 if (!frame.isValid()) {
216 x = y = w = h = 0;
217 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800218 mCurrentState.active.transform.set(x, y);
219 mCurrentState.active.w = w;
220 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700221
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800222 mCurrentState.sequence++;
223 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700224 setTransactionFlags(eTransactionNeeded);
225 return true;
226}
227
Ady Abraham09bd3922019-04-08 10:44:56 -0700228bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700229 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800230 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700231 mReleasePreviousBuffer = true;
232 }
233
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800234 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700235 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800236 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700237 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700238
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700239 const int32_t layerID = getSequence();
240 mFlinger->mTimeStats->setPostTime(layerID, mFrameNumber, getName().c_str(), postTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700241 mFlinger->mFrameTracer->traceNewLayer(layerID, getName().c_str());
242 mFlinger->mFrameTracer->traceTimestamp(layerID, buffer->getId(), mFrameNumber, postTime,
243 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700244 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700245
246 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700247 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700248 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
249 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700250 }
251
Marissa Wall61c58622018-07-18 10:12:20 -0700252 return true;
253}
254
255bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700256 // The acquire fences of BufferStateLayers have already signaled before they are set
257 mCallbackHandleAcquireTime = fence->getSignalTime();
258
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800259 mCurrentState.acquireFence = fence;
260 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700261 setTransactionFlags(eTransactionNeeded);
262 return true;
263}
264
265bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800266 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 mCurrentState.dataspace = dataspace;
268 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700269 setTransactionFlags(eTransactionNeeded);
270 return true;
271}
272
273bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 mCurrentState.hdrMetadata = hdrMetadata;
276 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700277 setTransactionFlags(eTransactionNeeded);
278 return true;
279}
280
281bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800282 mCurrentState.surfaceDamageRegion = surfaceDamage;
283 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700284 setTransactionFlags(eTransactionNeeded);
285 return true;
286}
287
288bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800289 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800290 mCurrentState.api = api;
291 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700292 setTransactionFlags(eTransactionNeeded);
293 return true;
294}
295
296bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800297 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 mCurrentState.sidebandStream = sidebandStream;
299 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700300 setTransactionFlags(eTransactionNeeded);
301
302 if (!mSidebandStreamChanged.exchange(true)) {
303 // mSidebandStreamChanged was false
304 mFlinger->signalLayerUpdate();
305 }
306 return true;
307}
308
Marissa Walle2ffb422018-10-12 11:33:52 -0700309bool BufferStateLayer::setTransactionCompletedListeners(
310 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700311 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700312 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700313 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700314 return false;
315 }
316
317 const bool willPresent = willPresentCurrentTransaction();
318
319 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700320 // If this transaction set a buffer on this layer, release its previous buffer
321 handle->releasePreviousBuffer = mReleasePreviousBuffer;
322
Marissa Walle2ffb422018-10-12 11:33:52 -0700323 // If this layer will be presented in this frame
324 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700325 // If this transaction set an acquire fence on this layer, set its acquire time
326 handle->acquireTime = mCallbackHandleAcquireTime;
327
Marissa Walle2ffb422018-10-12 11:33:52 -0700328 // Notify the transaction completed thread that there is a pending latched callback
329 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800330 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700331
332 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800333 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700334
335 } else { // If this layer will NOT need to be relatched and presented this frame
336 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700337 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700338 }
339 }
340
Marissa Wallfda30bb2018-10-12 11:34:28 -0700341 mReleasePreviousBuffer = false;
342 mCallbackHandleAcquireTime = -1;
343
Marissa Walle2ffb422018-10-12 11:33:52 -0700344 return willPresent;
345}
346
Marissa Wall61c58622018-07-18 10:12:20 -0700347bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800348 mCurrentState.transparentRegionHint = transparent;
349 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700350 setTransactionFlags(eTransactionNeeded);
351 return true;
352}
353
Marissa Wall861616d2018-10-22 12:52:23 -0700354Rect BufferStateLayer::getBufferSize(const State& s) const {
355 // for buffer state layers we use the display frame size as the buffer size.
356 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
357 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700358 }
359
Marissa Wall861616d2018-10-22 12:52:23 -0700360 // if the display frame is not defined, use the parent bounds as the buffer size.
361 const auto& p = mDrawingParent.promote();
362 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800363 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700364 if (!parentBounds.isEmpty()) {
365 return parentBounds;
366 }
367 }
368
Marissa Wall861616d2018-10-22 12:52:23 -0700369 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700370}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800371
372FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
373 const State& s(getDrawingState());
374 // for buffer state layers we use the display frame size as the buffer size.
375 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
376 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
377 }
378
379 // if the display frame is not defined, use the parent bounds as the buffer size.
380 return parentBounds;
381}
382
Marissa Wall61c58622018-07-18 10:12:20 -0700383// -----------------------------------------------------------------------
384
385// -----------------------------------------------------------------------
386// Interface implementation for BufferLayer
387// -----------------------------------------------------------------------
388bool BufferStateLayer::fenceHasSignaled() const {
389 if (latchUnsignaledBuffers()) {
390 return true;
391 }
392
393 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
394}
395
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700396bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700397 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
398 return true;
399 }
400
chaviwfa67b552019-08-12 16:51:55 -0700401 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700402}
403
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700404uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700405 return mFrameNumber;
406}
407
408bool BufferStateLayer::getAutoRefresh() const {
409 // TODO(marissaw): support shared buffer mode
410 return false;
411}
412
413bool BufferStateLayer::getSidebandStreamChanged() const {
414 return mSidebandStreamChanged.load();
415}
416
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800417bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700418 if (mSidebandStreamChanged.exchange(false)) {
419 const State& s(getDrawingState());
420 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800421 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
422 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700423 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800424 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700425 setTransactionFlags(eTransactionNeeded);
426 mFlinger->setTransactionFlags(eTraversalNeeded);
427 }
428 recomputeVisibleRegions = true;
429
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800430 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700431 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800432 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700433}
434
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800435bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800436 const State& c(getCurrentState());
437 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
Alec Mouri39801c02018-10-10 10:44:47 -0700440status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const State& s(getDrawingState());
442 auto& engine(mFlinger->getRenderEngine());
443
Alec Mourib5c4f352019-02-19 19:46:38 -0800444 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700445}
446
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700447status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
448 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700449 const State& s(getDrawingState());
450
451 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800452 if (s.bgColorLayer) {
453 for (auto& handle : mDrawingState.callbackHandles) {
454 handle->latchTime = latchTime;
455 }
456 }
Marissa Wall61c58622018-07-18 10:12:20 -0700457 return NO_ERROR;
458 }
459
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700460 const int32_t layerID = getSequence();
461
Marissa Wall61c58622018-07-18 10:12:20 -0700462 // Reject if the layer is invalid
463 uint32_t bufferWidth = s.buffer->width;
464 uint32_t bufferHeight = s.buffer->height;
465
Peiyong Linefefaac2018-08-17 12:27:51 -0700466 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700467 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700468 }
469
470 if (s.transformToDisplayInverse) {
471 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700472 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700473 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700474 }
475 }
476
Vishnu Nair60356342018-11-13 13:00:45 -0800477 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700478 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
479 ALOGE("[%s] rejecting buffer: "
480 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
481 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700482 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700483 return BAD_VALUE;
484 }
485
Marissa Wall5a68a772018-12-22 17:43:42 -0800486 for (auto& handle : mDrawingState.callbackHandles) {
487 handle->latchTime = latchTime;
488 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700489
Alec Mouri56e538f2019-01-14 15:22:01 -0800490 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700491 // Bind the new buffer to the GL texture.
492 //
493 // Older devices require the "implicit" synchronization provided
494 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
495 // devices will either call this in Layer::onDraw, or (if it's not
496 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800497 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700498 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800499 mFlinger->mTimeStats->onDestroy(layerID);
Mikael Pessa90092f42019-08-26 17:22:04 -0700500 mFlinger->mFrameTracer->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700501 return BAD_VALUE;
502 }
503 }
504
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700505 const uint64_t bufferID = getCurrentBufferId();
chaviw4244e032019-09-04 11:27:49 -0700506 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, mBufferInfo.mFenceTime);
507 mFlinger->mFrameTracer->traceFence(layerID, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700508 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700509 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700510 mFlinger->mFrameTracer->traceTimestamp(layerID, bufferID, mFrameNumber, latchTime,
511 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700512
Marissa Wall16c112d2019-03-20 13:21:13 -0700513 mCurrentStateModified = false;
514
Marissa Wall61c58622018-07-18 10:12:20 -0700515 return NO_ERROR;
516}
517
518status_t BufferStateLayer::updateActiveBuffer() {
519 const State& s(getDrawingState());
520
521 if (s.buffer == nullptr) {
522 return BAD_VALUE;
523 }
524
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700525 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700526 mBufferInfo.mBuffer = s.buffer;
527 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700528 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700529 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700530
531 return NO_ERROR;
532}
533
534status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
535 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700536 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700537 mCurrentFrameNumber = mFrameNumber;
538 return NO_ERROR;
539}
540
Lloyd Piquef5275482019-01-29 18:42:42 -0800541void BufferStateLayer::latchPerFrameState(
542 compositionengine::LayerFECompositionState& compositionState) const {
543 BufferLayer::latchPerFrameState(compositionState);
544 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
545 return;
546 }
Marissa Wall61c58622018-07-18 10:12:20 -0700547
chaviwf83ce182019-09-12 14:43:08 -0700548 compositionState.buffer = mBufferInfo.mBuffer;
549 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700550 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700551
552 mFrameNumber++;
553}
554
Marissa Wall947d34e2019-03-29 14:03:53 -0700555void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
556 std::lock_guard lock(mMutex);
557 if (!clientCacheId.isValid()) {
558 ALOGE("invalid process, failed to erase buffer");
559 return;
560 }
561 eraseBufferLocked(clientCacheId);
562}
563
564uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
565 std::lock_guard<std::mutex> lock(mMutex);
566 auto itr = mCachedBuffers.find(clientCacheId);
567 if (itr == mCachedBuffers.end()) {
568 return addCachedBuffer(clientCacheId);
569 }
570 auto& [hwcCacheSlot, counter] = itr->second;
571 counter = mCounter++;
572 return hwcCacheSlot;
573}
574
575uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
576 REQUIRES(mMutex) {
577 if (!clientCacheId.isValid()) {
578 ALOGE("invalid process, returning invalid slot");
579 return BufferQueue::INVALID_BUFFER_SLOT;
580 }
581
582 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
583
584 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
585 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
586 return hwcCacheSlot;
587}
588
589uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
590 if (mFreeHwcCacheSlots.empty()) {
591 evictLeastRecentlyUsed();
592 }
593
594 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
595 mFreeHwcCacheSlots.pop();
596 return hwcCacheSlot;
597}
598
599void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
600 uint64_t minCounter = UINT_MAX;
601 client_cache_t minClientCacheId = {};
602 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
603 const auto& [hwcCacheSlot, counter] = slotCounter;
604 if (counter < minCounter) {
605 minCounter = counter;
606 minClientCacheId = clientCacheId;
607 }
608 }
609 eraseBufferLocked(minClientCacheId);
610
611 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
612}
613
614void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
615 REQUIRES(mMutex) {
616 auto itr = mCachedBuffers.find(clientCacheId);
617 if (itr == mCachedBuffers.end()) {
618 return;
619 }
620 auto& [hwcCacheSlot, counter] = itr->second;
621
622 // TODO send to hwc cache and resources
623
624 mFreeHwcCacheSlots.push(hwcCacheSlot);
625 mCachedBuffers.erase(clientCacheId);
626}
chaviw4244e032019-09-04 11:27:49 -0700627
628void BufferStateLayer::gatherBufferInfo() {
629 const State& s(getDrawingState());
630
631 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
632 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
633 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700634 mBufferInfo.mTransform = s.transform;
635 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
636 mBufferInfo.mCrop = computeCrop(s);
637 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
638 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
639 mBufferInfo.mHdrMetadata = s.hdrMetadata;
640 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700641 mBufferInfo.mPixelFormat =
642 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700643 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700644 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700645}
646
647Rect BufferStateLayer::computeCrop(const State& s) {
648 if (s.crop.isEmpty() && s.buffer) {
649 return s.buffer->getBounds();
650 } else if (s.buffer) {
651 Rect crop = s.crop;
652 crop.left = std::max(crop.left, 0);
653 crop.top = std::max(crop.top, 0);
654 uint32_t bufferWidth = s.buffer->getWidth();
655 uint32_t bufferHeight = s.buffer->getHeight();
656 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
657 bufferWidth <= std::numeric_limits<int32_t>::max()) {
658 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
659 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
660 }
661 if (!crop.isValid()) {
662 // Crop rect is out of bounds, return whole buffer
663 return s.buffer->getBounds();
664 }
665 return crop;
666 }
667 return s.crop;
668}
669
chaviwb4c6e582019-08-16 14:35:07 -0700670sp<Layer> BufferStateLayer::createClone() {
671 const String8 name = mName + " (Mirror)";
672 LayerCreationArgs args =
673 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, 0, LayerMetadata());
674 args.textureName = mTextureName;
675 sp<BufferStateLayer> layer = new BufferStateLayer(args);
676 layer->mHwcSlotGenerator = mHwcSlotGenerator;
677 layer->setInitialValuesForClone(this);
678 return layer;
679}
Marissa Wall61c58622018-07-18 10:12:20 -0700680} // namespace android