blob: 45e3b6a991850db2d57b9eafd002f8d22b34fef3 [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;
45}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070046BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070047
48// -----------------------------------------------------------------------
49// Interface implementation for Layer
50// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070051void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080052 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
53 // buffer that was presented on this layer. The first transaction that came in this frame that
54 // replaced the previous buffer on this layer needs this release fence, because the fence will
55 // let the client know when that previous buffer is removed from the screen.
56 //
57 // Every other transaction on this layer does not need a release fence because no other
58 // Transactions that were set on this layer this frame are going to have their preceeding buffer
59 // removed from the display this frame.
60 //
61 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
62 // buffer so it doesn't need a previous release fence because the layer still needs the previous
63 // buffer. The second transaction contains a buffer so it needs a previous release fence because
64 // the previous buffer will be released this frame. The third transaction also contains a
65 // buffer. It replaces the buffer in the second transaction. The buffer in the second
66 // transaction will now no longer be presented so it is released immediately and the third
67 // transaction doesn't need a previous release fence.
68 for (auto& handle : mDrawingState.callbackHandles) {
69 if (handle->releasePreviousBuffer) {
70 handle->previousReleaseFence = releaseFence;
71 break;
72 }
73 }
Marissa Wall61c58622018-07-18 10:12:20 -070074}
75
76void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
77 // TODO(marissaw): send the transform hint to buffer owner
78 return;
79}
80
81void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080082 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
83 mDrawingState.callbackHandles);
84
85 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070086}
87
Ana Krulec010d2192018-10-08 06:29:54 -070088bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070089 if (getSidebandStreamChanged() || getAutoRefresh()) {
90 return true;
91 }
92
Marissa Wall024a1912018-08-13 13:55:35 -070093 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070094}
95
Marissa Walle2ffb422018-10-12 11:33:52 -070096bool BufferStateLayer::willPresentCurrentTransaction() const {
97 // Returns true if the most recent Transaction applied to CurrentState will be presented.
98 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -080099 (mCurrentState.modified &&
100 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700101}
102
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800103bool BufferStateLayer::getTransformToDisplayInverse() const {
104 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700105}
106
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107void BufferStateLayer::pushPendingState() {
108 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700109 return;
110 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mPendingStates.push_back(mCurrentState);
112 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700113}
114
115bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 const bool stateUpdateAvailable = !mPendingStates.empty();
117 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700118 popPendingState(stateToCommit);
119 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
121 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700122 return stateUpdateAvailable;
123}
124
Marissa Wall861616d2018-10-22 12:52:23 -0700125// Crop that applies to the window
126Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
127 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700128}
129
130bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800131 if (mCurrentState.transform == transform) return false;
132 mCurrentState.sequence++;
133 mCurrentState.transform = transform;
134 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700135 setTransactionFlags(eTransactionNeeded);
136 return true;
137}
138
139bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
141 mCurrentState.sequence++;
142 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
143 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700144 setTransactionFlags(eTransactionNeeded);
145 return true;
146}
147
148bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800149 if (mCurrentState.crop == crop) return false;
150 mCurrentState.sequence++;
151 mCurrentState.crop = crop;
152 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700153 setTransactionFlags(eTransactionNeeded);
154 return true;
155}
156
Marissa Wall861616d2018-10-22 12:52:23 -0700157bool BufferStateLayer::setFrame(const Rect& frame) {
158 int x = frame.left;
159 int y = frame.top;
160 int w = frame.getWidth();
161 int h = frame.getHeight();
162
Marissa Wall0f3242d2018-12-20 15:10:22 -0800163 if (x < 0) {
164 x = 0;
165 w = frame.right;
166 }
167
168 if (y < 0) {
169 y = 0;
170 h = frame.bottom;
171 }
172
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800173 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
174 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700175 return false;
176 }
177
178 if (!frame.isValid()) {
179 x = y = w = h = 0;
180 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800181 mCurrentState.active.transform.set(x, y);
182 mCurrentState.active.w = w;
183 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700184
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800185 mCurrentState.sequence++;
186 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700187 setTransactionFlags(eTransactionNeeded);
188 return true;
189}
190
Marissa Wallfda30bb2018-10-12 11:34:28 -0700191bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800192 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700193 mReleasePreviousBuffer = true;
194 }
195
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800196 mCurrentState.sequence++;
197 mCurrentState.buffer = buffer;
198 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700199 setTransactionFlags(eTransactionNeeded);
200 return true;
201}
202
203bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700204 // The acquire fences of BufferStateLayers have already signaled before they are set
205 mCallbackHandleAcquireTime = fence->getSignalTime();
206
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800207 mCurrentState.acquireFence = fence;
208 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700209 setTransactionFlags(eTransactionNeeded);
210 return true;
211}
212
213bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800214 if (mCurrentState.dataspace == dataspace) return false;
215 mCurrentState.sequence++;
216 mCurrentState.dataspace = dataspace;
217 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700218 setTransactionFlags(eTransactionNeeded);
219 return true;
220}
221
222bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800223 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
224 mCurrentState.sequence++;
225 mCurrentState.hdrMetadata = hdrMetadata;
226 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700227 setTransactionFlags(eTransactionNeeded);
228 return true;
229}
230
231bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800232 mCurrentState.sequence++;
233 mCurrentState.surfaceDamageRegion = surfaceDamage;
234 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700235 setTransactionFlags(eTransactionNeeded);
236 return true;
237}
238
239bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 if (mCurrentState.api == api) return false;
241 mCurrentState.sequence++;
242 mCurrentState.api = api;
243 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700244 setTransactionFlags(eTransactionNeeded);
245 return true;
246}
247
248bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 if (mCurrentState.sidebandStream == sidebandStream) return false;
250 mCurrentState.sequence++;
251 mCurrentState.sidebandStream = sidebandStream;
252 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700253 setTransactionFlags(eTransactionNeeded);
254
255 if (!mSidebandStreamChanged.exchange(true)) {
256 // mSidebandStreamChanged was false
257 mFlinger->signalLayerUpdate();
258 }
259 return true;
260}
261
Marissa Walle2ffb422018-10-12 11:33:52 -0700262bool BufferStateLayer::setTransactionCompletedListeners(
263 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700264 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700265 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700266 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700267 return false;
268 }
269
270 const bool willPresent = willPresentCurrentTransaction();
271
272 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700273 // If this transaction set a buffer on this layer, release its previous buffer
274 handle->releasePreviousBuffer = mReleasePreviousBuffer;
275
Marissa Walle2ffb422018-10-12 11:33:52 -0700276 // If this layer will be presented in this frame
277 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700278 // If this transaction set an acquire fence on this layer, set its acquire time
279 handle->acquireTime = mCallbackHandleAcquireTime;
280
Marissa Walle2ffb422018-10-12 11:33:52 -0700281 // Notify the transaction completed thread that there is a pending latched callback
282 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800283 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700284
285 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700287
288 } else { // If this layer will NOT need to be relatched and presented this frame
289 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800290 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700291 }
292 }
293
Marissa Wallfda30bb2018-10-12 11:34:28 -0700294 mReleasePreviousBuffer = false;
295 mCallbackHandleAcquireTime = -1;
296
Marissa Walle2ffb422018-10-12 11:33:52 -0700297 return willPresent;
298}
299
Marissa Wall61c58622018-07-18 10:12:20 -0700300bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 mCurrentState.transparentRegionHint = transparent;
302 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700303 setTransactionFlags(eTransactionNeeded);
304 return true;
305}
306
Marissa Wall861616d2018-10-22 12:52:23 -0700307Rect BufferStateLayer::getBufferSize(const State& s) const {
308 // for buffer state layers we use the display frame size as the buffer size.
309 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
310 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700311 }
312
Marissa Wall861616d2018-10-22 12:52:23 -0700313 // if the display frame is not defined, use the parent bounds as the buffer size.
314 const auto& p = mDrawingParent.promote();
315 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800316 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700317 if (!parentBounds.isEmpty()) {
318 return parentBounds;
319 }
320 }
321
322 // if there is no parent layer, use the buffer's bounds as the buffer size
323 if (s.buffer) {
324 return s.buffer->getBounds();
325 }
326 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700327}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328
329FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
330 const State& s(getDrawingState());
331 // for buffer state layers we use the display frame size as the buffer size.
332 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
333 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
334 }
335
336 // if the display frame is not defined, use the parent bounds as the buffer size.
337 return parentBounds;
338}
339
Marissa Wall61c58622018-07-18 10:12:20 -0700340// -----------------------------------------------------------------------
341
342// -----------------------------------------------------------------------
343// Interface implementation for BufferLayer
344// -----------------------------------------------------------------------
345bool BufferStateLayer::fenceHasSignaled() const {
346 if (latchUnsignaledBuffers()) {
347 return true;
348 }
349
350 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
351}
352
353nsecs_t BufferStateLayer::getDesiredPresentTime() {
354 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
355 return 0;
356}
357
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800358std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700359 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
360}
361
362void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
363 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
364}
365
366uint32_t BufferStateLayer::getDrawingTransform() const {
367 return getDrawingState().transform;
368}
369
370ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
371 return getDrawingState().dataspace;
372}
373
Marissa Wall861616d2018-10-22 12:52:23 -0700374// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700375Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700376 const State& s(getDrawingState());
377
378 if (s.crop.isEmpty() && s.buffer) {
379 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800380 } else if (s.buffer) {
381 Rect crop = s.crop;
382 crop.left = std::max(crop.left, 0);
383 crop.top = std::max(crop.top, 0);
384 uint32_t bufferWidth = s.buffer->getWidth();
385 uint32_t bufferHeight = s.buffer->getHeight();
386 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
387 bufferWidth <= std::numeric_limits<int32_t>::max()) {
388 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
389 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
390 }
391 if (!crop.isValid()) {
392 // Crop rect is out of bounds, return whole buffer
393 return s.buffer->getBounds();
394 }
395 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700396 }
397 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700398}
399
400uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700401 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700402}
403
404Region BufferStateLayer::getDrawingSurfaceDamage() const {
405 return getDrawingState().surfaceDamageRegion;
406}
407
408const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
409 return getDrawingState().hdrMetadata;
410}
411
412int BufferStateLayer::getDrawingApi() const {
413 return getDrawingState().api;
414}
415
416PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800417 if (!mActiveBuffer) {
418 return PIXEL_FORMAT_NONE;
419 }
Marissa Wall61c58622018-07-18 10:12:20 -0700420 return mActiveBuffer->format;
421}
422
423uint64_t BufferStateLayer::getFrameNumber() const {
424 return mFrameNumber;
425}
426
427bool BufferStateLayer::getAutoRefresh() const {
428 // TODO(marissaw): support shared buffer mode
429 return false;
430}
431
432bool BufferStateLayer::getSidebandStreamChanged() const {
433 return mSidebandStreamChanged.load();
434}
435
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800436bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700437 if (mSidebandStreamChanged.exchange(false)) {
438 const State& s(getDrawingState());
439 // mSidebandStreamChanged was true
440 // replicated in LayerBE until FE/BE is ready to be synchronized
441 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
442 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
443 setTransactionFlags(eTransactionNeeded);
444 mFlinger->setTransactionFlags(eTraversalNeeded);
445 }
446 recomputeVisibleRegions = true;
447
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800448 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700449 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800450 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700451}
452
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800453bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800454 const State& c(getCurrentState());
455 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700456}
457
458void BufferStateLayer::setFilteringEnabled(bool enabled) {
459 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
460 mCurrentTransform, enabled);
461}
462
Alec Mouri39801c02018-10-10 10:44:47 -0700463status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700464 const State& s(getDrawingState());
465 auto& engine(mFlinger->getRenderEngine());
466
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000467 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700468}
469
Alec Mouri86770e52018-09-24 22:40:58 +0000470status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
471 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700472 const State& s(getDrawingState());
473
474 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800475 if (s.bgColorLayer) {
476 for (auto& handle : mDrawingState.callbackHandles) {
477 handle->latchTime = latchTime;
478 }
479 }
Marissa Wall61c58622018-07-18 10:12:20 -0700480 return NO_ERROR;
481 }
482
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700483 const int32_t layerID = getSequence();
484
Marissa Wall61c58622018-07-18 10:12:20 -0700485 // Reject if the layer is invalid
486 uint32_t bufferWidth = s.buffer->width;
487 uint32_t bufferHeight = s.buffer->height;
488
Peiyong Linefefaac2018-08-17 12:27:51 -0700489 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700490 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700491 }
492
493 if (s.transformToDisplayInverse) {
494 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700495 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700496 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700497 }
498 }
499
Vishnu Nair60356342018-11-13 13:00:45 -0800500 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700501 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
502 ALOGE("[%s] rejecting buffer: "
503 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
504 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800505 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700506 return BAD_VALUE;
507 }
508
Marissa Wall5a68a772018-12-22 17:43:42 -0800509 for (auto& handle : mDrawingState.callbackHandles) {
510 handle->latchTime = latchTime;
511 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700512
Marissa Wall61c58622018-07-18 10:12:20 -0700513 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000514 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
515 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
516 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800517 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700518 return UNKNOWN_ERROR;
519 }
520
Marissa Wall61c58622018-07-18 10:12:20 -0700521 // Check status of fences first because merging is expensive.
522 // Merging an invalid fence with any other fence results in an
523 // invalid fence.
524 auto currentStatus = s.acquireFence->getStatus();
525 if (currentStatus == Fence::Status::Invalid) {
526 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800527 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700528 return BAD_VALUE;
529 }
530
Alec Mouri86770e52018-09-24 22:40:58 +0000531 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700532 if (incomingStatus == Fence::Status::Invalid) {
533 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800534 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800535 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700536 return BAD_VALUE;
537 }
538
539 // If both fences are signaled or both are unsignaled, we need to merge
540 // them to get an accurate timestamp.
541 if (currentStatus == incomingStatus) {
542 char fenceName[32] = {};
543 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000544 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800545 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700546 if (!mergedFence.get()) {
547 ALOGE("failed to merge release fences");
548 // synchronization is broken, the best we can do is hope fences
549 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800550 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800551 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700552 return BAD_VALUE;
553 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800554 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700555 } else if (incomingStatus == Fence::Status::Unsignaled) {
556 // If one fence has signaled and the other hasn't, the unsignaled
557 // fence will approximately correspond with the correct timestamp.
558 // There's a small race if both fences signal at about the same time
559 // and their statuses are retrieved with unfortunate timing. However,
560 // by this point, they will have both signaled and only the timestamp
561 // will be slightly off; any dependencies after this point will
562 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800563 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700564 }
565 } else {
566 // Bind the new buffer to the GL texture.
567 //
568 // Older devices require the "implicit" synchronization provided
569 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
570 // devices will either call this in Layer::onDraw, or (if it's not
571 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800572 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700573 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800574 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700575 return BAD_VALUE;
576 }
577 }
578
579 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800580 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800581 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800582 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700583
584 return NO_ERROR;
585}
586
587status_t BufferStateLayer::updateActiveBuffer() {
588 const State& s(getDrawingState());
589
590 if (s.buffer == nullptr) {
591 return BAD_VALUE;
592 }
593
594 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000595 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700596 getBE().compositionInfo.mBuffer = mActiveBuffer;
597 getBE().compositionInfo.mBufferSlot = 0;
598
599 return NO_ERROR;
600}
601
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000602bool BufferStateLayer::useCachedBufferForClientComposition() const {
603 // TODO: Store a proper staleness bit to support EGLImage caching.
604 return false;
605}
606
Marissa Wall61c58622018-07-18 10:12:20 -0700607status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
608 // TODO(marissaw): support frame history events
609 mCurrentFrameNumber = mFrameNumber;
610 return NO_ERROR;
611}
612
Dominik Laskowski075d3172018-05-24 15:50:06 -0700613void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700614 auto& hwcInfo = getBE().mHwcLayers[displayId];
615 auto& hwcLayer = hwcInfo.layer;
616
617 const State& s(getDrawingState());
618
619 // TODO(marissaw): support more than one slot
620 uint32_t hwcSlot = 0;
621
622 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
623 if (error != HWC2::Error::None) {
624 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
625 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
626 }
627
Marissa Wall024a1912018-08-13 13:55:35 -0700628 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700629 mFrameNumber++;
630}
631
632void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700633 BufferLayer::onFirstRef();
634
Marissa Wall61c58622018-07-18 10:12:20 -0700635 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
636 updateTransformHint(display);
637 }
638}
639
640} // namespace android