blob: ad05bc8671d346bf6b20f68a8704797ff6101352 [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;
Vishnu Nair60356342018-11-13 13:00:45 -080051}
Marissa Wall61c58622018-07-18 10:12:20 -070052
Alec Mouri4545a8a2019-08-08 20:05:32 -070053BufferStateLayer::~BufferStateLayer() {
chaviwd62d3062019-09-04 14:48:02 -070054 if (mBufferInfo.mBuffer != nullptr) {
55 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070056 // RenderEngine may have been using the buffer as an external texture
57 // after the client uncached the buffer.
58 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070059 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070060 }
61}
62
Marissa Wall61c58622018-07-18 10:12:20 -070063// -----------------------------------------------------------------------
64// Interface implementation for Layer
65// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070066void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080067 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
68 // buffer that was presented on this layer. The first transaction that came in this frame that
69 // replaced the previous buffer on this layer needs this release fence, because the fence will
70 // let the client know when that previous buffer is removed from the screen.
71 //
72 // Every other transaction on this layer does not need a release fence because no other
73 // Transactions that were set on this layer this frame are going to have their preceeding buffer
74 // removed from the display this frame.
75 //
76 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
77 // buffer so it doesn't need a previous release fence because the layer still needs the previous
78 // buffer. The second transaction contains a buffer so it needs a previous release fence because
79 // the previous buffer will be released this frame. The third transaction also contains a
80 // buffer. It replaces the buffer in the second transaction. The buffer in the second
81 // transaction will now no longer be presented so it is released immediately and the third
82 // transaction doesn't need a previous release fence.
83 for (auto& handle : mDrawingState.callbackHandles) {
84 if (handle->releasePreviousBuffer) {
85 handle->previousReleaseFence = releaseFence;
86 break;
87 }
88 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070089
90 // Prevent tracing the same release multiple times.
91 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -070092 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
93 std::make_shared<FenceTime>(releaseFence),
94 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -070095 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
96 }
Marissa Wall61c58622018-07-18 10:12:20 -070097}
98
99void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
100 // TODO(marissaw): send the transform hint to buffer owner
101 return;
102}
103
104void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700105 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800106 mDrawingState.callbackHandles);
107
108 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700109}
110
Ana Krulec010d2192018-10-08 06:29:54 -0700111bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700112 if (getSidebandStreamChanged() || getAutoRefresh()) {
113 return true;
114 }
115
Marissa Wall024a1912018-08-13 13:55:35 -0700116 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700117}
118
Marissa Walle2ffb422018-10-12 11:33:52 -0700119bool BufferStateLayer::willPresentCurrentTransaction() const {
120 // Returns true if the most recent Transaction applied to CurrentState will be presented.
121 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800122 (mCurrentState.modified &&
123 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700124}
125
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126void BufferStateLayer::pushPendingState() {
127 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700128 return;
129 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 mPendingStates.push_back(mCurrentState);
131 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700132}
133
134bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800135 const bool stateUpdateAvailable = !mPendingStates.empty();
136 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700137 popPendingState(stateToCommit);
138 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800139 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
140 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700141 return stateUpdateAvailable;
142}
143
Marissa Wall861616d2018-10-22 12:52:23 -0700144// Crop that applies to the window
145Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
146 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700147}
148
149bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151 mCurrentState.transform = transform;
152 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700153 setTransactionFlags(eTransactionNeeded);
154 return true;
155}
156
157bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800158 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
159 mCurrentState.sequence++;
160 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
161 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700162 setTransactionFlags(eTransactionNeeded);
163 return true;
164}
165
166bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800167 Rect c = crop;
168 if (c.left < 0) {
169 c.left = 0;
170 }
171 if (c.top < 0) {
172 c.top = 0;
173 }
174 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
175 // treats all invalid rectangles the same.
176 if (!c.isValid()) {
177 c.makeInvalid();
178 }
179
180 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800181 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800182 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700183 setTransactionFlags(eTransactionNeeded);
184 return true;
185}
186
Marissa Wall861616d2018-10-22 12:52:23 -0700187bool BufferStateLayer::setFrame(const Rect& frame) {
188 int x = frame.left;
189 int y = frame.top;
190 int w = frame.getWidth();
191 int h = frame.getHeight();
192
Marissa Wall0f3242d2018-12-20 15:10:22 -0800193 if (x < 0) {
194 x = 0;
195 w = frame.right;
196 }
197
198 if (y < 0) {
199 y = 0;
200 h = frame.bottom;
201 }
202
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800203 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
204 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700205 return false;
206 }
207
208 if (!frame.isValid()) {
209 x = y = w = h = 0;
210 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800211 mCurrentState.active.transform.set(x, y);
212 mCurrentState.active.w = w;
213 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700214
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800215 mCurrentState.sequence++;
216 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700217 setTransactionFlags(eTransactionNeeded);
218 return true;
219}
220
Ady Abraham09bd3922019-04-08 10:44:56 -0700221bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700222 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800223 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700224 mReleasePreviousBuffer = true;
225 }
226
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800227 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700228 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800229 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700230 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700231
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700232 const int32_t layerID = getSequence();
233 mFlinger->mTimeStats->setPostTime(layerID, mFrameNumber, getName().c_str(), postTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700234 mFlinger->mFrameTracer->traceNewLayer(layerID, getName().c_str());
235 mFlinger->mFrameTracer->traceTimestamp(layerID, buffer->getId(), mFrameNumber, postTime,
236 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700237 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700238
239 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700240 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700241 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
242 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700243 }
244
Marissa Wall61c58622018-07-18 10:12:20 -0700245 return true;
246}
247
248bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700249 // The acquire fences of BufferStateLayers have already signaled before they are set
250 mCallbackHandleAcquireTime = fence->getSignalTime();
251
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800252 mCurrentState.acquireFence = fence;
253 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700254 setTransactionFlags(eTransactionNeeded);
255 return true;
256}
257
258bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800259 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800260 mCurrentState.dataspace = dataspace;
261 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700262 setTransactionFlags(eTransactionNeeded);
263 return true;
264}
265
266bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 mCurrentState.hdrMetadata = hdrMetadata;
269 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700270 setTransactionFlags(eTransactionNeeded);
271 return true;
272}
273
274bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 mCurrentState.surfaceDamageRegion = surfaceDamage;
276 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700277 setTransactionFlags(eTransactionNeeded);
278 return true;
279}
280
281bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800282 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 mCurrentState.api = api;
284 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700285 setTransactionFlags(eTransactionNeeded);
286 return true;
287}
288
289bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800290 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.sidebandStream = sidebandStream;
292 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700293 setTransactionFlags(eTransactionNeeded);
294
295 if (!mSidebandStreamChanged.exchange(true)) {
296 // mSidebandStreamChanged was false
297 mFlinger->signalLayerUpdate();
298 }
299 return true;
300}
301
Marissa Walle2ffb422018-10-12 11:33:52 -0700302bool BufferStateLayer::setTransactionCompletedListeners(
303 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700304 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700305 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700306 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700307 return false;
308 }
309
310 const bool willPresent = willPresentCurrentTransaction();
311
312 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700313 // If this transaction set a buffer on this layer, release its previous buffer
314 handle->releasePreviousBuffer = mReleasePreviousBuffer;
315
Marissa Walle2ffb422018-10-12 11:33:52 -0700316 // If this layer will be presented in this frame
317 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700318 // If this transaction set an acquire fence on this layer, set its acquire time
319 handle->acquireTime = mCallbackHandleAcquireTime;
320
Marissa Walle2ffb422018-10-12 11:33:52 -0700321 // Notify the transaction completed thread that there is a pending latched callback
322 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800323 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700324
325 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800326 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700327
328 } else { // If this layer will NOT need to be relatched and presented this frame
329 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700330 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700331 }
332 }
333
Marissa Wallfda30bb2018-10-12 11:34:28 -0700334 mReleasePreviousBuffer = false;
335 mCallbackHandleAcquireTime = -1;
336
Marissa Walle2ffb422018-10-12 11:33:52 -0700337 return willPresent;
338}
339
Marissa Wall61c58622018-07-18 10:12:20 -0700340bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800341 mCurrentState.transparentRegionHint = transparent;
342 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700343 setTransactionFlags(eTransactionNeeded);
344 return true;
345}
346
Marissa Wall861616d2018-10-22 12:52:23 -0700347Rect BufferStateLayer::getBufferSize(const State& s) const {
348 // for buffer state layers we use the display frame size as the buffer size.
349 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
350 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700351 }
352
Marissa Wall861616d2018-10-22 12:52:23 -0700353 // if the display frame is not defined, use the parent bounds as the buffer size.
354 const auto& p = mDrawingParent.promote();
355 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700357 if (!parentBounds.isEmpty()) {
358 return parentBounds;
359 }
360 }
361
Marissa Wall861616d2018-10-22 12:52:23 -0700362 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700363}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800364
365FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
366 const State& s(getDrawingState());
367 // for buffer state layers we use the display frame size as the buffer size.
368 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
369 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
370 }
371
372 // if the display frame is not defined, use the parent bounds as the buffer size.
373 return parentBounds;
374}
375
Marissa Wall61c58622018-07-18 10:12:20 -0700376// -----------------------------------------------------------------------
377
378// -----------------------------------------------------------------------
379// Interface implementation for BufferLayer
380// -----------------------------------------------------------------------
381bool BufferStateLayer::fenceHasSignaled() const {
382 if (latchUnsignaledBuffers()) {
383 return true;
384 }
385
386 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
387}
388
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700389bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700390 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
391 return true;
392 }
393
chaviwfa67b552019-08-12 16:51:55 -0700394 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700395}
396
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700397uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700398 return mFrameNumber;
399}
400
401bool BufferStateLayer::getAutoRefresh() const {
402 // TODO(marissaw): support shared buffer mode
403 return false;
404}
405
406bool BufferStateLayer::getSidebandStreamChanged() const {
407 return mSidebandStreamChanged.load();
408}
409
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800410bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700411 if (mSidebandStreamChanged.exchange(false)) {
412 const State& s(getDrawingState());
413 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800414 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
415 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700416 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800417 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700418 setTransactionFlags(eTransactionNeeded);
419 mFlinger->setTransactionFlags(eTraversalNeeded);
420 }
421 recomputeVisibleRegions = true;
422
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800423 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700424 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800425 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700426}
427
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800428bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800429 const State& c(getCurrentState());
430 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700431}
432
Alec Mouri39801c02018-10-10 10:44:47 -0700433status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700434 const State& s(getDrawingState());
435 auto& engine(mFlinger->getRenderEngine());
436
Alec Mourib5c4f352019-02-19 19:46:38 -0800437 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700440status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
441 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700442 const State& s(getDrawingState());
443
444 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800445 if (s.bgColorLayer) {
446 for (auto& handle : mDrawingState.callbackHandles) {
447 handle->latchTime = latchTime;
448 }
449 }
Marissa Wall61c58622018-07-18 10:12:20 -0700450 return NO_ERROR;
451 }
452
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700453 const int32_t layerID = getSequence();
454
Marissa Wall61c58622018-07-18 10:12:20 -0700455 // Reject if the layer is invalid
456 uint32_t bufferWidth = s.buffer->width;
457 uint32_t bufferHeight = s.buffer->height;
458
Peiyong Linefefaac2018-08-17 12:27:51 -0700459 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700460 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700461 }
462
463 if (s.transformToDisplayInverse) {
464 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700465 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700466 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700467 }
468 }
469
Vishnu Nair60356342018-11-13 13:00:45 -0800470 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700471 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
472 ALOGE("[%s] rejecting buffer: "
473 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
474 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700475 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700476 return BAD_VALUE;
477 }
478
Marissa Wall5a68a772018-12-22 17:43:42 -0800479 for (auto& handle : mDrawingState.callbackHandles) {
480 handle->latchTime = latchTime;
481 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700482
Alec Mouri56e538f2019-01-14 15:22:01 -0800483 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700484 // Bind the new buffer to the GL texture.
485 //
486 // Older devices require the "implicit" synchronization provided
487 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
488 // devices will either call this in Layer::onDraw, or (if it's not
489 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800490 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700491 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800492 mFlinger->mTimeStats->onDestroy(layerID);
Mikael Pessa90092f42019-08-26 17:22:04 -0700493 mFlinger->mFrameTracer->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700494 return BAD_VALUE;
495 }
496 }
497
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700498 const uint64_t bufferID = getCurrentBufferId();
chaviw4244e032019-09-04 11:27:49 -0700499 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, mBufferInfo.mFenceTime);
500 mFlinger->mFrameTracer->traceFence(layerID, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700501 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700502 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700503 mFlinger->mFrameTracer->traceTimestamp(layerID, bufferID, mFrameNumber, latchTime,
504 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700505
Marissa Wall16c112d2019-03-20 13:21:13 -0700506 mCurrentStateModified = false;
507
Marissa Wall61c58622018-07-18 10:12:20 -0700508 return NO_ERROR;
509}
510
511status_t BufferStateLayer::updateActiveBuffer() {
512 const State& s(getDrawingState());
513
514 if (s.buffer == nullptr) {
515 return BAD_VALUE;
516 }
517
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700518 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700519 mBufferInfo.mBuffer = s.buffer;
520 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700521 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700522 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700523
524 return NO_ERROR;
525}
526
527status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
528 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700529 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700530 mCurrentFrameNumber = mFrameNumber;
531 return NO_ERROR;
532}
533
Lloyd Piquef5275482019-01-29 18:42:42 -0800534void BufferStateLayer::latchPerFrameState(
535 compositionengine::LayerFECompositionState& compositionState) const {
536 BufferLayer::latchPerFrameState(compositionState);
537 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
538 return;
539 }
Marissa Wall61c58622018-07-18 10:12:20 -0700540
chaviwf83ce182019-09-12 14:43:08 -0700541 compositionState.buffer = mBufferInfo.mBuffer;
542 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700543 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700544
545 mFrameNumber++;
546}
547
548void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700549 BufferLayer::onFirstRef();
550
Marissa Wall61c58622018-07-18 10:12:20 -0700551 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
552 updateTransformHint(display);
553 }
554}
555
Marissa Wall947d34e2019-03-29 14:03:53 -0700556void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
557 std::lock_guard lock(mMutex);
558 if (!clientCacheId.isValid()) {
559 ALOGE("invalid process, failed to erase buffer");
560 return;
561 }
562 eraseBufferLocked(clientCacheId);
563}
564
565uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
566 std::lock_guard<std::mutex> lock(mMutex);
567 auto itr = mCachedBuffers.find(clientCacheId);
568 if (itr == mCachedBuffers.end()) {
569 return addCachedBuffer(clientCacheId);
570 }
571 auto& [hwcCacheSlot, counter] = itr->second;
572 counter = mCounter++;
573 return hwcCacheSlot;
574}
575
576uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
577 REQUIRES(mMutex) {
578 if (!clientCacheId.isValid()) {
579 ALOGE("invalid process, returning invalid slot");
580 return BufferQueue::INVALID_BUFFER_SLOT;
581 }
582
583 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
584
585 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
586 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
587 return hwcCacheSlot;
588}
589
590uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
591 if (mFreeHwcCacheSlots.empty()) {
592 evictLeastRecentlyUsed();
593 }
594
595 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
596 mFreeHwcCacheSlots.pop();
597 return hwcCacheSlot;
598}
599
600void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
601 uint64_t minCounter = UINT_MAX;
602 client_cache_t minClientCacheId = {};
603 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
604 const auto& [hwcCacheSlot, counter] = slotCounter;
605 if (counter < minCounter) {
606 minCounter = counter;
607 minClientCacheId = clientCacheId;
608 }
609 }
610 eraseBufferLocked(minClientCacheId);
611
612 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
613}
614
615void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
616 REQUIRES(mMutex) {
617 auto itr = mCachedBuffers.find(clientCacheId);
618 if (itr == mCachedBuffers.end()) {
619 return;
620 }
621 auto& [hwcCacheSlot, counter] = itr->second;
622
623 // TODO send to hwc cache and resources
624
625 mFreeHwcCacheSlots.push(hwcCacheSlot);
626 mCachedBuffers.erase(clientCacheId);
627}
chaviw4244e032019-09-04 11:27:49 -0700628
629void BufferStateLayer::gatherBufferInfo() {
630 const State& s(getDrawingState());
631
632 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
633 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
634 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700635 mBufferInfo.mTransform = s.transform;
636 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
637 mBufferInfo.mCrop = computeCrop(s);
638 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
639 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
640 mBufferInfo.mHdrMetadata = s.hdrMetadata;
641 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700642 mBufferInfo.mPixelFormat =
643 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700644 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700645 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700646}
647
648Rect BufferStateLayer::computeCrop(const State& s) {
649 if (s.crop.isEmpty() && s.buffer) {
650 return s.buffer->getBounds();
651 } else if (s.buffer) {
652 Rect crop = s.crop;
653 crop.left = std::max(crop.left, 0);
654 crop.top = std::max(crop.top, 0);
655 uint32_t bufferWidth = s.buffer->getWidth();
656 uint32_t bufferHeight = s.buffer->getHeight();
657 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
658 bufferWidth <= std::numeric_limits<int32_t>::max()) {
659 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
660 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
661 }
662 if (!crop.isValid()) {
663 // Crop rect is out of bounds, return whole buffer
664 return s.buffer->getBounds();
665 }
666 return crop;
667 }
668 return s.crop;
669}
670
Marissa Wall61c58622018-07-18 10:12:20 -0700671} // namespace android