blob: c077b6841fdf11d118c019b53b417f0e6fb442bc [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);
72
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080073 if (destroyAllHwcLayersPlusChildren()) {
David Sodman0c69cad2017-08-21 12:12:51 -070074 ALOGE("Found stale hardware composer layers when destroying "
75 "surface flinger layer %s",
76 mName.string());
David Sodman0c69cad2017-08-21 12:12:51 -070077 }
Yiwei Zhangdc224042018-10-18 15:34:00 -070078
Yiwei Zhang7e666a52018-11-15 13:33:42 -080079 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070080}
81
David Sodmaneb085e02017-10-05 18:49:04 -070082void BufferLayer::useSurfaceDamage() {
83 if (mFlinger->mForceFullDamage) {
84 surfaceDamageRegion = Region::INVALID_REGION;
85 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070086 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070087 }
88}
89
90void BufferLayer::useEmptyDamage() {
91 surfaceDamageRegion.clear();
92}
93
Marissa Wallfd668622018-05-10 10:21:13 -070094bool BufferLayer::isOpaque(const Layer::State& s) const {
95 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
96 // layer's opaque flag.
Lloyd Pique0b785d82018-12-04 17:25:27 -080097 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070098 return false;
99 }
100
101 // if the layer has the opaque flag, then we're always opaque,
102 // otherwise we use the current buffer's format.
103 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700104}
105
106bool BufferLayer::isVisible() const {
107 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800108 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700109}
110
111bool BufferLayer::isFixedSize() const {
112 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
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,
135 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700136 ATRACE_CALL();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000137 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800138 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700139 // the texture has not been created yet, this Layer has
140 // in fact never been drawn into. This happens frequently with
141 // SurfaceView because the WindowManager can't know when the client
142 // has drawn the first time.
143
144 // If there is nothing under us, we paint the screen in black, otherwise
145 // we just skip this update.
146
147 // figure out if there is something below us
148 Region under;
149 bool finished = false;
150 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
151 if (finished || layer == static_cast<BufferLayer const*>(this)) {
152 finished = true;
153 return;
154 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000155 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700156 });
157 // if not everything below us is covered, we plug the holes!
158 Region holes(clip.subtract(under));
159 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000160 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700161 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000162 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700163 }
David Sodman0c69cad2017-08-21 12:12:51 -0700164 bool blackOutLayer = isProtected() || (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;
170 layer.source.buffer.cacheHint = useCachedBufferForClientComposition()
171 ? renderengine::Buffer::CachingHint::USE_CACHE
172 : renderengine::Buffer::CachingHint::NO_CACHE;
173 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()};
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000214 const float bufferWidth = getBufferSize(s).getWidth();
215 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700216
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000217 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
218 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
219 const float translateY = float(win.top) / bufferHeight;
220 const float translateX = float(win.left) / bufferWidth;
221
222 // Flip y-coordinates because GLConsumer expects OpenGL convention.
223 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
224 mat4::translate(vec4(-.5, -.5, 0, 1)) *
225 mat4::translate(vec4(translateX, translateY, 0, 1)) *
226 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
227
228 layer.source.buffer.useTextureFiltering = useFiltering;
229 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700230 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000231 // If layer is blacked out, force alpha to 1 so that we draw a black color
232 // layer.
233 layer.source.buffer.buffer = nullptr;
234 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700235 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000236
237 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700238}
239
Marissa Wallfd668622018-05-10 10:21:13 -0700240bool BufferLayer::isHdrY410() const {
241 // pixel format is HDR Y410 masquerading as RGBA_1010102
242 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
243 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800244 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700245}
246
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800247void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
248 const ui::Transform& transform, const Rect& viewport,
249 int32_t supportedPerFrameMetadata) {
250 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700251
David Sodman0c69cad2017-08-21 12:12:51 -0700252 // Apply this display's projection's viewport to the visible region
253 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700254 Region visible = transform.transform(visibleRegion.intersect(viewport));
255
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800256 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
257 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
258
259 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700260 auto error = hwcLayer->setVisibleRegion(visible);
261 if (error != HWC2::Error::None) {
262 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
263 to_string(error).c_str(), static_cast<int32_t>(error));
264 visible.dump(LOG_TAG);
265 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800266 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700267
Lloyd Pique0b785d82018-12-04 17:25:27 -0800268 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
269
David Sodman15094112018-10-11 09:39:37 -0700270 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
271 if (error != HWC2::Error::None) {
272 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
273 to_string(error).c_str(), static_cast<int32_t>(error));
274 surfaceDamageRegion.dump(LOG_TAG);
275 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800276 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700277
278 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800279 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800280 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700281 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800282 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700283 if (error != HWC2::Error::None) {
284 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800285 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700286 static_cast<int32_t>(error));
287 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800288 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700289 return;
290 }
291
David Sodman15094112018-10-11 09:39:37 -0700292 // Device or Cursor layers
293 if (mPotentialCursor) {
294 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800295 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700296 } else {
297 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800298 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700299 }
300
David Sodman15094112018-10-11 09:39:37 -0700301 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
302 error = hwcLayer->setDataspace(mCurrentDataSpace);
303 if (error != HWC2::Error::None) {
304 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
305 to_string(error).c_str(), static_cast<int32_t>(error));
306 }
307
308 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700309 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700310 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
311 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
312 to_string(error).c_str(), static_cast<int32_t>(error));
313 }
314
315 error = hwcLayer->setColorTransform(getColorTransform());
316 if (error != HWC2::Error::None) {
317 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
318 to_string(error).c_str(), static_cast<int32_t>(error));
319 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800320 layerCompositionState.dataspace = mCurrentDataSpace;
321 layerCompositionState.colorTransform = getColorTransform();
322 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700323
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800324 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700325}
326
Marissa Wallfd668622018-05-10 10:21:13 -0700327bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
328 if (mBufferLatched) {
329 Mutex::Autolock lock(mFrameEventHistoryMutex);
330 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700331 }
Marissa Wallfd668622018-05-10 10:21:13 -0700332 mRefreshPending = false;
333 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700334}
335
Dominik Laskowski075d3172018-05-24 15:50:06 -0700336bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
337 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700338 const std::shared_ptr<FenceTime>& presentFence,
339 const CompositorTiming& compositorTiming) {
340 // mFrameLatencyNeeded is true when a new frame was latched for the
341 // composition.
342 if (!mFrameLatencyNeeded) return false;
343
344 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700345 {
Marissa Wallfd668622018-05-10 10:21:13 -0700346 Mutex::Autolock lock(mFrameEventHistoryMutex);
347 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
348 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700349 }
350
Marissa Wallfd668622018-05-10 10:21:13 -0700351 // Update mFrameTracker.
352 nsecs_t desiredPresentTime = getDesiredPresentTime();
353 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
354
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700355 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800356 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700357
358 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
359 if (frameReadyFence->isValid()) {
360 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
361 } else {
362 // There was no fence for this frame, so assume that it was ready
363 // to be presented at the desired present time.
364 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700365 }
Marissa Wallfd668622018-05-10 10:21:13 -0700366
367 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800368 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700369 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700370 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700371 // The HWC doesn't support present fences, so use the refresh
372 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700373 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800374 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700375 mFrameTracker.setActualPresentTime(actualPresentTime);
376 }
377
378 mFrameTracker.advanceFrame();
379 mFrameLatencyNeeded = false;
380 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700381}
382
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800383bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
384 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700385 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700386
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800387 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700388
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800389 if (refreshRequired) {
390 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700391 }
392
Marissa Wallfd668622018-05-10 10:21:13 -0700393 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800394 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700395 }
David Sodman0c69cad2017-08-21 12:12:51 -0700396
Marissa Wallfd668622018-05-10 10:21:13 -0700397 // if we've already called updateTexImage() without going through
398 // a composition step, we have to skip this layer at this point
399 // because we cannot call updateTeximage() without a corresponding
400 // compositionComplete() call.
401 // we'll trigger an update in onPreComposition().
402 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800403 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700404 }
405
406 // If the head buffer's acquire fence hasn't signaled yet, return and
407 // try again later
408 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700409 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800410 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700411 }
412
413 // Capture the old state of the layer for comparisons later
414 const State& s(getDrawingState());
415 const bool oldOpacity = isOpaque(s);
416 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
417
418 if (!allTransactionsSignaled()) {
419 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800420 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700421 }
422
Alec Mouri86770e52018-09-24 22:40:58 +0000423 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700424 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800425 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700426 }
427
428 err = updateActiveBuffer();
429 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800430 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700431 }
432
433 mBufferLatched = true;
434
435 err = updateFrameNumber(latchTime);
436 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800437 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700438 }
439
440 mRefreshPending = true;
441 mFrameLatencyNeeded = true;
442 if (oldBuffer == nullptr) {
443 // the first time we receive a buffer, we need to trigger a
444 // geometry invalidation.
445 recomputeVisibleRegions = true;
446 }
447
448 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800449 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700450 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800451 case ui::Dataspace::SRGB:
452 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700453 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800454 case ui::Dataspace::SRGB_LINEAR:
455 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700456 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800457 case ui::Dataspace::JFIF:
458 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700459 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800460 case ui::Dataspace::BT601_625:
461 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700462 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800463 case ui::Dataspace::BT601_525:
464 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700465 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800466 case ui::Dataspace::BT709:
467 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700468 break;
469 default:
470 break;
471 }
472 mCurrentDataSpace = dataSpace;
473
474 Rect crop(getDrawingCrop());
475 const uint32_t transform(getDrawingTransform());
476 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700478 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800479 (scalingMode != mCurrentScalingMode) ||
480 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700481 mCurrentCrop = crop;
482 mCurrentTransform = transform;
483 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800484 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700485 recomputeVisibleRegions = true;
486 }
487
488 if (oldBuffer != nullptr) {
489 uint32_t bufWidth = mActiveBuffer->getWidth();
490 uint32_t bufHeight = mActiveBuffer->getHeight();
491 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
492 recomputeVisibleRegions = true;
493 }
494 }
495
496 if (oldOpacity != isOpaque(s)) {
497 recomputeVisibleRegions = true;
498 }
499
500 // Remove any sync points corresponding to the buffer which was just
501 // latched
502 {
503 Mutex::Autolock lock(mLocalSyncPointMutex);
504 auto point = mLocalSyncPoints.begin();
505 while (point != mLocalSyncPoints.end()) {
506 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
507 // This sync point must have been added since we started
508 // latching. Don't drop it yet.
509 ++point;
510 continue;
511 }
512
513 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
514 point = mLocalSyncPoints.erase(point);
515 } else {
516 ++point;
517 }
518 }
519 }
520
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800521 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700522}
523
524// transaction
525void BufferLayer::notifyAvailableFrames() {
526 auto headFrameNumber = getHeadFrameNumber();
527 bool headFenceSignaled = fenceHasSignaled();
528 Mutex::Autolock lock(mLocalSyncPointMutex);
529 for (auto& point : mLocalSyncPoints) {
530 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
531 point->setFrameAvailable();
532 }
David Sodman0c69cad2017-08-21 12:12:51 -0700533 }
534}
535
Marissa Wallfd668622018-05-10 10:21:13 -0700536bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700537 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700538}
539
540uint32_t BufferLayer::getEffectiveScalingMode() const {
541 if (mOverrideScalingMode >= 0) {
542 return mOverrideScalingMode;
543 }
544
545 return mCurrentScalingMode;
546}
547
548bool BufferLayer::isProtected() const {
549 const sp<GraphicBuffer>& buffer(mActiveBuffer);
550 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
551}
552
553bool BufferLayer::latchUnsignaledBuffers() {
554 static bool propertyLoaded = false;
555 static bool latch = false;
556 static std::mutex mutex;
557 std::lock_guard<std::mutex> lock(mutex);
558 if (!propertyLoaded) {
559 char value[PROPERTY_VALUE_MAX] = {};
560 property_get("debug.sf.latch_unsignaled", value, "0");
561 latch = atoi(value);
562 propertyLoaded = true;
563 }
564 return latch;
565}
566
567// h/w composer set-up
568bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700570 bool matchingFramesFound = false;
571 bool allTransactionsApplied = true;
572 Mutex::Autolock lock(mLocalSyncPointMutex);
573
574 for (auto& point : mLocalSyncPoints) {
575 if (point->getFrameNumber() > headFrameNumber) {
576 break;
577 }
578 matchingFramesFound = true;
579
580 if (!point->frameIsAvailable()) {
581 // We haven't notified the remote layer that the frame for
582 // this point is available yet. Notify it now, and then
583 // abort this attempt to latch.
584 point->setFrameAvailable();
585 allTransactionsApplied = false;
586 break;
587 }
588
589 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
590 }
591 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700592}
593
594// As documented in libhardware header, formats in the range
595// 0x100 - 0x1FF are specific to the HAL implementation, and
596// are known to have no alpha channel
597// TODO: move definition for device-specific range into
598// hardware.h, instead of using hard-coded values here.
599#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
600
601bool BufferLayer::getOpacityForFormat(uint32_t format) {
602 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
603 return true;
604 }
605 switch (format) {
606 case HAL_PIXEL_FORMAT_RGBA_8888:
607 case HAL_PIXEL_FORMAT_BGRA_8888:
608 case HAL_PIXEL_FORMAT_RGBA_FP16:
609 case HAL_PIXEL_FORMAT_RGBA_1010102:
610 return false;
611 }
612 // in all other case, we have no blending (also for unknown formats)
613 return true;
614}
615
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800616bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
617 // If we are not capturing based on the state of a known display device, we
618 // only return mNeedsFiltering
619 if (displayDevice == nullptr) {
620 return mNeedsFiltering;
621 }
622
623 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
624 if (outputLayer == nullptr) {
625 return mNeedsFiltering;
626 }
627
628 const auto& compositionState = outputLayer->getState();
629 const auto displayFrame = compositionState.displayFrame;
630 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800631 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
632 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700633}
634
David Sodman0c69cad2017-08-21 12:12:51 -0700635uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800636 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700637 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700638 } else {
639 return mCurrentFrameNumber;
640 }
641}
642
Vishnu Nair60356342018-11-13 13:00:45 -0800643Rect BufferLayer::getBufferSize(const State& s) const {
644 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
645 // we cannot determine the buffer size.
646 if ((s.sidebandStream != nullptr) ||
647 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
648 return Rect(getActiveWidth(s), getActiveHeight(s));
649 }
650
651 if (mActiveBuffer == nullptr) {
652 return Rect::INVALID_RECT;
653 }
654
655 uint32_t bufWidth = mActiveBuffer->getWidth();
656 uint32_t bufHeight = mActiveBuffer->getHeight();
657
658 // Undo any transformations on the buffer and return the result.
659 if (mCurrentTransform & ui::Transform::ROT_90) {
660 std::swap(bufWidth, bufHeight);
661 }
662
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800664 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
665 if (invTransform & ui::Transform::ROT_90) {
666 std::swap(bufWidth, bufHeight);
667 }
668 }
669
670 return Rect(bufWidth, bufHeight);
671}
672
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800673std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
674 return mCompositionLayer;
675}
676
Vishnu Nair4351ad52019-02-11 14:13:02 -0800677FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
678 const State& s(getDrawingState());
679
680 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
681 // we cannot determine the buffer size.
682 if ((s.sidebandStream != nullptr) ||
683 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
684 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
685 }
686
687 if (mActiveBuffer == nullptr) {
688 return parentBounds;
689 }
690
691 uint32_t bufWidth = mActiveBuffer->getWidth();
692 uint32_t bufHeight = mActiveBuffer->getHeight();
693
694 // Undo any transformations on the buffer and return the result.
695 if (mCurrentTransform & ui::Transform::ROT_90) {
696 std::swap(bufWidth, bufHeight);
697 }
698
699 if (getTransformToDisplayInverse()) {
700 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
701 if (invTransform & ui::Transform::ROT_90) {
702 std::swap(bufWidth, bufHeight);
703 }
704 }
705
706 return FloatRect(0, 0, bufWidth, bufHeight);
707}
708
David Sodman0c69cad2017-08-21 12:12:51 -0700709} // namespace android
710
711#if defined(__gl_h_)
712#error "don't include gl/gl.h in this file"
713#endif
714
715#if defined(__gl2_h_)
716#error "don't include gl2/gl2.h in this file"
717#endif