blob: 4751e5f122f23551998e415dfd87492d153623a7 [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 {
100 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800101 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700102}
103
104bool BufferLayer::isFixedSize() const {
105 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
106}
107
Lloyd Piquea83776c2019-01-29 18:42:32 -0800108bool BufferLayer::usesSourceCrop() const {
109 return true;
110}
111
David Sodman0c69cad2017-08-21 12:12:51 -0700112static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800113 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
114 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
115 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 -0700116 mat4 tr;
117
118 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
119 tr = tr * rot90;
120 }
121 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
122 tr = tr * flipH;
123 }
124 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
125 tr = tr * flipV;
126 }
127 return inverse(tr);
128}
129
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000130bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
131 bool useIdentityTransform, Region& clearRegion,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800132 const bool supportProtectedContent,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000133 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700134 ATRACE_CALL();
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800135 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion,
136 supportProtectedContent, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800137 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700138 // the texture has not been created yet, this Layer has
139 // in fact never been drawn into. This happens frequently with
140 // SurfaceView because the WindowManager can't know when the client
141 // has drawn the first time.
142
143 // If there is nothing under us, we paint the screen in black, otherwise
144 // we just skip this update.
145
146 // figure out if there is something below us
147 Region under;
148 bool finished = false;
149 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
150 if (finished || layer == static_cast<BufferLayer const*>(this)) {
151 finished = true;
152 return;
153 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000154 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700155 });
156 // if not everything below us is covered, we plug the holes!
157 Region holes(clip.subtract(under));
158 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700160 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000161 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700162 }
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800163 bool blackOutLayer =
164 (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000165 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700166 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000167 layer.source.buffer.buffer = mActiveBuffer;
168 layer.source.buffer.isOpaque = isOpaque(s);
169 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000170 layer.source.buffer.textureName = mTextureName;
171 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
172 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700173 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800174 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
175 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700176
177 // Query the texture matrix given our current filtering mode.
178 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700179 setFilteringEnabled(useFiltering);
180 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700181
182 if (getTransformToDisplayInverse()) {
183 /*
184 * the code below applies the primary display's inverse transform to
185 * the texture transform
186 */
187 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
188 mat4 tr = inverseOrientation(transform);
189
190 /**
191 * TODO(b/36727915): This is basically a hack.
192 *
193 * Ensure that regardless of the parent transformation,
194 * this buffer is always transformed from native display
195 * orientation to display orientation. For example, in the case
196 * of a camera where the buffer remains in native orientation,
197 * we want the pixels to always be upright.
198 */
199 sp<Layer> p = mDrawingParent.promote();
200 if (p != nullptr) {
201 const auto parentTransform = p->getTransform();
202 tr = tr * inverseOrientation(parentTransform.getOrientation());
203 }
204
205 // and finally apply it to the original texture matrix
206 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
207 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
208 }
209
Vishnu Nair4351ad52019-02-11 14:13:02 -0800210 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800211 float bufferWidth = getBufferSize(s).getWidth();
212 float bufferHeight = getBufferSize(s).getHeight();
213
214 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
215 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
216 // ignore them.
217 if (!getBufferSize(s).isValid()) {
218 bufferWidth = float(win.right) - float(win.left);
219 bufferHeight = float(win.bottom) - float(win.top);
220 }
David Sodman0c69cad2017-08-21 12:12:51 -0700221
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000222 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
223 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
224 const float translateY = float(win.top) / bufferHeight;
225 const float translateX = float(win.left) / bufferWidth;
226
227 // Flip y-coordinates because GLConsumer expects OpenGL convention.
228 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
229 mat4::translate(vec4(-.5, -.5, 0, 1)) *
230 mat4::translate(vec4(translateX, translateY, 0, 1)) *
231 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
232
233 layer.source.buffer.useTextureFiltering = useFiltering;
234 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700235 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000236 // If layer is blacked out, force alpha to 1 so that we draw a black color
237 // layer.
238 layer.source.buffer.buffer = nullptr;
239 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700240 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000241
242 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700243}
244
Marissa Wallfd668622018-05-10 10:21:13 -0700245bool BufferLayer::isHdrY410() const {
246 // pixel format is HDR Y410 masquerading as RGBA_1010102
247 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
248 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800249 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700250}
251
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800252void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
253 const ui::Transform& transform, const Rect& viewport,
Peiyong Linc502cb72019-03-01 15:00:23 -0800254 int32_t supportedPerFrameMetadata,
255 const ui::Dataspace targetDataspace) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800256 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700257
David Sodman0c69cad2017-08-21 12:12:51 -0700258 // Apply this display's projection's viewport to the visible region
259 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700260 Region visible = transform.transform(visibleRegion.intersect(viewport));
261
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800262 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
263 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
264
265 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700266 auto error = hwcLayer->setVisibleRegion(visible);
267 if (error != HWC2::Error::None) {
268 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
269 to_string(error).c_str(), static_cast<int32_t>(error));
270 visible.dump(LOG_TAG);
271 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800272 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700273
Lloyd Pique0b785d82018-12-04 17:25:27 -0800274 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
275
David Sodman15094112018-10-11 09:39:37 -0700276 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
277 if (error != HWC2::Error::None) {
278 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
279 to_string(error).c_str(), static_cast<int32_t>(error));
280 surfaceDamageRegion.dump(LOG_TAG);
281 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800282 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700283
284 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800285 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800286 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700287 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800288 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700289 if (error != HWC2::Error::None) {
290 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800291 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700292 static_cast<int32_t>(error));
293 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800294 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700295 return;
296 }
297
David Sodman15094112018-10-11 09:39:37 -0700298 // Device or Cursor layers
299 if (mPotentialCursor) {
300 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800301 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700302 } else {
303 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800304 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700305 }
306
Peiyong Linc502cb72019-03-01 15:00:23 -0800307 ui::Dataspace dataspace = isColorSpaceAgnostic() ? targetDataspace : mCurrentDataSpace;
308 error = hwcLayer->setDataspace(dataspace);
David Sodman15094112018-10-11 09:39:37 -0700309 if (error != HWC2::Error::None) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800310 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), dataspace,
David Sodman15094112018-10-11 09:39:37 -0700311 to_string(error).c_str(), static_cast<int32_t>(error));
312 }
313
314 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700315 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700316 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
317 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
318 to_string(error).c_str(), static_cast<int32_t>(error));
319 }
320
321 error = hwcLayer->setColorTransform(getColorTransform());
322 if (error != HWC2::Error::None) {
323 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
324 to_string(error).c_str(), static_cast<int32_t>(error));
325 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800326 layerCompositionState.dataspace = mCurrentDataSpace;
327 layerCompositionState.colorTransform = getColorTransform();
328 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700329
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800330 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700331}
332
Marissa Wallfd668622018-05-10 10:21:13 -0700333bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
334 if (mBufferLatched) {
335 Mutex::Autolock lock(mFrameEventHistoryMutex);
336 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700337 }
Marissa Wallfd668622018-05-10 10:21:13 -0700338 mRefreshPending = false;
339 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700340}
341
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
343 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700344 const std::shared_ptr<FenceTime>& presentFence,
345 const CompositorTiming& compositorTiming) {
346 // mFrameLatencyNeeded is true when a new frame was latched for the
347 // composition.
348 if (!mFrameLatencyNeeded) return false;
349
350 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700351 {
Marissa Wallfd668622018-05-10 10:21:13 -0700352 Mutex::Autolock lock(mFrameEventHistoryMutex);
353 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
354 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700355 }
356
Marissa Wallfd668622018-05-10 10:21:13 -0700357 // Update mFrameTracker.
358 nsecs_t desiredPresentTime = getDesiredPresentTime();
359 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
360
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700361 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800362 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700363
364 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
365 if (frameReadyFence->isValid()) {
366 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
367 } else {
368 // There was no fence for this frame, so assume that it was ready
369 // to be presented at the desired present time.
370 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700371 }
Marissa Wallfd668622018-05-10 10:21:13 -0700372
373 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800374 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700375 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700376 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700377 // The HWC doesn't support present fences, so use the refresh
378 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700379 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800380 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700381 mFrameTracker.setActualPresentTime(actualPresentTime);
382 }
383
384 mFrameTracker.advanceFrame();
385 mFrameLatencyNeeded = false;
386 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700387}
388
Alec Mouri56e538f2019-01-14 15:22:01 -0800389bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700390 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700391
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800392 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700393
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800394 if (refreshRequired) {
395 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700396 }
397
Marissa Wallfd668622018-05-10 10:21:13 -0700398 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800399 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700400 }
David Sodman0c69cad2017-08-21 12:12:51 -0700401
Marissa Wallfd668622018-05-10 10:21:13 -0700402 // if we've already called updateTexImage() without going through
403 // a composition step, we have to skip this layer at this point
404 // because we cannot call updateTeximage() without a corresponding
405 // compositionComplete() call.
406 // we'll trigger an update in onPreComposition().
407 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800408 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700409 }
410
411 // If the head buffer's acquire fence hasn't signaled yet, return and
412 // try again later
413 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700414 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800415 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700416 }
417
418 // Capture the old state of the layer for comparisons later
419 const State& s(getDrawingState());
420 const bool oldOpacity = isOpaque(s);
421 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
422
423 if (!allTransactionsSignaled()) {
424 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800425 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700426 }
427
Alec Mouri56e538f2019-01-14 15:22:01 -0800428 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700429 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800430 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700431 }
432
433 err = updateActiveBuffer();
434 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800435 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700436 }
437
438 mBufferLatched = true;
439
440 err = updateFrameNumber(latchTime);
441 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800442 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700443 }
444
445 mRefreshPending = true;
446 mFrameLatencyNeeded = true;
447 if (oldBuffer == nullptr) {
448 // the first time we receive a buffer, we need to trigger a
449 // geometry invalidation.
450 recomputeVisibleRegions = true;
451 }
452
453 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800454 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700455 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800456 case ui::Dataspace::SRGB:
457 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700458 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800459 case ui::Dataspace::SRGB_LINEAR:
460 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700461 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800462 case ui::Dataspace::JFIF:
463 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700464 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800465 case ui::Dataspace::BT601_625:
466 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700467 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800468 case ui::Dataspace::BT601_525:
469 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700470 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800471 case ui::Dataspace::BT709:
472 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700473 break;
474 default:
475 break;
476 }
477 mCurrentDataSpace = dataSpace;
478
479 Rect crop(getDrawingCrop());
480 const uint32_t transform(getDrawingTransform());
481 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800482 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700483 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800484 (scalingMode != mCurrentScalingMode) ||
485 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700486 mCurrentCrop = crop;
487 mCurrentTransform = transform;
488 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800489 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700490 recomputeVisibleRegions = true;
491 }
492
493 if (oldBuffer != nullptr) {
494 uint32_t bufWidth = mActiveBuffer->getWidth();
495 uint32_t bufHeight = mActiveBuffer->getHeight();
496 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
497 recomputeVisibleRegions = true;
498 }
499 }
500
501 if (oldOpacity != isOpaque(s)) {
502 recomputeVisibleRegions = true;
503 }
504
505 // Remove any sync points corresponding to the buffer which was just
506 // latched
507 {
508 Mutex::Autolock lock(mLocalSyncPointMutex);
509 auto point = mLocalSyncPoints.begin();
510 while (point != mLocalSyncPoints.end()) {
511 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
512 // This sync point must have been added since we started
513 // latching. Don't drop it yet.
514 ++point;
515 continue;
516 }
517
518 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
519 point = mLocalSyncPoints.erase(point);
520 } else {
521 ++point;
522 }
523 }
524 }
525
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800526 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700527}
528
529// transaction
530void BufferLayer::notifyAvailableFrames() {
531 auto headFrameNumber = getHeadFrameNumber();
532 bool headFenceSignaled = fenceHasSignaled();
533 Mutex::Autolock lock(mLocalSyncPointMutex);
534 for (auto& point : mLocalSyncPoints) {
535 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
536 point->setFrameAvailable();
537 }
David Sodman0c69cad2017-08-21 12:12:51 -0700538 }
539}
540
Marissa Wallfd668622018-05-10 10:21:13 -0700541bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700542 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700543}
544
545uint32_t BufferLayer::getEffectiveScalingMode() const {
546 if (mOverrideScalingMode >= 0) {
547 return mOverrideScalingMode;
548 }
549
550 return mCurrentScalingMode;
551}
552
553bool BufferLayer::isProtected() const {
554 const sp<GraphicBuffer>& buffer(mActiveBuffer);
555 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
556}
557
558bool BufferLayer::latchUnsignaledBuffers() {
559 static bool propertyLoaded = false;
560 static bool latch = false;
561 static std::mutex mutex;
562 std::lock_guard<std::mutex> lock(mutex);
563 if (!propertyLoaded) {
564 char value[PROPERTY_VALUE_MAX] = {};
565 property_get("debug.sf.latch_unsignaled", value, "0");
566 latch = atoi(value);
567 propertyLoaded = true;
568 }
569 return latch;
570}
571
572// h/w composer set-up
573bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800574 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700575 bool matchingFramesFound = false;
576 bool allTransactionsApplied = true;
577 Mutex::Autolock lock(mLocalSyncPointMutex);
578
579 for (auto& point : mLocalSyncPoints) {
580 if (point->getFrameNumber() > headFrameNumber) {
581 break;
582 }
583 matchingFramesFound = true;
584
585 if (!point->frameIsAvailable()) {
586 // We haven't notified the remote layer that the frame for
587 // this point is available yet. Notify it now, and then
588 // abort this attempt to latch.
589 point->setFrameAvailable();
590 allTransactionsApplied = false;
591 break;
592 }
593
594 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
595 }
596 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700597}
598
599// As documented in libhardware header, formats in the range
600// 0x100 - 0x1FF are specific to the HAL implementation, and
601// are known to have no alpha channel
602// TODO: move definition for device-specific range into
603// hardware.h, instead of using hard-coded values here.
604#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
605
606bool BufferLayer::getOpacityForFormat(uint32_t format) {
607 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
608 return true;
609 }
610 switch (format) {
611 case HAL_PIXEL_FORMAT_RGBA_8888:
612 case HAL_PIXEL_FORMAT_BGRA_8888:
613 case HAL_PIXEL_FORMAT_RGBA_FP16:
614 case HAL_PIXEL_FORMAT_RGBA_1010102:
615 return false;
616 }
617 // in all other case, we have no blending (also for unknown formats)
618 return true;
619}
620
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800621bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
622 // If we are not capturing based on the state of a known display device, we
623 // only return mNeedsFiltering
624 if (displayDevice == nullptr) {
625 return mNeedsFiltering;
626 }
627
628 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
629 if (outputLayer == nullptr) {
630 return mNeedsFiltering;
631 }
632
633 const auto& compositionState = outputLayer->getState();
634 const auto displayFrame = compositionState.displayFrame;
635 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800636 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
637 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700638}
639
David Sodman0c69cad2017-08-21 12:12:51 -0700640uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800641 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700642 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700643 } else {
644 return mCurrentFrameNumber;
645 }
646}
647
Vishnu Nair60356342018-11-13 13:00:45 -0800648Rect BufferLayer::getBufferSize(const State& s) const {
649 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
650 // we cannot determine the buffer size.
651 if ((s.sidebandStream != nullptr) ||
652 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
653 return Rect(getActiveWidth(s), getActiveHeight(s));
654 }
655
656 if (mActiveBuffer == nullptr) {
657 return Rect::INVALID_RECT;
658 }
659
660 uint32_t bufWidth = mActiveBuffer->getWidth();
661 uint32_t bufHeight = mActiveBuffer->getHeight();
662
663 // Undo any transformations on the buffer and return the result.
664 if (mCurrentTransform & ui::Transform::ROT_90) {
665 std::swap(bufWidth, bufHeight);
666 }
667
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800668 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800669 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
670 if (invTransform & ui::Transform::ROT_90) {
671 std::swap(bufWidth, bufHeight);
672 }
673 }
674
675 return Rect(bufWidth, bufHeight);
676}
677
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800678std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
679 return mCompositionLayer;
680}
681
Vishnu Nair4351ad52019-02-11 14:13:02 -0800682FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
683 const State& s(getDrawingState());
684
685 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
686 // we cannot determine the buffer size.
687 if ((s.sidebandStream != nullptr) ||
688 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
689 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
690 }
691
692 if (mActiveBuffer == nullptr) {
693 return parentBounds;
694 }
695
696 uint32_t bufWidth = mActiveBuffer->getWidth();
697 uint32_t bufHeight = mActiveBuffer->getHeight();
698
699 // Undo any transformations on the buffer and return the result.
700 if (mCurrentTransform & ui::Transform::ROT_90) {
701 std::swap(bufWidth, bufHeight);
702 }
703
704 if (getTransformToDisplayInverse()) {
705 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
706 if (invTransform & ui::Transform::ROT_90) {
707 std::swap(bufWidth, bufHeight);
708 }
709 }
710
711 return FloatRect(0, 0, bufWidth, bufHeight);
712}
713
David Sodman0c69cad2017-08-21 12:12:51 -0700714} // namespace android
715
716#if defined(__gl_h_)
717#error "don't include gl/gl.h in this file"
718#endif
719
720#if defined(__gl2_h_)
721#error "don't include gl2/gl2.h in this file"
722#endif