blob: 6ef4518a471226188660c27cbf36c2f5e453ab07 [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 Pique37c2c9b2018-12-04 17:25:10 -080022#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070023
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080025#include <compositionengine/Layer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/impl/OutputLayerCompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070029#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070030#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070032
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include "BufferStateLayer.h"
34#include "ColorLayer.h"
35#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080036
Marissa Wall61c58622018-07-18 10:12:20 -070037namespace android {
38
Lloyd Pique42ab75e2018-09-12 20:46:03 -070039// clang-format off
40const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
41 1, 0, 0, 0,
42 0, 1, 0, 0,
43 0, 0, 1, 0,
44 0, 0, 0, 1
45};
46// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070047
Marissa Wall947d34e2019-03-29 14:03:53 -070048BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
49 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080050 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080051 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080052}
Alec Mourib5c4f352019-02-19 19:46:38 -080053BufferStateLayer::~BufferStateLayer() {
54 if (mActiveBuffer != nullptr) {
55 auto& engine(mFlinger->getRenderEngine());
56 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
57 }
58}
Marissa Wall61c58622018-07-18 10:12:20 -070059
60// -----------------------------------------------------------------------
61// Interface implementation for Layer
62// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070063void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080064 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
65 // buffer that was presented on this layer. The first transaction that came in this frame that
66 // replaced the previous buffer on this layer needs this release fence, because the fence will
67 // let the client know when that previous buffer is removed from the screen.
68 //
69 // Every other transaction on this layer does not need a release fence because no other
70 // Transactions that were set on this layer this frame are going to have their preceeding buffer
71 // removed from the display this frame.
72 //
73 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
74 // buffer so it doesn't need a previous release fence because the layer still needs the previous
75 // buffer. The second transaction contains a buffer so it needs a previous release fence because
76 // the previous buffer will be released this frame. The third transaction also contains a
77 // buffer. It replaces the buffer in the second transaction. The buffer in the second
78 // transaction will now no longer be presented so it is released immediately and the third
79 // transaction doesn't need a previous release fence.
80 for (auto& handle : mDrawingState.callbackHandles) {
81 if (handle->releasePreviousBuffer) {
82 handle->previousReleaseFence = releaseFence;
83 break;
84 }
85 }
Marissa Wall61c58622018-07-18 10:12:20 -070086}
87
88void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
89 // TODO(marissaw): send the transform hint to buffer owner
90 return;
91}
92
93void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080094 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
95 mDrawingState.callbackHandles);
96
97 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070098}
99
Ana Krulec010d2192018-10-08 06:29:54 -0700100bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700101 if (getSidebandStreamChanged() || getAutoRefresh()) {
102 return true;
103 }
104
Marissa Wall024a1912018-08-13 13:55:35 -0700105 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
Marissa Walle2ffb422018-10-12 11:33:52 -0700108bool BufferStateLayer::willPresentCurrentTransaction() const {
109 // Returns true if the most recent Transaction applied to CurrentState will be presented.
110 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800111 (mCurrentState.modified &&
112 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700113}
114
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115bool BufferStateLayer::getTransformToDisplayInverse() const {
116 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700117}
118
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119void BufferStateLayer::pushPendingState() {
120 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700121 return;
122 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 mPendingStates.push_back(mCurrentState);
124 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700125}
126
127bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800128 const bool stateUpdateAvailable = !mPendingStates.empty();
129 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700130 popPendingState(stateToCommit);
131 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800132 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
133 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 return stateUpdateAvailable;
135}
136
Marissa Wall861616d2018-10-22 12:52:23 -0700137// Crop that applies to the window
138Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
139 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700140}
141
142bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 mCurrentState.transform = transform;
145 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700146 setTransactionFlags(eTransactionNeeded);
147 return true;
148}
149
150bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
152 mCurrentState.sequence++;
153 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
154 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700155 setTransactionFlags(eTransactionNeeded);
156 return true;
157}
158
159bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800160 Rect c = crop;
161 if (c.left < 0) {
162 c.left = 0;
163 }
164 if (c.top < 0) {
165 c.top = 0;
166 }
167 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
168 // treats all invalid rectangles the same.
169 if (!c.isValid()) {
170 c.makeInvalid();
171 }
172
173 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800174 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800175 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700176 setTransactionFlags(eTransactionNeeded);
177 return true;
178}
179
Marissa Wall861616d2018-10-22 12:52:23 -0700180bool BufferStateLayer::setFrame(const Rect& frame) {
181 int x = frame.left;
182 int y = frame.top;
183 int w = frame.getWidth();
184 int h = frame.getHeight();
185
Marissa Wall0f3242d2018-12-20 15:10:22 -0800186 if (x < 0) {
187 x = 0;
188 w = frame.right;
189 }
190
191 if (y < 0) {
192 y = 0;
193 h = frame.bottom;
194 }
195
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800196 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
197 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700198 return false;
199 }
200
201 if (!frame.isValid()) {
202 x = y = w = h = 0;
203 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800204 mCurrentState.active.transform.set(x, y);
205 mCurrentState.active.w = w;
206 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700207
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800208 mCurrentState.sequence++;
209 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700210 setTransactionFlags(eTransactionNeeded);
211 return true;
212}
213
Ady Abraham09bd3922019-04-08 10:44:56 -0700214bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700215 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800216 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700217 mReleasePreviousBuffer = true;
218 }
219
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800220 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700221 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800222 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700223 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700224
225 mFlinger->mTimeStats->setPostTime(getSequence(), getFrameNumber(), getName().c_str(), postTime);
226 mDesiredPresentTime = desiredPresentTime;
227
228 if (mFlinger->mUseSmart90ForVideo) {
229 const nsecs_t presentTime = (mDesiredPresentTime == -1) ? 0 : mDesiredPresentTime;
230 mFlinger->mScheduler->addLayerPresentTime(mSchedulerLayerHandle, presentTime);
231 }
232
Marissa Wall61c58622018-07-18 10:12:20 -0700233 return true;
234}
235
236bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700237 // The acquire fences of BufferStateLayers have already signaled before they are set
238 mCallbackHandleAcquireTime = fence->getSignalTime();
239
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 mCurrentState.acquireFence = fence;
241 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700242 setTransactionFlags(eTransactionNeeded);
243 return true;
244}
245
246bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800247 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800248 mCurrentState.dataspace = dataspace;
249 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700250 setTransactionFlags(eTransactionNeeded);
251 return true;
252}
253
254bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800255 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800256 mCurrentState.hdrMetadata = hdrMetadata;
257 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700258 setTransactionFlags(eTransactionNeeded);
259 return true;
260}
261
262bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800263 mCurrentState.surfaceDamageRegion = surfaceDamage;
264 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700265 setTransactionFlags(eTransactionNeeded);
266 return true;
267}
268
269bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800271 mCurrentState.api = api;
272 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700273 setTransactionFlags(eTransactionNeeded);
274 return true;
275}
276
277bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800278 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800279 mCurrentState.sidebandStream = sidebandStream;
280 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700281 setTransactionFlags(eTransactionNeeded);
282
283 if (!mSidebandStreamChanged.exchange(true)) {
284 // mSidebandStreamChanged was false
285 mFlinger->signalLayerUpdate();
286 }
287 return true;
288}
289
Marissa Walle2ffb422018-10-12 11:33:52 -0700290bool BufferStateLayer::setTransactionCompletedListeners(
291 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700292 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700293 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700294 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700295 return false;
296 }
297
298 const bool willPresent = willPresentCurrentTransaction();
299
300 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700301 // If this transaction set a buffer on this layer, release its previous buffer
302 handle->releasePreviousBuffer = mReleasePreviousBuffer;
303
Marissa Walle2ffb422018-10-12 11:33:52 -0700304 // If this layer will be presented in this frame
305 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700306 // If this transaction set an acquire fence on this layer, set its acquire time
307 handle->acquireTime = mCallbackHandleAcquireTime;
308
Marissa Walle2ffb422018-10-12 11:33:52 -0700309 // Notify the transaction completed thread that there is a pending latched callback
310 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800311 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700312
313 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800314 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700315
316 } else { // If this layer will NOT need to be relatched and presented this frame
317 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800318 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700319 }
320 }
321
Marissa Wallfda30bb2018-10-12 11:34:28 -0700322 mReleasePreviousBuffer = false;
323 mCallbackHandleAcquireTime = -1;
324
Marissa Walle2ffb422018-10-12 11:33:52 -0700325 return willPresent;
326}
327
Marissa Wall61c58622018-07-18 10:12:20 -0700328bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800329 mCurrentState.transparentRegionHint = transparent;
330 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700331 setTransactionFlags(eTransactionNeeded);
332 return true;
333}
334
Marissa Wall861616d2018-10-22 12:52:23 -0700335Rect BufferStateLayer::getBufferSize(const State& s) const {
336 // for buffer state layers we use the display frame size as the buffer size.
337 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
338 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700339 }
340
Marissa Wall861616d2018-10-22 12:52:23 -0700341 // if the display frame is not defined, use the parent bounds as the buffer size.
342 const auto& p = mDrawingParent.promote();
343 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700345 if (!parentBounds.isEmpty()) {
346 return parentBounds;
347 }
348 }
349
Marissa Wall861616d2018-10-22 12:52:23 -0700350 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700351}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800352
353FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
354 const State& s(getDrawingState());
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 FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
358 }
359
360 // if the display frame is not defined, use the parent bounds as the buffer size.
361 return parentBounds;
362}
363
Marissa Wall61c58622018-07-18 10:12:20 -0700364// -----------------------------------------------------------------------
365
366// -----------------------------------------------------------------------
367// Interface implementation for BufferLayer
368// -----------------------------------------------------------------------
369bool BufferStateLayer::fenceHasSignaled() const {
370 if (latchUnsignaledBuffers()) {
371 return true;
372 }
373
374 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
375}
376
377nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800378 return mDesiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700379}
380
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800381std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700382 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
383}
384
385void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
386 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
387}
388
389uint32_t BufferStateLayer::getDrawingTransform() const {
390 return getDrawingState().transform;
391}
392
393ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
394 return getDrawingState().dataspace;
395}
396
Marissa Wall861616d2018-10-22 12:52:23 -0700397// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700398Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700399 const State& s(getDrawingState());
400
401 if (s.crop.isEmpty() && s.buffer) {
402 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800403 } else if (s.buffer) {
404 Rect crop = s.crop;
405 crop.left = std::max(crop.left, 0);
406 crop.top = std::max(crop.top, 0);
407 uint32_t bufferWidth = s.buffer->getWidth();
408 uint32_t bufferHeight = s.buffer->getHeight();
409 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
410 bufferWidth <= std::numeric_limits<int32_t>::max()) {
411 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
412 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
413 }
414 if (!crop.isValid()) {
415 // Crop rect is out of bounds, return whole buffer
416 return s.buffer->getBounds();
417 }
418 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700419 }
420 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700421}
422
423uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700424 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700425}
426
427Region BufferStateLayer::getDrawingSurfaceDamage() const {
428 return getDrawingState().surfaceDamageRegion;
429}
430
431const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
432 return getDrawingState().hdrMetadata;
433}
434
435int BufferStateLayer::getDrawingApi() const {
436 return getDrawingState().api;
437}
438
439PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800440 if (!mActiveBuffer) {
441 return PIXEL_FORMAT_NONE;
442 }
Marissa Wall61c58622018-07-18 10:12:20 -0700443 return mActiveBuffer->format;
444}
445
446uint64_t BufferStateLayer::getFrameNumber() const {
447 return mFrameNumber;
448}
449
450bool BufferStateLayer::getAutoRefresh() const {
451 // TODO(marissaw): support shared buffer mode
452 return false;
453}
454
455bool BufferStateLayer::getSidebandStreamChanged() const {
456 return mSidebandStreamChanged.load();
457}
458
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800459bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700460 if (mSidebandStreamChanged.exchange(false)) {
461 const State& s(getDrawingState());
462 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800463 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
464 mSidebandStream = s.sidebandStream;
465 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
466 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700467 setTransactionFlags(eTransactionNeeded);
468 mFlinger->setTransactionFlags(eTraversalNeeded);
469 }
470 recomputeVisibleRegions = true;
471
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800472 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700473 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800474 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700475}
476
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800477bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800478 const State& c(getCurrentState());
479 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700480}
481
482void BufferStateLayer::setFilteringEnabled(bool enabled) {
483 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
484 mCurrentTransform, enabled);
485}
486
Alec Mouri39801c02018-10-10 10:44:47 -0700487status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700488 const State& s(getDrawingState());
489 auto& engine(mFlinger->getRenderEngine());
490
Alec Mourib5c4f352019-02-19 19:46:38 -0800491 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700492}
493
Alec Mouri56e538f2019-01-14 15:22:01 -0800494status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700495 const State& s(getDrawingState());
496
497 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800498 if (s.bgColorLayer) {
499 for (auto& handle : mDrawingState.callbackHandles) {
500 handle->latchTime = latchTime;
501 }
502 }
Marissa Wall61c58622018-07-18 10:12:20 -0700503 return NO_ERROR;
504 }
505
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700506 const int32_t layerID = getSequence();
507
Marissa Wall61c58622018-07-18 10:12:20 -0700508 // Reject if the layer is invalid
509 uint32_t bufferWidth = s.buffer->width;
510 uint32_t bufferHeight = s.buffer->height;
511
Peiyong Linefefaac2018-08-17 12:27:51 -0700512 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700513 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700514 }
515
516 if (s.transformToDisplayInverse) {
517 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700518 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700519 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700520 }
521 }
522
Vishnu Nair60356342018-11-13 13:00:45 -0800523 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700524 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
525 ALOGE("[%s] rejecting buffer: "
526 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
527 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800528 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700529 return BAD_VALUE;
530 }
531
Marissa Wall5a68a772018-12-22 17:43:42 -0800532 for (auto& handle : mDrawingState.callbackHandles) {
533 handle->latchTime = latchTime;
534 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700535
Alec Mouri56e538f2019-01-14 15:22:01 -0800536 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700537 // Bind the new buffer to the GL texture.
538 //
539 // Older devices require the "implicit" synchronization provided
540 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
541 // devices will either call this in Layer::onDraw, or (if it's not
542 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800543 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700544 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800545 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700546 return BAD_VALUE;
547 }
548 }
549
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800550 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800551 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700552
Marissa Wall16c112d2019-03-20 13:21:13 -0700553 mCurrentStateModified = false;
554
Marissa Wall61c58622018-07-18 10:12:20 -0700555 return NO_ERROR;
556}
557
558status_t BufferStateLayer::updateActiveBuffer() {
559 const State& s(getDrawingState());
560
561 if (s.buffer == nullptr) {
562 return BAD_VALUE;
563 }
564
Alec Mourib5c4f352019-02-19 19:46:38 -0800565 if (mActiveBuffer != nullptr) {
566 // todo: get this to work with BufferStateLayerCache
567 auto& engine(mFlinger->getRenderEngine());
568 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
569 }
Marissa Wall61c58622018-07-18 10:12:20 -0700570 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000571 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800572 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
573 layerCompositionState.buffer = mActiveBuffer;
574 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700575
576 return NO_ERROR;
577}
578
579status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
580 // TODO(marissaw): support frame history events
581 mCurrentFrameNumber = mFrameNumber;
582 return NO_ERROR;
583}
584
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800585void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
586 const auto outputLayer = findOutputLayerForDisplay(display);
587 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000588 auto& hwcInfo = *outputLayer->editState().hwc;
589 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700590
591 const State& s(getDrawingState());
592
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700593 uint32_t hwcSlot;
Valerie Hau6f89c372019-03-02 00:13:33 +0000594 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700595 hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId),
596 s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700597
Valerie Hau6f89c372019-03-02 00:13:33 +0000598 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700599 if (error != HWC2::Error::None) {
600 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
601 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
602 }
603
604 mFrameNumber++;
605}
606
607void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700608 BufferLayer::onFirstRef();
609
Marissa Wall61c58622018-07-18 10:12:20 -0700610 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
611 updateTransformHint(display);
612 }
613}
614
Marissa Wall947d34e2019-03-29 14:03:53 -0700615void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
616 std::lock_guard lock(mMutex);
617 if (!clientCacheId.isValid()) {
618 ALOGE("invalid process, failed to erase buffer");
619 return;
620 }
621 eraseBufferLocked(clientCacheId);
622}
623
624uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
625 std::lock_guard<std::mutex> lock(mMutex);
626 auto itr = mCachedBuffers.find(clientCacheId);
627 if (itr == mCachedBuffers.end()) {
628 return addCachedBuffer(clientCacheId);
629 }
630 auto& [hwcCacheSlot, counter] = itr->second;
631 counter = mCounter++;
632 return hwcCacheSlot;
633}
634
635uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
636 REQUIRES(mMutex) {
637 if (!clientCacheId.isValid()) {
638 ALOGE("invalid process, returning invalid slot");
639 return BufferQueue::INVALID_BUFFER_SLOT;
640 }
641
642 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
643
644 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
645 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
646 return hwcCacheSlot;
647}
648
649uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
650 if (mFreeHwcCacheSlots.empty()) {
651 evictLeastRecentlyUsed();
652 }
653
654 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
655 mFreeHwcCacheSlots.pop();
656 return hwcCacheSlot;
657}
658
659void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
660 uint64_t minCounter = UINT_MAX;
661 client_cache_t minClientCacheId = {};
662 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
663 const auto& [hwcCacheSlot, counter] = slotCounter;
664 if (counter < minCounter) {
665 minCounter = counter;
666 minClientCacheId = clientCacheId;
667 }
668 }
669 eraseBufferLocked(minClientCacheId);
670
671 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
672}
673
674void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
675 REQUIRES(mMutex) {
676 auto itr = mCachedBuffers.find(clientCacheId);
677 if (itr == mCachedBuffers.end()) {
678 return;
679 }
680 auto& [hwcCacheSlot, counter] = itr->second;
681
682 // TODO send to hwc cache and resources
683
684 mFreeHwcCacheSlots.push(hwcCacheSlot);
685 mCachedBuffers.erase(clientCacheId);
686}
Marissa Wall61c58622018-07-18 10:12:20 -0700687} // namespace android