blob: ba0f160c6950ddfd11573bf00fd334e829ee7bb8 [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
Lloyd Piquefeb73d72018-12-04 17:23:44 -080022#include <cmath>
23#include <cstdlib>
24#include <mutex>
25
26#include <compositionengine/CompositionEngine.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <compositionengine/Display.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080028#include <compositionengine/Layer.h>
29#include <compositionengine/LayerCreationArgs.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080033#include <cutils/compiler.h>
34#include <cutils/native_handle.h>
35#include <cutils/properties.h>
36#include <gui/BufferItem.h>
37#include <gui/BufferQueue.h>
38#include <gui/LayerDebugInfo.h>
39#include <gui/Surface.h>
40#include <renderengine/RenderEngine.h>
41#include <ui/DebugUtils.h>
42#include <utils/Errors.h>
43#include <utils/Log.h>
44#include <utils/NativeHandle.h>
45#include <utils/StopWatch.h>
46#include <utils/Trace.h>
47
David Sodman0c69cad2017-08-21 12:12:51 -070048#include "BufferLayer.h"
49#include "Colorizer.h"
50#include "DisplayDevice.h"
51#include "LayerRejecter.h"
David Sodman0c69cad2017-08-21 12:12:51 -070052
Yiwei Zhang7e666a52018-11-15 13:33:42 -080053#include "TimeStats/TimeStats.h"
54
David Sodman0c69cad2017-08-21 12:12:51 -070055namespace android {
56
Lloyd Pique42ab75e2018-09-12 20:46:03 -070057BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080058 : Layer(args),
59 mTextureName(args.flinger->getNewTexture()),
60 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
61 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070062 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070063
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070065
Lloyd Pique42ab75e2018-09-12 20:46:03 -070066 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
67 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070068}
69
70BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070071 mFlinger->deleteTextureAsync(mTextureName);
Yiwei Zhang7e666a52018-11-15 13:33:42 -080072 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070073}
74
David Sodmaneb085e02017-10-05 18:49:04 -070075void BufferLayer::useSurfaceDamage() {
76 if (mFlinger->mForceFullDamage) {
77 surfaceDamageRegion = Region::INVALID_REGION;
78 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070079 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070080 }
81}
82
83void BufferLayer::useEmptyDamage() {
84 surfaceDamageRegion.clear();
85}
86
Marissa Wallfd668622018-05-10 10:21:13 -070087bool BufferLayer::isOpaque(const Layer::State& s) const {
88 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
89 // layer's opaque flag.
Lloyd Pique0b785d82018-12-04 17:25:27 -080090 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070091 return false;
92 }
93
94 // if the layer has the opaque flag, then we're always opaque,
95 // otherwise we use the current buffer's format.
96 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070097}
98
99bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700100 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800101 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700102 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
103
104 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700105}
106
107bool BufferLayer::isFixedSize() const {
108 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
109}
110
Lloyd Piquea83776c2019-01-29 18:42:32 -0800111bool BufferLayer::usesSourceCrop() const {
112 return true;
113}
114
David Sodman0c69cad2017-08-21 12:12:51 -0700115static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800116 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
117 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
118 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 -0700119 mat4 tr;
120
121 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
122 tr = tr * rot90;
123 }
124 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
125 tr = tr * flipH;
126 }
127 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
128 tr = tr * flipV;
129 }
130 return inverse(tr);
131}
132
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000133bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
134 bool useIdentityTransform, Region& clearRegion,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800135 const bool supportProtectedContent,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000136 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700137 ATRACE_CALL();
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800138 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion,
139 supportProtectedContent, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800140 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700141 // the texture has not been created yet, this Layer has
142 // in fact never been drawn into. This happens frequently with
143 // SurfaceView because the WindowManager can't know when the client
144 // has drawn the first time.
145
146 // If there is nothing under us, we paint the screen in black, otherwise
147 // we just skip this update.
148
149 // figure out if there is something below us
150 Region under;
151 bool finished = false;
152 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
153 if (finished || layer == static_cast<BufferLayer const*>(this)) {
154 finished = true;
155 return;
156 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000157 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700158 });
159 // if not everything below us is covered, we plug the holes!
160 Region holes(clip.subtract(under));
161 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000162 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700163 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000164 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700165 }
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800166 bool blackOutLayer =
167 (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000168 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700169 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000170 layer.source.buffer.buffer = mActiveBuffer;
171 layer.source.buffer.isOpaque = isOpaque(s);
172 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000173 layer.source.buffer.textureName = mTextureName;
174 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
175 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700176 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800177 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
178 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700179
180 // Query the texture matrix given our current filtering mode.
181 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700182 setFilteringEnabled(useFiltering);
183 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700184
185 if (getTransformToDisplayInverse()) {
186 /*
187 * the code below applies the primary display's inverse transform to
188 * the texture transform
189 */
190 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
191 mat4 tr = inverseOrientation(transform);
192
193 /**
194 * TODO(b/36727915): This is basically a hack.
195 *
196 * Ensure that regardless of the parent transformation,
197 * this buffer is always transformed from native display
198 * orientation to display orientation. For example, in the case
199 * of a camera where the buffer remains in native orientation,
200 * we want the pixels to always be upright.
201 */
202 sp<Layer> p = mDrawingParent.promote();
203 if (p != nullptr) {
204 const auto parentTransform = p->getTransform();
205 tr = tr * inverseOrientation(parentTransform.getOrientation());
206 }
207
208 // and finally apply it to the original texture matrix
209 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
210 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
211 }
212
Vishnu Nair4351ad52019-02-11 14:13:02 -0800213 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800214 float bufferWidth = getBufferSize(s).getWidth();
215 float bufferHeight = getBufferSize(s).getHeight();
216
217 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
218 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
219 // ignore them.
220 if (!getBufferSize(s).isValid()) {
221 bufferWidth = float(win.right) - float(win.left);
222 bufferHeight = float(win.bottom) - float(win.top);
223 }
David Sodman0c69cad2017-08-21 12:12:51 -0700224
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000225 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
226 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
227 const float translateY = float(win.top) / bufferHeight;
228 const float translateX = float(win.left) / bufferWidth;
229
230 // Flip y-coordinates because GLConsumer expects OpenGL convention.
231 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
232 mat4::translate(vec4(-.5, -.5, 0, 1)) *
233 mat4::translate(vec4(translateX, translateY, 0, 1)) *
234 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
235
236 layer.source.buffer.useTextureFiltering = useFiltering;
237 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700238 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000239 // If layer is blacked out, force alpha to 1 so that we draw a black color
240 // layer.
241 layer.source.buffer.buffer = nullptr;
242 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700243 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000244
245 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700246}
247
Marissa Wallfd668622018-05-10 10:21:13 -0700248bool BufferLayer::isHdrY410() const {
249 // pixel format is HDR Y410 masquerading as RGBA_1010102
250 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
251 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800252 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700253}
254
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800255void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
256 const ui::Transform& transform, const Rect& viewport,
Peiyong Linc502cb72019-03-01 15:00:23 -0800257 int32_t supportedPerFrameMetadata,
258 const ui::Dataspace targetDataspace) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800259 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700260
David Sodman0c69cad2017-08-21 12:12:51 -0700261 // Apply this display's projection's viewport to the visible region
262 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700263 Region visible = transform.transform(visibleRegion.intersect(viewport));
264
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800265 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
266 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
267
268 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700269 auto error = hwcLayer->setVisibleRegion(visible);
270 if (error != HWC2::Error::None) {
271 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
272 to_string(error).c_str(), static_cast<int32_t>(error));
273 visible.dump(LOG_TAG);
274 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800275 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700276
Lloyd Pique0b785d82018-12-04 17:25:27 -0800277 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
278
David Sodman15094112018-10-11 09:39:37 -0700279 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
280 if (error != HWC2::Error::None) {
281 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
282 to_string(error).c_str(), static_cast<int32_t>(error));
283 surfaceDamageRegion.dump(LOG_TAG);
284 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800285 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700286
287 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800288 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800289 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700290 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800291 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700292 if (error != HWC2::Error::None) {
293 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800294 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700295 static_cast<int32_t>(error));
296 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800297 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700298 return;
299 }
300
David Sodman15094112018-10-11 09:39:37 -0700301 // Device or Cursor layers
302 if (mPotentialCursor) {
303 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800304 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700305 } else {
306 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800307 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700308 }
309
Peiyong Lin34ea5b92019-03-15 18:40:15 -0700310 ui::Dataspace dataspace = isColorSpaceAgnostic() && targetDataspace != ui::Dataspace::UNKNOWN
311 ? targetDataspace
312 : mCurrentDataSpace;
Peiyong Linc502cb72019-03-01 15:00:23 -0800313 error = hwcLayer->setDataspace(dataspace);
David Sodman15094112018-10-11 09:39:37 -0700314 if (error != HWC2::Error::None) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800315 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), dataspace,
David Sodman15094112018-10-11 09:39:37 -0700316 to_string(error).c_str(), static_cast<int32_t>(error));
317 }
318
319 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700320 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700321 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
322 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
323 to_string(error).c_str(), static_cast<int32_t>(error));
324 }
325
326 error = hwcLayer->setColorTransform(getColorTransform());
Peiyong Lin04d25872019-04-18 10:26:19 -0700327 if (error == HWC2::Error::Unsupported) {
328 // If per layer color transform is not supported, we use GPU composition.
329 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CLIENT);
330 } else if (error != HWC2::Error::None) {
David Sodman15094112018-10-11 09:39:37 -0700331 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
332 to_string(error).c_str(), static_cast<int32_t>(error));
333 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800334 layerCompositionState.dataspace = mCurrentDataSpace;
335 layerCompositionState.colorTransform = getColorTransform();
336 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700337
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800338 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700339}
340
Marissa Wallfd668622018-05-10 10:21:13 -0700341bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
342 if (mBufferLatched) {
343 Mutex::Autolock lock(mFrameEventHistoryMutex);
344 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700345 }
Marissa Wallfd668622018-05-10 10:21:13 -0700346 mRefreshPending = false;
347 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700348}
349
Dominik Laskowski075d3172018-05-24 15:50:06 -0700350bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
351 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700352 const std::shared_ptr<FenceTime>& presentFence,
353 const CompositorTiming& compositorTiming) {
354 // mFrameLatencyNeeded is true when a new frame was latched for the
355 // composition.
356 if (!mFrameLatencyNeeded) return false;
357
358 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700359 {
Marissa Wallfd668622018-05-10 10:21:13 -0700360 Mutex::Autolock lock(mFrameEventHistoryMutex);
361 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
362 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700363 }
364
Marissa Wallfd668622018-05-10 10:21:13 -0700365 // Update mFrameTracker.
366 nsecs_t desiredPresentTime = getDesiredPresentTime();
367 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
368
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700369 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800370 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700371
372 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
373 if (frameReadyFence->isValid()) {
374 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
375 } else {
376 // There was no fence for this frame, so assume that it was ready
377 // to be presented at the desired present time.
378 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700379 }
Marissa Wallfd668622018-05-10 10:21:13 -0700380
381 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800382 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700383 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700384 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700385 // The HWC doesn't support present fences, so use the refresh
386 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700387 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800388 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700389 mFrameTracker.setActualPresentTime(actualPresentTime);
390 }
391
392 mFrameTracker.advanceFrame();
393 mFrameLatencyNeeded = false;
394 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700395}
396
Alec Mouri56e538f2019-01-14 15:22:01 -0800397bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700398 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700399
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800400 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700401
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800402 if (refreshRequired) {
403 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700404 }
405
Marissa Wallfd668622018-05-10 10:21:13 -0700406 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800407 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700408 }
David Sodman0c69cad2017-08-21 12:12:51 -0700409
Marissa Wallfd668622018-05-10 10:21:13 -0700410 // if we've already called updateTexImage() without going through
411 // a composition step, we have to skip this layer at this point
412 // because we cannot call updateTeximage() without a corresponding
413 // compositionComplete() call.
414 // we'll trigger an update in onPreComposition().
415 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800416 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700417 }
418
419 // If the head buffer's acquire fence hasn't signaled yet, return and
420 // try again later
421 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700422 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700423 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800424 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700425 }
426
427 // Capture the old state of the layer for comparisons later
428 const State& s(getDrawingState());
429 const bool oldOpacity = isOpaque(s);
430 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
431
432 if (!allTransactionsSignaled()) {
433 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800434 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700435 }
436
Alec Mouri56e538f2019-01-14 15:22:01 -0800437 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700438 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800439 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700440 }
441
442 err = updateActiveBuffer();
443 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800444 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700445 }
446
447 mBufferLatched = true;
448
449 err = updateFrameNumber(latchTime);
450 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800451 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700452 }
453
454 mRefreshPending = true;
455 mFrameLatencyNeeded = true;
456 if (oldBuffer == nullptr) {
457 // the first time we receive a buffer, we need to trigger a
458 // geometry invalidation.
459 recomputeVisibleRegions = true;
460 }
461
462 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800463 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700464 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800465 case ui::Dataspace::SRGB:
466 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700467 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800468 case ui::Dataspace::SRGB_LINEAR:
469 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700470 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800471 case ui::Dataspace::JFIF:
472 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700473 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800474 case ui::Dataspace::BT601_625:
475 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700476 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800477 case ui::Dataspace::BT601_525:
478 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700479 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800480 case ui::Dataspace::BT709:
481 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700482 break;
483 default:
484 break;
485 }
486 mCurrentDataSpace = dataSpace;
487
488 Rect crop(getDrawingCrop());
489 const uint32_t transform(getDrawingTransform());
490 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800491 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700492 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800493 (scalingMode != mCurrentScalingMode) ||
494 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700495 mCurrentCrop = crop;
496 mCurrentTransform = transform;
497 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800498 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700499 recomputeVisibleRegions = true;
500 }
501
502 if (oldBuffer != nullptr) {
503 uint32_t bufWidth = mActiveBuffer->getWidth();
504 uint32_t bufHeight = mActiveBuffer->getHeight();
505 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
506 recomputeVisibleRegions = true;
507 }
508 }
509
510 if (oldOpacity != isOpaque(s)) {
511 recomputeVisibleRegions = true;
512 }
513
514 // Remove any sync points corresponding to the buffer which was just
515 // latched
516 {
517 Mutex::Autolock lock(mLocalSyncPointMutex);
518 auto point = mLocalSyncPoints.begin();
519 while (point != mLocalSyncPoints.end()) {
520 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
521 // This sync point must have been added since we started
522 // latching. Don't drop it yet.
523 ++point;
524 continue;
525 }
526
527 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
528 point = mLocalSyncPoints.erase(point);
529 } else {
530 ++point;
531 }
532 }
533 }
534
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800535 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700536}
537
538// transaction
539void BufferLayer::notifyAvailableFrames() {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700540 const auto headFrameNumber = getHeadFrameNumber();
541 const bool headFenceSignaled = fenceHasSignaled();
542 const bool presentTimeIsCurrent = framePresentTimeIsCurrent();
Marissa Wallfd668622018-05-10 10:21:13 -0700543 Mutex::Autolock lock(mLocalSyncPointMutex);
544 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700545 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
546 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700547 point->setFrameAvailable();
548 }
David Sodman0c69cad2017-08-21 12:12:51 -0700549 }
550}
551
Marissa Wallfd668622018-05-10 10:21:13 -0700552bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700553 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700554}
555
556uint32_t BufferLayer::getEffectiveScalingMode() const {
557 if (mOverrideScalingMode >= 0) {
558 return mOverrideScalingMode;
559 }
560
561 return mCurrentScalingMode;
562}
563
564bool BufferLayer::isProtected() const {
565 const sp<GraphicBuffer>& buffer(mActiveBuffer);
566 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
567}
568
569bool BufferLayer::latchUnsignaledBuffers() {
570 static bool propertyLoaded = false;
571 static bool latch = false;
572 static std::mutex mutex;
573 std::lock_guard<std::mutex> lock(mutex);
574 if (!propertyLoaded) {
575 char value[PROPERTY_VALUE_MAX] = {};
576 property_get("debug.sf.latch_unsignaled", value, "0");
577 latch = atoi(value);
578 propertyLoaded = true;
579 }
580 return latch;
581}
582
583// h/w composer set-up
584bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800585 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700586 bool matchingFramesFound = false;
587 bool allTransactionsApplied = true;
588 Mutex::Autolock lock(mLocalSyncPointMutex);
589
590 for (auto& point : mLocalSyncPoints) {
591 if (point->getFrameNumber() > headFrameNumber) {
592 break;
593 }
594 matchingFramesFound = true;
595
596 if (!point->frameIsAvailable()) {
597 // We haven't notified the remote layer that the frame for
598 // this point is available yet. Notify it now, and then
599 // abort this attempt to latch.
600 point->setFrameAvailable();
601 allTransactionsApplied = false;
602 break;
603 }
604
605 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
606 }
607 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700608}
609
610// As documented in libhardware header, formats in the range
611// 0x100 - 0x1FF are specific to the HAL implementation, and
612// are known to have no alpha channel
613// TODO: move definition for device-specific range into
614// hardware.h, instead of using hard-coded values here.
615#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
616
617bool BufferLayer::getOpacityForFormat(uint32_t format) {
618 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
619 return true;
620 }
621 switch (format) {
622 case HAL_PIXEL_FORMAT_RGBA_8888:
623 case HAL_PIXEL_FORMAT_BGRA_8888:
624 case HAL_PIXEL_FORMAT_RGBA_FP16:
625 case HAL_PIXEL_FORMAT_RGBA_1010102:
626 return false;
627 }
628 // in all other case, we have no blending (also for unknown formats)
629 return true;
630}
631
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800632bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
633 // If we are not capturing based on the state of a known display device, we
634 // only return mNeedsFiltering
635 if (displayDevice == nullptr) {
636 return mNeedsFiltering;
637 }
638
639 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
640 if (outputLayer == nullptr) {
641 return mNeedsFiltering;
642 }
643
644 const auto& compositionState = outputLayer->getState();
645 const auto displayFrame = compositionState.displayFrame;
646 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800647 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
648 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700649}
650
David Sodman0c69cad2017-08-21 12:12:51 -0700651uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800652 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700653 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700654 } else {
655 return mCurrentFrameNumber;
656 }
657}
658
Vishnu Nair60356342018-11-13 13:00:45 -0800659Rect BufferLayer::getBufferSize(const State& s) const {
660 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
661 // we cannot determine the buffer size.
662 if ((s.sidebandStream != nullptr) ||
663 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
664 return Rect(getActiveWidth(s), getActiveHeight(s));
665 }
666
667 if (mActiveBuffer == nullptr) {
668 return Rect::INVALID_RECT;
669 }
670
671 uint32_t bufWidth = mActiveBuffer->getWidth();
672 uint32_t bufHeight = mActiveBuffer->getHeight();
673
674 // Undo any transformations on the buffer and return the result.
675 if (mCurrentTransform & ui::Transform::ROT_90) {
676 std::swap(bufWidth, bufHeight);
677 }
678
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800679 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800680 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
681 if (invTransform & ui::Transform::ROT_90) {
682 std::swap(bufWidth, bufHeight);
683 }
684 }
685
686 return Rect(bufWidth, bufHeight);
687}
688
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800689std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
690 return mCompositionLayer;
691}
692
Vishnu Nair4351ad52019-02-11 14:13:02 -0800693FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
694 const State& s(getDrawingState());
695
696 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
697 // we cannot determine the buffer size.
698 if ((s.sidebandStream != nullptr) ||
699 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
700 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
701 }
702
703 if (mActiveBuffer == nullptr) {
704 return parentBounds;
705 }
706
707 uint32_t bufWidth = mActiveBuffer->getWidth();
708 uint32_t bufHeight = mActiveBuffer->getHeight();
709
710 // Undo any transformations on the buffer and return the result.
711 if (mCurrentTransform & ui::Transform::ROT_90) {
712 std::swap(bufWidth, bufHeight);
713 }
714
715 if (getTransformToDisplayInverse()) {
716 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
717 if (invTransform & ui::Transform::ROT_90) {
718 std::swap(bufWidth, bufHeight);
719 }
720 }
721
722 return FloatRect(0, 0, bufWidth, bufHeight);
723}
724
David Sodman0c69cad2017-08-21 12:12:51 -0700725} // namespace android
726
727#if defined(__gl_h_)
728#error "don't include gl/gl.h in this file"
729#endif
730
731#if defined(__gl2_h_)
732#error "don't include gl2/gl2.h in this file"
733#endif