blob: efc2c9f7b39312af8764fb521ca7576e4c8ac41c [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
29namespace android {
30
Lloyd Pique42ab75e2018-09-12 20:46:03 -070031// clang-format off
32const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
33 1, 0, 0, 0,
34 0, 1, 0, 0,
35 0, 0, 1, 0,
36 0, 0, 0, 1
37};
38// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070039
Vishnu Nair60356342018-11-13 13:00:45 -080040BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
41 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
42}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070043BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070044
45// -----------------------------------------------------------------------
46// Interface implementation for Layer
47// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070048void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
49 // The transaction completed callback can only be sent if the release fence from the PREVIOUS
50 // frame has fired. In practice, we should never actually wait on the previous release fence
51 // but we should store it just in case.
52 mPreviousReleaseFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -070053}
54
55void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
56 // TODO(marissaw): send the transform hint to buffer owner
57 return;
58}
59
60void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -070061 return;
62}
63
Ana Krulec010d2192018-10-08 06:29:54 -070064bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070065 if (getSidebandStreamChanged() || getAutoRefresh()) {
66 return true;
67 }
68
Marissa Wall024a1912018-08-13 13:55:35 -070069 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070070}
71
Marissa Walle2ffb422018-10-12 11:33:52 -070072bool BufferStateLayer::willPresentCurrentTransaction() const {
73 // Returns true if the most recent Transaction applied to CurrentState will be presented.
74 return getSidebandStreamChanged() || getAutoRefresh() ||
75 (mCurrentState.modified && mCurrentState.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -070076}
77
78bool BufferStateLayer::getTransformToDisplayInverse() const {
79 return mCurrentState.transformToDisplayInverse;
80}
81
82void BufferStateLayer::pushPendingState() {
83 if (!mCurrentState.modified) {
84 return;
85 }
86 mPendingStates.push_back(mCurrentState);
87 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
88}
89
90bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
91 const bool stateUpdateAvailable = !mPendingStates.empty();
92 while (!mPendingStates.empty()) {
93 popPendingState(stateToCommit);
94 }
Marissa Wall024a1912018-08-13 13:55:35 -070095 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
Marissa Wall61c58622018-07-18 10:12:20 -070096 mCurrentState.modified = false;
97 return stateUpdateAvailable;
98}
99
Marissa Wall861616d2018-10-22 12:52:23 -0700100// Crop that applies to the window
101Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
102 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700103}
104
105bool BufferStateLayer::setTransform(uint32_t transform) {
106 if (mCurrentState.transform == transform) return false;
107 mCurrentState.sequence++;
108 mCurrentState.transform = transform;
109 mCurrentState.modified = true;
110 setTransactionFlags(eTransactionNeeded);
111 return true;
112}
113
114bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
115 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
116 mCurrentState.sequence++;
117 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
118 mCurrentState.modified = true;
119 setTransactionFlags(eTransactionNeeded);
120 return true;
121}
122
123bool BufferStateLayer::setCrop(const Rect& crop) {
124 if (mCurrentState.crop == crop) return false;
125 mCurrentState.sequence++;
126 mCurrentState.crop = crop;
127 mCurrentState.modified = true;
128 setTransactionFlags(eTransactionNeeded);
129 return true;
130}
131
Marissa Wall861616d2018-10-22 12:52:23 -0700132bool BufferStateLayer::setFrame(const Rect& frame) {
133 int x = frame.left;
134 int y = frame.top;
135 int w = frame.getWidth();
136 int h = frame.getHeight();
137
138 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
139 mCurrentState.active.w == w && mCurrentState.active.h == h) {
140 return false;
141 }
142
143 if (!frame.isValid()) {
144 x = y = w = h = 0;
145 }
146 mCurrentState.active.transform.set(x, y);
147 mCurrentState.active.w = w;
148 mCurrentState.active.h = h;
149
150 mCurrentState.sequence++;
151 mCurrentState.modified = true;
152 setTransactionFlags(eTransactionNeeded);
153 return true;
154}
155
Marissa Wallfda30bb2018-10-12 11:34:28 -0700156bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
157 if (mCurrentState.buffer) {
158 mReleasePreviousBuffer = true;
159 }
160
Marissa Wall61c58622018-07-18 10:12:20 -0700161 mCurrentState.sequence++;
162 mCurrentState.buffer = buffer;
163 mCurrentState.modified = true;
164 setTransactionFlags(eTransactionNeeded);
165 return true;
166}
167
168bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700169 // The acquire fences of BufferStateLayers have already signaled before they are set
170 mCallbackHandleAcquireTime = fence->getSignalTime();
171
Marissa Wall61c58622018-07-18 10:12:20 -0700172 mCurrentState.acquireFence = fence;
173 mCurrentState.modified = true;
174 setTransactionFlags(eTransactionNeeded);
175 return true;
176}
177
178bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
179 if (mCurrentState.dataspace == dataspace) return false;
180 mCurrentState.sequence++;
181 mCurrentState.dataspace = dataspace;
182 mCurrentState.modified = true;
183 setTransactionFlags(eTransactionNeeded);
184 return true;
185}
186
187bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
188 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
189 mCurrentState.sequence++;
190 mCurrentState.hdrMetadata = hdrMetadata;
191 mCurrentState.modified = true;
192 setTransactionFlags(eTransactionNeeded);
193 return true;
194}
195
196bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
197 mCurrentState.sequence++;
198 mCurrentState.surfaceDamageRegion = surfaceDamage;
199 mCurrentState.modified = true;
200 setTransactionFlags(eTransactionNeeded);
201 return true;
202}
203
204bool BufferStateLayer::setApi(int32_t api) {
205 if (mCurrentState.api == api) return false;
206 mCurrentState.sequence++;
207 mCurrentState.api = api;
208 mCurrentState.modified = true;
209 setTransactionFlags(eTransactionNeeded);
210 return true;
211}
212
213bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
214 if (mCurrentState.sidebandStream == sidebandStream) return false;
215 mCurrentState.sequence++;
216 mCurrentState.sidebandStream = sidebandStream;
217 mCurrentState.modified = true;
218 setTransactionFlags(eTransactionNeeded);
219
220 if (!mSidebandStreamChanged.exchange(true)) {
221 // mSidebandStreamChanged was false
222 mFlinger->signalLayerUpdate();
223 }
224 return true;
225}
226
Marissa Walle2ffb422018-10-12 11:33:52 -0700227bool BufferStateLayer::setTransactionCompletedListeners(
228 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700229 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700230 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700231 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700232 return false;
233 }
234
235 const bool willPresent = willPresentCurrentTransaction();
236
237 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700238 // If this transaction set a buffer on this layer, release its previous buffer
239 handle->releasePreviousBuffer = mReleasePreviousBuffer;
240
Marissa Walle2ffb422018-10-12 11:33:52 -0700241 // If this layer will be presented in this frame
242 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700243 // If this transaction set an acquire fence on this layer, set its acquire time
244 handle->acquireTime = mCallbackHandleAcquireTime;
245
Marissa Walle2ffb422018-10-12 11:33:52 -0700246 // Notify the transaction completed thread that there is a pending latched callback
247 // handle
248 mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle);
249
250 // Store so latched time and release fence can be set
251 mCurrentState.callbackHandles.push_back(handle);
252
253 } else { // If this layer will NOT need to be relatched and presented this frame
254 // Notify the transaction completed thread this handle is done
255 mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle);
256 }
257 }
258
Marissa Wallfda30bb2018-10-12 11:34:28 -0700259 mReleasePreviousBuffer = false;
260 mCallbackHandleAcquireTime = -1;
261
Marissa Walle2ffb422018-10-12 11:33:52 -0700262 return willPresent;
263}
264
Marissa Wall61c58622018-07-18 10:12:20 -0700265bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
266 mCurrentState.transparentRegionHint = transparent;
267 mCurrentState.modified = true;
268 setTransactionFlags(eTransactionNeeded);
269 return true;
270}
271
Marissa Wall861616d2018-10-22 12:52:23 -0700272Rect BufferStateLayer::getBufferSize(const State& s) const {
273 // for buffer state layers we use the display frame size as the buffer size.
274 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
275 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700276 }
277
Marissa Wall861616d2018-10-22 12:52:23 -0700278 // if the display frame is not defined, use the parent bounds as the buffer size.
279 const auto& p = mDrawingParent.promote();
280 if (p != nullptr) {
281 Rect parentBounds = Rect(p->computeBounds(Region()));
282 if (!parentBounds.isEmpty()) {
283 return parentBounds;
284 }
285 }
286
287 // if there is no parent layer, use the buffer's bounds as the buffer size
288 if (s.buffer) {
289 return s.buffer->getBounds();
290 }
291 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700292}
293// -----------------------------------------------------------------------
294
295// -----------------------------------------------------------------------
296// Interface implementation for BufferLayer
297// -----------------------------------------------------------------------
298bool BufferStateLayer::fenceHasSignaled() const {
299 if (latchUnsignaledBuffers()) {
300 return true;
301 }
302
303 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
304}
305
306nsecs_t BufferStateLayer::getDesiredPresentTime() {
307 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
308 return 0;
309}
310
311std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
312 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
313}
314
315void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
316 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
317}
318
319uint32_t BufferStateLayer::getDrawingTransform() const {
320 return getDrawingState().transform;
321}
322
323ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
324 return getDrawingState().dataspace;
325}
326
Marissa Wall861616d2018-10-22 12:52:23 -0700327// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700328Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700329 const State& s(getDrawingState());
330
331 if (s.crop.isEmpty() && s.buffer) {
332 return s.buffer->getBounds();
333 }
334 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700335}
336
337uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700338 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700339}
340
341Region BufferStateLayer::getDrawingSurfaceDamage() const {
342 return getDrawingState().surfaceDamageRegion;
343}
344
345const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
346 return getDrawingState().hdrMetadata;
347}
348
349int BufferStateLayer::getDrawingApi() const {
350 return getDrawingState().api;
351}
352
353PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800354 if (!mActiveBuffer) {
355 return PIXEL_FORMAT_NONE;
356 }
Marissa Wall61c58622018-07-18 10:12:20 -0700357 return mActiveBuffer->format;
358}
359
360uint64_t BufferStateLayer::getFrameNumber() const {
361 return mFrameNumber;
362}
363
364bool BufferStateLayer::getAutoRefresh() const {
365 // TODO(marissaw): support shared buffer mode
366 return false;
367}
368
369bool BufferStateLayer::getSidebandStreamChanged() const {
370 return mSidebandStreamChanged.load();
371}
372
373std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
374 if (mSidebandStreamChanged.exchange(false)) {
375 const State& s(getDrawingState());
376 // mSidebandStreamChanged was true
377 // replicated in LayerBE until FE/BE is ready to be synchronized
378 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
379 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
380 setTransactionFlags(eTransactionNeeded);
381 mFlinger->setTransactionFlags(eTraversalNeeded);
382 }
383 recomputeVisibleRegions = true;
384
385 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
386 }
387 return {};
388}
389
Marissa Wall024a1912018-08-13 13:55:35 -0700390bool BufferStateLayer::hasFrameUpdate() const {
391 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700392}
393
394void BufferStateLayer::setFilteringEnabled(bool enabled) {
395 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
396 mCurrentTransform, enabled);
397}
398
Alec Mouri39801c02018-10-10 10:44:47 -0700399status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700400 const State& s(getDrawingState());
401 auto& engine(mFlinger->getRenderEngine());
402
Marissa Wall61c58622018-07-18 10:12:20 -0700403 engine.checkErrors();
404
Alec Mouri39801c02018-10-10 10:44:47 -0700405 // TODO(marissaw): once buffers are cached, don't create a new image everytime
406 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700407
408 bool created =
409 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
410 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
411 if (!created) {
412 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
413 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
414 s.buffer->getUsage(), s.buffer->getPixelFormat());
415 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
416 return NO_INIT;
417 }
418
419 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
420
421 // Wait for the new buffer to be ready.
422 if (s.acquireFence->isValid()) {
423 if (SyncFeatures::getInstance().useWaitSync()) {
424 base::unique_fd fenceFd(s.acquireFence->dup());
425 if (fenceFd == -1) {
426 ALOGE("error dup'ing fence fd: %d", errno);
427 return -errno;
428 }
429 if (!engine.waitFence(std::move(fenceFd))) {
430 ALOGE("failed to wait on fence fd");
431 return UNKNOWN_ERROR;
432 }
433 } else {
434 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
435 if (err != NO_ERROR) {
436 ALOGE("error waiting for fence: %d", err);
437 return err;
438 }
439 }
440 }
441
442 return NO_ERROR;
443}
444
Alec Mouri86770e52018-09-24 22:40:58 +0000445status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
446 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700447 const State& s(getDrawingState());
448
449 if (!s.buffer) {
450 return NO_ERROR;
451 }
452
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700453 const int32_t layerID = getSequence();
454
Marissa Wall61c58622018-07-18 10:12:20 -0700455 // Reject if the layer is invalid
456 uint32_t bufferWidth = s.buffer->width;
457 uint32_t bufferHeight = s.buffer->height;
458
Peiyong Linefefaac2018-08-17 12:27:51 -0700459 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700460 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700461 }
462
463 if (s.transformToDisplayInverse) {
464 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700465 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700466 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700467 }
468 }
469
Vishnu Nair60356342018-11-13 13:00:45 -0800470 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700471 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
472 ALOGE("[%s] rejecting buffer: "
473 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
474 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800475 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700476 return BAD_VALUE;
477 }
478
Marissa Wallfda30bb2018-10-12 11:34:28 -0700479 mFlinger->getTransactionCompletedThread()
480 .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime,
481 mPreviousReleaseFence);
Marissa Walle2ffb422018-10-12 11:33:52 -0700482
Marissa Wall61c58622018-07-18 10:12:20 -0700483 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000484 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
485 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
486 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800487 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700488 return UNKNOWN_ERROR;
489 }
490
Marissa Wall61c58622018-07-18 10:12:20 -0700491 // Check status of fences first because merging is expensive.
492 // Merging an invalid fence with any other fence results in an
493 // invalid fence.
494 auto currentStatus = s.acquireFence->getStatus();
495 if (currentStatus == Fence::Status::Invalid) {
496 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800497 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700498 return BAD_VALUE;
499 }
500
Alec Mouri86770e52018-09-24 22:40:58 +0000501 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700502 if (incomingStatus == Fence::Status::Invalid) {
503 ALOGE("New fence has invalid state");
Alec Mouri86770e52018-09-24 22:40:58 +0000504 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800505 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700506 return BAD_VALUE;
507 }
508
509 // If both fences are signaled or both are unsignaled, we need to merge
510 // them to get an accurate timestamp.
511 if (currentStatus == incomingStatus) {
512 char fenceName[32] = {};
513 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000514 sp<Fence> mergedFence =
515 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700516 if (!mergedFence.get()) {
517 ALOGE("failed to merge release fences");
518 // synchronization is broken, the best we can do is hope fences
519 // signal in order so the new fence will act like a union
Alec Mouri86770e52018-09-24 22:40:58 +0000520 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800521 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700522 return BAD_VALUE;
523 }
524 mDrawingState.acquireFence = mergedFence;
525 } else if (incomingStatus == Fence::Status::Unsignaled) {
526 // If one fence has signaled and the other hasn't, the unsignaled
527 // fence will approximately correspond with the correct timestamp.
528 // There's a small race if both fences signal at about the same time
529 // and their statuses are retrieved with unfortunate timing. However,
530 // by this point, they will have both signaled and only the timestamp
531 // will be slightly off; any dependencies after this point will
532 // already have been met.
Alec Mouri86770e52018-09-24 22:40:58 +0000533 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700534 }
535 } else {
536 // Bind the new buffer to the GL texture.
537 //
538 // Older devices require the "implicit" synchronization provided
539 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
540 // devices will either call this in Layer::onDraw, or (if it's not
541 // a GL-composited layer) not at all.
542 status_t err = bindTextureImage();
543 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800544 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700545 return BAD_VALUE;
546 }
547 }
548
549 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800550 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
551 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
552 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700553
554 return NO_ERROR;
555}
556
557status_t BufferStateLayer::updateActiveBuffer() {
558 const State& s(getDrawingState());
559
560 if (s.buffer == nullptr) {
561 return BAD_VALUE;
562 }
563
564 mActiveBuffer = s.buffer;
565 getBE().compositionInfo.mBuffer = mActiveBuffer;
566 getBE().compositionInfo.mBufferSlot = 0;
567
568 return NO_ERROR;
569}
570
571status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
572 // TODO(marissaw): support frame history events
573 mCurrentFrameNumber = mFrameNumber;
574 return NO_ERROR;
575}
576
Dominik Laskowski075d3172018-05-24 15:50:06 -0700577void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700578 auto& hwcInfo = getBE().mHwcLayers[displayId];
579 auto& hwcLayer = hwcInfo.layer;
580
581 const State& s(getDrawingState());
582
583 // TODO(marissaw): support more than one slot
584 uint32_t hwcSlot = 0;
585
586 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
587 if (error != HWC2::Error::None) {
588 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
589 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
590 }
591
Marissa Wall024a1912018-08-13 13:55:35 -0700592 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700593 mFrameNumber++;
594}
595
596void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700597 BufferLayer::onFirstRef();
598
Marissa Wall61c58622018-07-18 10:12:20 -0700599 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
600 updateTransformHint(display);
601 }
602}
603
604} // namespace android