blob: bb18aa10dbff3ba790686b2cc3617a3a1bbef642 [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
David Sodman0c69cad2017-08-21 12:12:51 -0700108static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800109 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
110 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
111 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 -0700112 mat4 tr;
113
114 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
115 tr = tr * rot90;
116 }
117 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
118 tr = tr * flipH;
119 }
120 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
121 tr = tr * flipV;
122 }
123 return inverse(tr);
124}
125
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000126bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
127 bool useIdentityTransform, Region& clearRegion,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800128 const bool supportProtectedContent,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000129 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700130 ATRACE_CALL();
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800131 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion,
132 supportProtectedContent, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800133 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700134 // the texture has not been created yet, this Layer has
135 // in fact never been drawn into. This happens frequently with
136 // SurfaceView because the WindowManager can't know when the client
137 // has drawn the first time.
138
139 // If there is nothing under us, we paint the screen in black, otherwise
140 // we just skip this update.
141
142 // figure out if there is something below us
143 Region under;
144 bool finished = false;
145 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
146 if (finished || layer == static_cast<BufferLayer const*>(this)) {
147 finished = true;
148 return;
149 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000150 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700151 });
152 // if not everything below us is covered, we plug the holes!
153 Region holes(clip.subtract(under));
154 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000155 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700156 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000157 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700158 }
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800159 bool blackOutLayer =
160 (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000161 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700162 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000163 layer.source.buffer.buffer = mActiveBuffer;
164 layer.source.buffer.isOpaque = isOpaque(s);
165 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000166 layer.source.buffer.textureName = mTextureName;
167 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
168 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700169 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800170 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
171 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700172
173 // Query the texture matrix given our current filtering mode.
174 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700175 setFilteringEnabled(useFiltering);
176 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700177
178 if (getTransformToDisplayInverse()) {
179 /*
180 * the code below applies the primary display's inverse transform to
181 * the texture transform
182 */
183 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
184 mat4 tr = inverseOrientation(transform);
185
186 /**
187 * TODO(b/36727915): This is basically a hack.
188 *
189 * Ensure that regardless of the parent transformation,
190 * this buffer is always transformed from native display
191 * orientation to display orientation. For example, in the case
192 * of a camera where the buffer remains in native orientation,
193 * we want the pixels to always be upright.
194 */
195 sp<Layer> p = mDrawingParent.promote();
196 if (p != nullptr) {
197 const auto parentTransform = p->getTransform();
198 tr = tr * inverseOrientation(parentTransform.getOrientation());
199 }
200
201 // and finally apply it to the original texture matrix
202 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
203 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
204 }
205
Vishnu Nair4351ad52019-02-11 14:13:02 -0800206 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800207 float bufferWidth = getBufferSize(s).getWidth();
208 float bufferHeight = getBufferSize(s).getHeight();
209
210 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
211 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
212 // ignore them.
213 if (!getBufferSize(s).isValid()) {
214 bufferWidth = float(win.right) - float(win.left);
215 bufferHeight = float(win.bottom) - float(win.top);
216 }
David Sodman0c69cad2017-08-21 12:12:51 -0700217
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000218 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
219 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
220 const float translateY = float(win.top) / bufferHeight;
221 const float translateX = float(win.left) / bufferWidth;
222
223 // Flip y-coordinates because GLConsumer expects OpenGL convention.
224 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
225 mat4::translate(vec4(-.5, -.5, 0, 1)) *
226 mat4::translate(vec4(translateX, translateY, 0, 1)) *
227 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
228
229 layer.source.buffer.useTextureFiltering = useFiltering;
230 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700231 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000232 // If layer is blacked out, force alpha to 1 so that we draw a black color
233 // layer.
234 layer.source.buffer.buffer = nullptr;
235 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700236 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000237
238 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700239}
240
Marissa Wallfd668622018-05-10 10:21:13 -0700241bool BufferLayer::isHdrY410() const {
242 // pixel format is HDR Y410 masquerading as RGBA_1010102
243 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
244 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800245 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700246}
247
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800248void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
249 const ui::Transform& transform, const Rect& viewport,
Peiyong Linc502cb72019-03-01 15:00:23 -0800250 int32_t supportedPerFrameMetadata,
251 const ui::Dataspace targetDataspace) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800252 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700253
David Sodman0c69cad2017-08-21 12:12:51 -0700254 // Apply this display's projection's viewport to the visible region
255 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700256 Region visible = transform.transform(visibleRegion.intersect(viewport));
257
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800258 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
259 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
260
261 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700262 auto error = hwcLayer->setVisibleRegion(visible);
263 if (error != HWC2::Error::None) {
264 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
265 to_string(error).c_str(), static_cast<int32_t>(error));
266 visible.dump(LOG_TAG);
267 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800268 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700269
Lloyd Pique0b785d82018-12-04 17:25:27 -0800270 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
271
David Sodman15094112018-10-11 09:39:37 -0700272 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
273 if (error != HWC2::Error::None) {
274 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
275 to_string(error).c_str(), static_cast<int32_t>(error));
276 surfaceDamageRegion.dump(LOG_TAG);
277 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800278 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700279
280 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800281 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800282 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700283 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800284 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700285 if (error != HWC2::Error::None) {
286 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800287 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700288 static_cast<int32_t>(error));
289 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800290 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700291 return;
292 }
293
David Sodman15094112018-10-11 09:39:37 -0700294 // Device or Cursor layers
295 if (mPotentialCursor) {
296 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800297 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700298 } else {
299 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800300 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700301 }
302
Peiyong Linc502cb72019-03-01 15:00:23 -0800303 ui::Dataspace dataspace = isColorSpaceAgnostic() ? targetDataspace : mCurrentDataSpace;
304 error = hwcLayer->setDataspace(dataspace);
David Sodman15094112018-10-11 09:39:37 -0700305 if (error != HWC2::Error::None) {
Peiyong Linc502cb72019-03-01 15:00:23 -0800306 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), dataspace,
David Sodman15094112018-10-11 09:39:37 -0700307 to_string(error).c_str(), static_cast<int32_t>(error));
308 }
309
310 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700311 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700312 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
313 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
314 to_string(error).c_str(), static_cast<int32_t>(error));
315 }
316
317 error = hwcLayer->setColorTransform(getColorTransform());
318 if (error != HWC2::Error::None) {
319 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
320 to_string(error).c_str(), static_cast<int32_t>(error));
321 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800322 layerCompositionState.dataspace = mCurrentDataSpace;
323 layerCompositionState.colorTransform = getColorTransform();
324 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700325
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800326 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700327}
328
Marissa Wallfd668622018-05-10 10:21:13 -0700329bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
330 if (mBufferLatched) {
331 Mutex::Autolock lock(mFrameEventHistoryMutex);
332 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700333 }
Marissa Wallfd668622018-05-10 10:21:13 -0700334 mRefreshPending = false;
335 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700336}
337
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
339 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700340 const std::shared_ptr<FenceTime>& presentFence,
341 const CompositorTiming& compositorTiming) {
342 // mFrameLatencyNeeded is true when a new frame was latched for the
343 // composition.
344 if (!mFrameLatencyNeeded) return false;
345
346 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700347 {
Marissa Wallfd668622018-05-10 10:21:13 -0700348 Mutex::Autolock lock(mFrameEventHistoryMutex);
349 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
350 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700351 }
352
Marissa Wallfd668622018-05-10 10:21:13 -0700353 // Update mFrameTracker.
354 nsecs_t desiredPresentTime = getDesiredPresentTime();
355 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
356
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700357 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800358 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700359
360 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
361 if (frameReadyFence->isValid()) {
362 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
363 } else {
364 // There was no fence for this frame, so assume that it was ready
365 // to be presented at the desired present time.
366 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700367 }
Marissa Wallfd668622018-05-10 10:21:13 -0700368
369 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800370 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700371 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700372 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700373 // The HWC doesn't support present fences, so use the refresh
374 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700375 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800376 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700377 mFrameTracker.setActualPresentTime(actualPresentTime);
378 }
379
380 mFrameTracker.advanceFrame();
381 mFrameLatencyNeeded = false;
382 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700383}
384
Alec Mouri56e538f2019-01-14 15:22:01 -0800385bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700386 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700387
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800388 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700389
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800390 if (refreshRequired) {
391 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700392 }
393
Marissa Wallfd668622018-05-10 10:21:13 -0700394 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800395 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700396 }
David Sodman0c69cad2017-08-21 12:12:51 -0700397
Marissa Wallfd668622018-05-10 10:21:13 -0700398 // if we've already called updateTexImage() without going through
399 // a composition step, we have to skip this layer at this point
400 // because we cannot call updateTeximage() without a corresponding
401 // compositionComplete() call.
402 // we'll trigger an update in onPreComposition().
403 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800404 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700405 }
406
407 // If the head buffer's acquire fence hasn't signaled yet, return and
408 // try again later
409 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700410 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800411 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700412 }
413
414 // Capture the old state of the layer for comparisons later
415 const State& s(getDrawingState());
416 const bool oldOpacity = isOpaque(s);
417 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
418
419 if (!allTransactionsSignaled()) {
420 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800421 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700422 }
423
Alec Mouri56e538f2019-01-14 15:22:01 -0800424 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700425 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800426 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700427 }
428
429 err = updateActiveBuffer();
430 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800431 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700432 }
433
434 mBufferLatched = true;
435
436 err = updateFrameNumber(latchTime);
437 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800438 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700439 }
440
441 mRefreshPending = true;
442 mFrameLatencyNeeded = true;
443 if (oldBuffer == nullptr) {
444 // the first time we receive a buffer, we need to trigger a
445 // geometry invalidation.
446 recomputeVisibleRegions = true;
447 }
448
449 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800450 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700451 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800452 case ui::Dataspace::SRGB:
453 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700454 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800455 case ui::Dataspace::SRGB_LINEAR:
456 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700457 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800458 case ui::Dataspace::JFIF:
459 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700460 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800461 case ui::Dataspace::BT601_625:
462 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700463 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800464 case ui::Dataspace::BT601_525:
465 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700466 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800467 case ui::Dataspace::BT709:
468 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700469 break;
470 default:
471 break;
472 }
473 mCurrentDataSpace = dataSpace;
474
475 Rect crop(getDrawingCrop());
476 const uint32_t transform(getDrawingTransform());
477 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800478 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700479 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800480 (scalingMode != mCurrentScalingMode) ||
481 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700482 mCurrentCrop = crop;
483 mCurrentTransform = transform;
484 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800485 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700486 recomputeVisibleRegions = true;
487 }
488
489 if (oldBuffer != nullptr) {
490 uint32_t bufWidth = mActiveBuffer->getWidth();
491 uint32_t bufHeight = mActiveBuffer->getHeight();
492 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
493 recomputeVisibleRegions = true;
494 }
495 }
496
497 if (oldOpacity != isOpaque(s)) {
498 recomputeVisibleRegions = true;
499 }
500
501 // Remove any sync points corresponding to the buffer which was just
502 // latched
503 {
504 Mutex::Autolock lock(mLocalSyncPointMutex);
505 auto point = mLocalSyncPoints.begin();
506 while (point != mLocalSyncPoints.end()) {
507 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
508 // This sync point must have been added since we started
509 // latching. Don't drop it yet.
510 ++point;
511 continue;
512 }
513
514 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
515 point = mLocalSyncPoints.erase(point);
516 } else {
517 ++point;
518 }
519 }
520 }
521
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800522 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700523}
524
525// transaction
526void BufferLayer::notifyAvailableFrames() {
527 auto headFrameNumber = getHeadFrameNumber();
528 bool headFenceSignaled = fenceHasSignaled();
529 Mutex::Autolock lock(mLocalSyncPointMutex);
530 for (auto& point : mLocalSyncPoints) {
531 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
532 point->setFrameAvailable();
533 }
David Sodman0c69cad2017-08-21 12:12:51 -0700534 }
535}
536
Marissa Wallfd668622018-05-10 10:21:13 -0700537bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700538 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700539}
540
541uint32_t BufferLayer::getEffectiveScalingMode() const {
542 if (mOverrideScalingMode >= 0) {
543 return mOverrideScalingMode;
544 }
545
546 return mCurrentScalingMode;
547}
548
549bool BufferLayer::isProtected() const {
550 const sp<GraphicBuffer>& buffer(mActiveBuffer);
551 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
552}
553
554bool BufferLayer::latchUnsignaledBuffers() {
555 static bool propertyLoaded = false;
556 static bool latch = false;
557 static std::mutex mutex;
558 std::lock_guard<std::mutex> lock(mutex);
559 if (!propertyLoaded) {
560 char value[PROPERTY_VALUE_MAX] = {};
561 property_get("debug.sf.latch_unsignaled", value, "0");
562 latch = atoi(value);
563 propertyLoaded = true;
564 }
565 return latch;
566}
567
568// h/w composer set-up
569bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800570 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700571 bool matchingFramesFound = false;
572 bool allTransactionsApplied = true;
573 Mutex::Autolock lock(mLocalSyncPointMutex);
574
575 for (auto& point : mLocalSyncPoints) {
576 if (point->getFrameNumber() > headFrameNumber) {
577 break;
578 }
579 matchingFramesFound = true;
580
581 if (!point->frameIsAvailable()) {
582 // We haven't notified the remote layer that the frame for
583 // this point is available yet. Notify it now, and then
584 // abort this attempt to latch.
585 point->setFrameAvailable();
586 allTransactionsApplied = false;
587 break;
588 }
589
590 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
591 }
592 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700593}
594
595// As documented in libhardware header, formats in the range
596// 0x100 - 0x1FF are specific to the HAL implementation, and
597// are known to have no alpha channel
598// TODO: move definition for device-specific range into
599// hardware.h, instead of using hard-coded values here.
600#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
601
602bool BufferLayer::getOpacityForFormat(uint32_t format) {
603 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
604 return true;
605 }
606 switch (format) {
607 case HAL_PIXEL_FORMAT_RGBA_8888:
608 case HAL_PIXEL_FORMAT_BGRA_8888:
609 case HAL_PIXEL_FORMAT_RGBA_FP16:
610 case HAL_PIXEL_FORMAT_RGBA_1010102:
611 return false;
612 }
613 // in all other case, we have no blending (also for unknown formats)
614 return true;
615}
616
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800617bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
618 // If we are not capturing based on the state of a known display device, we
619 // only return mNeedsFiltering
620 if (displayDevice == nullptr) {
621 return mNeedsFiltering;
622 }
623
624 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
625 if (outputLayer == nullptr) {
626 return mNeedsFiltering;
627 }
628
629 const auto& compositionState = outputLayer->getState();
630 const auto displayFrame = compositionState.displayFrame;
631 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800632 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
633 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700634}
635
David Sodman0c69cad2017-08-21 12:12:51 -0700636uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800637 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700638 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700639 } else {
640 return mCurrentFrameNumber;
641 }
642}
643
Vishnu Nair60356342018-11-13 13:00:45 -0800644Rect BufferLayer::getBufferSize(const State& s) const {
645 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
646 // we cannot determine the buffer size.
647 if ((s.sidebandStream != nullptr) ||
648 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
649 return Rect(getActiveWidth(s), getActiveHeight(s));
650 }
651
652 if (mActiveBuffer == nullptr) {
653 return Rect::INVALID_RECT;
654 }
655
656 uint32_t bufWidth = mActiveBuffer->getWidth();
657 uint32_t bufHeight = mActiveBuffer->getHeight();
658
659 // Undo any transformations on the buffer and return the result.
660 if (mCurrentTransform & ui::Transform::ROT_90) {
661 std::swap(bufWidth, bufHeight);
662 }
663
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800664 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800665 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
666 if (invTransform & ui::Transform::ROT_90) {
667 std::swap(bufWidth, bufHeight);
668 }
669 }
670
671 return Rect(bufWidth, bufHeight);
672}
673
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800674std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
675 return mCompositionLayer;
676}
677
Vishnu Nair4351ad52019-02-11 14:13:02 -0800678FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
679 const State& s(getDrawingState());
680
681 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
682 // we cannot determine the buffer size.
683 if ((s.sidebandStream != nullptr) ||
684 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
685 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
686 }
687
688 if (mActiveBuffer == nullptr) {
689 return parentBounds;
690 }
691
692 uint32_t bufWidth = mActiveBuffer->getWidth();
693 uint32_t bufHeight = mActiveBuffer->getHeight();
694
695 // Undo any transformations on the buffer and return the result.
696 if (mCurrentTransform & ui::Transform::ROT_90) {
697 std::swap(bufWidth, bufHeight);
698 }
699
700 if (getTransformToDisplayInverse()) {
701 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
702 if (invTransform & ui::Transform::ROT_90) {
703 std::swap(bufWidth, bufHeight);
704 }
705 }
706
707 return FloatRect(0, 0, bufWidth, bufHeight);
708}
709
David Sodman0c69cad2017-08-21 12:12:51 -0700710} // namespace android
711
712#if defined(__gl_h_)
713#error "don't include gl/gl.h in this file"
714#endif
715
716#if defined(__gl2_h_)
717#error "don't include gl2/gl2.h in this file"
718#endif