blob: 4a8261d99f892fdebddced9eefdc4a985d6ff655 [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}
Marissa Wall61c58622018-07-18 10:12:20 -070053
Alec Mouri4545a8a2019-08-08 20:05:32 -070054BufferStateLayer::~BufferStateLayer() {
55 if (mActiveBuffer != nullptr) {
56 // Ensure that mActiveBuffer is uncached from RenderEngine here, as
57 // RenderEngine may have been using the buffer as an external texture
58 // after the client uncached the buffer.
59 auto& engine(mFlinger->getRenderEngine());
60 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
61 }
62}
63
Marissa Wall61c58622018-07-18 10:12:20 -070064// -----------------------------------------------------------------------
65// Interface implementation for Layer
66// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070067void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080068 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
69 // buffer that was presented on this layer. The first transaction that came in this frame that
70 // replaced the previous buffer on this layer needs this release fence, because the fence will
71 // let the client know when that previous buffer is removed from the screen.
72 //
73 // Every other transaction on this layer does not need a release fence because no other
74 // Transactions that were set on this layer this frame are going to have their preceeding buffer
75 // removed from the display this frame.
76 //
77 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
78 // buffer so it doesn't need a previous release fence because the layer still needs the previous
79 // buffer. The second transaction contains a buffer so it needs a previous release fence because
80 // the previous buffer will be released this frame. The third transaction also contains a
81 // buffer. It replaces the buffer in the second transaction. The buffer in the second
82 // transaction will now no longer be presented so it is released immediately and the third
83 // transaction doesn't need a previous release fence.
84 for (auto& handle : mDrawingState.callbackHandles) {
85 if (handle->releasePreviousBuffer) {
86 handle->previousReleaseFence = releaseFence;
87 break;
88 }
89 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070090
91 // Prevent tracing the same release multiple times.
92 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
93 mFlinger->mTimeStats->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
94 std::make_shared<FenceTime>(releaseFence),
95 TimeStats::FrameEvent::RELEASE_FENCE);
96 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
97 }
Marissa Wall61c58622018-07-18 10:12:20 -070098}
99
100void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
101 // TODO(marissaw): send the transform hint to buffer owner
102 return;
103}
104
105void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700106 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800107 mDrawingState.callbackHandles);
108
109 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700110}
111
Ana Krulec010d2192018-10-08 06:29:54 -0700112bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700113 if (getSidebandStreamChanged() || getAutoRefresh()) {
114 return true;
115 }
116
Marissa Wall024a1912018-08-13 13:55:35 -0700117 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700118}
119
Marissa Walle2ffb422018-10-12 11:33:52 -0700120bool BufferStateLayer::willPresentCurrentTransaction() const {
121 // Returns true if the most recent Transaction applied to CurrentState will be presented.
122 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800123 (mCurrentState.modified &&
124 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700125}
126
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800127bool BufferStateLayer::getTransformToDisplayInverse() const {
128 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700129}
130
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800131void BufferStateLayer::pushPendingState() {
132 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700133 return;
134 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800135 mPendingStates.push_back(mCurrentState);
136 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700137}
138
139bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 const bool stateUpdateAvailable = !mPendingStates.empty();
141 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700142 popPendingState(stateToCommit);
143 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
145 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700146 return stateUpdateAvailable;
147}
148
Marissa Wall861616d2018-10-22 12:52:23 -0700149// Crop that applies to the window
150Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
151 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700152}
153
154bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800155 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800156 mCurrentState.transform = transform;
157 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700158 setTransactionFlags(eTransactionNeeded);
159 return true;
160}
161
162bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800163 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
164 mCurrentState.sequence++;
165 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
166 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700167 setTransactionFlags(eTransactionNeeded);
168 return true;
169}
170
171bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800172 Rect c = crop;
173 if (c.left < 0) {
174 c.left = 0;
175 }
176 if (c.top < 0) {
177 c.top = 0;
178 }
179 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
180 // treats all invalid rectangles the same.
181 if (!c.isValid()) {
182 c.makeInvalid();
183 }
184
185 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800186 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800187 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700188 setTransactionFlags(eTransactionNeeded);
189 return true;
190}
191
Marissa Wall861616d2018-10-22 12:52:23 -0700192bool BufferStateLayer::setFrame(const Rect& frame) {
193 int x = frame.left;
194 int y = frame.top;
195 int w = frame.getWidth();
196 int h = frame.getHeight();
197
Marissa Wall0f3242d2018-12-20 15:10:22 -0800198 if (x < 0) {
199 x = 0;
200 w = frame.right;
201 }
202
203 if (y < 0) {
204 y = 0;
205 h = frame.bottom;
206 }
207
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800208 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
209 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700210 return false;
211 }
212
213 if (!frame.isValid()) {
214 x = y = w = h = 0;
215 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800216 mCurrentState.active.transform.set(x, y);
217 mCurrentState.active.w = w;
218 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700219
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800220 mCurrentState.sequence++;
221 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700222 setTransactionFlags(eTransactionNeeded);
223 return true;
224}
225
Ady Abraham09bd3922019-04-08 10:44:56 -0700226bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700227 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800228 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700229 mReleasePreviousBuffer = true;
230 }
231
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800232 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700233 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800234 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700235 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700236
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700237 const int32_t layerID = getSequence();
238 mFlinger->mTimeStats->setPostTime(layerID, mFrameNumber, getName().c_str(), postTime);
239 mFlinger->mTimeStats->traceNewLayer(layerID, getName().c_str());
240 mFlinger->mTimeStats->traceTimestamp(layerID, buffer->getId(), mFrameNumber, postTime,
241 TimeStats::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700242 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700243
244 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700245 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700246 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
247 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700248 }
249
Marissa Wall61c58622018-07-18 10:12:20 -0700250 return true;
251}
252
253bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700254 // The acquire fences of BufferStateLayers have already signaled before they are set
255 mCallbackHandleAcquireTime = fence->getSignalTime();
256
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800257 mCurrentState.acquireFence = fence;
258 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700259 setTransactionFlags(eTransactionNeeded);
260 return true;
261}
262
263bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 mCurrentState.dataspace = dataspace;
266 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700267 setTransactionFlags(eTransactionNeeded);
268 return true;
269}
270
271bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800272 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800273 mCurrentState.hdrMetadata = hdrMetadata;
274 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700275 setTransactionFlags(eTransactionNeeded);
276 return true;
277}
278
279bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800280 mCurrentState.surfaceDamageRegion = surfaceDamage;
281 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700282 setTransactionFlags(eTransactionNeeded);
283 return true;
284}
285
286bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800287 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800288 mCurrentState.api = api;
289 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700290 setTransactionFlags(eTransactionNeeded);
291 return true;
292}
293
294bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800295 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800296 mCurrentState.sidebandStream = sidebandStream;
297 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700298 setTransactionFlags(eTransactionNeeded);
299
300 if (!mSidebandStreamChanged.exchange(true)) {
301 // mSidebandStreamChanged was false
302 mFlinger->signalLayerUpdate();
303 }
304 return true;
305}
306
Marissa Walle2ffb422018-10-12 11:33:52 -0700307bool BufferStateLayer::setTransactionCompletedListeners(
308 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700309 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700310 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700311 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700312 return false;
313 }
314
315 const bool willPresent = willPresentCurrentTransaction();
316
317 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700318 // If this transaction set a buffer on this layer, release its previous buffer
319 handle->releasePreviousBuffer = mReleasePreviousBuffer;
320
Marissa Walle2ffb422018-10-12 11:33:52 -0700321 // If this layer will be presented in this frame
322 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700323 // If this transaction set an acquire fence on this layer, set its acquire time
324 handle->acquireTime = mCallbackHandleAcquireTime;
325
Marissa Walle2ffb422018-10-12 11:33:52 -0700326 // Notify the transaction completed thread that there is a pending latched callback
327 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800328 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700329
330 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800331 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700332
333 } else { // If this layer will NOT need to be relatched and presented this frame
334 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700335 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700336 }
337 }
338
Marissa Wallfda30bb2018-10-12 11:34:28 -0700339 mReleasePreviousBuffer = false;
340 mCallbackHandleAcquireTime = -1;
341
Marissa Walle2ffb422018-10-12 11:33:52 -0700342 return willPresent;
343}
344
Marissa Wall61c58622018-07-18 10:12:20 -0700345bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800346 mCurrentState.transparentRegionHint = transparent;
347 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700348 setTransactionFlags(eTransactionNeeded);
349 return true;
350}
351
Marissa Wall861616d2018-10-22 12:52:23 -0700352Rect BufferStateLayer::getBufferSize(const State& s) const {
353 // for buffer state layers we use the display frame size as the buffer size.
354 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
355 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700356 }
357
Marissa Wall861616d2018-10-22 12:52:23 -0700358 // if the display frame is not defined, use the parent bounds as the buffer size.
359 const auto& p = mDrawingParent.promote();
360 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800361 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700362 if (!parentBounds.isEmpty()) {
363 return parentBounds;
364 }
365 }
366
Marissa Wall861616d2018-10-22 12:52:23 -0700367 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700368}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369
370FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
371 const State& s(getDrawingState());
372 // for buffer state layers we use the display frame size as the buffer size.
373 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
374 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
375 }
376
377 // if the display frame is not defined, use the parent bounds as the buffer size.
378 return parentBounds;
379}
380
Marissa Wall61c58622018-07-18 10:12:20 -0700381// -----------------------------------------------------------------------
382
383// -----------------------------------------------------------------------
384// Interface implementation for BufferLayer
385// -----------------------------------------------------------------------
386bool BufferStateLayer::fenceHasSignaled() const {
387 if (latchUnsignaledBuffers()) {
388 return true;
389 }
390
391 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
392}
393
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700394bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700395 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
396 return true;
397 }
398
chaviwfa67b552019-08-12 16:51:55 -0700399 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700400}
401
Marissa Wall61c58622018-07-18 10:12:20 -0700402nsecs_t BufferStateLayer::getDesiredPresentTime() {
chaviwfa67b552019-08-12 16:51:55 -0700403 return getDrawingState().desiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700404}
405
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800406std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700407 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
408}
409
410void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
411 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
412}
413
414uint32_t BufferStateLayer::getDrawingTransform() const {
415 return getDrawingState().transform;
416}
417
418ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
419 return getDrawingState().dataspace;
420}
421
Marissa Wall861616d2018-10-22 12:52:23 -0700422// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700423Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700424 const State& s(getDrawingState());
425
426 if (s.crop.isEmpty() && s.buffer) {
427 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800428 } else if (s.buffer) {
429 Rect crop = s.crop;
430 crop.left = std::max(crop.left, 0);
431 crop.top = std::max(crop.top, 0);
432 uint32_t bufferWidth = s.buffer->getWidth();
433 uint32_t bufferHeight = s.buffer->getHeight();
434 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
435 bufferWidth <= std::numeric_limits<int32_t>::max()) {
436 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
437 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
438 }
439 if (!crop.isValid()) {
440 // Crop rect is out of bounds, return whole buffer
441 return s.buffer->getBounds();
442 }
443 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700444 }
445 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700446}
447
448uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700449 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700450}
451
452Region BufferStateLayer::getDrawingSurfaceDamage() const {
Marissa Wall0ef8d602019-04-23 14:09:28 -0700453 return getDrawingState().surfaceDamageRegion;
Marissa Wall61c58622018-07-18 10:12:20 -0700454}
455
456const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
457 return getDrawingState().hdrMetadata;
458}
459
460int BufferStateLayer::getDrawingApi() const {
461 return getDrawingState().api;
462}
463
464PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800465 if (!mActiveBuffer) {
466 return PIXEL_FORMAT_NONE;
467 }
Marissa Wall61c58622018-07-18 10:12:20 -0700468 return mActiveBuffer->format;
469}
470
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700471uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700472 return mFrameNumber;
473}
474
475bool BufferStateLayer::getAutoRefresh() const {
476 // TODO(marissaw): support shared buffer mode
477 return false;
478}
479
480bool BufferStateLayer::getSidebandStreamChanged() const {
481 return mSidebandStreamChanged.load();
482}
483
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800484bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700485 if (mSidebandStreamChanged.exchange(false)) {
486 const State& s(getDrawingState());
487 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800488 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
489 mSidebandStream = s.sidebandStream;
490 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
491 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700492 setTransactionFlags(eTransactionNeeded);
493 mFlinger->setTransactionFlags(eTraversalNeeded);
494 }
495 recomputeVisibleRegions = true;
496
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800497 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700498 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800499 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700500}
501
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800502bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800503 const State& c(getCurrentState());
504 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700505}
506
507void BufferStateLayer::setFilteringEnabled(bool enabled) {
508 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
509 mCurrentTransform, enabled);
510}
511
Alec Mouri39801c02018-10-10 10:44:47 -0700512status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700513 const State& s(getDrawingState());
514 auto& engine(mFlinger->getRenderEngine());
515
Alec Mourib5c4f352019-02-19 19:46:38 -0800516 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700517}
518
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700519status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
520 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700521 const State& s(getDrawingState());
522
523 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800524 if (s.bgColorLayer) {
525 for (auto& handle : mDrawingState.callbackHandles) {
526 handle->latchTime = latchTime;
527 }
528 }
Marissa Wall61c58622018-07-18 10:12:20 -0700529 return NO_ERROR;
530 }
531
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700532 const int32_t layerID = getSequence();
533
Marissa Wall61c58622018-07-18 10:12:20 -0700534 // Reject if the layer is invalid
535 uint32_t bufferWidth = s.buffer->width;
536 uint32_t bufferHeight = s.buffer->height;
537
Peiyong Linefefaac2018-08-17 12:27:51 -0700538 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700539 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700540 }
541
542 if (s.transformToDisplayInverse) {
543 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700544 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700545 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700546 }
547 }
548
Vishnu Nair60356342018-11-13 13:00:45 -0800549 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700550 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
551 ALOGE("[%s] rejecting buffer: "
552 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
553 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700554 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 return BAD_VALUE;
556 }
557
Marissa Wall5a68a772018-12-22 17:43:42 -0800558 for (auto& handle : mDrawingState.callbackHandles) {
559 handle->latchTime = latchTime;
560 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700561
Alec Mouri56e538f2019-01-14 15:22:01 -0800562 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700563 // Bind the new buffer to the GL texture.
564 //
565 // Older devices require the "implicit" synchronization provided
566 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
567 // devices will either call this in Layer::onDraw, or (if it's not
568 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700570 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800571 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700572 return BAD_VALUE;
573 }
574 }
575
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700576 const uint64_t bufferID = getCurrentBufferId();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700577 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, getCurrentFenceTime());
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700578 mFlinger->mTimeStats->traceFence(layerID, bufferID, mFrameNumber, getCurrentFenceTime(),
579 TimeStats::FrameEvent::ACQUIRE_FENCE);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700580 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700581 mFlinger->mTimeStats->traceTimestamp(layerID, bufferID, mFrameNumber, latchTime,
582 TimeStats::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700583
Marissa Wall16c112d2019-03-20 13:21:13 -0700584 mCurrentStateModified = false;
585
Marissa Wall61c58622018-07-18 10:12:20 -0700586 return NO_ERROR;
587}
588
589status_t BufferStateLayer::updateActiveBuffer() {
590 const State& s(getDrawingState());
591
592 if (s.buffer == nullptr) {
593 return BAD_VALUE;
594 }
595
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700596 mPreviousBufferId = getCurrentBufferId();
Marissa Wall61c58622018-07-18 10:12:20 -0700597 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000598 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800599 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
600 layerCompositionState.buffer = mActiveBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700601
602 return NO_ERROR;
603}
604
605status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
606 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700607 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700608 mCurrentFrameNumber = mFrameNumber;
609 return NO_ERROR;
610}
611
Lloyd Piquef5275482019-01-29 18:42:42 -0800612void BufferStateLayer::latchPerFrameState(
613 compositionengine::LayerFECompositionState& compositionState) const {
614 BufferLayer::latchPerFrameState(compositionState);
615 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
616 return;
617 }
Marissa Wall61c58622018-07-18 10:12:20 -0700618
619 const State& s(getDrawingState());
620
Lloyd Piquef5275482019-01-29 18:42:42 -0800621 compositionState.buffer = s.buffer;
622 compositionState.bufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
623 compositionState.acquireFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700624
625 mFrameNumber++;
626}
627
628void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700629 BufferLayer::onFirstRef();
630
Marissa Wall61c58622018-07-18 10:12:20 -0700631 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
632 updateTransformHint(display);
633 }
634}
635
Marissa Wall947d34e2019-03-29 14:03:53 -0700636void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
637 std::lock_guard lock(mMutex);
638 if (!clientCacheId.isValid()) {
639 ALOGE("invalid process, failed to erase buffer");
640 return;
641 }
642 eraseBufferLocked(clientCacheId);
643}
644
645uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
646 std::lock_guard<std::mutex> lock(mMutex);
647 auto itr = mCachedBuffers.find(clientCacheId);
648 if (itr == mCachedBuffers.end()) {
649 return addCachedBuffer(clientCacheId);
650 }
651 auto& [hwcCacheSlot, counter] = itr->second;
652 counter = mCounter++;
653 return hwcCacheSlot;
654}
655
656uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
657 REQUIRES(mMutex) {
658 if (!clientCacheId.isValid()) {
659 ALOGE("invalid process, returning invalid slot");
660 return BufferQueue::INVALID_BUFFER_SLOT;
661 }
662
663 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
664
665 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
666 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
667 return hwcCacheSlot;
668}
669
670uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
671 if (mFreeHwcCacheSlots.empty()) {
672 evictLeastRecentlyUsed();
673 }
674
675 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
676 mFreeHwcCacheSlots.pop();
677 return hwcCacheSlot;
678}
679
680void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
681 uint64_t minCounter = UINT_MAX;
682 client_cache_t minClientCacheId = {};
683 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
684 const auto& [hwcCacheSlot, counter] = slotCounter;
685 if (counter < minCounter) {
686 minCounter = counter;
687 minClientCacheId = clientCacheId;
688 }
689 }
690 eraseBufferLocked(minClientCacheId);
691
692 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
693}
694
695void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
696 REQUIRES(mMutex) {
697 auto itr = mCachedBuffers.find(clientCacheId);
698 if (itr == mCachedBuffers.end()) {
699 return;
700 }
701 auto& [hwcCacheSlot, counter] = itr->second;
702
703 // TODO send to hwc cache and resources
704
705 mFreeHwcCacheSlots.push(hwcCacheSlot);
706 mCachedBuffers.erase(clientCacheId);
707}
Marissa Wall61c58622018-07-18 10:12:20 -0700708} // namespace android