blob: 30848d6eb5c568bf90cfa38b6be47534f36fbb81 [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
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700377bool BufferStateLayer::framePresentTimeIsCurrent() const {
378 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
379 return true;
380 }
381
382 return mDesiredPresentTime <= mFlinger->mScheduler->expectedPresentTime();
383}
384
Marissa Wall61c58622018-07-18 10:12:20 -0700385nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800386 return mDesiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700387}
388
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800389std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700390 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
391}
392
393void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
394 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
395}
396
397uint32_t BufferStateLayer::getDrawingTransform() const {
398 return getDrawingState().transform;
399}
400
401ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
402 return getDrawingState().dataspace;
403}
404
Marissa Wall861616d2018-10-22 12:52:23 -0700405// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700406Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700407 const State& s(getDrawingState());
408
409 if (s.crop.isEmpty() && s.buffer) {
410 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800411 } else if (s.buffer) {
412 Rect crop = s.crop;
413 crop.left = std::max(crop.left, 0);
414 crop.top = std::max(crop.top, 0);
415 uint32_t bufferWidth = s.buffer->getWidth();
416 uint32_t bufferHeight = s.buffer->getHeight();
417 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
418 bufferWidth <= std::numeric_limits<int32_t>::max()) {
419 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
420 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
421 }
422 if (!crop.isValid()) {
423 // Crop rect is out of bounds, return whole buffer
424 return s.buffer->getBounds();
425 }
426 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700427 }
428 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700429}
430
431uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700432 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700433}
434
435Region BufferStateLayer::getDrawingSurfaceDamage() const {
Marissa Wallae33e1a2019-04-18 13:21:25 -0700436 return Region::INVALID_REGION;
Marissa Wall61c58622018-07-18 10:12:20 -0700437}
438
439const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
440 return getDrawingState().hdrMetadata;
441}
442
443int BufferStateLayer::getDrawingApi() const {
444 return getDrawingState().api;
445}
446
447PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800448 if (!mActiveBuffer) {
449 return PIXEL_FORMAT_NONE;
450 }
Marissa Wall61c58622018-07-18 10:12:20 -0700451 return mActiveBuffer->format;
452}
453
454uint64_t BufferStateLayer::getFrameNumber() const {
455 return mFrameNumber;
456}
457
458bool BufferStateLayer::getAutoRefresh() const {
459 // TODO(marissaw): support shared buffer mode
460 return false;
461}
462
463bool BufferStateLayer::getSidebandStreamChanged() const {
464 return mSidebandStreamChanged.load();
465}
466
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800467bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700468 if (mSidebandStreamChanged.exchange(false)) {
469 const State& s(getDrawingState());
470 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800471 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
472 mSidebandStream = s.sidebandStream;
473 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
474 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700475 setTransactionFlags(eTransactionNeeded);
476 mFlinger->setTransactionFlags(eTraversalNeeded);
477 }
478 recomputeVisibleRegions = true;
479
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800480 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700481 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800482 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700483}
484
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800485bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800486 const State& c(getCurrentState());
487 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700488}
489
490void BufferStateLayer::setFilteringEnabled(bool enabled) {
491 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
492 mCurrentTransform, enabled);
493}
494
Alec Mouri39801c02018-10-10 10:44:47 -0700495status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700496 const State& s(getDrawingState());
497 auto& engine(mFlinger->getRenderEngine());
498
Alec Mourib5c4f352019-02-19 19:46:38 -0800499 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700500}
501
Alec Mouri56e538f2019-01-14 15:22:01 -0800502status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700503 const State& s(getDrawingState());
504
505 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800506 if (s.bgColorLayer) {
507 for (auto& handle : mDrawingState.callbackHandles) {
508 handle->latchTime = latchTime;
509 }
510 }
Marissa Wall61c58622018-07-18 10:12:20 -0700511 return NO_ERROR;
512 }
513
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700514 const int32_t layerID = getSequence();
515
Marissa Wall61c58622018-07-18 10:12:20 -0700516 // Reject if the layer is invalid
517 uint32_t bufferWidth = s.buffer->width;
518 uint32_t bufferHeight = s.buffer->height;
519
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700521 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700522 }
523
524 if (s.transformToDisplayInverse) {
525 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700526 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700527 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700528 }
529 }
530
Vishnu Nair60356342018-11-13 13:00:45 -0800531 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700532 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
533 ALOGE("[%s] rejecting buffer: "
534 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
535 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800536 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700537 return BAD_VALUE;
538 }
539
Marissa Wall5a68a772018-12-22 17:43:42 -0800540 for (auto& handle : mDrawingState.callbackHandles) {
541 handle->latchTime = latchTime;
542 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700543
Alec Mouri56e538f2019-01-14 15:22:01 -0800544 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700545 // Bind the new buffer to the GL texture.
546 //
547 // Older devices require the "implicit" synchronization provided
548 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
549 // devices will either call this in Layer::onDraw, or (if it's not
550 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800551 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700552 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800553 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700554 return BAD_VALUE;
555 }
556 }
557
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800558 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800559 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700560
Marissa Wall16c112d2019-03-20 13:21:13 -0700561 mCurrentStateModified = false;
562
Marissa Wall61c58622018-07-18 10:12:20 -0700563 return NO_ERROR;
564}
565
566status_t BufferStateLayer::updateActiveBuffer() {
567 const State& s(getDrawingState());
568
569 if (s.buffer == nullptr) {
570 return BAD_VALUE;
571 }
572
Alec Mourib5c4f352019-02-19 19:46:38 -0800573 if (mActiveBuffer != nullptr) {
574 // todo: get this to work with BufferStateLayerCache
575 auto& engine(mFlinger->getRenderEngine());
576 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
577 }
Marissa Wall61c58622018-07-18 10:12:20 -0700578 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000579 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800580 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
581 layerCompositionState.buffer = mActiveBuffer;
582 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700583
584 return NO_ERROR;
585}
586
587status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
588 // TODO(marissaw): support frame history events
589 mCurrentFrameNumber = mFrameNumber;
590 return NO_ERROR;
591}
592
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800593void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
594 const auto outputLayer = findOutputLayerForDisplay(display);
595 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000596 auto& hwcInfo = *outputLayer->editState().hwc;
597 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700598
599 const State& s(getDrawingState());
600
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700601 uint32_t hwcSlot;
Valerie Hau6f89c372019-03-02 00:13:33 +0000602 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700603 hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId),
604 s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700605
Valerie Hau6f89c372019-03-02 00:13:33 +0000606 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700607 if (error != HWC2::Error::None) {
608 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
609 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
610 }
611
612 mFrameNumber++;
613}
614
615void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700616 BufferLayer::onFirstRef();
617
Marissa Wall61c58622018-07-18 10:12:20 -0700618 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
619 updateTransformHint(display);
620 }
621}
622
Marissa Wall947d34e2019-03-29 14:03:53 -0700623void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
624 std::lock_guard lock(mMutex);
625 if (!clientCacheId.isValid()) {
626 ALOGE("invalid process, failed to erase buffer");
627 return;
628 }
629 eraseBufferLocked(clientCacheId);
630}
631
632uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
633 std::lock_guard<std::mutex> lock(mMutex);
634 auto itr = mCachedBuffers.find(clientCacheId);
635 if (itr == mCachedBuffers.end()) {
636 return addCachedBuffer(clientCacheId);
637 }
638 auto& [hwcCacheSlot, counter] = itr->second;
639 counter = mCounter++;
640 return hwcCacheSlot;
641}
642
643uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
644 REQUIRES(mMutex) {
645 if (!clientCacheId.isValid()) {
646 ALOGE("invalid process, returning invalid slot");
647 return BufferQueue::INVALID_BUFFER_SLOT;
648 }
649
650 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
651
652 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
653 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
654 return hwcCacheSlot;
655}
656
657uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
658 if (mFreeHwcCacheSlots.empty()) {
659 evictLeastRecentlyUsed();
660 }
661
662 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
663 mFreeHwcCacheSlots.pop();
664 return hwcCacheSlot;
665}
666
667void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
668 uint64_t minCounter = UINT_MAX;
669 client_cache_t minClientCacheId = {};
670 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
671 const auto& [hwcCacheSlot, counter] = slotCounter;
672 if (counter < minCounter) {
673 minCounter = counter;
674 minClientCacheId = clientCacheId;
675 }
676 }
677 eraseBufferLocked(minClientCacheId);
678
679 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
680}
681
682void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
683 REQUIRES(mMutex) {
684 auto itr = mCachedBuffers.find(clientCacheId);
685 if (itr == mCachedBuffers.end()) {
686 return;
687 }
688 auto& [hwcCacheSlot, counter] = itr->second;
689
690 // TODO send to hwc cache and resources
691
692 mFreeHwcCacheSlots.push(hwcCacheSlot);
693 mCachedBuffers.erase(clientCacheId);
694}
Marissa Wall61c58622018-07-18 10:12:20 -0700695} // namespace android