blob: 0e09f318bd15c55b490bfe47645331b4c16dc65c [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;
Marissa Wall3ff826c2019-02-07 11:58:25 -080045 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080046}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070047BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070048
49// -----------------------------------------------------------------------
50// Interface implementation for Layer
51// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070052void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080053 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
54 // buffer that was presented on this layer. The first transaction that came in this frame that
55 // replaced the previous buffer on this layer needs this release fence, because the fence will
56 // let the client know when that previous buffer is removed from the screen.
57 //
58 // Every other transaction on this layer does not need a release fence because no other
59 // Transactions that were set on this layer this frame are going to have their preceeding buffer
60 // removed from the display this frame.
61 //
62 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
63 // buffer so it doesn't need a previous release fence because the layer still needs the previous
64 // buffer. The second transaction contains a buffer so it needs a previous release fence because
65 // the previous buffer will be released this frame. The third transaction also contains a
66 // buffer. It replaces the buffer in the second transaction. The buffer in the second
67 // transaction will now no longer be presented so it is released immediately and the third
68 // transaction doesn't need a previous release fence.
69 for (auto& handle : mDrawingState.callbackHandles) {
70 if (handle->releasePreviousBuffer) {
71 handle->previousReleaseFence = releaseFence;
72 break;
73 }
74 }
Marissa Wall61c58622018-07-18 10:12:20 -070075}
76
77void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
78 // TODO(marissaw): send the transform hint to buffer owner
79 return;
80}
81
82void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080083 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
84 mDrawingState.callbackHandles);
85
86 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070087}
88
Ana Krulec010d2192018-10-08 06:29:54 -070089bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070090 if (getSidebandStreamChanged() || getAutoRefresh()) {
91 return true;
92 }
93
Marissa Wall024a1912018-08-13 13:55:35 -070094 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070095}
96
Marissa Walle2ffb422018-10-12 11:33:52 -070097bool BufferStateLayer::willPresentCurrentTransaction() const {
98 // Returns true if the most recent Transaction applied to CurrentState will be presented.
99 return getSidebandStreamChanged() || getAutoRefresh() ||
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 (mCurrentState.modified && mCurrentState.buffer != 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
Valerie Haua72e2812019-01-23 13:40:39 -0800307bool BufferStateLayer::setColor(const half3& color) {
308 // create color layer if one does not yet exist
309 if (!mCurrentState.bgColorLayer) {
310 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
311 const String8& name = mName + "BackgroundColorLayer";
312 mCurrentState.bgColorLayer =
313 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
314
315 // add to child list
316 addChild(mCurrentState.bgColorLayer);
317 mFlinger->mLayersAdded = true;
318 // set up SF to handle added color layer
319 if (isRemovedFromCurrentState()) {
320 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
321 }
322 mFlinger->setTransactionFlags(eTransactionNeeded);
323 }
324
325 mCurrentState.bgColorLayer->setColor(color);
326 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
327
328 return true;
329}
330
331bool BufferStateLayer::setColorAlpha(float alpha) {
332 if (!mCurrentState.bgColorLayer) {
333 ALOGE("Attempting to set color alpha on a buffer state layer with no background color");
334 return false;
335 }
336 mCurrentState.bgColorLayer->setAlpha(alpha);
337 return true;
338}
339
340bool BufferStateLayer::setColorDataspace(ui::Dataspace dataspace) {
341 if (!mCurrentState.bgColorLayer) {
342 ALOGE("Attempting to set color dataspace on a buffer state layer with no background color");
343 return false;
344 }
345 mCurrentState.bgColorLayer->setDataspace(dataspace);
346 return true;
347}
348
Marissa Wall861616d2018-10-22 12:52:23 -0700349Rect BufferStateLayer::getBufferSize(const State& s) const {
350 // for buffer state layers we use the display frame size as the buffer size.
351 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
352 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700353 }
354
Marissa Wall861616d2018-10-22 12:52:23 -0700355 // if the display frame is not defined, use the parent bounds as the buffer size.
356 const auto& p = mDrawingParent.promote();
357 if (p != nullptr) {
358 Rect parentBounds = Rect(p->computeBounds(Region()));
359 if (!parentBounds.isEmpty()) {
360 return parentBounds;
361 }
362 }
363
364 // if there is no parent layer, use the buffer's bounds as the buffer size
365 if (s.buffer) {
366 return s.buffer->getBounds();
367 }
368 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700369}
370// -----------------------------------------------------------------------
371
372// -----------------------------------------------------------------------
373// Interface implementation for BufferLayer
374// -----------------------------------------------------------------------
375bool BufferStateLayer::fenceHasSignaled() const {
376 if (latchUnsignaledBuffers()) {
377 return true;
378 }
379
380 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
381}
382
383nsecs_t BufferStateLayer::getDesiredPresentTime() {
384 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
385 return 0;
386}
387
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800388std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700389 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
390}
391
392void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
393 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
394}
395
396uint32_t BufferStateLayer::getDrawingTransform() const {
397 return getDrawingState().transform;
398}
399
400ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
401 return getDrawingState().dataspace;
402}
403
Marissa Wall861616d2018-10-22 12:52:23 -0700404// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700405Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700406 const State& s(getDrawingState());
407
408 if (s.crop.isEmpty() && s.buffer) {
409 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800410 } else if (s.buffer) {
411 Rect crop = s.crop;
412 crop.left = std::max(crop.left, 0);
413 crop.top = std::max(crop.top, 0);
414 uint32_t bufferWidth = s.buffer->getWidth();
415 uint32_t bufferHeight = s.buffer->getHeight();
416 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
417 bufferWidth <= std::numeric_limits<int32_t>::max()) {
418 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
419 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
420 }
421 if (!crop.isValid()) {
422 // Crop rect is out of bounds, return whole buffer
423 return s.buffer->getBounds();
424 }
425 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700426 }
427 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700428}
429
430uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700431 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700432}
433
434Region BufferStateLayer::getDrawingSurfaceDamage() const {
435 return getDrawingState().surfaceDamageRegion;
436}
437
438const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
439 return getDrawingState().hdrMetadata;
440}
441
442int BufferStateLayer::getDrawingApi() const {
443 return getDrawingState().api;
444}
445
446PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800447 if (!mActiveBuffer) {
448 return PIXEL_FORMAT_NONE;
449 }
Marissa Wall61c58622018-07-18 10:12:20 -0700450 return mActiveBuffer->format;
451}
452
453uint64_t BufferStateLayer::getFrameNumber() const {
454 return mFrameNumber;
455}
456
457bool BufferStateLayer::getAutoRefresh() const {
458 // TODO(marissaw): support shared buffer mode
459 return false;
460}
461
462bool BufferStateLayer::getSidebandStreamChanged() const {
463 return mSidebandStreamChanged.load();
464}
465
466std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
467 if (mSidebandStreamChanged.exchange(false)) {
468 const State& s(getDrawingState());
469 // mSidebandStreamChanged was true
470 // replicated in LayerBE until FE/BE is ready to be synchronized
471 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
472 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
473 setTransactionFlags(eTransactionNeeded);
474 mFlinger->setTransactionFlags(eTraversalNeeded);
475 }
476 recomputeVisibleRegions = true;
477
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800478 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700479 }
480 return {};
481}
482
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800483bool BufferStateLayer::hasFrameUpdate() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700484 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700485}
486
487void BufferStateLayer::setFilteringEnabled(bool enabled) {
488 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
489 mCurrentTransform, enabled);
490}
491
Alec Mouri39801c02018-10-10 10:44:47 -0700492status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700493 const State& s(getDrawingState());
494 auto& engine(mFlinger->getRenderEngine());
495
Alec Mouri0f714832018-11-12 15:31:06 -0800496 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700497}
498
Alec Mouri86770e52018-09-24 22:40:58 +0000499status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
500 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700501 const State& s(getDrawingState());
502
503 if (!s.buffer) {
504 return NO_ERROR;
505 }
506
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700507 const int32_t layerID = getSequence();
508
Marissa Wall61c58622018-07-18 10:12:20 -0700509 // Reject if the layer is invalid
510 uint32_t bufferWidth = s.buffer->width;
511 uint32_t bufferHeight = s.buffer->height;
512
Peiyong Linefefaac2018-08-17 12:27:51 -0700513 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700514 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700515 }
516
517 if (s.transformToDisplayInverse) {
518 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700519 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700520 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700521 }
522 }
523
Vishnu Nair60356342018-11-13 13:00:45 -0800524 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700525 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
526 ALOGE("[%s] rejecting buffer: "
527 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
528 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800529 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700530 return BAD_VALUE;
531 }
532
Marissa Wall5a68a772018-12-22 17:43:42 -0800533 for (auto& handle : mDrawingState.callbackHandles) {
534 handle->latchTime = latchTime;
535 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700536
Marissa Wall61c58622018-07-18 10:12:20 -0700537 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000538 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
539 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
540 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800541 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700542 return UNKNOWN_ERROR;
543 }
544
Marissa Wall61c58622018-07-18 10:12:20 -0700545 // Check status of fences first because merging is expensive.
546 // Merging an invalid fence with any other fence results in an
547 // invalid fence.
548 auto currentStatus = s.acquireFence->getStatus();
549 if (currentStatus == Fence::Status::Invalid) {
550 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800551 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700552 return BAD_VALUE;
553 }
554
Alec Mouri86770e52018-09-24 22:40:58 +0000555 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700556 if (incomingStatus == Fence::Status::Invalid) {
557 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800558 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800559 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700560 return BAD_VALUE;
561 }
562
563 // If both fences are signaled or both are unsignaled, we need to merge
564 // them to get an accurate timestamp.
565 if (currentStatus == incomingStatus) {
566 char fenceName[32] = {};
567 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000568 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700570 if (!mergedFence.get()) {
571 ALOGE("failed to merge release fences");
572 // synchronization is broken, the best we can do is hope fences
573 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800574 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800575 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700576 return BAD_VALUE;
577 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800578 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700579 } else if (incomingStatus == Fence::Status::Unsignaled) {
580 // If one fence has signaled and the other hasn't, the unsignaled
581 // fence will approximately correspond with the correct timestamp.
582 // There's a small race if both fences signal at about the same time
583 // and their statuses are retrieved with unfortunate timing. However,
584 // by this point, they will have both signaled and only the timestamp
585 // will be slightly off; any dependencies after this point will
586 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800587 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700588 }
589 } else {
590 // Bind the new buffer to the GL texture.
591 //
592 // Older devices require the "implicit" synchronization provided
593 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
594 // devices will either call this in Layer::onDraw, or (if it's not
595 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800596 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700597 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800598 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700599 return BAD_VALUE;
600 }
601 }
602
603 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800604 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800605 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800606 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700607
608 return NO_ERROR;
609}
610
611status_t BufferStateLayer::updateActiveBuffer() {
612 const State& s(getDrawingState());
613
614 if (s.buffer == nullptr) {
615 return BAD_VALUE;
616 }
617
618 mActiveBuffer = s.buffer;
Alec Mouri0f714832018-11-12 15:31:06 -0800619 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700620 getBE().compositionInfo.mBuffer = mActiveBuffer;
621 getBE().compositionInfo.mBufferSlot = 0;
622
623 return NO_ERROR;
624}
625
Alec Mouri0f714832018-11-12 15:31:06 -0800626bool BufferStateLayer::useCachedBufferForClientComposition() const {
627 // TODO: Store a proper staleness bit to support EGLImage caching.
628 return false;
629}
630
Marissa Wall61c58622018-07-18 10:12:20 -0700631status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
632 // TODO(marissaw): support frame history events
633 mCurrentFrameNumber = mFrameNumber;
634 return NO_ERROR;
635}
636
Dominik Laskowski075d3172018-05-24 15:50:06 -0700637void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700638 auto& hwcInfo = getBE().mHwcLayers[displayId];
639 auto& hwcLayer = hwcInfo.layer;
640
641 const State& s(getDrawingState());
642
643 // TODO(marissaw): support more than one slot
644 uint32_t hwcSlot = 0;
645
646 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
647 if (error != HWC2::Error::None) {
648 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
649 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
650 }
651
Marissa Wall024a1912018-08-13 13:55:35 -0700652 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700653 mFrameNumber++;
654}
655
656void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700657 BufferLayer::onFirstRef();
658
Marissa Wall61c58622018-07-18 10:12:20 -0700659 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
660 updateTransformHint(display);
661 }
662}
663
664} // namespace android