blob: 0fc733303dc0099fcca30fb5ed7665ce65f39013 [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 Wall61c58622018-07-18 10:12:20 -070029#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070030#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070031
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include "BufferStateLayer.h"
33#include "ColorLayer.h"
34#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080035
Marissa Wall61c58622018-07-18 10:12:20 -070036namespace android {
37
Lloyd Pique42ab75e2018-09-12 20:46:03 -070038// clang-format off
39const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1
44};
45// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070046
Vishnu Nair60356342018-11-13 13:00:45 -080047BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
48 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080049 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080050}
Alec Mourib5c4f352019-02-19 19:46:38 -080051BufferStateLayer::~BufferStateLayer() {
52 if (mActiveBuffer != nullptr) {
53 auto& engine(mFlinger->getRenderEngine());
54 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
55 }
56}
Marissa Wall61c58622018-07-18 10:12:20 -070057
58// -----------------------------------------------------------------------
59// Interface implementation for Layer
60// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070061void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080062 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
63 // buffer that was presented on this layer. The first transaction that came in this frame that
64 // replaced the previous buffer on this layer needs this release fence, because the fence will
65 // let the client know when that previous buffer is removed from the screen.
66 //
67 // Every other transaction on this layer does not need a release fence because no other
68 // Transactions that were set on this layer this frame are going to have their preceeding buffer
69 // removed from the display this frame.
70 //
71 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
72 // buffer so it doesn't need a previous release fence because the layer still needs the previous
73 // buffer. The second transaction contains a buffer so it needs a previous release fence because
74 // the previous buffer will be released this frame. The third transaction also contains a
75 // buffer. It replaces the buffer in the second transaction. The buffer in the second
76 // transaction will now no longer be presented so it is released immediately and the third
77 // transaction doesn't need a previous release fence.
78 for (auto& handle : mDrawingState.callbackHandles) {
79 if (handle->releasePreviousBuffer) {
80 handle->previousReleaseFence = releaseFence;
81 break;
82 }
83 }
Marissa Wall61c58622018-07-18 10:12:20 -070084}
85
86void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
87 // TODO(marissaw): send the transform hint to buffer owner
88 return;
89}
90
91void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080092 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
93 mDrawingState.callbackHandles);
94
95 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070096}
97
Ana Krulec010d2192018-10-08 06:29:54 -070098bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070099 if (getSidebandStreamChanged() || getAutoRefresh()) {
100 return true;
101 }
102
Marissa Wall024a1912018-08-13 13:55:35 -0700103 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
Marissa Walle2ffb422018-10-12 11:33:52 -0700106bool BufferStateLayer::willPresentCurrentTransaction() const {
107 // Returns true if the most recent Transaction applied to CurrentState will be presented.
108 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800109 (mCurrentState.modified &&
110 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700111}
112
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113bool BufferStateLayer::getTransformToDisplayInverse() const {
114 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700115}
116
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117void BufferStateLayer::pushPendingState() {
118 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700119 return;
120 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 mPendingStates.push_back(mCurrentState);
122 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700123}
124
125bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 const bool stateUpdateAvailable = !mPendingStates.empty();
127 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700128 popPendingState(stateToCommit);
129 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
131 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700132 return stateUpdateAvailable;
133}
134
Marissa Wall861616d2018-10-22 12:52:23 -0700135// Crop that applies to the window
136Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
137 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700138}
139
140bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 if (mCurrentState.transform == transform) return false;
142 mCurrentState.sequence++;
143 mCurrentState.transform = transform;
144 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700145 setTransactionFlags(eTransactionNeeded);
146 return true;
147}
148
149bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
151 mCurrentState.sequence++;
152 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
153 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700154 setTransactionFlags(eTransactionNeeded);
155 return true;
156}
157
158bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800159 Rect c = crop;
160 if (c.left < 0) {
161 c.left = 0;
162 }
163 if (c.top < 0) {
164 c.top = 0;
165 }
166 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
167 // treats all invalid rectangles the same.
168 if (!c.isValid()) {
169 c.makeInvalid();
170 }
171
172 if (mCurrentState.crop == c) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800173 mCurrentState.sequence++;
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
Marissa Wallfda30bb2018-10-12 11:34:28 -0700214bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800215 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700216 mReleasePreviousBuffer = true;
217 }
218
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800219 mCurrentState.buffer = buffer;
220 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700221 setTransactionFlags(eTransactionNeeded);
222 return true;
223}
224
225bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700226 // The acquire fences of BufferStateLayers have already signaled before they are set
227 mCallbackHandleAcquireTime = fence->getSignalTime();
228
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800229 mCurrentState.acquireFence = fence;
230 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700231 setTransactionFlags(eTransactionNeeded);
232 return true;
233}
234
235bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800236 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800237 mCurrentState.dataspace = dataspace;
238 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700239 setTransactionFlags(eTransactionNeeded);
240 return true;
241}
242
243bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800244 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
245 mCurrentState.sequence++;
246 mCurrentState.hdrMetadata = hdrMetadata;
247 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700248 setTransactionFlags(eTransactionNeeded);
249 return true;
250}
251
252bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800253 mCurrentState.sequence++;
254 mCurrentState.surfaceDamageRegion = surfaceDamage;
255 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700256 setTransactionFlags(eTransactionNeeded);
257 return true;
258}
259
260bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800261 if (mCurrentState.api == api) return false;
262 mCurrentState.sequence++;
263 mCurrentState.api = api;
264 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700265 setTransactionFlags(eTransactionNeeded);
266 return true;
267}
268
269bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 if (mCurrentState.sidebandStream == sidebandStream) return false;
271 mCurrentState.sequence++;
272 mCurrentState.sidebandStream = sidebandStream;
273 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700274 setTransactionFlags(eTransactionNeeded);
275
276 if (!mSidebandStreamChanged.exchange(true)) {
277 // mSidebandStreamChanged was false
278 mFlinger->signalLayerUpdate();
279 }
280 return true;
281}
282
Marissa Walle2ffb422018-10-12 11:33:52 -0700283bool BufferStateLayer::setTransactionCompletedListeners(
284 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700285 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700286 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700287 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700288 return false;
289 }
290
291 const bool willPresent = willPresentCurrentTransaction();
292
293 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700294 // If this transaction set a buffer on this layer, release its previous buffer
295 handle->releasePreviousBuffer = mReleasePreviousBuffer;
296
Marissa Walle2ffb422018-10-12 11:33:52 -0700297 // If this layer will be presented in this frame
298 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700299 // If this transaction set an acquire fence on this layer, set its acquire time
300 handle->acquireTime = mCallbackHandleAcquireTime;
301
Marissa Walle2ffb422018-10-12 11:33:52 -0700302 // Notify the transaction completed thread that there is a pending latched callback
303 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800304 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700305
306 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800307 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700308
309 } else { // If this layer will NOT need to be relatched and presented this frame
310 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800311 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700312 }
313 }
314
Marissa Wallfda30bb2018-10-12 11:34:28 -0700315 mReleasePreviousBuffer = false;
316 mCallbackHandleAcquireTime = -1;
317
Marissa Walle2ffb422018-10-12 11:33:52 -0700318 return willPresent;
319}
320
Marissa Wall61c58622018-07-18 10:12:20 -0700321bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800322 mCurrentState.transparentRegionHint = transparent;
323 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700324 setTransactionFlags(eTransactionNeeded);
325 return true;
326}
327
Marissa Wall861616d2018-10-22 12:52:23 -0700328Rect BufferStateLayer::getBufferSize(const State& s) const {
329 // for buffer state layers we use the display frame size as the buffer size.
330 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
331 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700332 }
333
Marissa Wall861616d2018-10-22 12:52:23 -0700334 // if the display frame is not defined, use the parent bounds as the buffer size.
335 const auto& p = mDrawingParent.promote();
336 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800337 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700338 if (!parentBounds.isEmpty()) {
339 return parentBounds;
340 }
341 }
342
Marissa Wall861616d2018-10-22 12:52:23 -0700343 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700344}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345
346FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
347 const State& s(getDrawingState());
348 // for buffer state layers we use the display frame size as the buffer size.
349 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
350 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
351 }
352
353 // if the display frame is not defined, use the parent bounds as the buffer size.
354 return parentBounds;
355}
356
Valerie Haubc6ddb12019-03-08 11:10:15 -0800357void BufferStateLayer::setPostTime(nsecs_t postTime) {
358 mFlinger->mTimeStats->setPostTime(getSequence(), getFrameNumber(), getName().c_str(), postTime);
359}
360
361void BufferStateLayer::setDesiredPresentTime(nsecs_t desiredPresentTime) {
362 mDesiredPresentTime = desiredPresentTime;
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
378nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800379 return mDesiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700380}
381
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800382std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700383 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
384}
385
386void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
387 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
388}
389
390uint32_t BufferStateLayer::getDrawingTransform() const {
391 return getDrawingState().transform;
392}
393
394ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
395 return getDrawingState().dataspace;
396}
397
Marissa Wall861616d2018-10-22 12:52:23 -0700398// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700399Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700400 const State& s(getDrawingState());
401
402 if (s.crop.isEmpty() && s.buffer) {
403 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800404 } else if (s.buffer) {
405 Rect crop = s.crop;
406 crop.left = std::max(crop.left, 0);
407 crop.top = std::max(crop.top, 0);
408 uint32_t bufferWidth = s.buffer->getWidth();
409 uint32_t bufferHeight = s.buffer->getHeight();
410 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
411 bufferWidth <= std::numeric_limits<int32_t>::max()) {
412 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
413 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
414 }
415 if (!crop.isValid()) {
416 // Crop rect is out of bounds, return whole buffer
417 return s.buffer->getBounds();
418 }
419 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700420 }
421 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700422}
423
424uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700425 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700426}
427
428Region BufferStateLayer::getDrawingSurfaceDamage() const {
429 return getDrawingState().surfaceDamageRegion;
430}
431
432const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
433 return getDrawingState().hdrMetadata;
434}
435
436int BufferStateLayer::getDrawingApi() const {
437 return getDrawingState().api;
438}
439
440PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800441 if (!mActiveBuffer) {
442 return PIXEL_FORMAT_NONE;
443 }
Marissa Wall61c58622018-07-18 10:12:20 -0700444 return mActiveBuffer->format;
445}
446
447uint64_t BufferStateLayer::getFrameNumber() const {
448 return mFrameNumber;
449}
450
451bool BufferStateLayer::getAutoRefresh() const {
452 // TODO(marissaw): support shared buffer mode
453 return false;
454}
455
456bool BufferStateLayer::getSidebandStreamChanged() const {
457 return mSidebandStreamChanged.load();
458}
459
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800460bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700461 if (mSidebandStreamChanged.exchange(false)) {
462 const State& s(getDrawingState());
463 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800464 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
465 mSidebandStream = s.sidebandStream;
466 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
467 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700468 setTransactionFlags(eTransactionNeeded);
469 mFlinger->setTransactionFlags(eTraversalNeeded);
470 }
471 recomputeVisibleRegions = true;
472
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800473 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700474 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800475 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700476}
477
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800478bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800479 const State& c(getCurrentState());
480 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700481}
482
483void BufferStateLayer::setFilteringEnabled(bool enabled) {
484 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
485 mCurrentTransform, enabled);
486}
487
Alec Mouri39801c02018-10-10 10:44:47 -0700488status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700489 const State& s(getDrawingState());
490 auto& engine(mFlinger->getRenderEngine());
491
Alec Mourib5c4f352019-02-19 19:46:38 -0800492 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700493}
494
Alec Mouri56e538f2019-01-14 15:22:01 -0800495status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700496 const State& s(getDrawingState());
497
498 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800499 if (s.bgColorLayer) {
500 for (auto& handle : mDrawingState.callbackHandles) {
501 handle->latchTime = latchTime;
502 }
503 }
Marissa Wall61c58622018-07-18 10:12:20 -0700504 return NO_ERROR;
505 }
506
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700507 const int32_t layerID = getSequence();
508
Marissa Wall61c58622018-07-18 10:12:20 -0700509 // Reject if the layer is invalid
510 uint32_t bufferWidth = s.buffer->width;
511 uint32_t bufferHeight = s.buffer->height;
512
Peiyong Linefefaac2018-08-17 12:27:51 -0700513 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700514 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700515 }
516
517 if (s.transformToDisplayInverse) {
518 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700519 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700520 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700521 }
522 }
523
Vishnu Nair60356342018-11-13 13:00:45 -0800524 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700525 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
526 ALOGE("[%s] rejecting buffer: "
527 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
528 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800529 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700530 return BAD_VALUE;
531 }
532
Marissa Wall5a68a772018-12-22 17:43:42 -0800533 for (auto& handle : mDrawingState.callbackHandles) {
534 handle->latchTime = latchTime;
535 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700536
Alec Mouri56e538f2019-01-14 15:22:01 -0800537 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700538 // Bind the new buffer to the GL texture.
539 //
540 // Older devices require the "implicit" synchronization provided
541 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
542 // devices will either call this in Layer::onDraw, or (if it's not
543 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800544 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700545 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800546 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700547 return BAD_VALUE;
548 }
549 }
550
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800551 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800552 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700553
Marissa Wall16c112d2019-03-20 13:21:13 -0700554 mCurrentStateModified = false;
555
Marissa Wall61c58622018-07-18 10:12:20 -0700556 return NO_ERROR;
557}
558
559status_t BufferStateLayer::updateActiveBuffer() {
560 const State& s(getDrawingState());
561
562 if (s.buffer == nullptr) {
563 return BAD_VALUE;
564 }
565
Alec Mourib5c4f352019-02-19 19:46:38 -0800566 if (mActiveBuffer != nullptr) {
567 // todo: get this to work with BufferStateLayerCache
568 auto& engine(mFlinger->getRenderEngine());
569 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
570 }
Marissa Wall61c58622018-07-18 10:12:20 -0700571 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000572 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800573 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
574 layerCompositionState.buffer = mActiveBuffer;
575 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700576
577 return NO_ERROR;
578}
579
580status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
581 // TODO(marissaw): support frame history events
582 mCurrentFrameNumber = mFrameNumber;
583 return NO_ERROR;
584}
585
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800586void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
587 const auto outputLayer = findOutputLayerForDisplay(display);
588 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000589 auto& hwcInfo = *outputLayer->editState().hwc;
590 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700591
592 const State& s(getDrawingState());
593
Valerie Hau6f89c372019-03-02 00:13:33 +0000594 // obtain slot
Marissa Wall61c58622018-07-18 10:12:20 -0700595 uint32_t hwcSlot = 0;
Valerie Hau6f89c372019-03-02 00:13:33 +0000596 sp<GraphicBuffer> buffer;
597 hwcInfo.hwcBufferCache.getHwcBuffer(s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700598
Valerie Hau6f89c372019-03-02 00:13:33 +0000599 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700600 if (error != HWC2::Error::None) {
601 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
602 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
603 }
604
605 mFrameNumber++;
606}
607
608void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700609 BufferLayer::onFirstRef();
610
Marissa Wall61c58622018-07-18 10:12:20 -0700611 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
612 updateTransformHint(display);
613 }
614}
615
616} // namespace android