blob: a7970db4736bfe172c3fa6c54e0984bd3785e7a3 [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -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 "BufferLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "BufferLayer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080023
24#include <compositionengine/CompositionEngine.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Display.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080026#include <compositionengine/Layer.h>
27#include <compositionengine/LayerCreationArgs.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080032#include <cutils/compiler.h>
33#include <cutils/native_handle.h>
34#include <cutils/properties.h>
35#include <gui/BufferItem.h>
36#include <gui/BufferQueue.h>
37#include <gui/LayerDebugInfo.h>
38#include <gui/Surface.h>
39#include <renderengine/RenderEngine.h>
40#include <ui/DebugUtils.h>
41#include <utils/Errors.h>
42#include <utils/Log.h>
43#include <utils/NativeHandle.h>
44#include <utils/StopWatch.h>
45#include <utils/Trace.h>
46
Alec Mourie60041e2019-06-14 18:59:51 -070047#include <cmath>
48#include <cstdlib>
49#include <mutex>
50#include <sstream>
51
David Sodman0c69cad2017-08-21 12:12:51 -070052#include "Colorizer.h"
53#include "DisplayDevice.h"
54#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080055#include "TimeStats/TimeStats.h"
56
David Sodman0c69cad2017-08-21 12:12:51 -070057namespace android {
58
Lloyd Pique42ab75e2018-09-12 20:46:03 -070059BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080060 : Layer(args),
61 mTextureName(args.flinger->getNewTexture()),
62 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
63 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070065
Lloyd Pique42ab75e2018-09-12 20:46:03 -070066 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070067
Lloyd Pique42ab75e2018-09-12 20:46:03 -070068 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
69 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070070}
71
72BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070073 mFlinger->deleteTextureAsync(mTextureName);
Yiwei Zhang7e666a52018-11-15 13:33:42 -080074 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070075}
76
David Sodmaneb085e02017-10-05 18:49:04 -070077void BufferLayer::useSurfaceDamage() {
78 if (mFlinger->mForceFullDamage) {
79 surfaceDamageRegion = Region::INVALID_REGION;
80 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070081 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070082 }
83}
84
85void BufferLayer::useEmptyDamage() {
86 surfaceDamageRegion.clear();
87}
88
Marissa Wallfd668622018-05-10 10:21:13 -070089bool BufferLayer::isOpaque(const Layer::State& s) const {
90 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
91 // layer's opaque flag.
Lloyd Pique0b785d82018-12-04 17:25:27 -080092 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070093 return false;
94 }
95
96 // if the layer has the opaque flag, then we're always opaque,
97 // otherwise we use the current buffer's format.
98 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070099}
100
101bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700102 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800103 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700104 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
105
106 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700107}
108
109bool BufferLayer::isFixedSize() const {
110 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
111}
112
Lloyd Piquea83776c2019-01-29 18:42:32 -0800113bool BufferLayer::usesSourceCrop() const {
114 return true;
115}
116
David Sodman0c69cad2017-08-21 12:12:51 -0700117static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800118 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
119 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
120 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
David Sodman0c69cad2017-08-21 12:12:51 -0700121 mat4 tr;
122
123 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
124 tr = tr * rot90;
125 }
126 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
127 tr = tr * flipH;
128 }
129 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
130 tr = tr * flipV;
131 }
132 return inverse(tr);
133}
134
Lloyd Piquef16688f2019-02-19 17:47:57 -0800135std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
136 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700137 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800138
139 auto result = Layer::prepareClientComposition(targetSettings);
140 if (!result) {
141 return result;
142 }
143
David Sodman0cf8f8d2017-12-20 18:19:45 -0800144 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700145 // the texture has not been created yet, this Layer has
146 // in fact never been drawn into. This happens frequently with
147 // SurfaceView because the WindowManager can't know when the client
148 // has drawn the first time.
149
150 // If there is nothing under us, we paint the screen in black, otherwise
151 // we just skip this update.
152
153 // figure out if there is something below us
154 Region under;
155 bool finished = false;
156 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
157 if (finished || layer == static_cast<BufferLayer const*>(this)) {
158 finished = true;
159 return;
160 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800161
162 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700163 });
164 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800165 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700166 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800167 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700168 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800169 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700170 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800171 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800172 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000173 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800174 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700175 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000176 layer.source.buffer.buffer = mActiveBuffer;
177 layer.source.buffer.isOpaque = isOpaque(s);
178 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000179 layer.source.buffer.textureName = mTextureName;
180 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
181 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700182 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800183 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700184
185 // Query the texture matrix given our current filtering mode.
186 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700187 setFilteringEnabled(useFiltering);
188 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700189
190 if (getTransformToDisplayInverse()) {
191 /*
192 * the code below applies the primary display's inverse transform to
193 * the texture transform
194 */
195 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
196 mat4 tr = inverseOrientation(transform);
197
198 /**
199 * TODO(b/36727915): This is basically a hack.
200 *
201 * Ensure that regardless of the parent transformation,
202 * this buffer is always transformed from native display
203 * orientation to display orientation. For example, in the case
204 * of a camera where the buffer remains in native orientation,
205 * we want the pixels to always be upright.
206 */
207 sp<Layer> p = mDrawingParent.promote();
208 if (p != nullptr) {
209 const auto parentTransform = p->getTransform();
210 tr = tr * inverseOrientation(parentTransform.getOrientation());
211 }
212
213 // and finally apply it to the original texture matrix
214 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
215 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
216 }
217
Vishnu Nair4351ad52019-02-11 14:13:02 -0800218 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800219 float bufferWidth = getBufferSize(s).getWidth();
220 float bufferHeight = getBufferSize(s).getHeight();
221
222 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
223 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
224 // ignore them.
225 if (!getBufferSize(s).isValid()) {
226 bufferWidth = float(win.right) - float(win.left);
227 bufferHeight = float(win.bottom) - float(win.top);
228 }
David Sodman0c69cad2017-08-21 12:12:51 -0700229
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000230 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
231 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
232 const float translateY = float(win.top) / bufferHeight;
233 const float translateX = float(win.left) / bufferWidth;
234
235 // Flip y-coordinates because GLConsumer expects OpenGL convention.
236 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
237 mat4::translate(vec4(-.5, -.5, 0, 1)) *
238 mat4::translate(vec4(translateX, translateY, 0, 1)) *
239 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
240
241 layer.source.buffer.useTextureFiltering = useFiltering;
242 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700243 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000244 // If layer is blacked out, force alpha to 1 so that we draw a black color
245 // layer.
246 layer.source.buffer.buffer = nullptr;
247 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700248 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000249
Lloyd Piquef16688f2019-02-19 17:47:57 -0800250 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700251}
252
Marissa Wallfd668622018-05-10 10:21:13 -0700253bool BufferLayer::isHdrY410() const {
254 // pixel format is HDR Y410 masquerading as RGBA_1010102
255 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
256 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800257 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700258}
259
Lloyd Piquef5275482019-01-29 18:42:42 -0800260void BufferLayer::latchPerFrameState(
261 compositionengine::LayerFECompositionState& compositionState) const {
262 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700263
264 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800265 if (compositionState.sidebandStream.get()) {
266 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700267 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800268 // Normal buffer layers
269 compositionState.hdrMetadata = getDrawingHdrMetadata();
270 compositionState.compositionType = mPotentialCursor
271 ? Hwc2::IComposerClient::Composition::CURSOR
272 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700273 }
David Sodman0c69cad2017-08-21 12:12:51 -0700274}
275
Marissa Wallfd668622018-05-10 10:21:13 -0700276bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
277 if (mBufferLatched) {
278 Mutex::Autolock lock(mFrameEventHistoryMutex);
279 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700280 }
Marissa Wallfd668622018-05-10 10:21:13 -0700281 mRefreshPending = false;
282 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700283}
284
Dominik Laskowski075d3172018-05-24 15:50:06 -0700285bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
286 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700287 const std::shared_ptr<FenceTime>& presentFence,
288 const CompositorTiming& compositorTiming) {
289 // mFrameLatencyNeeded is true when a new frame was latched for the
290 // composition.
291 if (!mFrameLatencyNeeded) return false;
292
293 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700294 {
Marissa Wallfd668622018-05-10 10:21:13 -0700295 Mutex::Autolock lock(mFrameEventHistoryMutex);
296 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
297 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700298 }
299
Marissa Wallfd668622018-05-10 10:21:13 -0700300 // Update mFrameTracker.
301 nsecs_t desiredPresentTime = getDesiredPresentTime();
302 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
303
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700304 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800305 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700306
307 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
308 if (frameReadyFence->isValid()) {
309 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
310 } else {
311 // There was no fence for this frame, so assume that it was ready
312 // to be presented at the desired present time.
313 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700314 }
Marissa Wallfd668622018-05-10 10:21:13 -0700315
316 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800317 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700318 mFlinger->mTimeStats->traceFence(layerID, getCurrentBufferId(), mCurrentFrameNumber,
319 presentFence, TimeStats::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700320 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700322 // The HWC doesn't support present fences, so use the refresh
323 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700324 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800325 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700326 mFlinger->mTimeStats->traceTimestamp(layerID, getCurrentBufferId(), mCurrentFrameNumber,
327 actualPresentTime,
328 TimeStats::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700329 mFrameTracker.setActualPresentTime(actualPresentTime);
330 }
331
332 mFrameTracker.advanceFrame();
333 mFrameLatencyNeeded = false;
334 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700335}
336
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700337bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
338 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700339 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700340
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800341 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700342
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800343 if (refreshRequired) {
344 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700345 }
346
Marissa Wallfd668622018-05-10 10:21:13 -0700347 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800348 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700349 }
David Sodman0c69cad2017-08-21 12:12:51 -0700350
Marissa Wallfd668622018-05-10 10:21:13 -0700351 // if we've already called updateTexImage() without going through
352 // a composition step, we have to skip this layer at this point
353 // because we cannot call updateTeximage() without a corresponding
354 // compositionComplete() call.
355 // we'll trigger an update in onPreComposition().
356 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800357 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700358 }
359
360 // If the head buffer's acquire fence hasn't signaled yet, return and
361 // try again later
362 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700363 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700364 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800365 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700366 }
367
368 // Capture the old state of the layer for comparisons later
369 const State& s(getDrawingState());
370 const bool oldOpacity = isOpaque(s);
371 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
372
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700373 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700374 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800375 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700376 }
377
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700378 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700379 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800380 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700381 }
382
383 err = updateActiveBuffer();
384 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800385 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700386 }
387
388 mBufferLatched = true;
389
390 err = updateFrameNumber(latchTime);
391 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800392 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700393 }
394
395 mRefreshPending = true;
396 mFrameLatencyNeeded = true;
397 if (oldBuffer == nullptr) {
398 // the first time we receive a buffer, we need to trigger a
399 // geometry invalidation.
400 recomputeVisibleRegions = true;
401 }
402
403 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800404 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700405 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800406 case ui::Dataspace::SRGB:
407 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700408 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800409 case ui::Dataspace::SRGB_LINEAR:
410 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700411 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800412 case ui::Dataspace::JFIF:
413 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700414 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800415 case ui::Dataspace::BT601_625:
416 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700417 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800418 case ui::Dataspace::BT601_525:
419 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700420 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800421 case ui::Dataspace::BT709:
422 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700423 break;
424 default:
425 break;
426 }
427 mCurrentDataSpace = dataSpace;
428
429 Rect crop(getDrawingCrop());
430 const uint32_t transform(getDrawingTransform());
431 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800432 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700433 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800434 (scalingMode != mCurrentScalingMode) ||
435 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700436 mCurrentCrop = crop;
437 mCurrentTransform = transform;
438 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800439 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700440 recomputeVisibleRegions = true;
441 }
442
443 if (oldBuffer != nullptr) {
444 uint32_t bufWidth = mActiveBuffer->getWidth();
445 uint32_t bufHeight = mActiveBuffer->getHeight();
446 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
447 recomputeVisibleRegions = true;
448 }
449 }
450
451 if (oldOpacity != isOpaque(s)) {
452 recomputeVisibleRegions = true;
453 }
454
455 // Remove any sync points corresponding to the buffer which was just
456 // latched
457 {
458 Mutex::Autolock lock(mLocalSyncPointMutex);
459 auto point = mLocalSyncPoints.begin();
460 while (point != mLocalSyncPoints.end()) {
461 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
462 // This sync point must have been added since we started
463 // latching. Don't drop it yet.
464 ++point;
465 continue;
466 }
467
468 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700469 std::stringstream ss;
470 ss << "Dropping sync point " << (*point)->getFrameNumber();
471 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700472 point = mLocalSyncPoints.erase(point);
473 } else {
474 ++point;
475 }
476 }
477 }
478
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800479 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700480}
481
482// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700483void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
484 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700485 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700486 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700487 Mutex::Autolock lock(mLocalSyncPointMutex);
488 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700489 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
490 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700491 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700492 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
493 if (requestedSyncLayer) {
494 // Need to update the transaction flag to ensure the layer's pending transaction
495 // gets applied.
496 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
497 }
Marissa Wallfd668622018-05-10 10:21:13 -0700498 }
David Sodman0c69cad2017-08-21 12:12:51 -0700499 }
500}
501
Marissa Wallfd668622018-05-10 10:21:13 -0700502bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700503 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700504}
505
506uint32_t BufferLayer::getEffectiveScalingMode() const {
507 if (mOverrideScalingMode >= 0) {
508 return mOverrideScalingMode;
509 }
510
511 return mCurrentScalingMode;
512}
513
514bool BufferLayer::isProtected() const {
515 const sp<GraphicBuffer>& buffer(mActiveBuffer);
516 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
517}
518
519bool BufferLayer::latchUnsignaledBuffers() {
520 static bool propertyLoaded = false;
521 static bool latch = false;
522 static std::mutex mutex;
523 std::lock_guard<std::mutex> lock(mutex);
524 if (!propertyLoaded) {
525 char value[PROPERTY_VALUE_MAX] = {};
526 property_get("debug.sf.latch_unsignaled", value, "0");
527 latch = atoi(value);
528 propertyLoaded = true;
529 }
530 return latch;
531}
532
533// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700534bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
535 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700536 bool matchingFramesFound = false;
537 bool allTransactionsApplied = true;
538 Mutex::Autolock lock(mLocalSyncPointMutex);
539
540 for (auto& point : mLocalSyncPoints) {
541 if (point->getFrameNumber() > headFrameNumber) {
542 break;
543 }
544 matchingFramesFound = true;
545
546 if (!point->frameIsAvailable()) {
547 // We haven't notified the remote layer that the frame for
548 // this point is available yet. Notify it now, and then
549 // abort this attempt to latch.
550 point->setFrameAvailable();
551 allTransactionsApplied = false;
552 break;
553 }
554
555 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
556 }
557 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700558}
559
560// As documented in libhardware header, formats in the range
561// 0x100 - 0x1FF are specific to the HAL implementation, and
562// are known to have no alpha channel
563// TODO: move definition for device-specific range into
564// hardware.h, instead of using hard-coded values here.
565#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
566
567bool BufferLayer::getOpacityForFormat(uint32_t format) {
568 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
569 return true;
570 }
571 switch (format) {
572 case HAL_PIXEL_FORMAT_RGBA_8888:
573 case HAL_PIXEL_FORMAT_BGRA_8888:
574 case HAL_PIXEL_FORMAT_RGBA_FP16:
575 case HAL_PIXEL_FORMAT_RGBA_1010102:
576 return false;
577 }
578 // in all other case, we have no blending (also for unknown formats)
579 return true;
580}
581
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800582bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800583 // If we are not capturing based on the state of a known display device,
584 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800585 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800586 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800587 }
588
589 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
590 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800591 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800592 }
593
Lloyd Piquef16688f2019-02-19 17:47:57 -0800594 // We need filtering if the sourceCrop rectangle size does not match the
595 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800596 const auto& compositionState = outputLayer->getState();
597 const auto displayFrame = compositionState.displayFrame;
598 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800599 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800600 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700601}
602
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700603uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800604 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700605 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700606 } else {
607 return mCurrentFrameNumber;
608 }
609}
610
Vishnu Nair60356342018-11-13 13:00:45 -0800611Rect BufferLayer::getBufferSize(const State& s) const {
612 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
613 // we cannot determine the buffer size.
614 if ((s.sidebandStream != nullptr) ||
615 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
616 return Rect(getActiveWidth(s), getActiveHeight(s));
617 }
618
619 if (mActiveBuffer == nullptr) {
620 return Rect::INVALID_RECT;
621 }
622
623 uint32_t bufWidth = mActiveBuffer->getWidth();
624 uint32_t bufHeight = mActiveBuffer->getHeight();
625
626 // Undo any transformations on the buffer and return the result.
627 if (mCurrentTransform & ui::Transform::ROT_90) {
628 std::swap(bufWidth, bufHeight);
629 }
630
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800632 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
633 if (invTransform & ui::Transform::ROT_90) {
634 std::swap(bufWidth, bufHeight);
635 }
636 }
637
638 return Rect(bufWidth, bufHeight);
639}
640
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800641std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
642 return mCompositionLayer;
643}
644
Vishnu Nair4351ad52019-02-11 14:13:02 -0800645FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
646 const State& s(getDrawingState());
647
648 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
649 // we cannot determine the buffer size.
650 if ((s.sidebandStream != nullptr) ||
651 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
652 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
653 }
654
655 if (mActiveBuffer == nullptr) {
656 return parentBounds;
657 }
658
659 uint32_t bufWidth = mActiveBuffer->getWidth();
660 uint32_t bufHeight = mActiveBuffer->getHeight();
661
662 // Undo any transformations on the buffer and return the result.
663 if (mCurrentTransform & ui::Transform::ROT_90) {
664 std::swap(bufWidth, bufHeight);
665 }
666
667 if (getTransformToDisplayInverse()) {
668 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
669 if (invTransform & ui::Transform::ROT_90) {
670 std::swap(bufWidth, bufHeight);
671 }
672 }
673
674 return FloatRect(0, 0, bufWidth, bufHeight);
675}
676
chaviw49a108c2019-08-12 11:23:06 -0700677void BufferLayer::latchAndReleaseBuffer() {
678 mRefreshPending = false;
679 if (hasReadyFrame()) {
680 bool ignored = false;
681 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
682 }
683 releasePendingBuffer(systemTime());
684}
685
David Sodman0c69cad2017-08-21 12:12:51 -0700686} // namespace android
687
688#if defined(__gl_h_)
689#error "don't include gl/gl.h in this file"
690#endif
691
692#if defined(__gl2_h_)
693#error "don't include gl2/gl2.h in this file"
694#endif