blob: 203bd72e6f2182aac86270b324b5a0966ecf1ce2 [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;
Ady Abrahama315ce72019-04-24 14:35:20 -0700230 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
231 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700232 }
233
Marissa Wall61c58622018-07-18 10:12:20 -0700234 return true;
235}
236
237bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700238 // The acquire fences of BufferStateLayers have already signaled before they are set
239 mCallbackHandleAcquireTime = fence->getSignalTime();
240
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800241 mCurrentState.acquireFence = fence;
242 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700243 setTransactionFlags(eTransactionNeeded);
244 return true;
245}
246
247bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800248 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 mCurrentState.dataspace = dataspace;
250 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700251 setTransactionFlags(eTransactionNeeded);
252 return true;
253}
254
255bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800256 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800257 mCurrentState.hdrMetadata = hdrMetadata;
258 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700259 setTransactionFlags(eTransactionNeeded);
260 return true;
261}
262
263bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 mCurrentState.surfaceDamageRegion = surfaceDamage;
265 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700266 setTransactionFlags(eTransactionNeeded);
267 return true;
268}
269
270bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800271 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800272 mCurrentState.api = api;
273 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700274 setTransactionFlags(eTransactionNeeded);
275 return true;
276}
277
278bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800279 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800280 mCurrentState.sidebandStream = sidebandStream;
281 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700282 setTransactionFlags(eTransactionNeeded);
283
284 if (!mSidebandStreamChanged.exchange(true)) {
285 // mSidebandStreamChanged was false
286 mFlinger->signalLayerUpdate();
287 }
288 return true;
289}
290
Marissa Walle2ffb422018-10-12 11:33:52 -0700291bool BufferStateLayer::setTransactionCompletedListeners(
292 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700293 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700294 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700295 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700296 return false;
297 }
298
299 const bool willPresent = willPresentCurrentTransaction();
300
301 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700302 // If this transaction set a buffer on this layer, release its previous buffer
303 handle->releasePreviousBuffer = mReleasePreviousBuffer;
304
Marissa Walle2ffb422018-10-12 11:33:52 -0700305 // If this layer will be presented in this frame
306 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700307 // If this transaction set an acquire fence on this layer, set its acquire time
308 handle->acquireTime = mCallbackHandleAcquireTime;
309
Marissa Walle2ffb422018-10-12 11:33:52 -0700310 // Notify the transaction completed thread that there is a pending latched callback
311 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800312 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700313
314 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800315 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700316
317 } else { // If this layer will NOT need to be relatched and presented this frame
318 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800319 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700320 }
321 }
322
Marissa Wallfda30bb2018-10-12 11:34:28 -0700323 mReleasePreviousBuffer = false;
324 mCallbackHandleAcquireTime = -1;
325
Marissa Walle2ffb422018-10-12 11:33:52 -0700326 return willPresent;
327}
328
Marissa Wall61c58622018-07-18 10:12:20 -0700329bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800330 mCurrentState.transparentRegionHint = transparent;
331 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700332 setTransactionFlags(eTransactionNeeded);
333 return true;
334}
335
Marissa Wall861616d2018-10-22 12:52:23 -0700336Rect BufferStateLayer::getBufferSize(const State& s) const {
337 // for buffer state layers we use the display frame size as the buffer size.
338 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
339 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700340 }
341
Marissa Wall861616d2018-10-22 12:52:23 -0700342 // if the display frame is not defined, use the parent bounds as the buffer size.
343 const auto& p = mDrawingParent.promote();
344 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700346 if (!parentBounds.isEmpty()) {
347 return parentBounds;
348 }
349 }
350
Marissa Wall861616d2018-10-22 12:52:23 -0700351 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700352}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800353
354FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
355 const State& s(getDrawingState());
356 // for buffer state layers we use the display frame size as the buffer size.
357 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
358 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
359 }
360
361 // if the display frame is not defined, use the parent bounds as the buffer size.
362 return parentBounds;
363}
364
Marissa Wall61c58622018-07-18 10:12:20 -0700365// -----------------------------------------------------------------------
366
367// -----------------------------------------------------------------------
368// Interface implementation for BufferLayer
369// -----------------------------------------------------------------------
370bool BufferStateLayer::fenceHasSignaled() const {
371 if (latchUnsignaledBuffers()) {
372 return true;
373 }
374
375 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
376}
377
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700378bool BufferStateLayer::framePresentTimeIsCurrent() const {
379 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
380 return true;
381 }
382
Alec Mouriaa614192019-06-06 13:28:34 -0700383 return mDesiredPresentTime <= mFlinger->getExpectedPresentTime();
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700384}
385
Marissa Wall61c58622018-07-18 10:12:20 -0700386nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800387 return mDesiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700388}
389
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800390std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700391 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
392}
393
394void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
395 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
396}
397
398uint32_t BufferStateLayer::getDrawingTransform() const {
399 return getDrawingState().transform;
400}
401
402ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
403 return getDrawingState().dataspace;
404}
405
Marissa Wall861616d2018-10-22 12:52:23 -0700406// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700407Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700408 const State& s(getDrawingState());
409
410 if (s.crop.isEmpty() && s.buffer) {
411 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800412 } else if (s.buffer) {
413 Rect crop = s.crop;
414 crop.left = std::max(crop.left, 0);
415 crop.top = std::max(crop.top, 0);
416 uint32_t bufferWidth = s.buffer->getWidth();
417 uint32_t bufferHeight = s.buffer->getHeight();
418 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
419 bufferWidth <= std::numeric_limits<int32_t>::max()) {
420 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
421 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
422 }
423 if (!crop.isValid()) {
424 // Crop rect is out of bounds, return whole buffer
425 return s.buffer->getBounds();
426 }
427 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700428 }
429 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700430}
431
432uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700433 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700434}
435
436Region BufferStateLayer::getDrawingSurfaceDamage() const {
Marissa Wall0ef8d602019-04-23 14:09:28 -0700437 return getDrawingState().surfaceDamageRegion;
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
440const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
441 return getDrawingState().hdrMetadata;
442}
443
444int BufferStateLayer::getDrawingApi() const {
445 return getDrawingState().api;
446}
447
448PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800449 if (!mActiveBuffer) {
450 return PIXEL_FORMAT_NONE;
451 }
Marissa Wall61c58622018-07-18 10:12:20 -0700452 return mActiveBuffer->format;
453}
454
455uint64_t BufferStateLayer::getFrameNumber() const {
456 return mFrameNumber;
457}
458
459bool BufferStateLayer::getAutoRefresh() const {
460 // TODO(marissaw): support shared buffer mode
461 return false;
462}
463
464bool BufferStateLayer::getSidebandStreamChanged() const {
465 return mSidebandStreamChanged.load();
466}
467
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800468bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700469 if (mSidebandStreamChanged.exchange(false)) {
470 const State& s(getDrawingState());
471 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800472 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
473 mSidebandStream = s.sidebandStream;
474 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
475 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700476 setTransactionFlags(eTransactionNeeded);
477 mFlinger->setTransactionFlags(eTraversalNeeded);
478 }
479 recomputeVisibleRegions = true;
480
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800481 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700482 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800483 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700484}
485
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800486bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800487 const State& c(getCurrentState());
488 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700489}
490
491void BufferStateLayer::setFilteringEnabled(bool enabled) {
492 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
493 mCurrentTransform, enabled);
494}
495
Alec Mouri39801c02018-10-10 10:44:47 -0700496status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700497 const State& s(getDrawingState());
498 auto& engine(mFlinger->getRenderEngine());
499
Alec Mourib5c4f352019-02-19 19:46:38 -0800500 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700501}
502
Alec Mouri56e538f2019-01-14 15:22:01 -0800503status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700504 const State& s(getDrawingState());
505
506 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800507 if (s.bgColorLayer) {
508 for (auto& handle : mDrawingState.callbackHandles) {
509 handle->latchTime = latchTime;
510 }
511 }
Marissa Wall61c58622018-07-18 10:12:20 -0700512 return NO_ERROR;
513 }
514
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700515 const int32_t layerID = getSequence();
516
Marissa Wall61c58622018-07-18 10:12:20 -0700517 // Reject if the layer is invalid
518 uint32_t bufferWidth = s.buffer->width;
519 uint32_t bufferHeight = s.buffer->height;
520
Peiyong Linefefaac2018-08-17 12:27:51 -0700521 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700522 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700523 }
524
525 if (s.transformToDisplayInverse) {
526 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700527 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700528 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700529 }
530 }
531
Vishnu Nair60356342018-11-13 13:00:45 -0800532 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700533 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
534 ALOGE("[%s] rejecting buffer: "
535 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
536 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800537 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700538 return BAD_VALUE;
539 }
540
Marissa Wall5a68a772018-12-22 17:43:42 -0800541 for (auto& handle : mDrawingState.callbackHandles) {
542 handle->latchTime = latchTime;
543 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700544
Alec Mouri56e538f2019-01-14 15:22:01 -0800545 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700546 // Bind the new buffer to the GL texture.
547 //
548 // Older devices require the "implicit" synchronization provided
549 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
550 // devices will either call this in Layer::onDraw, or (if it's not
551 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800552 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700553 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800554 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 return BAD_VALUE;
556 }
557 }
558
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800559 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800560 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700561
Marissa Wall16c112d2019-03-20 13:21:13 -0700562 mCurrentStateModified = false;
563
Marissa Wall61c58622018-07-18 10:12:20 -0700564 return NO_ERROR;
565}
566
567status_t BufferStateLayer::updateActiveBuffer() {
568 const State& s(getDrawingState());
569
570 if (s.buffer == nullptr) {
571 return BAD_VALUE;
572 }
573
Alec Mourib5c4f352019-02-19 19:46:38 -0800574 if (mActiveBuffer != nullptr) {
575 // todo: get this to work with BufferStateLayerCache
576 auto& engine(mFlinger->getRenderEngine());
577 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
578 }
Marissa Wall61c58622018-07-18 10:12:20 -0700579 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000580 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800581 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
582 layerCompositionState.buffer = mActiveBuffer;
583 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700584
585 return NO_ERROR;
586}
587
588status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
589 // TODO(marissaw): support frame history events
590 mCurrentFrameNumber = mFrameNumber;
591 return NO_ERROR;
592}
593
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800594void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
595 const auto outputLayer = findOutputLayerForDisplay(display);
596 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000597 auto& hwcInfo = *outputLayer->editState().hwc;
598 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700599
600 const State& s(getDrawingState());
601
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700602 uint32_t hwcSlot;
Valerie Hau6f89c372019-03-02 00:13:33 +0000603 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700604 hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId),
605 s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700606
Valerie Hau6f89c372019-03-02 00:13:33 +0000607 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700608 if (error != HWC2::Error::None) {
609 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
610 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
611 }
612
613 mFrameNumber++;
614}
615
616void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700617 BufferLayer::onFirstRef();
618
Marissa Wall61c58622018-07-18 10:12:20 -0700619 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
620 updateTransformHint(display);
621 }
622}
623
Marissa Wall947d34e2019-03-29 14:03:53 -0700624void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
625 std::lock_guard lock(mMutex);
626 if (!clientCacheId.isValid()) {
627 ALOGE("invalid process, failed to erase buffer");
628 return;
629 }
630 eraseBufferLocked(clientCacheId);
631}
632
633uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
634 std::lock_guard<std::mutex> lock(mMutex);
635 auto itr = mCachedBuffers.find(clientCacheId);
636 if (itr == mCachedBuffers.end()) {
637 return addCachedBuffer(clientCacheId);
638 }
639 auto& [hwcCacheSlot, counter] = itr->second;
640 counter = mCounter++;
641 return hwcCacheSlot;
642}
643
644uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
645 REQUIRES(mMutex) {
646 if (!clientCacheId.isValid()) {
647 ALOGE("invalid process, returning invalid slot");
648 return BufferQueue::INVALID_BUFFER_SLOT;
649 }
650
651 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
652
653 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
654 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
655 return hwcCacheSlot;
656}
657
658uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
659 if (mFreeHwcCacheSlots.empty()) {
660 evictLeastRecentlyUsed();
661 }
662
663 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
664 mFreeHwcCacheSlots.pop();
665 return hwcCacheSlot;
666}
667
668void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
669 uint64_t minCounter = UINT_MAX;
670 client_cache_t minClientCacheId = {};
671 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
672 const auto& [hwcCacheSlot, counter] = slotCounter;
673 if (counter < minCounter) {
674 minCounter = counter;
675 minClientCacheId = clientCacheId;
676 }
677 }
678 eraseBufferLocked(minClientCacheId);
679
680 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
681}
682
683void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
684 REQUIRES(mMutex) {
685 auto itr = mCachedBuffers.find(clientCacheId);
686 if (itr == mCachedBuffers.end()) {
687 return;
688 }
689 auto& [hwcCacheSlot, counter] = itr->second;
690
691 // TODO send to hwc cache and resources
692
693 mFreeHwcCacheSlots.push(hwcCacheSlot);
694 mCachedBuffers.erase(clientCacheId);
695}
Marissa Wall61c58622018-07-18 10:12:20 -0700696} // namespace android