blob: 83b958573d763bbd348f009afc71defa569ebdd5 [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
Valerie Haua72e2812019-01-23 13:40:39 -0800306bool BufferStateLayer::setColor(const half3& color) {
307 // create color layer if one does not yet exist
308 if (!mCurrentState.bgColorLayer) {
309 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
310 const String8& name = mName + "BackgroundColorLayer";
311 mCurrentState.bgColorLayer =
312 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
313
314 // add to child list
315 addChild(mCurrentState.bgColorLayer);
316 mFlinger->mLayersAdded = true;
317 // set up SF to handle added color layer
318 if (isRemovedFromCurrentState()) {
319 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
320 }
321 mFlinger->setTransactionFlags(eTransactionNeeded);
322 }
323
324 mCurrentState.bgColorLayer->setColor(color);
325 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
326
327 return true;
328}
329
330bool BufferStateLayer::setColorAlpha(float alpha) {
331 if (!mCurrentState.bgColorLayer) {
332 ALOGE("Attempting to set color alpha on a buffer state layer with no background color");
333 return false;
334 }
335 mCurrentState.bgColorLayer->setAlpha(alpha);
336 return true;
337}
338
339bool BufferStateLayer::setColorDataspace(ui::Dataspace dataspace) {
340 if (!mCurrentState.bgColorLayer) {
341 ALOGE("Attempting to set color dataspace on a buffer state layer with no background color");
342 return false;
343 }
344 mCurrentState.bgColorLayer->setDataspace(dataspace);
345 return true;
346}
347
Marissa Wall861616d2018-10-22 12:52:23 -0700348Rect BufferStateLayer::getBufferSize(const State& s) const {
349 // for buffer state layers we use the display frame size as the buffer size.
350 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
351 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700352 }
353
Marissa Wall861616d2018-10-22 12:52:23 -0700354 // if the display frame is not defined, use the parent bounds as the buffer size.
355 const auto& p = mDrawingParent.promote();
356 if (p != nullptr) {
357 Rect parentBounds = Rect(p->computeBounds(Region()));
358 if (!parentBounds.isEmpty()) {
359 return parentBounds;
360 }
361 }
362
363 // if there is no parent layer, use the buffer's bounds as the buffer size
364 if (s.buffer) {
365 return s.buffer->getBounds();
366 }
367 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700368}
369// -----------------------------------------------------------------------
370
371// -----------------------------------------------------------------------
372// Interface implementation for BufferLayer
373// -----------------------------------------------------------------------
374bool BufferStateLayer::fenceHasSignaled() const {
375 if (latchUnsignaledBuffers()) {
376 return true;
377 }
378
379 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
380}
381
382nsecs_t BufferStateLayer::getDesiredPresentTime() {
383 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
384 return 0;
385}
386
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800387std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700388 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
389}
390
391void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
392 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
393}
394
395uint32_t BufferStateLayer::getDrawingTransform() const {
396 return getDrawingState().transform;
397}
398
399ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
400 return getDrawingState().dataspace;
401}
402
Marissa Wall861616d2018-10-22 12:52:23 -0700403// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700404Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700405 const State& s(getDrawingState());
406
407 if (s.crop.isEmpty() && s.buffer) {
408 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800409 } else if (s.buffer) {
410 Rect crop = s.crop;
411 crop.left = std::max(crop.left, 0);
412 crop.top = std::max(crop.top, 0);
413 uint32_t bufferWidth = s.buffer->getWidth();
414 uint32_t bufferHeight = s.buffer->getHeight();
415 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
416 bufferWidth <= std::numeric_limits<int32_t>::max()) {
417 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
418 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
419 }
420 if (!crop.isValid()) {
421 // Crop rect is out of bounds, return whole buffer
422 return s.buffer->getBounds();
423 }
424 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700425 }
426 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700427}
428
429uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700430 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700431}
432
433Region BufferStateLayer::getDrawingSurfaceDamage() const {
434 return getDrawingState().surfaceDamageRegion;
435}
436
437const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
438 return getDrawingState().hdrMetadata;
439}
440
441int BufferStateLayer::getDrawingApi() const {
442 return getDrawingState().api;
443}
444
445PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800446 if (!mActiveBuffer) {
447 return PIXEL_FORMAT_NONE;
448 }
Marissa Wall61c58622018-07-18 10:12:20 -0700449 return mActiveBuffer->format;
450}
451
452uint64_t BufferStateLayer::getFrameNumber() const {
453 return mFrameNumber;
454}
455
456bool BufferStateLayer::getAutoRefresh() const {
457 // TODO(marissaw): support shared buffer mode
458 return false;
459}
460
461bool BufferStateLayer::getSidebandStreamChanged() const {
462 return mSidebandStreamChanged.load();
463}
464
465std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
466 if (mSidebandStreamChanged.exchange(false)) {
467 const State& s(getDrawingState());
468 // mSidebandStreamChanged was true
469 // replicated in LayerBE until FE/BE is ready to be synchronized
470 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
471 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
472 setTransactionFlags(eTransactionNeeded);
473 mFlinger->setTransactionFlags(eTraversalNeeded);
474 }
475 recomputeVisibleRegions = true;
476
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800477 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700478 }
479 return {};
480}
481
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800482bool BufferStateLayer::hasFrameUpdate() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700483 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700484}
485
486void BufferStateLayer::setFilteringEnabled(bool enabled) {
487 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
488 mCurrentTransform, enabled);
489}
490
Alec Mouri39801c02018-10-10 10:44:47 -0700491status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700492 const State& s(getDrawingState());
493 auto& engine(mFlinger->getRenderEngine());
494
Marissa Wall61c58622018-07-18 10:12:20 -0700495 engine.checkErrors();
496
Alec Mouri39801c02018-10-10 10:44:47 -0700497 // TODO(marissaw): once buffers are cached, don't create a new image everytime
498 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700499
500 bool created =
501 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
502 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
503 if (!created) {
504 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
505 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
506 s.buffer->getUsage(), s.buffer->getPixelFormat());
507 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
508 return NO_INIT;
509 }
510
511 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
512
513 // Wait for the new buffer to be ready.
514 if (s.acquireFence->isValid()) {
515 if (SyncFeatures::getInstance().useWaitSync()) {
516 base::unique_fd fenceFd(s.acquireFence->dup());
517 if (fenceFd == -1) {
518 ALOGE("error dup'ing fence fd: %d", errno);
519 return -errno;
520 }
521 if (!engine.waitFence(std::move(fenceFd))) {
522 ALOGE("failed to wait on fence fd");
523 return UNKNOWN_ERROR;
524 }
525 } else {
526 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
527 if (err != NO_ERROR) {
528 ALOGE("error waiting for fence: %d", err);
529 return err;
530 }
531 }
532 }
533
534 return NO_ERROR;
535}
536
Alec Mouri86770e52018-09-24 22:40:58 +0000537status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
538 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700539 const State& s(getDrawingState());
540
541 if (!s.buffer) {
542 return NO_ERROR;
543 }
544
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700545 const int32_t layerID = getSequence();
546
Marissa Wall61c58622018-07-18 10:12:20 -0700547 // Reject if the layer is invalid
548 uint32_t bufferWidth = s.buffer->width;
549 uint32_t bufferHeight = s.buffer->height;
550
Peiyong Linefefaac2018-08-17 12:27:51 -0700551 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700552 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700553 }
554
555 if (s.transformToDisplayInverse) {
556 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700557 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700558 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700559 }
560 }
561
Vishnu Nair60356342018-11-13 13:00:45 -0800562 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700563 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
564 ALOGE("[%s] rejecting buffer: "
565 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
566 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800567 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700568 return BAD_VALUE;
569 }
570
Marissa Wall5a68a772018-12-22 17:43:42 -0800571 for (auto& handle : mDrawingState.callbackHandles) {
572 handle->latchTime = latchTime;
573 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700574
Marissa Wall61c58622018-07-18 10:12:20 -0700575 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000576 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
577 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
578 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800579 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700580 return UNKNOWN_ERROR;
581 }
582
Marissa Wall61c58622018-07-18 10:12:20 -0700583 // Check status of fences first because merging is expensive.
584 // Merging an invalid fence with any other fence results in an
585 // invalid fence.
586 auto currentStatus = s.acquireFence->getStatus();
587 if (currentStatus == Fence::Status::Invalid) {
588 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800589 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700590 return BAD_VALUE;
591 }
592
Alec Mouri86770e52018-09-24 22:40:58 +0000593 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700594 if (incomingStatus == Fence::Status::Invalid) {
595 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800596 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800597 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700598 return BAD_VALUE;
599 }
600
601 // If both fences are signaled or both are unsignaled, we need to merge
602 // them to get an accurate timestamp.
603 if (currentStatus == incomingStatus) {
604 char fenceName[32] = {};
605 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000606 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700608 if (!mergedFence.get()) {
609 ALOGE("failed to merge release fences");
610 // synchronization is broken, the best we can do is hope fences
611 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800613 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700614 return BAD_VALUE;
615 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800616 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700617 } else if (incomingStatus == Fence::Status::Unsignaled) {
618 // If one fence has signaled and the other hasn't, the unsignaled
619 // fence will approximately correspond with the correct timestamp.
620 // There's a small race if both fences signal at about the same time
621 // and their statuses are retrieved with unfortunate timing. However,
622 // by this point, they will have both signaled and only the timestamp
623 // will be slightly off; any dependencies after this point will
624 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800625 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700626 }
627 } else {
628 // Bind the new buffer to the GL texture.
629 //
630 // Older devices require the "implicit" synchronization provided
631 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
632 // devices will either call this in Layer::onDraw, or (if it's not
633 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800634 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700635 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800636 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700637 return BAD_VALUE;
638 }
639 }
640
641 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800642 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800643 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800644 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700645
646 return NO_ERROR;
647}
648
649status_t BufferStateLayer::updateActiveBuffer() {
650 const State& s(getDrawingState());
651
652 if (s.buffer == nullptr) {
653 return BAD_VALUE;
654 }
655
656 mActiveBuffer = s.buffer;
657 getBE().compositionInfo.mBuffer = mActiveBuffer;
658 getBE().compositionInfo.mBufferSlot = 0;
659
660 return NO_ERROR;
661}
662
663status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
664 // TODO(marissaw): support frame history events
665 mCurrentFrameNumber = mFrameNumber;
666 return NO_ERROR;
667}
668
Dominik Laskowski075d3172018-05-24 15:50:06 -0700669void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700670 auto& hwcInfo = getBE().mHwcLayers[displayId];
671 auto& hwcLayer = hwcInfo.layer;
672
673 const State& s(getDrawingState());
674
675 // TODO(marissaw): support more than one slot
676 uint32_t hwcSlot = 0;
677
678 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
679 if (error != HWC2::Error::None) {
680 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
681 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
682 }
683
Marissa Wall024a1912018-08-13 13:55:35 -0700684 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700685 mFrameNumber++;
686}
687
688void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700689 BufferLayer::onFirstRef();
690
Marissa Wall61c58622018-07-18 10:12:20 -0700691 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
692 updateTransformHint(display);
693 }
694}
695
696} // namespace android