blob: 4cf37a4d30879eaafa3a89bdb4e573c60d425f1e [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 Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080029#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080031#include <cutils/compiler.h>
32#include <cutils/native_handle.h>
33#include <cutils/properties.h>
34#include <gui/BufferItem.h>
35#include <gui/BufferQueue.h>
36#include <gui/LayerDebugInfo.h>
37#include <gui/Surface.h>
38#include <renderengine/RenderEngine.h>
39#include <ui/DebugUtils.h>
40#include <utils/Errors.h>
41#include <utils/Log.h>
42#include <utils/NativeHandle.h>
43#include <utils/StopWatch.h>
44#include <utils/Trace.h>
45
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <cmath>
47#include <cstdlib>
48#include <mutex>
49#include <sstream>
50
David Sodman0c69cad2017-08-21 12:12:51 -070051#include "Colorizer.h"
52#include "DisplayDevice.h"
53#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080054#include "TimeStats/TimeStats.h"
55
David Sodman0c69cad2017-08-21 12:12:51 -070056namespace android {
57
Lloyd Pique42ab75e2018-09-12 20:46:03 -070058BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080059 : Layer(args),
60 mTextureName(args.flinger->getNewTexture()),
61 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
62 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070063 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070064
Lloyd Pique42ab75e2018-09-12 20:46:03 -070065 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
68 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070069}
70
71BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070072 mFlinger->deleteTextureAsync(mTextureName);
Yiwei Zhang7e666a52018-11-15 13:33:42 -080073 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070074}
75
David Sodmaneb085e02017-10-05 18:49:04 -070076void BufferLayer::useSurfaceDamage() {
77 if (mFlinger->mForceFullDamage) {
78 surfaceDamageRegion = Region::INVALID_REGION;
79 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070080 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070081 }
82}
83
84void BufferLayer::useEmptyDamage() {
85 surfaceDamageRegion.clear();
86}
87
Marissa Wallfd668622018-05-10 10:21:13 -070088bool BufferLayer::isOpaque(const Layer::State& s) const {
89 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
90 // layer's opaque flag.
Lloyd Pique0b785d82018-12-04 17:25:27 -080091 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070092 return false;
93 }
94
95 // if the layer has the opaque flag, then we're always opaque,
96 // otherwise we use the current buffer's format.
97 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070098}
99
100bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700101 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800102 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700103 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
104
105 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700106}
107
108bool BufferLayer::isFixedSize() const {
109 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
110}
111
Lloyd Piquea83776c2019-01-29 18:42:32 -0800112bool BufferLayer::usesSourceCrop() const {
113 return true;
114}
115
David Sodman0c69cad2017-08-21 12:12:51 -0700116static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800117 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
118 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
119 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 -0700120 mat4 tr;
121
122 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
123 tr = tr * rot90;
124 }
125 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
126 tr = tr * flipH;
127 }
128 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
129 tr = tr * flipV;
130 }
131 return inverse(tr);
132}
133
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000134bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
135 bool useIdentityTransform, Region& clearRegion,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800136 const bool supportProtectedContent,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000137 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700138 ATRACE_CALL();
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800139 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion,
140 supportProtectedContent, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800141 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700142 // the texture has not been created yet, this Layer has
143 // in fact never been drawn into. This happens frequently with
144 // SurfaceView because the WindowManager can't know when the client
145 // has drawn the first time.
146
147 // If there is nothing under us, we paint the screen in black, otherwise
148 // we just skip this update.
149
150 // figure out if there is something below us
151 Region under;
152 bool finished = false;
153 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
154 if (finished || layer == static_cast<BufferLayer const*>(this)) {
155 finished = true;
156 return;
157 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000158 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700159 });
160 // if not everything below us is covered, we plug the holes!
161 Region holes(clip.subtract(under));
162 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000163 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700164 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000165 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700166 }
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800167 bool blackOutLayer =
168 (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000169 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700170 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000171 layer.source.buffer.buffer = mActiveBuffer;
172 layer.source.buffer.isOpaque = isOpaque(s);
173 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000174 layer.source.buffer.textureName = mTextureName;
175 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
176 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700177 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800178 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
179 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700180
181 // Query the texture matrix given our current filtering mode.
182 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700183 setFilteringEnabled(useFiltering);
184 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700185
186 if (getTransformToDisplayInverse()) {
187 /*
188 * the code below applies the primary display's inverse transform to
189 * the texture transform
190 */
191 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
192 mat4 tr = inverseOrientation(transform);
193
194 /**
195 * TODO(b/36727915): This is basically a hack.
196 *
197 * Ensure that regardless of the parent transformation,
198 * this buffer is always transformed from native display
199 * orientation to display orientation. For example, in the case
200 * of a camera where the buffer remains in native orientation,
201 * we want the pixels to always be upright.
202 */
203 sp<Layer> p = mDrawingParent.promote();
204 if (p != nullptr) {
205 const auto parentTransform = p->getTransform();
206 tr = tr * inverseOrientation(parentTransform.getOrientation());
207 }
208
209 // and finally apply it to the original texture matrix
210 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
211 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
212 }
213
Vishnu Nair4351ad52019-02-11 14:13:02 -0800214 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800215 float bufferWidth = getBufferSize(s).getWidth();
216 float bufferHeight = getBufferSize(s).getHeight();
217
218 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
219 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
220 // ignore them.
221 if (!getBufferSize(s).isValid()) {
222 bufferWidth = float(win.right) - float(win.left);
223 bufferHeight = float(win.bottom) - float(win.top);
224 }
David Sodman0c69cad2017-08-21 12:12:51 -0700225
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000226 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
227 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
228 const float translateY = float(win.top) / bufferHeight;
229 const float translateX = float(win.left) / bufferWidth;
230
231 // Flip y-coordinates because GLConsumer expects OpenGL convention.
232 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
233 mat4::translate(vec4(-.5, -.5, 0, 1)) *
234 mat4::translate(vec4(translateX, translateY, 0, 1)) *
235 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
236
237 layer.source.buffer.useTextureFiltering = useFiltering;
238 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700239 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000240 // If layer is blacked out, force alpha to 1 so that we draw a black color
241 // layer.
242 layer.source.buffer.buffer = nullptr;
243 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700244 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000245
246 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700247}
248
Marissa Wallfd668622018-05-10 10:21:13 -0700249bool BufferLayer::isHdrY410() const {
250 // pixel format is HDR Y410 masquerading as RGBA_1010102
251 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
252 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800253 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700254}
255
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800256void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
257 const ui::Transform& transform, const Rect& viewport,
Peiyong Linc502cb72019-03-01 15:00:23 -0800258 int32_t supportedPerFrameMetadata,
259 const ui::Dataspace targetDataspace) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800260 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700261
David Sodman0c69cad2017-08-21 12:12:51 -0700262 // Apply this display's projection's viewport to the visible region
263 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700264 Region visible = transform.transform(visibleRegion.intersect(viewport));
265
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800266 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
267 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
268
269 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700270 auto error = hwcLayer->setVisibleRegion(visible);
271 if (error != HWC2::Error::None) {
272 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
273 to_string(error).c_str(), static_cast<int32_t>(error));
274 visible.dump(LOG_TAG);
275 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800276 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700277
Lloyd Pique0b785d82018-12-04 17:25:27 -0800278 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
279
David Sodman15094112018-10-11 09:39:37 -0700280 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
281 if (error != HWC2::Error::None) {
282 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
283 to_string(error).c_str(), static_cast<int32_t>(error));
284 surfaceDamageRegion.dump(LOG_TAG);
285 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800286 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700287
288 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800289 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800290 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700291 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800292 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700293 if (error != HWC2::Error::None) {
294 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800295 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700296 static_cast<int32_t>(error));
297 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800298 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700299 return;
300 }
301
David Sodman15094112018-10-11 09:39:37 -0700302 // Device or Cursor layers
303 if (mPotentialCursor) {
304 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800305 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700306 } else {
307 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800308 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700309 }
310
Peiyong Lin34ea5b92019-03-15 18:40:15 -0700311 ui::Dataspace dataspace = isColorSpaceAgnostic() && targetDataspace != ui::Dataspace::UNKNOWN
312 ? targetDataspace
313 : mCurrentDataSpace;
Peiyong Linc502cb72019-03-01 15:00:23 -0800314 error = hwcLayer->setDataspace(dataspace);
David Sodman15094112018-10-11 09:39:37 -0700315 if (error != HWC2::Error::None) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800316 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), dataspace,
David Sodman15094112018-10-11 09:39:37 -0700317 to_string(error).c_str(), static_cast<int32_t>(error));
318 }
319
320 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700322 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
323 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
324 to_string(error).c_str(), static_cast<int32_t>(error));
325 }
326
327 error = hwcLayer->setColorTransform(getColorTransform());
Peiyong Lin04d25872019-04-18 10:26:19 -0700328 if (error == HWC2::Error::Unsupported) {
329 // If per layer color transform is not supported, we use GPU composition.
330 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CLIENT);
331 } else if (error != HWC2::Error::None) {
David Sodman15094112018-10-11 09:39:37 -0700332 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
333 to_string(error).c_str(), static_cast<int32_t>(error));
334 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800335 layerCompositionState.dataspace = mCurrentDataSpace;
336 layerCompositionState.colorTransform = getColorTransform();
337 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700338
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800339 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700340}
341
Marissa Wallfd668622018-05-10 10:21:13 -0700342bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
343 if (mBufferLatched) {
344 Mutex::Autolock lock(mFrameEventHistoryMutex);
345 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700346 }
Marissa Wallfd668622018-05-10 10:21:13 -0700347 mRefreshPending = false;
348 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700349}
350
Dominik Laskowski075d3172018-05-24 15:50:06 -0700351bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
352 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700353 const std::shared_ptr<FenceTime>& presentFence,
354 const CompositorTiming& compositorTiming) {
355 // mFrameLatencyNeeded is true when a new frame was latched for the
356 // composition.
357 if (!mFrameLatencyNeeded) return false;
358
359 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700360 {
Marissa Wallfd668622018-05-10 10:21:13 -0700361 Mutex::Autolock lock(mFrameEventHistoryMutex);
362 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
363 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700364 }
365
Marissa Wallfd668622018-05-10 10:21:13 -0700366 // Update mFrameTracker.
367 nsecs_t desiredPresentTime = getDesiredPresentTime();
368 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
369
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700370 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800371 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700372
373 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
374 if (frameReadyFence->isValid()) {
375 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
376 } else {
377 // There was no fence for this frame, so assume that it was ready
378 // to be presented at the desired present time.
379 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700380 }
Marissa Wallfd668622018-05-10 10:21:13 -0700381
382 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800383 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700384 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700385 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700386 // The HWC doesn't support present fences, so use the refresh
387 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700388 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800389 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700390 mFrameTracker.setActualPresentTime(actualPresentTime);
391 }
392
393 mFrameTracker.advanceFrame();
394 mFrameLatencyNeeded = false;
395 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700396}
397
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700398bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
399 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700400 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700401
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800402 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700403
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800404 if (refreshRequired) {
405 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700406 }
407
Marissa Wallfd668622018-05-10 10:21:13 -0700408 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800409 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700410 }
David Sodman0c69cad2017-08-21 12:12:51 -0700411
Marissa Wallfd668622018-05-10 10:21:13 -0700412 // if we've already called updateTexImage() without going through
413 // a composition step, we have to skip this layer at this point
414 // because we cannot call updateTeximage() without a corresponding
415 // compositionComplete() call.
416 // we'll trigger an update in onPreComposition().
417 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800418 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700419 }
420
421 // If the head buffer's acquire fence hasn't signaled yet, return and
422 // try again later
423 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700424 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700425 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800426 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700427 }
428
429 // Capture the old state of the layer for comparisons later
430 const State& s(getDrawingState());
431 const bool oldOpacity = isOpaque(s);
432 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
433
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700434 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700435 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800436 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700437 }
438
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700439 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700440 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800441 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700442 }
443
444 err = updateActiveBuffer();
445 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800446 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700447 }
448
449 mBufferLatched = true;
450
451 err = updateFrameNumber(latchTime);
452 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800453 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700454 }
455
456 mRefreshPending = true;
457 mFrameLatencyNeeded = true;
458 if (oldBuffer == nullptr) {
459 // the first time we receive a buffer, we need to trigger a
460 // geometry invalidation.
461 recomputeVisibleRegions = true;
462 }
463
464 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800465 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700466 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800467 case ui::Dataspace::SRGB:
468 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700469 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800470 case ui::Dataspace::SRGB_LINEAR:
471 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700472 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800473 case ui::Dataspace::JFIF:
474 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700475 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800476 case ui::Dataspace::BT601_625:
477 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700478 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800479 case ui::Dataspace::BT601_525:
480 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700481 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800482 case ui::Dataspace::BT709:
483 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700484 break;
485 default:
486 break;
487 }
488 mCurrentDataSpace = dataSpace;
489
490 Rect crop(getDrawingCrop());
491 const uint32_t transform(getDrawingTransform());
492 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800493 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700494 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800495 (scalingMode != mCurrentScalingMode) ||
496 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700497 mCurrentCrop = crop;
498 mCurrentTransform = transform;
499 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800500 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700501 recomputeVisibleRegions = true;
502 }
503
504 if (oldBuffer != nullptr) {
505 uint32_t bufWidth = mActiveBuffer->getWidth();
506 uint32_t bufHeight = mActiveBuffer->getHeight();
507 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
508 recomputeVisibleRegions = true;
509 }
510 }
511
512 if (oldOpacity != isOpaque(s)) {
513 recomputeVisibleRegions = true;
514 }
515
516 // Remove any sync points corresponding to the buffer which was just
517 // latched
518 {
519 Mutex::Autolock lock(mLocalSyncPointMutex);
520 auto point = mLocalSyncPoints.begin();
521 while (point != mLocalSyncPoints.end()) {
522 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
523 // This sync point must have been added since we started
524 // latching. Don't drop it yet.
525 ++point;
526 continue;
527 }
528
529 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700530 std::stringstream ss;
531 ss << "Dropping sync point " << (*point)->getFrameNumber();
532 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700533 point = mLocalSyncPoints.erase(point);
534 } else {
535 ++point;
536 }
537 }
538 }
539
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800540 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700541}
542
543// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700544void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
545 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700546 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700547 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700548 Mutex::Autolock lock(mLocalSyncPointMutex);
549 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700550 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
551 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700552 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700553 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
554 if (requestedSyncLayer) {
555 // Need to update the transaction flag to ensure the layer's pending transaction
556 // gets applied.
557 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
558 }
Marissa Wallfd668622018-05-10 10:21:13 -0700559 }
David Sodman0c69cad2017-08-21 12:12:51 -0700560 }
561}
562
Marissa Wallfd668622018-05-10 10:21:13 -0700563bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700564 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700565}
566
567uint32_t BufferLayer::getEffectiveScalingMode() const {
568 if (mOverrideScalingMode >= 0) {
569 return mOverrideScalingMode;
570 }
571
572 return mCurrentScalingMode;
573}
574
575bool BufferLayer::isProtected() const {
576 const sp<GraphicBuffer>& buffer(mActiveBuffer);
577 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
578}
579
580bool BufferLayer::latchUnsignaledBuffers() {
581 static bool propertyLoaded = false;
582 static bool latch = false;
583 static std::mutex mutex;
584 std::lock_guard<std::mutex> lock(mutex);
585 if (!propertyLoaded) {
586 char value[PROPERTY_VALUE_MAX] = {};
587 property_get("debug.sf.latch_unsignaled", value, "0");
588 latch = atoi(value);
589 propertyLoaded = true;
590 }
591 return latch;
592}
593
594// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700595bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
596 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700597 bool matchingFramesFound = false;
598 bool allTransactionsApplied = true;
599 Mutex::Autolock lock(mLocalSyncPointMutex);
600
601 for (auto& point : mLocalSyncPoints) {
602 if (point->getFrameNumber() > headFrameNumber) {
603 break;
604 }
605 matchingFramesFound = true;
606
607 if (!point->frameIsAvailable()) {
608 // We haven't notified the remote layer that the frame for
609 // this point is available yet. Notify it now, and then
610 // abort this attempt to latch.
611 point->setFrameAvailable();
612 allTransactionsApplied = false;
613 break;
614 }
615
616 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
617 }
618 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700619}
620
621// As documented in libhardware header, formats in the range
622// 0x100 - 0x1FF are specific to the HAL implementation, and
623// are known to have no alpha channel
624// TODO: move definition for device-specific range into
625// hardware.h, instead of using hard-coded values here.
626#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
627
628bool BufferLayer::getOpacityForFormat(uint32_t format) {
629 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
630 return true;
631 }
632 switch (format) {
633 case HAL_PIXEL_FORMAT_RGBA_8888:
634 case HAL_PIXEL_FORMAT_BGRA_8888:
635 case HAL_PIXEL_FORMAT_RGBA_FP16:
636 case HAL_PIXEL_FORMAT_RGBA_1010102:
637 return false;
638 }
639 // in all other case, we have no blending (also for unknown formats)
640 return true;
641}
642
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800643bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
644 // If we are not capturing based on the state of a known display device, we
645 // only return mNeedsFiltering
646 if (displayDevice == nullptr) {
647 return mNeedsFiltering;
648 }
649
650 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
651 if (outputLayer == nullptr) {
652 return mNeedsFiltering;
653 }
654
655 const auto& compositionState = outputLayer->getState();
656 const auto displayFrame = compositionState.displayFrame;
657 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800658 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
659 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700660}
661
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700662uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700664 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700665 } else {
666 return mCurrentFrameNumber;
667 }
668}
669
Vishnu Nair60356342018-11-13 13:00:45 -0800670Rect BufferLayer::getBufferSize(const State& s) const {
671 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
672 // we cannot determine the buffer size.
673 if ((s.sidebandStream != nullptr) ||
674 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
675 return Rect(getActiveWidth(s), getActiveHeight(s));
676 }
677
678 if (mActiveBuffer == nullptr) {
679 return Rect::INVALID_RECT;
680 }
681
682 uint32_t bufWidth = mActiveBuffer->getWidth();
683 uint32_t bufHeight = mActiveBuffer->getHeight();
684
685 // Undo any transformations on the buffer and return the result.
686 if (mCurrentTransform & ui::Transform::ROT_90) {
687 std::swap(bufWidth, bufHeight);
688 }
689
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800690 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800691 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
692 if (invTransform & ui::Transform::ROT_90) {
693 std::swap(bufWidth, bufHeight);
694 }
695 }
696
697 return Rect(bufWidth, bufHeight);
698}
699
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800700std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
701 return mCompositionLayer;
702}
703
Vishnu Nair4351ad52019-02-11 14:13:02 -0800704FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
705 const State& s(getDrawingState());
706
707 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
708 // we cannot determine the buffer size.
709 if ((s.sidebandStream != nullptr) ||
710 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
711 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
712 }
713
714 if (mActiveBuffer == nullptr) {
715 return parentBounds;
716 }
717
718 uint32_t bufWidth = mActiveBuffer->getWidth();
719 uint32_t bufHeight = mActiveBuffer->getHeight();
720
721 // Undo any transformations on the buffer and return the result.
722 if (mCurrentTransform & ui::Transform::ROT_90) {
723 std::swap(bufWidth, bufHeight);
724 }
725
726 if (getTransformToDisplayInverse()) {
727 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
728 if (invTransform & ui::Transform::ROT_90) {
729 std::swap(bufWidth, bufHeight);
730 }
731 }
732
733 return FloatRect(0, 0, bufWidth, bufHeight);
734}
735
David Sodman0c69cad2017-08-21 12:12:51 -0700736} // namespace android
737
738#if defined(__gl_h_)
739#error "don't include gl/gl.h in this file"
740#endif
741
742#if defined(__gl2_h_)
743#error "don't include gl2/gl2.h in this file"
744#endif