blob: c0997a00d5fb6a6a36ca60562faaac818e5767d4 [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) {
316 Rect parentBounds = Rect(p->computeBounds(Region()));
317 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}
328// -----------------------------------------------------------------------
329
330// -----------------------------------------------------------------------
331// Interface implementation for BufferLayer
332// -----------------------------------------------------------------------
333bool BufferStateLayer::fenceHasSignaled() const {
334 if (latchUnsignaledBuffers()) {
335 return true;
336 }
337
338 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
339}
340
341nsecs_t BufferStateLayer::getDesiredPresentTime() {
342 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
343 return 0;
344}
345
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800346std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700347 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
348}
349
350void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
351 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
352}
353
354uint32_t BufferStateLayer::getDrawingTransform() const {
355 return getDrawingState().transform;
356}
357
358ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
359 return getDrawingState().dataspace;
360}
361
Marissa Wall861616d2018-10-22 12:52:23 -0700362// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700363Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700364 const State& s(getDrawingState());
365
366 if (s.crop.isEmpty() && s.buffer) {
367 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800368 } else if (s.buffer) {
369 Rect crop = s.crop;
370 crop.left = std::max(crop.left, 0);
371 crop.top = std::max(crop.top, 0);
372 uint32_t bufferWidth = s.buffer->getWidth();
373 uint32_t bufferHeight = s.buffer->getHeight();
374 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
375 bufferWidth <= std::numeric_limits<int32_t>::max()) {
376 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
377 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
378 }
379 if (!crop.isValid()) {
380 // Crop rect is out of bounds, return whole buffer
381 return s.buffer->getBounds();
382 }
383 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700384 }
385 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700386}
387
388uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700389 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700390}
391
392Region BufferStateLayer::getDrawingSurfaceDamage() const {
393 return getDrawingState().surfaceDamageRegion;
394}
395
396const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
397 return getDrawingState().hdrMetadata;
398}
399
400int BufferStateLayer::getDrawingApi() const {
401 return getDrawingState().api;
402}
403
404PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800405 if (!mActiveBuffer) {
406 return PIXEL_FORMAT_NONE;
407 }
Marissa Wall61c58622018-07-18 10:12:20 -0700408 return mActiveBuffer->format;
409}
410
411uint64_t BufferStateLayer::getFrameNumber() const {
412 return mFrameNumber;
413}
414
415bool BufferStateLayer::getAutoRefresh() const {
416 // TODO(marissaw): support shared buffer mode
417 return false;
418}
419
420bool BufferStateLayer::getSidebandStreamChanged() const {
421 return mSidebandStreamChanged.load();
422}
423
424std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
425 if (mSidebandStreamChanged.exchange(false)) {
426 const State& s(getDrawingState());
427 // mSidebandStreamChanged was true
428 // replicated in LayerBE until FE/BE is ready to be synchronized
429 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
430 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
431 setTransactionFlags(eTransactionNeeded);
432 mFlinger->setTransactionFlags(eTraversalNeeded);
433 }
434 recomputeVisibleRegions = true;
435
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800436 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700437 }
438 return {};
439}
440
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800441bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800442 const State& c(getCurrentState());
443 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700444}
445
446void BufferStateLayer::setFilteringEnabled(bool enabled) {
447 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
448 mCurrentTransform, enabled);
449}
450
Alec Mouri39801c02018-10-10 10:44:47 -0700451status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700452 const State& s(getDrawingState());
453 auto& engine(mFlinger->getRenderEngine());
454
Alec Mouri79108df2019-02-04 19:33:44 +0000455 engine.checkErrors();
456
457 // TODO(marissaw): once buffers are cached, don't create a new image everytime
458 mTextureImage = engine.createImage();
459
460 bool created =
461 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
462 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
463 if (!created) {
464 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
465 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
466 s.buffer->getUsage(), s.buffer->getPixelFormat());
467 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
468 return NO_INIT;
469 }
470
471 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
472
473 // Wait for the new buffer to be ready.
474 if (s.acquireFence->isValid()) {
475 if (SyncFeatures::getInstance().useWaitSync()) {
476 base::unique_fd fenceFd(s.acquireFence->dup());
477 if (fenceFd == -1) {
478 ALOGE("error dup'ing fence fd: %d", errno);
479 return -errno;
480 }
481 if (!engine.waitFence(std::move(fenceFd))) {
482 ALOGE("failed to wait on fence fd");
483 return UNKNOWN_ERROR;
484 }
485 } else {
486 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
487 if (err != NO_ERROR) {
488 ALOGE("error waiting for fence: %d", err);
489 return err;
490 }
491 }
492 }
493
494 return NO_ERROR;
Marissa Wall61c58622018-07-18 10:12:20 -0700495}
496
Alec Mouri86770e52018-09-24 22:40:58 +0000497status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
498 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700499 const State& s(getDrawingState());
500
501 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800502 if (s.bgColorLayer) {
503 for (auto& handle : mDrawingState.callbackHandles) {
504 handle->latchTime = latchTime;
505 }
506 }
Marissa Wall61c58622018-07-18 10:12:20 -0700507 return NO_ERROR;
508 }
509
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700510 const int32_t layerID = getSequence();
511
Marissa Wall61c58622018-07-18 10:12:20 -0700512 // Reject if the layer is invalid
513 uint32_t bufferWidth = s.buffer->width;
514 uint32_t bufferHeight = s.buffer->height;
515
Peiyong Linefefaac2018-08-17 12:27:51 -0700516 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700517 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700518 }
519
520 if (s.transformToDisplayInverse) {
521 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700522 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700523 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700524 }
525 }
526
Vishnu Nair60356342018-11-13 13:00:45 -0800527 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700528 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
529 ALOGE("[%s] rejecting buffer: "
530 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
531 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800532 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700533 return BAD_VALUE;
534 }
535
Marissa Wall5a68a772018-12-22 17:43:42 -0800536 for (auto& handle : mDrawingState.callbackHandles) {
537 handle->latchTime = latchTime;
538 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700539
Marissa Wall61c58622018-07-18 10:12:20 -0700540 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000541 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
542 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
543 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800544 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700545 return UNKNOWN_ERROR;
546 }
547
Marissa Wall61c58622018-07-18 10:12:20 -0700548 // Check status of fences first because merging is expensive.
549 // Merging an invalid fence with any other fence results in an
550 // invalid fence.
551 auto currentStatus = s.acquireFence->getStatus();
552 if (currentStatus == Fence::Status::Invalid) {
553 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800554 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 return BAD_VALUE;
556 }
557
Alec Mouri86770e52018-09-24 22:40:58 +0000558 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700559 if (incomingStatus == Fence::Status::Invalid) {
560 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800561 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800562 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700563 return BAD_VALUE;
564 }
565
566 // If both fences are signaled or both are unsignaled, we need to merge
567 // them to get an accurate timestamp.
568 if (currentStatus == incomingStatus) {
569 char fenceName[32] = {};
570 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000571 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800572 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700573 if (!mergedFence.get()) {
574 ALOGE("failed to merge release fences");
575 // synchronization is broken, the best we can do is hope fences
576 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800577 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800578 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700579 return BAD_VALUE;
580 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800581 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700582 } else if (incomingStatus == Fence::Status::Unsignaled) {
583 // If one fence has signaled and the other hasn't, the unsignaled
584 // fence will approximately correspond with the correct timestamp.
585 // There's a small race if both fences signal at about the same time
586 // and their statuses are retrieved with unfortunate timing. However,
587 // by this point, they will have both signaled and only the timestamp
588 // will be slightly off; any dependencies after this point will
589 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800590 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700591 }
592 } else {
593 // Bind the new buffer to the GL texture.
594 //
595 // Older devices require the "implicit" synchronization provided
596 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
597 // devices will either call this in Layer::onDraw, or (if it's not
598 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800599 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700600 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800601 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700602 return BAD_VALUE;
603 }
604 }
605
606 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800607 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800608 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800609 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700610
611 return NO_ERROR;
612}
613
614status_t BufferStateLayer::updateActiveBuffer() {
615 const State& s(getDrawingState());
616
617 if (s.buffer == nullptr) {
618 return BAD_VALUE;
619 }
620
621 mActiveBuffer = s.buffer;
622 getBE().compositionInfo.mBuffer = mActiveBuffer;
623 getBE().compositionInfo.mBufferSlot = 0;
624
625 return NO_ERROR;
626}
627
628status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
629 // TODO(marissaw): support frame history events
630 mCurrentFrameNumber = mFrameNumber;
631 return NO_ERROR;
632}
633
Dominik Laskowski075d3172018-05-24 15:50:06 -0700634void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700635 auto& hwcInfo = getBE().mHwcLayers[displayId];
636 auto& hwcLayer = hwcInfo.layer;
637
638 const State& s(getDrawingState());
639
640 // TODO(marissaw): support more than one slot
641 uint32_t hwcSlot = 0;
642
643 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
644 if (error != HWC2::Error::None) {
645 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
646 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
647 }
648
Marissa Wall024a1912018-08-13 13:55:35 -0700649 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700650 mFrameNumber++;
651}
652
653void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700654 BufferLayer::onFirstRef();
655
Marissa Wall61c58622018-07-18 10:12:20 -0700656 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
657 updateTransformHint(display);
658 }
659}
660
661} // namespace android