blob: 2501faec620439e46a2bf818e05c1f38341a641b [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,
128 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700129 ATRACE_CALL();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000130 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800131 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700132 // the texture has not been created yet, this Layer has
133 // in fact never been drawn into. This happens frequently with
134 // SurfaceView because the WindowManager can't know when the client
135 // has drawn the first time.
136
137 // If there is nothing under us, we paint the screen in black, otherwise
138 // we just skip this update.
139
140 // figure out if there is something below us
141 Region under;
142 bool finished = false;
143 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
144 if (finished || layer == static_cast<BufferLayer const*>(this)) {
145 finished = true;
146 return;
147 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000148 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700149 });
150 // if not everything below us is covered, we plug the holes!
151 Region holes(clip.subtract(under));
152 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000153 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700154 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000155 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700156 }
David Sodman0c69cad2017-08-21 12:12:51 -0700157 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000158 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700159 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000160 layer.source.buffer.buffer = mActiveBuffer;
161 layer.source.buffer.isOpaque = isOpaque(s);
162 layer.source.buffer.fence = mActiveBufferFence;
163 layer.source.buffer.cacheHint = useCachedBufferForClientComposition()
164 ? renderengine::Buffer::CachingHint::USE_CACHE
165 : renderengine::Buffer::CachingHint::NO_CACHE;
166 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()};
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000207 const float bufferWidth = getBufferSize(s).getWidth();
208 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700209
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000210 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
211 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
212 const float translateY = float(win.top) / bufferHeight;
213 const float translateX = float(win.left) / bufferWidth;
214
215 // Flip y-coordinates because GLConsumer expects OpenGL convention.
216 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
217 mat4::translate(vec4(-.5, -.5, 0, 1)) *
218 mat4::translate(vec4(translateX, translateY, 0, 1)) *
219 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
220
221 layer.source.buffer.useTextureFiltering = useFiltering;
222 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700223 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000224 // If layer is blacked out, force alpha to 1 so that we draw a black color
225 // layer.
226 layer.source.buffer.buffer = nullptr;
227 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700228 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000229
230 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700231}
232
Marissa Wallfd668622018-05-10 10:21:13 -0700233bool BufferLayer::isHdrY410() const {
234 // pixel format is HDR Y410 masquerading as RGBA_1010102
235 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
236 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800237 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700238}
239
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800240void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice,
241 const ui::Transform& transform, const Rect& viewport,
242 int32_t supportedPerFrameMetadata) {
243 RETURN_IF_NO_HWC_LAYER(displayDevice);
Dominik Laskowski34157762018-10-31 13:07:19 -0700244
David Sodman0c69cad2017-08-21 12:12:51 -0700245 // Apply this display's projection's viewport to the visible region
246 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700247 Region visible = transform.transform(visibleRegion.intersect(viewport));
248
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800249 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
250 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
251
252 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
David Sodman15094112018-10-11 09:39:37 -0700253 auto error = hwcLayer->setVisibleRegion(visible);
254 if (error != HWC2::Error::None) {
255 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
256 to_string(error).c_str(), static_cast<int32_t>(error));
257 visible.dump(LOG_TAG);
258 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800259 outputLayer->editState().visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700260
Lloyd Pique0b785d82018-12-04 17:25:27 -0800261 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
262
David Sodman15094112018-10-11 09:39:37 -0700263 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
264 if (error != HWC2::Error::None) {
265 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
266 to_string(error).c_str(), static_cast<int32_t>(error));
267 surfaceDamageRegion.dump(LOG_TAG);
268 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800269 layerCompositionState.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700270
271 // Sideband layers
Lloyd Pique0b785d82018-12-04 17:25:27 -0800272 if (layerCompositionState.sidebandStream.get()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800273 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND);
David Sodman15094112018-10-11 09:39:37 -0700274 ALOGV("[%s] Requesting Sideband composition", mName.string());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800275 error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle());
David Sodman15094112018-10-11 09:39:37 -0700276 if (error != HWC2::Error::None) {
277 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
Lloyd Pique0b785d82018-12-04 17:25:27 -0800278 layerCompositionState.sidebandStream->handle(), to_string(error).c_str(),
David Sodman15094112018-10-11 09:39:37 -0700279 static_cast<int32_t>(error));
280 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800281 layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman0c69cad2017-08-21 12:12:51 -0700282 return;
283 }
284
David Sodman15094112018-10-11 09:39:37 -0700285 // Device or Cursor layers
286 if (mPotentialCursor) {
287 ALOGV("[%s] Requesting Cursor composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800288 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR);
David Sodman15094112018-10-11 09:39:37 -0700289 } else {
290 ALOGV("[%s] Requesting Device composition", mName.string());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800291 setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE);
David Sodman0c69cad2017-08-21 12:12:51 -0700292 }
293
David Sodman15094112018-10-11 09:39:37 -0700294 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
295 error = hwcLayer->setDataspace(mCurrentDataSpace);
296 if (error != HWC2::Error::None) {
297 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
298 to_string(error).c_str(), static_cast<int32_t>(error));
299 }
300
301 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700302 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700303 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
304 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
305 to_string(error).c_str(), static_cast<int32_t>(error));
306 }
307
308 error = hwcLayer->setColorTransform(getColorTransform());
309 if (error != HWC2::Error::None) {
310 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
311 to_string(error).c_str(), static_cast<int32_t>(error));
312 }
Lloyd Pique0b785d82018-12-04 17:25:27 -0800313 layerCompositionState.dataspace = mCurrentDataSpace;
314 layerCompositionState.colorTransform = getColorTransform();
315 layerCompositionState.hdrMetadata = metadata;
Lloyd Pique074e8122018-07-26 12:57:23 -0700316
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800317 setHwcLayerBuffer(displayDevice);
David Sodman0c69cad2017-08-21 12:12:51 -0700318}
319
Marissa Wallfd668622018-05-10 10:21:13 -0700320bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
321 if (mBufferLatched) {
322 Mutex::Autolock lock(mFrameEventHistoryMutex);
323 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700324 }
Marissa Wallfd668622018-05-10 10:21:13 -0700325 mRefreshPending = false;
326 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700327}
328
Dominik Laskowski075d3172018-05-24 15:50:06 -0700329bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
330 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700331 const std::shared_ptr<FenceTime>& presentFence,
332 const CompositorTiming& compositorTiming) {
333 // mFrameLatencyNeeded is true when a new frame was latched for the
334 // composition.
335 if (!mFrameLatencyNeeded) return false;
336
337 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700338 {
Marissa Wallfd668622018-05-10 10:21:13 -0700339 Mutex::Autolock lock(mFrameEventHistoryMutex);
340 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
341 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700342 }
343
Marissa Wallfd668622018-05-10 10:21:13 -0700344 // Update mFrameTracker.
345 nsecs_t desiredPresentTime = getDesiredPresentTime();
346 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
347
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700348 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800349 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700350
351 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
352 if (frameReadyFence->isValid()) {
353 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
354 } else {
355 // There was no fence for this frame, so assume that it was ready
356 // to be presented at the desired present time.
357 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700358 }
Marissa Wallfd668622018-05-10 10:21:13 -0700359
360 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800361 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700362 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700363 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700364 // The HWC doesn't support present fences, so use the refresh
365 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700366 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800367 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700368 mFrameTracker.setActualPresentTime(actualPresentTime);
369 }
370
371 mFrameTracker.advanceFrame();
372 mFrameLatencyNeeded = false;
373 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700374}
375
Alec Mouri56e538f2019-01-14 15:22:01 -0800376bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700377 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700378
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800379 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700380
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800381 if (refreshRequired) {
382 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700383 }
384
Marissa Wallfd668622018-05-10 10:21:13 -0700385 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800386 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700387 }
David Sodman0c69cad2017-08-21 12:12:51 -0700388
Marissa Wallfd668622018-05-10 10:21:13 -0700389 // if we've already called updateTexImage() without going through
390 // a composition step, we have to skip this layer at this point
391 // because we cannot call updateTeximage() without a corresponding
392 // compositionComplete() call.
393 // we'll trigger an update in onPreComposition().
394 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800395 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700396 }
397
398 // If the head buffer's acquire fence hasn't signaled yet, return and
399 // try again later
400 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700401 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800402 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700403 }
404
405 // Capture the old state of the layer for comparisons later
406 const State& s(getDrawingState());
407 const bool oldOpacity = isOpaque(s);
408 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
409
410 if (!allTransactionsSignaled()) {
411 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800412 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700413 }
414
Alec Mouri56e538f2019-01-14 15:22:01 -0800415 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700416 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800417 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700418 }
419
420 err = updateActiveBuffer();
421 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800422 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700423 }
424
425 mBufferLatched = true;
426
427 err = updateFrameNumber(latchTime);
428 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800429 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700430 }
431
432 mRefreshPending = true;
433 mFrameLatencyNeeded = true;
434 if (oldBuffer == nullptr) {
435 // the first time we receive a buffer, we need to trigger a
436 // geometry invalidation.
437 recomputeVisibleRegions = true;
438 }
439
440 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800441 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700442 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800443 case ui::Dataspace::SRGB:
444 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700445 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800446 case ui::Dataspace::SRGB_LINEAR:
447 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700448 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800449 case ui::Dataspace::JFIF:
450 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700451 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800452 case ui::Dataspace::BT601_625:
453 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700454 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800455 case ui::Dataspace::BT601_525:
456 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700457 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800458 case ui::Dataspace::BT709:
459 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700460 break;
461 default:
462 break;
463 }
464 mCurrentDataSpace = dataSpace;
465
466 Rect crop(getDrawingCrop());
467 const uint32_t transform(getDrawingTransform());
468 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800469 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700470 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800471 (scalingMode != mCurrentScalingMode) ||
472 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700473 mCurrentCrop = crop;
474 mCurrentTransform = transform;
475 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800476 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700477 recomputeVisibleRegions = true;
478 }
479
480 if (oldBuffer != nullptr) {
481 uint32_t bufWidth = mActiveBuffer->getWidth();
482 uint32_t bufHeight = mActiveBuffer->getHeight();
483 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
484 recomputeVisibleRegions = true;
485 }
486 }
487
488 if (oldOpacity != isOpaque(s)) {
489 recomputeVisibleRegions = true;
490 }
491
492 // Remove any sync points corresponding to the buffer which was just
493 // latched
494 {
495 Mutex::Autolock lock(mLocalSyncPointMutex);
496 auto point = mLocalSyncPoints.begin();
497 while (point != mLocalSyncPoints.end()) {
498 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
499 // This sync point must have been added since we started
500 // latching. Don't drop it yet.
501 ++point;
502 continue;
503 }
504
505 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
506 point = mLocalSyncPoints.erase(point);
507 } else {
508 ++point;
509 }
510 }
511 }
512
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800513 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700514}
515
516// transaction
517void BufferLayer::notifyAvailableFrames() {
518 auto headFrameNumber = getHeadFrameNumber();
519 bool headFenceSignaled = fenceHasSignaled();
520 Mutex::Autolock lock(mLocalSyncPointMutex);
521 for (auto& point : mLocalSyncPoints) {
522 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
523 point->setFrameAvailable();
524 }
David Sodman0c69cad2017-08-21 12:12:51 -0700525 }
526}
527
Marissa Wallfd668622018-05-10 10:21:13 -0700528bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700529 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700530}
531
532uint32_t BufferLayer::getEffectiveScalingMode() const {
533 if (mOverrideScalingMode >= 0) {
534 return mOverrideScalingMode;
535 }
536
537 return mCurrentScalingMode;
538}
539
540bool BufferLayer::isProtected() const {
541 const sp<GraphicBuffer>& buffer(mActiveBuffer);
542 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
543}
544
545bool BufferLayer::latchUnsignaledBuffers() {
546 static bool propertyLoaded = false;
547 static bool latch = false;
548 static std::mutex mutex;
549 std::lock_guard<std::mutex> lock(mutex);
550 if (!propertyLoaded) {
551 char value[PROPERTY_VALUE_MAX] = {};
552 property_get("debug.sf.latch_unsignaled", value, "0");
553 latch = atoi(value);
554 propertyLoaded = true;
555 }
556 return latch;
557}
558
559// h/w composer set-up
560bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800561 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700562 bool matchingFramesFound = false;
563 bool allTransactionsApplied = true;
564 Mutex::Autolock lock(mLocalSyncPointMutex);
565
566 for (auto& point : mLocalSyncPoints) {
567 if (point->getFrameNumber() > headFrameNumber) {
568 break;
569 }
570 matchingFramesFound = true;
571
572 if (!point->frameIsAvailable()) {
573 // We haven't notified the remote layer that the frame for
574 // this point is available yet. Notify it now, and then
575 // abort this attempt to latch.
576 point->setFrameAvailable();
577 allTransactionsApplied = false;
578 break;
579 }
580
581 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
582 }
583 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700584}
585
586// As documented in libhardware header, formats in the range
587// 0x100 - 0x1FF are specific to the HAL implementation, and
588// are known to have no alpha channel
589// TODO: move definition for device-specific range into
590// hardware.h, instead of using hard-coded values here.
591#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
592
593bool BufferLayer::getOpacityForFormat(uint32_t format) {
594 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
595 return true;
596 }
597 switch (format) {
598 case HAL_PIXEL_FORMAT_RGBA_8888:
599 case HAL_PIXEL_FORMAT_BGRA_8888:
600 case HAL_PIXEL_FORMAT_RGBA_FP16:
601 case HAL_PIXEL_FORMAT_RGBA_1010102:
602 return false;
603 }
604 // in all other case, we have no blending (also for unknown formats)
605 return true;
606}
607
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800608bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
609 // If we are not capturing based on the state of a known display device, we
610 // only return mNeedsFiltering
611 if (displayDevice == nullptr) {
612 return mNeedsFiltering;
613 }
614
615 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
616 if (outputLayer == nullptr) {
617 return mNeedsFiltering;
618 }
619
620 const auto& compositionState = outputLayer->getState();
621 const auto displayFrame = compositionState.displayFrame;
622 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800623 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
624 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700625}
626
David Sodman0c69cad2017-08-21 12:12:51 -0700627uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800628 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700629 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700630 } else {
631 return mCurrentFrameNumber;
632 }
633}
634
Vishnu Nair60356342018-11-13 13:00:45 -0800635Rect BufferLayer::getBufferSize(const State& s) const {
636 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
637 // we cannot determine the buffer size.
638 if ((s.sidebandStream != nullptr) ||
639 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
640 return Rect(getActiveWidth(s), getActiveHeight(s));
641 }
642
643 if (mActiveBuffer == nullptr) {
644 return Rect::INVALID_RECT;
645 }
646
647 uint32_t bufWidth = mActiveBuffer->getWidth();
648 uint32_t bufHeight = mActiveBuffer->getHeight();
649
650 // Undo any transformations on the buffer and return the result.
651 if (mCurrentTransform & ui::Transform::ROT_90) {
652 std::swap(bufWidth, bufHeight);
653 }
654
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800655 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800656 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
657 if (invTransform & ui::Transform::ROT_90) {
658 std::swap(bufWidth, bufHeight);
659 }
660 }
661
662 return Rect(bufWidth, bufHeight);
663}
664
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800665std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
666 return mCompositionLayer;
667}
668
Vishnu Nair4351ad52019-02-11 14:13:02 -0800669FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
670 const State& s(getDrawingState());
671
672 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
673 // we cannot determine the buffer size.
674 if ((s.sidebandStream != nullptr) ||
675 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
676 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
677 }
678
679 if (mActiveBuffer == nullptr) {
680 return parentBounds;
681 }
682
683 uint32_t bufWidth = mActiveBuffer->getWidth();
684 uint32_t bufHeight = mActiveBuffer->getHeight();
685
686 // Undo any transformations on the buffer and return the result.
687 if (mCurrentTransform & ui::Transform::ROT_90) {
688 std::swap(bufWidth, bufHeight);
689 }
690
691 if (getTransformToDisplayInverse()) {
692 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
693 if (invTransform & ui::Transform::ROT_90) {
694 std::swap(bufWidth, bufHeight);
695 }
696 }
697
698 return FloatRect(0, 0, bufWidth, bufHeight);
699}
700
David Sodman0c69cad2017-08-21 12:12:51 -0700701} // namespace android
702
703#if defined(__gl_h_)
704#error "don't include gl/gl.h in this file"
705#endif
706
707#if defined(__gl2_h_)
708#error "don't include gl2/gl2.h in this file"
709#endif