blob: 078300449cbbbaa58559dc65d737425eae081b92 [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() ||
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 (mCurrentState.modified && mCurrentState.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700100}
101
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800102bool BufferStateLayer::getTransformToDisplayInverse() const {
103 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800106void BufferStateLayer::pushPendingState() {
107 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700108 return;
109 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mPendingStates.push_back(mCurrentState);
111 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700112}
113
114bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115 const bool stateUpdateAvailable = !mPendingStates.empty();
116 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700117 popPendingState(stateToCommit);
118 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
120 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700121 return stateUpdateAvailable;
122}
123
Marissa Wall861616d2018-10-22 12:52:23 -0700124// Crop that applies to the window
125Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
126 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700127}
128
129bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 if (mCurrentState.transform == transform) return false;
131 mCurrentState.sequence++;
132 mCurrentState.transform = transform;
133 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 setTransactionFlags(eTransactionNeeded);
135 return true;
136}
137
138bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800139 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
140 mCurrentState.sequence++;
141 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
142 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700143 setTransactionFlags(eTransactionNeeded);
144 return true;
145}
146
147bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 if (mCurrentState.crop == crop) return false;
149 mCurrentState.sequence++;
150 mCurrentState.crop = crop;
151 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700152 setTransactionFlags(eTransactionNeeded);
153 return true;
154}
155
Marissa Wall861616d2018-10-22 12:52:23 -0700156bool BufferStateLayer::setFrame(const Rect& frame) {
157 int x = frame.left;
158 int y = frame.top;
159 int w = frame.getWidth();
160 int h = frame.getHeight();
161
Marissa Wall0f3242d2018-12-20 15:10:22 -0800162 if (x < 0) {
163 x = 0;
164 w = frame.right;
165 }
166
167 if (y < 0) {
168 y = 0;
169 h = frame.bottom;
170 }
171
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800172 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
173 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700174 return false;
175 }
176
177 if (!frame.isValid()) {
178 x = y = w = h = 0;
179 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800180 mCurrentState.active.transform.set(x, y);
181 mCurrentState.active.w = w;
182 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700183
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800184 mCurrentState.sequence++;
185 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700186 setTransactionFlags(eTransactionNeeded);
187 return true;
188}
189
Marissa Wallfda30bb2018-10-12 11:34:28 -0700190bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800191 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700192 mReleasePreviousBuffer = true;
193 }
194
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800195 mCurrentState.sequence++;
196 mCurrentState.buffer = buffer;
197 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700198 setTransactionFlags(eTransactionNeeded);
199 return true;
200}
201
202bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700203 // The acquire fences of BufferStateLayers have already signaled before they are set
204 mCallbackHandleAcquireTime = fence->getSignalTime();
205
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800206 mCurrentState.acquireFence = fence;
207 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700208 setTransactionFlags(eTransactionNeeded);
209 return true;
210}
211
212bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800213 if (mCurrentState.dataspace == dataspace) return false;
214 mCurrentState.sequence++;
215 mCurrentState.dataspace = dataspace;
216 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700217 setTransactionFlags(eTransactionNeeded);
218 return true;
219}
220
221bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800222 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
223 mCurrentState.sequence++;
224 mCurrentState.hdrMetadata = hdrMetadata;
225 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700226 setTransactionFlags(eTransactionNeeded);
227 return true;
228}
229
230bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 mCurrentState.sequence++;
232 mCurrentState.surfaceDamageRegion = surfaceDamage;
233 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700234 setTransactionFlags(eTransactionNeeded);
235 return true;
236}
237
238bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800239 if (mCurrentState.api == api) return false;
240 mCurrentState.sequence++;
241 mCurrentState.api = api;
242 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700243 setTransactionFlags(eTransactionNeeded);
244 return true;
245}
246
247bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800248 if (mCurrentState.sidebandStream == sidebandStream) return false;
249 mCurrentState.sequence++;
250 mCurrentState.sidebandStream = sidebandStream;
251 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700252 setTransactionFlags(eTransactionNeeded);
253
254 if (!mSidebandStreamChanged.exchange(true)) {
255 // mSidebandStreamChanged was false
256 mFlinger->signalLayerUpdate();
257 }
258 return true;
259}
260
Marissa Walle2ffb422018-10-12 11:33:52 -0700261bool BufferStateLayer::setTransactionCompletedListeners(
262 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700263 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700264 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700265 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700266 return false;
267 }
268
269 const bool willPresent = willPresentCurrentTransaction();
270
271 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700272 // If this transaction set a buffer on this layer, release its previous buffer
273 handle->releasePreviousBuffer = mReleasePreviousBuffer;
274
Marissa Walle2ffb422018-10-12 11:33:52 -0700275 // If this layer will be presented in this frame
276 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700277 // If this transaction set an acquire fence on this layer, set its acquire time
278 handle->acquireTime = mCallbackHandleAcquireTime;
279
Marissa Walle2ffb422018-10-12 11:33:52 -0700280 // Notify the transaction completed thread that there is a pending latched callback
281 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800282 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700283
284 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700286
287 } else { // If this layer will NOT need to be relatched and presented this frame
288 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800289 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700290 }
291 }
292
Marissa Wallfda30bb2018-10-12 11:34:28 -0700293 mReleasePreviousBuffer = false;
294 mCallbackHandleAcquireTime = -1;
295
Marissa Walle2ffb422018-10-12 11:33:52 -0700296 return willPresent;
297}
298
Marissa Wall61c58622018-07-18 10:12:20 -0700299bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800300 mCurrentState.transparentRegionHint = transparent;
301 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700302 setTransactionFlags(eTransactionNeeded);
303 return true;
304}
305
Marissa Wall861616d2018-10-22 12:52:23 -0700306Rect BufferStateLayer::getBufferSize(const State& s) const {
307 // for buffer state layers we use the display frame size as the buffer size.
308 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
309 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700310 }
311
Marissa Wall861616d2018-10-22 12:52:23 -0700312 // if the display frame is not defined, use the parent bounds as the buffer size.
313 const auto& p = mDrawingParent.promote();
314 if (p != nullptr) {
315 Rect parentBounds = Rect(p->computeBounds(Region()));
316 if (!parentBounds.isEmpty()) {
317 return parentBounds;
318 }
319 }
320
321 // if there is no parent layer, use the buffer's bounds as the buffer size
322 if (s.buffer) {
323 return s.buffer->getBounds();
324 }
325 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700326}
327// -----------------------------------------------------------------------
328
329// -----------------------------------------------------------------------
330// Interface implementation for BufferLayer
331// -----------------------------------------------------------------------
332bool BufferStateLayer::fenceHasSignaled() const {
333 if (latchUnsignaledBuffers()) {
334 return true;
335 }
336
337 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
338}
339
340nsecs_t BufferStateLayer::getDesiredPresentTime() {
341 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
342 return 0;
343}
344
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800345std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700346 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
347}
348
349void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
350 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
351}
352
353uint32_t BufferStateLayer::getDrawingTransform() const {
354 return getDrawingState().transform;
355}
356
357ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
358 return getDrawingState().dataspace;
359}
360
Marissa Wall861616d2018-10-22 12:52:23 -0700361// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700362Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700363 const State& s(getDrawingState());
364
365 if (s.crop.isEmpty() && s.buffer) {
366 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800367 } else if (s.buffer) {
368 Rect crop = s.crop;
369 crop.left = std::max(crop.left, 0);
370 crop.top = std::max(crop.top, 0);
371 uint32_t bufferWidth = s.buffer->getWidth();
372 uint32_t bufferHeight = s.buffer->getHeight();
373 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
374 bufferWidth <= std::numeric_limits<int32_t>::max()) {
375 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
376 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
377 }
378 if (!crop.isValid()) {
379 // Crop rect is out of bounds, return whole buffer
380 return s.buffer->getBounds();
381 }
382 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700383 }
384 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700385}
386
387uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700388 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700389}
390
391Region BufferStateLayer::getDrawingSurfaceDamage() const {
392 return getDrawingState().surfaceDamageRegion;
393}
394
395const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
396 return getDrawingState().hdrMetadata;
397}
398
399int BufferStateLayer::getDrawingApi() const {
400 return getDrawingState().api;
401}
402
403PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800404 if (!mActiveBuffer) {
405 return PIXEL_FORMAT_NONE;
406 }
Marissa Wall61c58622018-07-18 10:12:20 -0700407 return mActiveBuffer->format;
408}
409
410uint64_t BufferStateLayer::getFrameNumber() const {
411 return mFrameNumber;
412}
413
414bool BufferStateLayer::getAutoRefresh() const {
415 // TODO(marissaw): support shared buffer mode
416 return false;
417}
418
419bool BufferStateLayer::getSidebandStreamChanged() const {
420 return mSidebandStreamChanged.load();
421}
422
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800423bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700424 if (mSidebandStreamChanged.exchange(false)) {
425 const State& s(getDrawingState());
426 // mSidebandStreamChanged was true
427 // replicated in LayerBE until FE/BE is ready to be synchronized
428 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
429 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
430 setTransactionFlags(eTransactionNeeded);
431 mFlinger->setTransactionFlags(eTraversalNeeded);
432 }
433 recomputeVisibleRegions = true;
434
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800435 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700436 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800437 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800440bool BufferStateLayer::hasFrameUpdate() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700441 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700442}
443
444void BufferStateLayer::setFilteringEnabled(bool enabled) {
445 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
446 mCurrentTransform, enabled);
447}
448
Alec Mouri39801c02018-10-10 10:44:47 -0700449status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700450 const State& s(getDrawingState());
451 auto& engine(mFlinger->getRenderEngine());
452
Alec Mouri79108df2019-02-04 19:33:44 +0000453 engine.checkErrors();
454
455 // TODO(marissaw): once buffers are cached, don't create a new image everytime
456 mTextureImage = engine.createImage();
457
458 bool created =
459 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
460 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
461 if (!created) {
462 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
463 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
464 s.buffer->getUsage(), s.buffer->getPixelFormat());
465 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
466 return NO_INIT;
467 }
468
469 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
470
471 // Wait for the new buffer to be ready.
472 if (s.acquireFence->isValid()) {
473 if (SyncFeatures::getInstance().useWaitSync()) {
474 base::unique_fd fenceFd(s.acquireFence->dup());
475 if (fenceFd == -1) {
476 ALOGE("error dup'ing fence fd: %d", errno);
477 return -errno;
478 }
479 if (!engine.waitFence(std::move(fenceFd))) {
480 ALOGE("failed to wait on fence fd");
481 return UNKNOWN_ERROR;
482 }
483 } else {
484 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
485 if (err != NO_ERROR) {
486 ALOGE("error waiting for fence: %d", err);
487 return err;
488 }
489 }
490 }
491
492 return NO_ERROR;
Marissa Wall61c58622018-07-18 10:12:20 -0700493}
494
Alec Mouri86770e52018-09-24 22:40:58 +0000495status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
496 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700497 const State& s(getDrawingState());
498
499 if (!s.buffer) {
500 return NO_ERROR;
501 }
502
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700503 const int32_t layerID = getSequence();
504
Marissa Wall61c58622018-07-18 10:12:20 -0700505 // Reject if the layer is invalid
506 uint32_t bufferWidth = s.buffer->width;
507 uint32_t bufferHeight = s.buffer->height;
508
Peiyong Linefefaac2018-08-17 12:27:51 -0700509 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700510 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700511 }
512
513 if (s.transformToDisplayInverse) {
514 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700515 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700516 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700517 }
518 }
519
Vishnu Nair60356342018-11-13 13:00:45 -0800520 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700521 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
522 ALOGE("[%s] rejecting buffer: "
523 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
524 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800525 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700526 return BAD_VALUE;
527 }
528
Marissa Wall5a68a772018-12-22 17:43:42 -0800529 for (auto& handle : mDrawingState.callbackHandles) {
530 handle->latchTime = latchTime;
531 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700532
Marissa Wall61c58622018-07-18 10:12:20 -0700533 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000534 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
535 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
536 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800537 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700538 return UNKNOWN_ERROR;
539 }
540
Marissa Wall61c58622018-07-18 10:12:20 -0700541 // Check status of fences first because merging is expensive.
542 // Merging an invalid fence with any other fence results in an
543 // invalid fence.
544 auto currentStatus = s.acquireFence->getStatus();
545 if (currentStatus == Fence::Status::Invalid) {
546 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800547 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700548 return BAD_VALUE;
549 }
550
Alec Mouri86770e52018-09-24 22:40:58 +0000551 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700552 if (incomingStatus == Fence::Status::Invalid) {
553 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800554 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800555 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700556 return BAD_VALUE;
557 }
558
559 // If both fences are signaled or both are unsignaled, we need to merge
560 // them to get an accurate timestamp.
561 if (currentStatus == incomingStatus) {
562 char fenceName[32] = {};
563 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000564 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800565 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700566 if (!mergedFence.get()) {
567 ALOGE("failed to merge release fences");
568 // synchronization is broken, the best we can do is hope fences
569 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800570 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800571 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700572 return BAD_VALUE;
573 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800574 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700575 } else if (incomingStatus == Fence::Status::Unsignaled) {
576 // If one fence has signaled and the other hasn't, the unsignaled
577 // fence will approximately correspond with the correct timestamp.
578 // There's a small race if both fences signal at about the same time
579 // and their statuses are retrieved with unfortunate timing. However,
580 // by this point, they will have both signaled and only the timestamp
581 // will be slightly off; any dependencies after this point will
582 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800583 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700584 }
585 } else {
586 // Bind the new buffer to the GL texture.
587 //
588 // Older devices require the "implicit" synchronization provided
589 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
590 // devices will either call this in Layer::onDraw, or (if it's not
591 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800592 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700593 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800594 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700595 return BAD_VALUE;
596 }
597 }
598
599 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800600 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800601 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800602 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700603
604 return NO_ERROR;
605}
606
607status_t BufferStateLayer::updateActiveBuffer() {
608 const State& s(getDrawingState());
609
610 if (s.buffer == nullptr) {
611 return BAD_VALUE;
612 }
613
614 mActiveBuffer = s.buffer;
615 getBE().compositionInfo.mBuffer = mActiveBuffer;
616 getBE().compositionInfo.mBufferSlot = 0;
617
618 return NO_ERROR;
619}
620
621status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
622 // TODO(marissaw): support frame history events
623 mCurrentFrameNumber = mFrameNumber;
624 return NO_ERROR;
625}
626
Dominik Laskowski075d3172018-05-24 15:50:06 -0700627void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700628 auto& hwcInfo = getBE().mHwcLayers[displayId];
629 auto& hwcLayer = hwcInfo.layer;
630
631 const State& s(getDrawingState());
632
633 // TODO(marissaw): support more than one slot
634 uint32_t hwcSlot = 0;
635
636 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
637 if (error != HWC2::Error::None) {
638 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
639 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
640 }
641
Marissa Wall024a1912018-08-13 13:55:35 -0700642 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700643 mFrameNumber++;
644}
645
646void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700647 BufferLayer::onFirstRef();
648
Marissa Wall61c58622018-07-18 10:12:20 -0700649 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
650 updateTransformHint(display);
651 }
652}
653
654} // namespace android