blob: e4db6dfff933ba760717d05da5387041ae182fc5 [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>
31#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080032#include <cutils/compiler.h>
33#include <cutils/native_handle.h>
34#include <cutils/properties.h>
35#include <gui/BufferItem.h>
36#include <gui/BufferQueue.h>
37#include <gui/LayerDebugInfo.h>
38#include <gui/Surface.h>
39#include <renderengine/RenderEngine.h>
40#include <ui/DebugUtils.h>
41#include <utils/Errors.h>
42#include <utils/Log.h>
43#include <utils/NativeHandle.h>
44#include <utils/StopWatch.h>
45#include <utils/Trace.h>
46
David Sodman0c69cad2017-08-21 12:12:51 -070047#include "BufferLayer.h"
48#include "Colorizer.h"
49#include "DisplayDevice.h"
50#include "LayerRejecter.h"
David Sodman0c69cad2017-08-21 12:12:51 -070051
Yiwei Zhang7e666a52018-11-15 13:33:42 -080052#include "TimeStats/TimeStats.h"
53
David Sodman0c69cad2017-08-21 12:12:51 -070054namespace android {
55
Lloyd Pique42ab75e2018-09-12 20:46:03 -070056BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057 : Layer(args),
58 mTextureName(args.flinger->getNewTexture()),
59 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
60 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070061 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070062
Lloyd Pique42ab75e2018-09-12 20:46:03 -070063 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070064
Lloyd Pique42ab75e2018-09-12 20:46:03 -070065 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
66 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070067}
68
69BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070070 mFlinger->deleteTextureAsync(mTextureName);
71
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080072 if (destroyAllHwcLayersPlusChildren()) {
David Sodman0c69cad2017-08-21 12:12:51 -070073 ALOGE("Found stale hardware composer layers when destroying "
74 "surface flinger layer %s",
75 mName.string());
David Sodman0c69cad2017-08-21 12:12:51 -070076 }
Yiwei Zhangdc224042018-10-18 15:34:00 -070077
Yiwei Zhang7e666a52018-11-15 13:33:42 -080078 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070079}
80
David Sodmaneb085e02017-10-05 18:49:04 -070081void BufferLayer::useSurfaceDamage() {
82 if (mFlinger->mForceFullDamage) {
83 surfaceDamageRegion = Region::INVALID_REGION;
84 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070085 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070086 }
87}
88
89void BufferLayer::useEmptyDamage() {
90 surfaceDamageRegion.clear();
91}
92
Marissa Wallfd668622018-05-10 10:21:13 -070093bool BufferLayer::isOpaque(const Layer::State& s) const {
94 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
95 // layer's opaque flag.
96 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
97 return false;
98 }
99
100 // if the layer has the opaque flag, then we're always opaque,
101 // otherwise we use the current buffer's format.
102 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700103}
104
105bool BufferLayer::isVisible() const {
106 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800107 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700108}
109
110bool BufferLayer::isFixedSize() const {
111 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
112}
113
David Sodman0c69cad2017-08-21 12:12:51 -0700114static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800115 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
116 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
117 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 -0700118 mat4 tr;
119
120 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
121 tr = tr * rot90;
122 }
123 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
124 tr = tr * flipH;
125 }
126 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
127 tr = tr * flipV;
128 }
129 return inverse(tr);
130}
131
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000132bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
133 bool useIdentityTransform, Region& clearRegion,
134 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700135 ATRACE_CALL();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000136 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion, 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 }
David Sodman0c69cad2017-08-21 12:12:51 -0700163 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000164 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700165 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000166 layer.source.buffer.buffer = mActiveBuffer;
167 layer.source.buffer.isOpaque = isOpaque(s);
168 layer.source.buffer.fence = mActiveBufferFence;
169 layer.source.buffer.cacheHint = useCachedBufferForClientComposition()
170 ? renderengine::Buffer::CachingHint::USE_CACHE
171 : renderengine::Buffer::CachingHint::NO_CACHE;
172 layer.source.buffer.textureName = mTextureName;
173 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
174 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700175 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800176 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
177 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700178
179 // Query the texture matrix given our current filtering mode.
180 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700181 setFilteringEnabled(useFiltering);
182 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700183
184 if (getTransformToDisplayInverse()) {
185 /*
186 * the code below applies the primary display's inverse transform to
187 * the texture transform
188 */
189 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
190 mat4 tr = inverseOrientation(transform);
191
192 /**
193 * TODO(b/36727915): This is basically a hack.
194 *
195 * Ensure that regardless of the parent transformation,
196 * this buffer is always transformed from native display
197 * orientation to display orientation. For example, in the case
198 * of a camera where the buffer remains in native orientation,
199 * we want the pixels to always be upright.
200 */
201 sp<Layer> p = mDrawingParent.promote();
202 if (p != nullptr) {
203 const auto parentTransform = p->getTransform();
204 tr = tr * inverseOrientation(parentTransform.getOrientation());
205 }
206
207 // and finally apply it to the original texture matrix
208 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
209 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
210 }
211
Vishnu Nair4351ad52019-02-11 14:13:02 -0800212 const Rect win{getBounds()};
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000213 const float bufferWidth = getBufferSize(s).getWidth();
214 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700215
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000216 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
217 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
218 const float translateY = float(win.top) / bufferHeight;
219 const float translateX = float(win.left) / bufferWidth;
220
221 // Flip y-coordinates because GLConsumer expects OpenGL convention.
222 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
223 mat4::translate(vec4(-.5, -.5, 0, 1)) *
224 mat4::translate(vec4(translateX, translateY, 0, 1)) *
225 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
226
227 layer.source.buffer.useTextureFiltering = useFiltering;
228 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700229 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000230 // If layer is blacked out, force alpha to 1 so that we draw a black color
231 // layer.
232 layer.source.buffer.buffer = nullptr;
233 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700234 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000235
236 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700237}
238
Marissa Wallfd668622018-05-10 10:21:13 -0700239bool BufferLayer::isHdrY410() const {
240 // pixel format is HDR Y410 masquerading as RGBA_1010102
241 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
242 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
243 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700244}
245
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800246void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
247 const ui::Transform& transform, const Rect& viewport,
248 int32_t supportedPerFrameMetadata) {
249 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700250
David Sodman0c69cad2017-08-21 12:12:51 -0700251 // Apply this display's projection's viewport to the visible region
252 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700253 Region visible = transform.transform(visibleRegion.intersect(viewport));
254
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800255 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
256 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
257
258 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700259 auto error = hwcLayer->setVisibleRegion(visible);
260 if (error != HWC2::Error::None) {
261 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
262 to_string(error).c_str(), static_cast<int32_t>(error));
263 visible.dump(LOG_TAG);
264 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800265 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700266
267 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
268 if (error != HWC2::Error::None) {
269 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
270 to_string(error).c_str(), static_cast<int32_t>(error));
271 surfaceDamageRegion.dump(LOG_TAG);
272 }
David Sodmanba340492018-08-05 21:51:33 -0700273 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700274
275 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800276 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800277 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700278 ALOGV("[%s] Requesting Sideband composition", mName.string());
279 error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
280 if (error != HWC2::Error::None) {
281 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
282 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
283 static_cast<int32_t>(error));
284 }
David Sodmanba340492018-08-05 21:51:33 -0700285 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700286 return;
287 }
288
David Sodman15094112018-10-11 09:39:37 -0700289 // Device or Cursor layers
290 if (mPotentialCursor) {
291 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800292 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700293 } else {
294 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800295 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700296 }
297
David Sodman15094112018-10-11 09:39:37 -0700298 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
299 error = hwcLayer->setDataspace(mCurrentDataSpace);
300 if (error != HWC2::Error::None) {
301 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
302 to_string(error).c_str(), static_cast<int32_t>(error));
303 }
304
305 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700306 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700307 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
308 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
309 to_string(error).c_str(), static_cast<int32_t>(error));
310 }
311
312 error = hwcLayer->setColorTransform(getColorTransform());
313 if (error != HWC2::Error::None) {
314 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
315 to_string(error).c_str(), static_cast<int32_t>(error));
316 }
David Sodmanba340492018-08-05 21:51:33 -0700317 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
318 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 getBE().compositionInfo.hwc.supportedPerFrameMetadata = supportedPerFrameMetadata;
Peiyong Lind3788632018-09-18 16:01:31 -0700320 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700321
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800322 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700323}
324
Marissa Wallfd668622018-05-10 10:21:13 -0700325bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
326 if (mBufferLatched) {
327 Mutex::Autolock lock(mFrameEventHistoryMutex);
328 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700329 }
Marissa Wallfd668622018-05-10 10:21:13 -0700330 mRefreshPending = false;
331 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700332}
333
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
335 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700336 const std::shared_ptr<FenceTime>& presentFence,
337 const CompositorTiming& compositorTiming) {
338 // mFrameLatencyNeeded is true when a new frame was latched for the
339 // composition.
340 if (!mFrameLatencyNeeded) return false;
341
342 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700343 {
Marissa Wallfd668622018-05-10 10:21:13 -0700344 Mutex::Autolock lock(mFrameEventHistoryMutex);
345 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
346 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700347 }
348
Marissa Wallfd668622018-05-10 10:21:13 -0700349 // Update mFrameTracker.
350 nsecs_t desiredPresentTime = getDesiredPresentTime();
351 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
352
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700353 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800354 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700355
356 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
357 if (frameReadyFence->isValid()) {
358 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
359 } else {
360 // There was no fence for this frame, so assume that it was ready
361 // to be presented at the desired present time.
362 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700363 }
Marissa Wallfd668622018-05-10 10:21:13 -0700364
365 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800366 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700367 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700368 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700369 // The HWC doesn't support present fences, so use the refresh
370 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700371 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800372 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700373 mFrameTracker.setActualPresentTime(actualPresentTime);
374 }
375
376 mFrameTracker.advanceFrame();
377 mFrameLatencyNeeded = false;
378 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700379}
380
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800381bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
382 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700383 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700384
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800385 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700386
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800387 if (refreshRequired) {
388 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700389 }
390
Marissa Wallfd668622018-05-10 10:21:13 -0700391 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800392 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700393 }
David Sodman0c69cad2017-08-21 12:12:51 -0700394
Marissa Wallfd668622018-05-10 10:21:13 -0700395 // if we've already called updateTexImage() without going through
396 // a composition step, we have to skip this layer at this point
397 // because we cannot call updateTeximage() without a corresponding
398 // compositionComplete() call.
399 // we'll trigger an update in onPreComposition().
400 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800401 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700402 }
403
404 // If the head buffer's acquire fence hasn't signaled yet, return and
405 // try again later
406 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700407 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800408 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700409 }
410
411 // Capture the old state of the layer for comparisons later
412 const State& s(getDrawingState());
413 const bool oldOpacity = isOpaque(s);
414 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
415
416 if (!allTransactionsSignaled()) {
417 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800418 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700419 }
420
Alec Mouri86770e52018-09-24 22:40:58 +0000421 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700422 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800423 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700424 }
425
426 err = updateActiveBuffer();
427 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800428 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700429 }
430
431 mBufferLatched = true;
432
433 err = updateFrameNumber(latchTime);
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 mRefreshPending = true;
439 mFrameLatencyNeeded = true;
440 if (oldBuffer == nullptr) {
441 // the first time we receive a buffer, we need to trigger a
442 // geometry invalidation.
443 recomputeVisibleRegions = true;
444 }
445
446 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800447 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700448 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800449 case ui::Dataspace::SRGB:
450 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700451 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800452 case ui::Dataspace::SRGB_LINEAR:
453 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700454 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800455 case ui::Dataspace::JFIF:
456 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700457 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800458 case ui::Dataspace::BT601_625:
459 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700460 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800461 case ui::Dataspace::BT601_525:
462 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700463 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800464 case ui::Dataspace::BT709:
465 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700466 break;
467 default:
468 break;
469 }
470 mCurrentDataSpace = dataSpace;
471
472 Rect crop(getDrawingCrop());
473 const uint32_t transform(getDrawingTransform());
474 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800475 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700476 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 (scalingMode != mCurrentScalingMode) ||
478 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700479 mCurrentCrop = crop;
480 mCurrentTransform = transform;
481 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800482 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700483 recomputeVisibleRegions = true;
484 }
485
486 if (oldBuffer != nullptr) {
487 uint32_t bufWidth = mActiveBuffer->getWidth();
488 uint32_t bufHeight = mActiveBuffer->getHeight();
489 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
490 recomputeVisibleRegions = true;
491 }
492 }
493
494 if (oldOpacity != isOpaque(s)) {
495 recomputeVisibleRegions = true;
496 }
497
498 // Remove any sync points corresponding to the buffer which was just
499 // latched
500 {
501 Mutex::Autolock lock(mLocalSyncPointMutex);
502 auto point = mLocalSyncPoints.begin();
503 while (point != mLocalSyncPoints.end()) {
504 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
505 // This sync point must have been added since we started
506 // latching. Don't drop it yet.
507 ++point;
508 continue;
509 }
510
511 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
512 point = mLocalSyncPoints.erase(point);
513 } else {
514 ++point;
515 }
516 }
517 }
518
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800519 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700520}
521
522// transaction
523void BufferLayer::notifyAvailableFrames() {
524 auto headFrameNumber = getHeadFrameNumber();
525 bool headFenceSignaled = fenceHasSignaled();
526 Mutex::Autolock lock(mLocalSyncPointMutex);
527 for (auto& point : mLocalSyncPoints) {
528 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
529 point->setFrameAvailable();
530 }
David Sodman0c69cad2017-08-21 12:12:51 -0700531 }
532}
533
Marissa Wallfd668622018-05-10 10:21:13 -0700534bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700535 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700536}
537
538uint32_t BufferLayer::getEffectiveScalingMode() const {
539 if (mOverrideScalingMode >= 0) {
540 return mOverrideScalingMode;
541 }
542
543 return mCurrentScalingMode;
544}
545
546bool BufferLayer::isProtected() const {
547 const sp<GraphicBuffer>& buffer(mActiveBuffer);
548 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
549}
550
551bool BufferLayer::latchUnsignaledBuffers() {
552 static bool propertyLoaded = false;
553 static bool latch = false;
554 static std::mutex mutex;
555 std::lock_guard<std::mutex> lock(mutex);
556 if (!propertyLoaded) {
557 char value[PROPERTY_VALUE_MAX] = {};
558 property_get("debug.sf.latch_unsignaled", value, "0");
559 latch = atoi(value);
560 propertyLoaded = true;
561 }
562 return latch;
563}
564
565// h/w composer set-up
566bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800567 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700568 bool matchingFramesFound = false;
569 bool allTransactionsApplied = true;
570 Mutex::Autolock lock(mLocalSyncPointMutex);
571
572 for (auto& point : mLocalSyncPoints) {
573 if (point->getFrameNumber() > headFrameNumber) {
574 break;
575 }
576 matchingFramesFound = true;
577
578 if (!point->frameIsAvailable()) {
579 // We haven't notified the remote layer that the frame for
580 // this point is available yet. Notify it now, and then
581 // abort this attempt to latch.
582 point->setFrameAvailable();
583 allTransactionsApplied = false;
584 break;
585 }
586
587 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
588 }
589 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700590}
591
592// As documented in libhardware header, formats in the range
593// 0x100 - 0x1FF are specific to the HAL implementation, and
594// are known to have no alpha channel
595// TODO: move definition for device-specific range into
596// hardware.h, instead of using hard-coded values here.
597#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
598
599bool BufferLayer::getOpacityForFormat(uint32_t format) {
600 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
601 return true;
602 }
603 switch (format) {
604 case HAL_PIXEL_FORMAT_RGBA_8888:
605 case HAL_PIXEL_FORMAT_BGRA_8888:
606 case HAL_PIXEL_FORMAT_RGBA_FP16:
607 case HAL_PIXEL_FORMAT_RGBA_1010102:
608 return false;
609 }
610 // in all other case, we have no blending (also for unknown formats)
611 return true;
612}
613
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800614bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
615 // If we are not capturing based on the state of a known display device, we
616 // only return mNeedsFiltering
617 if (displayDevice == nullptr) {
618 return mNeedsFiltering;
619 }
620
621 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
622 if (outputLayer == nullptr) {
623 return mNeedsFiltering;
624 }
625
626 const auto& compositionState = outputLayer->getState();
627 const auto displayFrame = compositionState.displayFrame;
628 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800629 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
630 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700631}
632
David Sodman0c69cad2017-08-21 12:12:51 -0700633uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800634 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700635 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700636 } else {
637 return mCurrentFrameNumber;
638 }
639}
640
Vishnu Nair60356342018-11-13 13:00:45 -0800641Rect BufferLayer::getBufferSize(const State& s) const {
642 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
643 // we cannot determine the buffer size.
644 if ((s.sidebandStream != nullptr) ||
645 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
646 return Rect(getActiveWidth(s), getActiveHeight(s));
647 }
648
649 if (mActiveBuffer == nullptr) {
650 return Rect::INVALID_RECT;
651 }
652
653 uint32_t bufWidth = mActiveBuffer->getWidth();
654 uint32_t bufHeight = mActiveBuffer->getHeight();
655
656 // Undo any transformations on the buffer and return the result.
657 if (mCurrentTransform & ui::Transform::ROT_90) {
658 std::swap(bufWidth, bufHeight);
659 }
660
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800662 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
663 if (invTransform & ui::Transform::ROT_90) {
664 std::swap(bufWidth, bufHeight);
665 }
666 }
667
668 return Rect(bufWidth, bufHeight);
669}
670
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800671std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
672 return mCompositionLayer;
673}
674
Vishnu Nair4351ad52019-02-11 14:13:02 -0800675FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
676 const State& s(getDrawingState());
677
678 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
679 // we cannot determine the buffer size.
680 if ((s.sidebandStream != nullptr) ||
681 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
682 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
683 }
684
685 if (mActiveBuffer == nullptr) {
686 return parentBounds;
687 }
688
689 uint32_t bufWidth = mActiveBuffer->getWidth();
690 uint32_t bufHeight = mActiveBuffer->getHeight();
691
692 // Undo any transformations on the buffer and return the result.
693 if (mCurrentTransform & ui::Transform::ROT_90) {
694 std::swap(bufWidth, bufHeight);
695 }
696
697 if (getTransformToDisplayInverse()) {
698 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
699 if (invTransform & ui::Transform::ROT_90) {
700 std::swap(bufWidth, bufHeight);
701 }
702 }
703
704 return FloatRect(0, 0, bufWidth, bufHeight);
705}
706
David Sodman0c69cad2017-08-21 12:12:51 -0700707} // namespace android
708
709#if defined(__gl_h_)
710#error "don't include gl/gl.h in this file"
711#endif
712
713#if defined(__gl2_h_)
714#error "don't include gl2/gl2.h in this file"
715#endif