blob: 2d6da765dc2a54e1a5370348b19b0cd89f6f3bf3 [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
22#include "BufferStateLayer.h"
Valerie Haua72e2812019-01-23 13:40:39 -080023#include "ColorLayer.h"
Marissa Wall61c58622018-07-18 10:12:20 -070024
Yiwei Zhang7e666a52018-11-15 13:33:42 -080025#include "TimeStats/TimeStats.h"
26
Marissa Wall61c58622018-07-18 10:12:20 -070027#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070028#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070029
Valerie Hau0bc09152018-12-20 07:42:47 -080030#include <limits>
31
Marissa Wall61c58622018-07-18 10:12:20 -070032namespace android {
33
Lloyd Pique42ab75e2018-09-12 20:46:03 -070034// clang-format off
35const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
36 1, 0, 0, 0,
37 0, 1, 0, 0,
38 0, 0, 1, 0,
39 0, 0, 0, 1
40};
41// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070042
Vishnu Nair60356342018-11-13 13:00:45 -080043BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
44 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080045 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080046}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070047BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070048
49// -----------------------------------------------------------------------
50// Interface implementation for Layer
51// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070052void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080053 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
54 // buffer that was presented on this layer. The first transaction that came in this frame that
55 // replaced the previous buffer on this layer needs this release fence, because the fence will
56 // let the client know when that previous buffer is removed from the screen.
57 //
58 // Every other transaction on this layer does not need a release fence because no other
59 // Transactions that were set on this layer this frame are going to have their preceeding buffer
60 // removed from the display this frame.
61 //
62 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
63 // buffer so it doesn't need a previous release fence because the layer still needs the previous
64 // buffer. The second transaction contains a buffer so it needs a previous release fence because
65 // the previous buffer will be released this frame. The third transaction also contains a
66 // buffer. It replaces the buffer in the second transaction. The buffer in the second
67 // transaction will now no longer be presented so it is released immediately and the third
68 // transaction doesn't need a previous release fence.
69 for (auto& handle : mDrawingState.callbackHandles) {
70 if (handle->releasePreviousBuffer) {
71 handle->previousReleaseFence = releaseFence;
72 break;
73 }
74 }
Marissa Wall61c58622018-07-18 10:12:20 -070075}
76
77void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
78 // TODO(marissaw): send the transform hint to buffer owner
79 return;
80}
81
82void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080083 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
84 mDrawingState.callbackHandles);
85
86 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070087}
88
Ana Krulec010d2192018-10-08 06:29:54 -070089bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070090 if (getSidebandStreamChanged() || getAutoRefresh()) {
91 return true;
92 }
93
Marissa Wall024a1912018-08-13 13:55:35 -070094 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070095}
96
Marissa Walle2ffb422018-10-12 11:33:52 -070097bool BufferStateLayer::willPresentCurrentTransaction() const {
98 // Returns true if the most recent Transaction applied to CurrentState will be presented.
99 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800100 (mCurrentState.modified &&
101 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700102}
103
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800104bool BufferStateLayer::getTransformToDisplayInverse() const {
105 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108void BufferStateLayer::pushPendingState() {
109 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700110 return;
111 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800112 mPendingStates.push_back(mCurrentState);
113 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700114}
115
116bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 const bool stateUpdateAvailable = !mPendingStates.empty();
118 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700119 popPendingState(stateToCommit);
120 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
122 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700123 return stateUpdateAvailable;
124}
125
Marissa Wall861616d2018-10-22 12:52:23 -0700126// Crop that applies to the window
127Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
128 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700129}
130
131bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800132 if (mCurrentState.transform == transform) return false;
133 mCurrentState.sequence++;
134 mCurrentState.transform = transform;
135 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700136 setTransactionFlags(eTransactionNeeded);
137 return true;
138}
139
140bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
142 mCurrentState.sequence++;
143 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
144 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700145 setTransactionFlags(eTransactionNeeded);
146 return true;
147}
148
149bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 if (mCurrentState.crop == crop) return false;
151 mCurrentState.sequence++;
152 mCurrentState.crop = crop;
153 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700154 setTransactionFlags(eTransactionNeeded);
155 return true;
156}
157
Marissa Wall861616d2018-10-22 12:52:23 -0700158bool BufferStateLayer::setFrame(const Rect& frame) {
159 int x = frame.left;
160 int y = frame.top;
161 int w = frame.getWidth();
162 int h = frame.getHeight();
163
Marissa Wall0f3242d2018-12-20 15:10:22 -0800164 if (x < 0) {
165 x = 0;
166 w = frame.right;
167 }
168
169 if (y < 0) {
170 y = 0;
171 h = frame.bottom;
172 }
173
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800174 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
175 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700176 return false;
177 }
178
179 if (!frame.isValid()) {
180 x = y = w = h = 0;
181 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800182 mCurrentState.active.transform.set(x, y);
183 mCurrentState.active.w = w;
184 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700185
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800186 mCurrentState.sequence++;
187 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700188 setTransactionFlags(eTransactionNeeded);
189 return true;
190}
191
Marissa Wallfda30bb2018-10-12 11:34:28 -0700192bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800193 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700194 mReleasePreviousBuffer = true;
195 }
196
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800197 mCurrentState.sequence++;
198 mCurrentState.buffer = buffer;
199 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700200 setTransactionFlags(eTransactionNeeded);
201 return true;
202}
203
204bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700205 // The acquire fences of BufferStateLayers have already signaled before they are set
206 mCallbackHandleAcquireTime = fence->getSignalTime();
207
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800208 mCurrentState.acquireFence = fence;
209 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700210 setTransactionFlags(eTransactionNeeded);
211 return true;
212}
213
214bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800215 if (mCurrentState.dataspace == dataspace) return false;
216 mCurrentState.sequence++;
217 mCurrentState.dataspace = dataspace;
218 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700219 setTransactionFlags(eTransactionNeeded);
220 return true;
221}
222
223bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800224 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
225 mCurrentState.sequence++;
226 mCurrentState.hdrMetadata = hdrMetadata;
227 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700228 setTransactionFlags(eTransactionNeeded);
229 return true;
230}
231
232bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800233 mCurrentState.sequence++;
234 mCurrentState.surfaceDamageRegion = surfaceDamage;
235 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700236 setTransactionFlags(eTransactionNeeded);
237 return true;
238}
239
240bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800241 if (mCurrentState.api == api) return false;
242 mCurrentState.sequence++;
243 mCurrentState.api = api;
244 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700245 setTransactionFlags(eTransactionNeeded);
246 return true;
247}
248
249bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800250 if (mCurrentState.sidebandStream == sidebandStream) return false;
251 mCurrentState.sequence++;
252 mCurrentState.sidebandStream = sidebandStream;
253 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700254 setTransactionFlags(eTransactionNeeded);
255
256 if (!mSidebandStreamChanged.exchange(true)) {
257 // mSidebandStreamChanged was false
258 mFlinger->signalLayerUpdate();
259 }
260 return true;
261}
262
Marissa Walle2ffb422018-10-12 11:33:52 -0700263bool BufferStateLayer::setTransactionCompletedListeners(
264 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700265 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700266 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700267 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700268 return false;
269 }
270
271 const bool willPresent = willPresentCurrentTransaction();
272
273 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700274 // If this transaction set a buffer on this layer, release its previous buffer
275 handle->releasePreviousBuffer = mReleasePreviousBuffer;
276
Marissa Walle2ffb422018-10-12 11:33:52 -0700277 // If this layer will be presented in this frame
278 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700279 // If this transaction set an acquire fence on this layer, set its acquire time
280 handle->acquireTime = mCallbackHandleAcquireTime;
281
Marissa Walle2ffb422018-10-12 11:33:52 -0700282 // Notify the transaction completed thread that there is a pending latched callback
283 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800284 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700285
286 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800287 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700288
289 } else { // If this layer will NOT need to be relatched and presented this frame
290 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800291 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700292 }
293 }
294
Marissa Wallfda30bb2018-10-12 11:34:28 -0700295 mReleasePreviousBuffer = false;
296 mCallbackHandleAcquireTime = -1;
297
Marissa Walle2ffb422018-10-12 11:33:52 -0700298 return willPresent;
299}
300
Marissa Wall61c58622018-07-18 10:12:20 -0700301bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 mCurrentState.transparentRegionHint = transparent;
303 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700304 setTransactionFlags(eTransactionNeeded);
305 return true;
306}
307
Marissa Wall861616d2018-10-22 12:52:23 -0700308Rect BufferStateLayer::getBufferSize(const State& s) const {
309 // for buffer state layers we use the display frame size as the buffer size.
310 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
311 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700312 }
313
Marissa Wall861616d2018-10-22 12:52:23 -0700314 // if the display frame is not defined, use the parent bounds as the buffer size.
315 const auto& p = mDrawingParent.promote();
316 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800317 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700318 if (!parentBounds.isEmpty()) {
319 return parentBounds;
320 }
321 }
322
323 // if there is no parent layer, use the buffer's bounds as the buffer size
324 if (s.buffer) {
325 return s.buffer->getBounds();
326 }
327 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700328}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800329
330FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
331 const State& s(getDrawingState());
332 // for buffer state layers we use the display frame size as the buffer size.
333 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
334 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
335 }
336
337 // if the display frame is not defined, use the parent bounds as the buffer size.
338 return parentBounds;
339}
340
Marissa Wall61c58622018-07-18 10:12:20 -0700341// -----------------------------------------------------------------------
342
343// -----------------------------------------------------------------------
344// Interface implementation for BufferLayer
345// -----------------------------------------------------------------------
346bool BufferStateLayer::fenceHasSignaled() const {
347 if (latchUnsignaledBuffers()) {
348 return true;
349 }
350
351 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
352}
353
354nsecs_t BufferStateLayer::getDesiredPresentTime() {
355 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
356 return 0;
357}
358
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800359std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700360 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
361}
362
363void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
364 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
365}
366
367uint32_t BufferStateLayer::getDrawingTransform() const {
368 return getDrawingState().transform;
369}
370
371ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
372 return getDrawingState().dataspace;
373}
374
Marissa Wall861616d2018-10-22 12:52:23 -0700375// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700376Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700377 const State& s(getDrawingState());
378
379 if (s.crop.isEmpty() && s.buffer) {
380 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800381 } else if (s.buffer) {
382 Rect crop = s.crop;
383 crop.left = std::max(crop.left, 0);
384 crop.top = std::max(crop.top, 0);
385 uint32_t bufferWidth = s.buffer->getWidth();
386 uint32_t bufferHeight = s.buffer->getHeight();
387 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
388 bufferWidth <= std::numeric_limits<int32_t>::max()) {
389 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
390 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
391 }
392 if (!crop.isValid()) {
393 // Crop rect is out of bounds, return whole buffer
394 return s.buffer->getBounds();
395 }
396 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700397 }
398 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700399}
400
401uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700402 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700403}
404
405Region BufferStateLayer::getDrawingSurfaceDamage() const {
406 return getDrawingState().surfaceDamageRegion;
407}
408
409const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
410 return getDrawingState().hdrMetadata;
411}
412
413int BufferStateLayer::getDrawingApi() const {
414 return getDrawingState().api;
415}
416
417PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800418 if (!mActiveBuffer) {
419 return PIXEL_FORMAT_NONE;
420 }
Marissa Wall61c58622018-07-18 10:12:20 -0700421 return mActiveBuffer->format;
422}
423
424uint64_t BufferStateLayer::getFrameNumber() const {
425 return mFrameNumber;
426}
427
428bool BufferStateLayer::getAutoRefresh() const {
429 // TODO(marissaw): support shared buffer mode
430 return false;
431}
432
433bool BufferStateLayer::getSidebandStreamChanged() const {
434 return mSidebandStreamChanged.load();
435}
436
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800437bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700438 if (mSidebandStreamChanged.exchange(false)) {
439 const State& s(getDrawingState());
440 // mSidebandStreamChanged was true
441 // replicated in LayerBE until FE/BE is ready to be synchronized
442 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
443 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
444 setTransactionFlags(eTransactionNeeded);
445 mFlinger->setTransactionFlags(eTraversalNeeded);
446 }
447 recomputeVisibleRegions = true;
448
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800449 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700450 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800451 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700452}
453
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800454bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800455 const State& c(getCurrentState());
456 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700457}
458
459void BufferStateLayer::setFilteringEnabled(bool enabled) {
460 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
461 mCurrentTransform, enabled);
462}
463
Alec Mouri39801c02018-10-10 10:44:47 -0700464status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700465 const State& s(getDrawingState());
466 auto& engine(mFlinger->getRenderEngine());
467
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000468 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700469}
470
Alec Mouri86770e52018-09-24 22:40:58 +0000471status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
472 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700473 const State& s(getDrawingState());
474
475 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800476 if (s.bgColorLayer) {
477 for (auto& handle : mDrawingState.callbackHandles) {
478 handle->latchTime = latchTime;
479 }
480 }
Marissa Wall61c58622018-07-18 10:12:20 -0700481 return NO_ERROR;
482 }
483
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700484 const int32_t layerID = getSequence();
485
Marissa Wall61c58622018-07-18 10:12:20 -0700486 // Reject if the layer is invalid
487 uint32_t bufferWidth = s.buffer->width;
488 uint32_t bufferHeight = s.buffer->height;
489
Peiyong Linefefaac2018-08-17 12:27:51 -0700490 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700491 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700492 }
493
494 if (s.transformToDisplayInverse) {
495 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700496 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700497 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700498 }
499 }
500
Vishnu Nair60356342018-11-13 13:00:45 -0800501 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700502 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
503 ALOGE("[%s] rejecting buffer: "
504 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
505 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800506 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700507 return BAD_VALUE;
508 }
509
Marissa Wall5a68a772018-12-22 17:43:42 -0800510 for (auto& handle : mDrawingState.callbackHandles) {
511 handle->latchTime = latchTime;
512 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700513
Marissa Wall61c58622018-07-18 10:12:20 -0700514 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000515 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
516 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
517 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800518 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700519 return UNKNOWN_ERROR;
520 }
521
Marissa Wall61c58622018-07-18 10:12:20 -0700522 // Check status of fences first because merging is expensive.
523 // Merging an invalid fence with any other fence results in an
524 // invalid fence.
525 auto currentStatus = s.acquireFence->getStatus();
526 if (currentStatus == Fence::Status::Invalid) {
527 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800528 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700529 return BAD_VALUE;
530 }
531
Alec Mouri86770e52018-09-24 22:40:58 +0000532 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700533 if (incomingStatus == Fence::Status::Invalid) {
534 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800535 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800536 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700537 return BAD_VALUE;
538 }
539
540 // If both fences are signaled or both are unsignaled, we need to merge
541 // them to get an accurate timestamp.
542 if (currentStatus == incomingStatus) {
543 char fenceName[32] = {};
544 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000545 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800546 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700547 if (!mergedFence.get()) {
548 ALOGE("failed to merge release fences");
549 // synchronization is broken, the best we can do is hope fences
550 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800551 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800552 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700553 return BAD_VALUE;
554 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800555 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700556 } else if (incomingStatus == Fence::Status::Unsignaled) {
557 // If one fence has signaled and the other hasn't, the unsignaled
558 // fence will approximately correspond with the correct timestamp.
559 // There's a small race if both fences signal at about the same time
560 // and their statuses are retrieved with unfortunate timing. However,
561 // by this point, they will have both signaled and only the timestamp
562 // will be slightly off; any dependencies after this point will
563 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800564 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700565 }
566 } else {
567 // Bind the new buffer to the GL texture.
568 //
569 // Older devices require the "implicit" synchronization provided
570 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
571 // devices will either call this in Layer::onDraw, or (if it's not
572 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800573 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700574 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800575 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700576 return BAD_VALUE;
577 }
578 }
579
580 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800581 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800582 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800583 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700584
585 return NO_ERROR;
586}
587
588status_t BufferStateLayer::updateActiveBuffer() {
589 const State& s(getDrawingState());
590
591 if (s.buffer == nullptr) {
592 return BAD_VALUE;
593 }
594
595 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000596 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700597 getBE().compositionInfo.mBuffer = mActiveBuffer;
598 getBE().compositionInfo.mBufferSlot = 0;
599
600 return NO_ERROR;
601}
602
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000603bool BufferStateLayer::useCachedBufferForClientComposition() const {
604 // TODO: Store a proper staleness bit to support EGLImage caching.
605 return false;
606}
607
Marissa Wall61c58622018-07-18 10:12:20 -0700608status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
609 // TODO(marissaw): support frame history events
610 mCurrentFrameNumber = mFrameNumber;
611 return NO_ERROR;
612}
613
Dominik Laskowski075d3172018-05-24 15:50:06 -0700614void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700615 auto& hwcInfo = getBE().mHwcLayers[displayId];
616 auto& hwcLayer = hwcInfo.layer;
617
618 const State& s(getDrawingState());
619
620 // TODO(marissaw): support more than one slot
621 uint32_t hwcSlot = 0;
622
623 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
624 if (error != HWC2::Error::None) {
625 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
626 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
627 }
628
Marissa Wall024a1912018-08-13 13:55:35 -0700629 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700630 mFrameNumber++;
631}
632
633void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700634 BufferLayer::onFirstRef();
635
Marissa Wall61c58622018-07-18 10:12:20 -0700636 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
637 updateTransformHint(display);
638 }
639}
640
641} // namespace android