blob: 0e4692833577512ff294b1e1928b42d467c38842 [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"
Marissa Wall61c58622018-07-18 10:12:20 -070023
Yiwei Zhang7e666a52018-11-15 13:33:42 -080024#include "TimeStats/TimeStats.h"
25
Marissa Wall61c58622018-07-18 10:12:20 -070026#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070027#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070028
Valerie Hau0bc09152018-12-20 07:42:47 -080029#include <limits>
30
Marissa Wall61c58622018-07-18 10:12:20 -070031namespace android {
32
Lloyd Pique42ab75e2018-09-12 20:46:03 -070033// clang-format off
34const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
35 1, 0, 0, 0,
36 0, 1, 0, 0,
37 0, 0, 1, 0,
38 0, 0, 0, 1
39};
40// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070041
Vishnu Nair60356342018-11-13 13:00:45 -080042BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
43 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
44}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070045BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070046
47// -----------------------------------------------------------------------
48// Interface implementation for Layer
49// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070050void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
51 // The transaction completed callback can only be sent if the release fence from the PREVIOUS
52 // frame has fired. In practice, we should never actually wait on the previous release fence
53 // but we should store it just in case.
54 mPreviousReleaseFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -070055}
56
57void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
58 // TODO(marissaw): send the transform hint to buffer owner
59 return;
60}
61
62void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -070063 return;
64}
65
Ana Krulec010d2192018-10-08 06:29:54 -070066bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070067 if (getSidebandStreamChanged() || getAutoRefresh()) {
68 return true;
69 }
70
Marissa Wall024a1912018-08-13 13:55:35 -070071 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070072}
73
Marissa Walle2ffb422018-10-12 11:33:52 -070074bool BufferStateLayer::willPresentCurrentTransaction() const {
Ady Abraham83729882018-12-07 12:26:48 -080075 Mutex::Autolock lock(mStateMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070076 // Returns true if the most recent Transaction applied to CurrentState will be presented.
77 return getSidebandStreamChanged() || getAutoRefresh() ||
Ady Abraham83729882018-12-07 12:26:48 -080078 (mState.current.modified && mState.current.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -070079}
80
Ady Abraham83729882018-12-07 12:26:48 -080081bool BufferStateLayer::getTransformToDisplayInverseLocked() const {
82 return mState.current.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -070083}
84
Ady Abraham83729882018-12-07 12:26:48 -080085void BufferStateLayer::pushPendingStateLocked() {
86 if (!mState.current.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -070087 return;
88 }
Ady Abraham83729882018-12-07 12:26:48 -080089 mState.pending.push_back(mState.current);
90 ATRACE_INT(mTransactionName.string(), mState.pending.size());
Marissa Wall61c58622018-07-18 10:12:20 -070091}
92
93bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Ady Abraham83729882018-12-07 12:26:48 -080094 const bool stateUpdateAvailable = !mState.pending.empty();
95 while (!mState.pending.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -070096 popPendingState(stateToCommit);
97 }
Ady Abraham83729882018-12-07 12:26:48 -080098 mCurrentStateModified = stateUpdateAvailable && mState.current.modified;
99 mState.current.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700100 return stateUpdateAvailable;
101}
102
Marissa Wall861616d2018-10-22 12:52:23 -0700103// Crop that applies to the window
104Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
105 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
108bool BufferStateLayer::setTransform(uint32_t transform) {
Ady Abraham83729882018-12-07 12:26:48 -0800109 Mutex::Autolock lock(mStateMutex);
110 if (mState.current.transform == transform) return false;
111 mState.current.sequence++;
112 mState.current.transform = transform;
113 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700114 setTransactionFlags(eTransactionNeeded);
115 return true;
116}
117
118bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Ady Abraham83729882018-12-07 12:26:48 -0800119 Mutex::Autolock lock(mStateMutex);
120 if (mState.current.transformToDisplayInverse == transformToDisplayInverse) return false;
121 mState.current.sequence++;
122 mState.current.transformToDisplayInverse = transformToDisplayInverse;
123 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700124 setTransactionFlags(eTransactionNeeded);
125 return true;
126}
127
128bool BufferStateLayer::setCrop(const Rect& crop) {
Ady Abraham83729882018-12-07 12:26:48 -0800129 Mutex::Autolock lock(mStateMutex);
130 if (mState.current.crop == crop) return false;
131 mState.current.sequence++;
132 mState.current.crop = crop;
133 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 setTransactionFlags(eTransactionNeeded);
135 return true;
136}
137
Marissa Wall861616d2018-10-22 12:52:23 -0700138bool BufferStateLayer::setFrame(const Rect& frame) {
139 int x = frame.left;
140 int y = frame.top;
141 int w = frame.getWidth();
142 int h = frame.getHeight();
143
Ady Abraham83729882018-12-07 12:26:48 -0800144 Mutex::Autolock lock(mStateMutex);
145 if (mState.current.active.transform.tx() == x && mState.current.active.transform.ty() == y &&
146 mState.current.active.w == w && mState.current.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700147 return false;
148 }
149
150 if (!frame.isValid()) {
151 x = y = w = h = 0;
152 }
Ady Abraham83729882018-12-07 12:26:48 -0800153 mState.current.active.transform.set(x, y);
154 mState.current.active.w = w;
155 mState.current.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700156
Ady Abraham83729882018-12-07 12:26:48 -0800157 mState.current.sequence++;
158 mState.current.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700159 setTransactionFlags(eTransactionNeeded);
160 return true;
161}
162
Marissa Wallfda30bb2018-10-12 11:34:28 -0700163bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Ady Abraham83729882018-12-07 12:26:48 -0800164 Mutex::Autolock lock(mStateMutex);
165 if (mState.current.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700166 mReleasePreviousBuffer = true;
167 }
168
Ady Abraham83729882018-12-07 12:26:48 -0800169 mState.current.sequence++;
170 mState.current.buffer = buffer;
171 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700172 setTransactionFlags(eTransactionNeeded);
173 return true;
174}
175
176bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Ady Abraham83729882018-12-07 12:26:48 -0800177 Mutex::Autolock lock(mStateMutex);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700178 // The acquire fences of BufferStateLayers have already signaled before they are set
179 mCallbackHandleAcquireTime = fence->getSignalTime();
180
Ady Abraham83729882018-12-07 12:26:48 -0800181 mState.current.acquireFence = fence;
182 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700183 setTransactionFlags(eTransactionNeeded);
184 return true;
185}
186
187bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Ady Abraham83729882018-12-07 12:26:48 -0800188 Mutex::Autolock lock(mStateMutex);
189 if (mState.current.dataspace == dataspace) return false;
190 mState.current.sequence++;
191 mState.current.dataspace = dataspace;
192 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700193 setTransactionFlags(eTransactionNeeded);
194 return true;
195}
196
197bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Ady Abraham83729882018-12-07 12:26:48 -0800198 Mutex::Autolock lock(mStateMutex);
199 if (mState.current.hdrMetadata == hdrMetadata) return false;
200 mState.current.sequence++;
201 mState.current.hdrMetadata = hdrMetadata;
202 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700203 setTransactionFlags(eTransactionNeeded);
204 return true;
205}
206
207bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Ady Abraham83729882018-12-07 12:26:48 -0800208 Mutex::Autolock lock(mStateMutex);
209 mState.current.sequence++;
210 mState.current.surfaceDamageRegion = surfaceDamage;
211 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700212 setTransactionFlags(eTransactionNeeded);
213 return true;
214}
215
216bool BufferStateLayer::setApi(int32_t api) {
Ady Abraham83729882018-12-07 12:26:48 -0800217 Mutex::Autolock lock(mStateMutex);
218 if (mState.current.api == api) return false;
219 mState.current.sequence++;
220 mState.current.api = api;
221 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700222 setTransactionFlags(eTransactionNeeded);
223 return true;
224}
225
226bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Ady Abraham83729882018-12-07 12:26:48 -0800227 Mutex::Autolock lock(mStateMutex);
228 if (mState.current.sidebandStream == sidebandStream) return false;
229 mState.current.sequence++;
230 mState.current.sidebandStream = sidebandStream;
231 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700232 setTransactionFlags(eTransactionNeeded);
233
234 if (!mSidebandStreamChanged.exchange(true)) {
235 // mSidebandStreamChanged was false
236 mFlinger->signalLayerUpdate();
237 }
238 return true;
239}
240
Marissa Walle2ffb422018-10-12 11:33:52 -0700241bool BufferStateLayer::setTransactionCompletedListeners(
242 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700243 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700244 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700245 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700246 return false;
247 }
248
249 const bool willPresent = willPresentCurrentTransaction();
250
251 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700252 // If this transaction set a buffer on this layer, release its previous buffer
253 handle->releasePreviousBuffer = mReleasePreviousBuffer;
254
Marissa Walle2ffb422018-10-12 11:33:52 -0700255 // If this layer will be presented in this frame
256 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700257 // If this transaction set an acquire fence on this layer, set its acquire time
258 handle->acquireTime = mCallbackHandleAcquireTime;
259
Marissa Walle2ffb422018-10-12 11:33:52 -0700260 // Notify the transaction completed thread that there is a pending latched callback
261 // handle
262 mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle);
263
264 // Store so latched time and release fence can be set
Ady Abraham83729882018-12-07 12:26:48 -0800265 {
266 Mutex::Autolock lock(mStateMutex);
267 mState.current.callbackHandles.push_back(handle);
268 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700269
270 } else { // If this layer will NOT need to be relatched and presented this frame
271 // Notify the transaction completed thread this handle is done
272 mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle);
273 }
274 }
275
Marissa Wallfda30bb2018-10-12 11:34:28 -0700276 mReleasePreviousBuffer = false;
277 mCallbackHandleAcquireTime = -1;
278
Marissa Walle2ffb422018-10-12 11:33:52 -0700279 return willPresent;
280}
281
Marissa Wall61c58622018-07-18 10:12:20 -0700282bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Ady Abraham83729882018-12-07 12:26:48 -0800283 Mutex::Autolock lock(mStateMutex);
284 mState.current.transparentRegionHint = transparent;
285 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700286 setTransactionFlags(eTransactionNeeded);
287 return true;
288}
289
Marissa Wall861616d2018-10-22 12:52:23 -0700290Rect BufferStateLayer::getBufferSize(const State& s) const {
291 // for buffer state layers we use the display frame size as the buffer size.
292 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
293 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700294 }
295
Marissa Wall861616d2018-10-22 12:52:23 -0700296 // if the display frame is not defined, use the parent bounds as the buffer size.
297 const auto& p = mDrawingParent.promote();
298 if (p != nullptr) {
299 Rect parentBounds = Rect(p->computeBounds(Region()));
300 if (!parentBounds.isEmpty()) {
301 return parentBounds;
302 }
303 }
304
305 // if there is no parent layer, use the buffer's bounds as the buffer size
306 if (s.buffer) {
307 return s.buffer->getBounds();
308 }
309 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700310}
311// -----------------------------------------------------------------------
312
313// -----------------------------------------------------------------------
314// Interface implementation for BufferLayer
315// -----------------------------------------------------------------------
316bool BufferStateLayer::fenceHasSignaled() const {
317 if (latchUnsignaledBuffers()) {
318 return true;
319 }
320
Ady Abraham83729882018-12-07 12:26:48 -0800321 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700322 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
323}
324
325nsecs_t BufferStateLayer::getDesiredPresentTime() {
326 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
327 return 0;
328}
329
Ady Abraham83729882018-12-07 12:26:48 -0800330std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTimeLocked() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700331 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
332}
333
334void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
335 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
336}
337
338uint32_t BufferStateLayer::getDrawingTransform() const {
339 return getDrawingState().transform;
340}
341
342ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
343 return getDrawingState().dataspace;
344}
345
Marissa Wall861616d2018-10-22 12:52:23 -0700346// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700347Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700348 const State& s(getDrawingState());
349
350 if (s.crop.isEmpty() && s.buffer) {
351 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800352 } else if (s.buffer) {
353 Rect crop = s.crop;
354 crop.left = std::max(crop.left, 0);
355 crop.top = std::max(crop.top, 0);
356 uint32_t bufferWidth = s.buffer->getWidth();
357 uint32_t bufferHeight = s.buffer->getHeight();
358 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
359 bufferWidth <= std::numeric_limits<int32_t>::max()) {
360 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
361 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
362 }
363 if (!crop.isValid()) {
364 // Crop rect is out of bounds, return whole buffer
365 return s.buffer->getBounds();
366 }
367 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700368 }
369 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700370}
371
372uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700373 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700374}
375
376Region BufferStateLayer::getDrawingSurfaceDamage() const {
Ady Abraham83729882018-12-07 12:26:48 -0800377 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700378 return getDrawingState().surfaceDamageRegion;
379}
380
381const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
Ady Abraham83729882018-12-07 12:26:48 -0800382 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700383 return getDrawingState().hdrMetadata;
384}
385
386int BufferStateLayer::getDrawingApi() const {
Ady Abraham83729882018-12-07 12:26:48 -0800387 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700388 return getDrawingState().api;
389}
390
391PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800392 if (!mActiveBuffer) {
393 return PIXEL_FORMAT_NONE;
394 }
Marissa Wall61c58622018-07-18 10:12:20 -0700395 return mActiveBuffer->format;
396}
397
398uint64_t BufferStateLayer::getFrameNumber() const {
399 return mFrameNumber;
400}
401
402bool BufferStateLayer::getAutoRefresh() const {
403 // TODO(marissaw): support shared buffer mode
404 return false;
405}
406
407bool BufferStateLayer::getSidebandStreamChanged() const {
408 return mSidebandStreamChanged.load();
409}
410
411std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Ady Abraham83729882018-12-07 12:26:48 -0800412 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700413 if (mSidebandStreamChanged.exchange(false)) {
414 const State& s(getDrawingState());
415 // mSidebandStreamChanged was true
416 // replicated in LayerBE until FE/BE is ready to be synchronized
417 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
418 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
419 setTransactionFlags(eTransactionNeeded);
420 mFlinger->setTransactionFlags(eTraversalNeeded);
421 }
422 recomputeVisibleRegions = true;
423
Ady Abraham83729882018-12-07 12:26:48 -0800424 return getTransformLocked().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700425 }
426 return {};
427}
428
Ady Abraham83729882018-12-07 12:26:48 -0800429bool BufferStateLayer::hasFrameUpdateLocked() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700430 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700431}
432
433void BufferStateLayer::setFilteringEnabled(bool enabled) {
434 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
435 mCurrentTransform, enabled);
436}
437
Alec Mouri39801c02018-10-10 10:44:47 -0700438status_t BufferStateLayer::bindTextureImage() {
Ady Abraham83729882018-12-07 12:26:48 -0800439 Mutex::Autolock lock(mStateMutex);
440 return bindTextureImageLocked();
441}
442status_t BufferStateLayer::bindTextureImageLocked() {
Marissa Wall61c58622018-07-18 10:12:20 -0700443 const State& s(getDrawingState());
444 auto& engine(mFlinger->getRenderEngine());
445
Marissa Wall61c58622018-07-18 10:12:20 -0700446 engine.checkErrors();
447
Alec Mouri39801c02018-10-10 10:44:47 -0700448 // TODO(marissaw): once buffers are cached, don't create a new image everytime
449 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700450
451 bool created =
452 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
453 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
454 if (!created) {
455 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
456 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
457 s.buffer->getUsage(), s.buffer->getPixelFormat());
458 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
459 return NO_INIT;
460 }
461
462 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
463
464 // Wait for the new buffer to be ready.
465 if (s.acquireFence->isValid()) {
466 if (SyncFeatures::getInstance().useWaitSync()) {
467 base::unique_fd fenceFd(s.acquireFence->dup());
468 if (fenceFd == -1) {
469 ALOGE("error dup'ing fence fd: %d", errno);
470 return -errno;
471 }
472 if (!engine.waitFence(std::move(fenceFd))) {
473 ALOGE("failed to wait on fence fd");
474 return UNKNOWN_ERROR;
475 }
476 } else {
477 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
478 if (err != NO_ERROR) {
479 ALOGE("error waiting for fence: %d", err);
480 return err;
481 }
482 }
483 }
484
485 return NO_ERROR;
486}
487
Alec Mouri86770e52018-09-24 22:40:58 +0000488status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
489 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700490 const State& s(getDrawingState());
491
492 if (!s.buffer) {
493 return NO_ERROR;
494 }
495
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700496 const int32_t layerID = getSequence();
497
Marissa Wall61c58622018-07-18 10:12:20 -0700498 // Reject if the layer is invalid
499 uint32_t bufferWidth = s.buffer->width;
500 uint32_t bufferHeight = s.buffer->height;
501
Peiyong Linefefaac2018-08-17 12:27:51 -0700502 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700503 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700504 }
505
506 if (s.transformToDisplayInverse) {
507 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700508 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700509 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700510 }
511 }
512
Vishnu Nair60356342018-11-13 13:00:45 -0800513 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700514 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
515 ALOGE("[%s] rejecting buffer: "
516 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
517 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800518 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700519 return BAD_VALUE;
520 }
521
Marissa Wallfda30bb2018-10-12 11:34:28 -0700522 mFlinger->getTransactionCompletedThread()
523 .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime,
524 mPreviousReleaseFence);
Marissa Walle2ffb422018-10-12 11:33:52 -0700525
Marissa Wall61c58622018-07-18 10:12:20 -0700526 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000527 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
528 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
529 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800530 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700531 return UNKNOWN_ERROR;
532 }
533
Marissa Wall61c58622018-07-18 10:12:20 -0700534 // Check status of fences first because merging is expensive.
535 // Merging an invalid fence with any other fence results in an
536 // invalid fence.
537 auto currentStatus = s.acquireFence->getStatus();
538 if (currentStatus == Fence::Status::Invalid) {
539 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800540 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700541 return BAD_VALUE;
542 }
543
Alec Mouri86770e52018-09-24 22:40:58 +0000544 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700545 if (incomingStatus == Fence::Status::Invalid) {
546 ALOGE("New fence has invalid state");
Ady Abraham83729882018-12-07 12:26:48 -0800547 mState.drawing.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800548 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700549 return BAD_VALUE;
550 }
551
552 // If both fences are signaled or both are unsignaled, we need to merge
553 // them to get an accurate timestamp.
554 if (currentStatus == incomingStatus) {
555 char fenceName[32] = {};
556 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000557 sp<Fence> mergedFence =
Ady Abraham83729882018-12-07 12:26:48 -0800558 Fence::merge(fenceName, mState.drawing.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700559 if (!mergedFence.get()) {
560 ALOGE("failed to merge release fences");
561 // synchronization is broken, the best we can do is hope fences
562 // signal in order so the new fence will act like a union
Ady Abraham83729882018-12-07 12:26:48 -0800563 mState.drawing.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800564 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700565 return BAD_VALUE;
566 }
Ady Abraham83729882018-12-07 12:26:48 -0800567 mState.drawing.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700568 } else if (incomingStatus == Fence::Status::Unsignaled) {
569 // If one fence has signaled and the other hasn't, the unsignaled
570 // fence will approximately correspond with the correct timestamp.
571 // There's a small race if both fences signal at about the same time
572 // and their statuses are retrieved with unfortunate timing. However,
573 // by this point, they will have both signaled and only the timestamp
574 // will be slightly off; any dependencies after this point will
575 // already have been met.
Ady Abraham83729882018-12-07 12:26:48 -0800576 mState.drawing.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700577 }
578 } else {
579 // Bind the new buffer to the GL texture.
580 //
581 // Older devices require the "implicit" synchronization provided
582 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
583 // devices will either call this in Layer::onDraw, or (if it's not
584 // a GL-composited layer) not at all.
Ady Abraham83729882018-12-07 12:26:48 -0800585 status_t err = bindTextureImageLocked();
Marissa Wall61c58622018-07-18 10:12:20 -0700586 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800587 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700588 return BAD_VALUE;
589 }
590 }
591
592 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800593 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Ady Abraham83729882018-12-07 12:26:48 -0800594 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTimeLocked());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800595 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700596
597 return NO_ERROR;
598}
599
600status_t BufferStateLayer::updateActiveBuffer() {
601 const State& s(getDrawingState());
602
603 if (s.buffer == nullptr) {
604 return BAD_VALUE;
605 }
606
607 mActiveBuffer = s.buffer;
608 getBE().compositionInfo.mBuffer = mActiveBuffer;
609 getBE().compositionInfo.mBufferSlot = 0;
610
611 return NO_ERROR;
612}
613
614status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
615 // TODO(marissaw): support frame history events
616 mCurrentFrameNumber = mFrameNumber;
617 return NO_ERROR;
618}
619
Dominik Laskowski075d3172018-05-24 15:50:06 -0700620void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Ady Abraham83729882018-12-07 12:26:48 -0800621 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700622 auto& hwcInfo = getBE().mHwcLayers[displayId];
623 auto& hwcLayer = hwcInfo.layer;
624
625 const State& s(getDrawingState());
626
627 // TODO(marissaw): support more than one slot
628 uint32_t hwcSlot = 0;
629
630 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
631 if (error != HWC2::Error::None) {
632 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
633 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
634 }
635
Marissa Wall024a1912018-08-13 13:55:35 -0700636 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700637 mFrameNumber++;
638}
639
640void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700641 BufferLayer::onFirstRef();
642
Marissa Wall61c58622018-07-18 10:12:20 -0700643 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
644 updateTransformHint(display);
645 }
646}
647
648} // namespace android