blob: 9662fcd01f37db87b9c3b2e59a4985131fa28ede [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
22#include "BufferLayer.h"
23#include "Colorizer.h"
24#include "DisplayDevice.h"
25#include "LayerRejecter.h"
David Sodman0c69cad2017-08-21 12:12:51 -070026
Yiwei Zhang7e666a52018-11-15 13:33:42 -080027#include "TimeStats/TimeStats.h"
28
Peiyong Lincbc184f2018-08-22 13:24:10 -070029#include <renderengine/RenderEngine.h>
David Sodman0c69cad2017-08-21 12:12:51 -070030
31#include <gui/BufferItem.h>
32#include <gui/BufferQueue.h>
33#include <gui/LayerDebugInfo.h>
34#include <gui/Surface.h>
35
36#include <ui/DebugUtils.h>
37
38#include <utils/Errors.h>
39#include <utils/Log.h>
40#include <utils/NativeHandle.h>
41#include <utils/StopWatch.h>
42#include <utils/Trace.h>
43
44#include <cutils/compiler.h>
45#include <cutils/native_handle.h>
46#include <cutils/properties.h>
47
48#include <math.h>
49#include <stdlib.h>
50#include <mutex>
51
52namespace android {
53
Lloyd Pique42ab75e2018-09-12 20:46:03 -070054BufferLayer::BufferLayer(const LayerCreationArgs& args)
55 : Layer(args), mTextureName(args.flinger->getNewTexture()) {
56 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070057
Lloyd Pique42ab75e2018-09-12 20:46:03 -070058 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070059
Lloyd Pique42ab75e2018-09-12 20:46:03 -070060 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
61 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070062}
63
64BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070065 mFlinger->deleteTextureAsync(mTextureName);
66
David Sodman6f65f3e2017-11-03 14:28:09 -070067 if (!getBE().mHwcLayers.empty()) {
David Sodman0c69cad2017-08-21 12:12:51 -070068 ALOGE("Found stale hardware composer layers when destroying "
69 "surface flinger layer %s",
70 mName.string());
chaviw61626f22018-11-15 16:26:27 -080071 destroyAllHwcLayersPlusChildren();
David Sodman0c69cad2017-08-21 12:12:51 -070072 }
Yiwei Zhangdc224042018-10-18 15:34:00 -070073
Yiwei Zhang7e666a52018-11-15 13:33:42 -080074 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070075}
76
David Sodmaneb085e02017-10-05 18:49:04 -070077void BufferLayer::useSurfaceDamage() {
78 if (mFlinger->mForceFullDamage) {
79 surfaceDamageRegion = Region::INVALID_REGION;
80 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070081 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070082 }
83}
84
85void BufferLayer::useEmptyDamage() {
86 surfaceDamageRegion.clear();
87}
88
Marissa Wallfd668622018-05-10 10:21:13 -070089bool BufferLayer::isOpaque(const Layer::State& s) const {
90 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
91 // layer's opaque flag.
92 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
93 return false;
94 }
95
96 // if the layer has the opaque flag, then we're always opaque,
97 // otherwise we use the current buffer's format.
98 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070099}
100
101bool BufferLayer::isVisible() const {
102 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800103 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700104}
105
106bool BufferLayer::isFixedSize() const {
107 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
108}
109
David Sodman0c69cad2017-08-21 12:12:51 -0700110static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800111 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
112 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
113 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 -0700114 mat4 tr;
115
116 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
117 tr = tr * rot90;
118 }
119 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
120 tr = tr * flipH;
121 }
122 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
123 tr = tr * flipV;
124 }
125 return inverse(tr);
126}
127
Alec Mouri0f714832018-11-12 15:31:06 -0800128bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
129 bool useIdentityTransform, Region& clearRegion,
130 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700131 ATRACE_CALL();
Alec Mouri0f714832018-11-12 15:31:06 -0800132 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800133 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700134 // the texture has not been created yet, this Layer has
135 // in fact never been drawn into. This happens frequently with
136 // SurfaceView because the WindowManager can't know when the client
137 // has drawn the first time.
138
139 // If there is nothing under us, we paint the screen in black, otherwise
140 // we just skip this update.
141
142 // figure out if there is something below us
143 Region under;
144 bool finished = false;
145 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
146 if (finished || layer == static_cast<BufferLayer const*>(this)) {
147 finished = true;
148 return;
149 }
Alec Mouri0f714832018-11-12 15:31:06 -0800150 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700151 });
152 // if not everything below us is covered, we plug the holes!
153 Region holes(clip.subtract(under));
154 if (!holes.isEmpty()) {
Alec Mouri0f714832018-11-12 15:31:06 -0800155 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700156 }
Alec Mouri0f714832018-11-12 15:31:06 -0800157 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700158 }
David Sodman0c69cad2017-08-21 12:12:51 -0700159 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
Alec Mouri0f714832018-11-12 15:31:06 -0800160 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700161 if (!blackOutLayer) {
Alec Mouri0f714832018-11-12 15:31:06 -0800162 layer.source.buffer.buffer = mActiveBuffer;
163 layer.source.buffer.isOpaque = isOpaque(s);
164 layer.source.buffer.fence = mActiveBufferFence;
165 layer.source.buffer.cacheHint = useCachedBufferForClientComposition()
166 ? renderengine::Buffer::CachingHint::USE_CACHE
167 : renderengine::Buffer::CachingHint::NO_CACHE;
168 layer.source.buffer.textureName = mTextureName;
169 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
170 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700171 // TODO: we could be more subtle with isFixedSize()
Peiyong Linc2020ca2019-01-10 11:36:12 -0800172 const bool useFiltering = needsFiltering() || renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700173
174 // Query the texture matrix given our current filtering mode.
175 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700176 setFilteringEnabled(useFiltering);
177 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700178
179 if (getTransformToDisplayInverse()) {
180 /*
181 * the code below applies the primary display's inverse transform to
182 * the texture transform
183 */
184 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
185 mat4 tr = inverseOrientation(transform);
186
187 /**
188 * TODO(b/36727915): This is basically a hack.
189 *
190 * Ensure that regardless of the parent transformation,
191 * this buffer is always transformed from native display
192 * orientation to display orientation. For example, in the case
193 * of a camera where the buffer remains in native orientation,
194 * we want the pixels to always be upright.
195 */
196 sp<Layer> p = mDrawingParent.promote();
197 if (p != nullptr) {
198 const auto parentTransform = p->getTransform();
199 tr = tr * inverseOrientation(parentTransform.getOrientation());
200 }
201
202 // and finally apply it to the original texture matrix
203 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
204 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
205 }
206
Alec Mouri0f714832018-11-12 15:31:06 -0800207 const Rect win{computeBounds()};
208 const float bufferWidth = getBufferSize(s).getWidth();
209 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700210
Alec Mouri0f714832018-11-12 15:31:06 -0800211 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
212 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
213 const float translateY = float(win.top) / bufferHeight;
214 const float translateX = float(win.left) / bufferWidth;
215
216 // Flip y-coordinates because GLConsumer expects OpenGL convention.
217 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
218 mat4::translate(vec4(-.5, -.5, 0, 1)) *
219 mat4::translate(vec4(translateX, translateY, 0, 1)) *
220 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
221
222 layer.source.buffer.useTextureFiltering = useFiltering;
223 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700224 } else {
Alec Mouri0f714832018-11-12 15:31:06 -0800225 // If layer is blacked out, force alpha to 1 so that we draw a black color
226 // layer.
227 layer.source.buffer.buffer = nullptr;
228 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700229 }
Alec Mouri0f714832018-11-12 15:31:06 -0800230
231 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700232}
233
Marissa Wallfd668622018-05-10 10:21:13 -0700234bool BufferLayer::isHdrY410() const {
235 // pixel format is HDR Y410 masquerading as RGBA_1010102
236 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
237 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
238 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700239}
240
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241void BufferLayer::setPerFrameData(DisplayId displayId, const ui::Transform& transform,
242 const Rect& viewport, int32_t supportedPerFrameMetadata) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700243 RETURN_IF_NO_HWC_LAYER(displayId);
244
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
David Sodman15094112018-10-11 09:39:37 -0700249 auto& hwcInfo = getBE().mHwcLayers[displayId];
250 auto& hwcLayer = hwcInfo.layer;
251 auto error = hwcLayer->setVisibleRegion(visible);
252 if (error != HWC2::Error::None) {
253 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
254 to_string(error).c_str(), static_cast<int32_t>(error));
255 visible.dump(LOG_TAG);
256 }
David Sodmanba340492018-08-05 21:51:33 -0700257 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700258
259 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
260 if (error != HWC2::Error::None) {
261 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
262 to_string(error).c_str(), static_cast<int32_t>(error));
263 surfaceDamageRegion.dump(LOG_TAG);
264 }
David Sodmanba340492018-08-05 21:51:33 -0700265 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700266
267 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800268 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700269 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodman15094112018-10-11 09:39:37 -0700270 ALOGV("[%s] Requesting Sideband composition", mName.string());
271 error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
272 if (error != HWC2::Error::None) {
273 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
274 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
275 static_cast<int32_t>(error));
276 }
David Sodmanba340492018-08-05 21:51:33 -0700277 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700278 return;
279 }
280
David Sodman15094112018-10-11 09:39:37 -0700281 // Device or Cursor layers
282 if (mPotentialCursor) {
283 ALOGV("[%s] Requesting Cursor composition", mName.string());
284 setCompositionType(displayId, HWC2::Composition::Cursor);
285 } else {
286 ALOGV("[%s] Requesting Device composition", mName.string());
287 setCompositionType(displayId, HWC2::Composition::Device);
David Sodman0c69cad2017-08-21 12:12:51 -0700288 }
289
David Sodman15094112018-10-11 09:39:37 -0700290 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
291 error = hwcLayer->setDataspace(mCurrentDataSpace);
292 if (error != HWC2::Error::None) {
293 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
294 to_string(error).c_str(), static_cast<int32_t>(error));
295 }
296
297 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700299 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
300 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
301 to_string(error).c_str(), static_cast<int32_t>(error));
302 }
303
304 error = hwcLayer->setColorTransform(getColorTransform());
305 if (error != HWC2::Error::None) {
306 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
307 to_string(error).c_str(), static_cast<int32_t>(error));
308 }
David Sodmanba340492018-08-05 21:51:33 -0700309 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
310 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700311 getBE().compositionInfo.hwc.supportedPerFrameMetadata = supportedPerFrameMetadata;
Peiyong Lind3788632018-09-18 16:01:31 -0700312 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700313
Dominik Laskowski075d3172018-05-24 15:50:06 -0700314 setHwcLayerBuffer(displayId);
David Sodman0c69cad2017-08-21 12:12:51 -0700315}
316
Marissa Wallfd668622018-05-10 10:21:13 -0700317bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
318 if (mBufferLatched) {
319 Mutex::Autolock lock(mFrameEventHistoryMutex);
320 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700321 }
Marissa Wallfd668622018-05-10 10:21:13 -0700322 mRefreshPending = false;
323 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700324}
325
Dominik Laskowski075d3172018-05-24 15:50:06 -0700326bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
327 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700328 const std::shared_ptr<FenceTime>& presentFence,
329 const CompositorTiming& compositorTiming) {
330 // mFrameLatencyNeeded is true when a new frame was latched for the
331 // composition.
332 if (!mFrameLatencyNeeded) return false;
333
334 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700335 {
Marissa Wallfd668622018-05-10 10:21:13 -0700336 Mutex::Autolock lock(mFrameEventHistoryMutex);
337 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
338 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700339 }
340
Marissa Wallfd668622018-05-10 10:21:13 -0700341 // Update mFrameTracker.
342 nsecs_t desiredPresentTime = getDesiredPresentTime();
343 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
344
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700345 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800346 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700347
348 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
349 if (frameReadyFence->isValid()) {
350 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
351 } else {
352 // There was no fence for this frame, so assume that it was ready
353 // to be presented at the desired present time.
354 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700355 }
Marissa Wallfd668622018-05-10 10:21:13 -0700356
357 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800358 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700359 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700360 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700361 // The HWC doesn't support present fences, so use the refresh
362 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700363 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800364 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700365 mFrameTracker.setActualPresentTime(actualPresentTime);
366 }
367
368 mFrameTracker.advanceFrame();
369 mFrameLatencyNeeded = false;
370 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700371}
372
Alec Mouri86770e52018-09-24 22:40:58 +0000373Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
374 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700375 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700376
Marissa Wallfd668622018-05-10 10:21:13 -0700377 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700378
Marissa Wallfd668622018-05-10 10:21:13 -0700379 if (sidebandStreamDirtyRegion) {
380 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700381 }
382
Marissa Wallfd668622018-05-10 10:21:13 -0700383 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700384
Marissa Wallfd668622018-05-10 10:21:13 -0700385 if (!hasReadyFrame()) {
386 return dirtyRegion;
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) {
395 return dirtyRegion;
396 }
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();
Marissa Wallfd668622018-05-10 10:21:13 -0700402 return dirtyRegion;
403 }
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();
412 return dirtyRegion;
413 }
414
Alec Mouri86770e52018-09-24 22:40:58 +0000415 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700416 if (err != NO_ERROR) {
417 return dirtyRegion;
418 }
419
420 err = updateActiveBuffer();
421 if (err != NO_ERROR) {
422 return dirtyRegion;
423 }
424
425 mBufferLatched = true;
426
427 err = updateFrameNumber(latchTime);
428 if (err != NO_ERROR) {
429 return dirtyRegion;
430 }
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());
469 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
470 (scalingMode != mCurrentScalingMode)) {
471 mCurrentCrop = crop;
472 mCurrentTransform = transform;
473 mCurrentScalingMode = scalingMode;
474 recomputeVisibleRegions = true;
475 }
476
477 if (oldBuffer != nullptr) {
478 uint32_t bufWidth = mActiveBuffer->getWidth();
479 uint32_t bufHeight = mActiveBuffer->getHeight();
480 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
481 recomputeVisibleRegions = true;
482 }
483 }
484
485 if (oldOpacity != isOpaque(s)) {
486 recomputeVisibleRegions = true;
487 }
488
489 // Remove any sync points corresponding to the buffer which was just
490 // latched
491 {
492 Mutex::Autolock lock(mLocalSyncPointMutex);
493 auto point = mLocalSyncPoints.begin();
494 while (point != mLocalSyncPoints.end()) {
495 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
496 // This sync point must have been added since we started
497 // latching. Don't drop it yet.
498 ++point;
499 continue;
500 }
501
502 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
503 point = mLocalSyncPoints.erase(point);
504 } else {
505 ++point;
506 }
507 }
508 }
509
510 // FIXME: postedRegion should be dirty & bounds
511 // transform the dirty region to window-manager space
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800512 return getTransform().transform(Region(getBufferSize(s)));
Marissa Wallfd668622018-05-10 10:21:13 -0700513}
514
515// transaction
516void BufferLayer::notifyAvailableFrames() {
517 auto headFrameNumber = getHeadFrameNumber();
518 bool headFenceSignaled = fenceHasSignaled();
519 Mutex::Autolock lock(mLocalSyncPointMutex);
520 for (auto& point : mLocalSyncPoints) {
521 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
522 point->setFrameAvailable();
523 }
David Sodman0c69cad2017-08-21 12:12:51 -0700524 }
525}
526
Marissa Wallfd668622018-05-10 10:21:13 -0700527bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700528 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700529}
530
531uint32_t BufferLayer::getEffectiveScalingMode() const {
532 if (mOverrideScalingMode >= 0) {
533 return mOverrideScalingMode;
534 }
535
536 return mCurrentScalingMode;
537}
538
539bool BufferLayer::isProtected() const {
540 const sp<GraphicBuffer>& buffer(mActiveBuffer);
541 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
542}
543
544bool BufferLayer::latchUnsignaledBuffers() {
545 static bool propertyLoaded = false;
546 static bool latch = false;
547 static std::mutex mutex;
548 std::lock_guard<std::mutex> lock(mutex);
549 if (!propertyLoaded) {
550 char value[PROPERTY_VALUE_MAX] = {};
551 property_get("debug.sf.latch_unsignaled", value, "0");
552 latch = atoi(value);
553 propertyLoaded = true;
554 }
555 return latch;
556}
557
558// h/w composer set-up
559bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800560 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700561 bool matchingFramesFound = false;
562 bool allTransactionsApplied = true;
563 Mutex::Autolock lock(mLocalSyncPointMutex);
564
565 for (auto& point : mLocalSyncPoints) {
566 if (point->getFrameNumber() > headFrameNumber) {
567 break;
568 }
569 matchingFramesFound = true;
570
571 if (!point->frameIsAvailable()) {
572 // We haven't notified the remote layer that the frame for
573 // this point is available yet. Notify it now, and then
574 // abort this attempt to latch.
575 point->setFrameAvailable();
576 allTransactionsApplied = false;
577 break;
578 }
579
580 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
581 }
582 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700583}
584
585// As documented in libhardware header, formats in the range
586// 0x100 - 0x1FF are specific to the HAL implementation, and
587// are known to have no alpha channel
588// TODO: move definition for device-specific range into
589// hardware.h, instead of using hard-coded values here.
590#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
591
592bool BufferLayer::getOpacityForFormat(uint32_t format) {
593 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
594 return true;
595 }
596 switch (format) {
597 case HAL_PIXEL_FORMAT_RGBA_8888:
598 case HAL_PIXEL_FORMAT_BGRA_8888:
599 case HAL_PIXEL_FORMAT_RGBA_FP16:
600 case HAL_PIXEL_FORMAT_RGBA_1010102:
601 return false;
602 }
603 // in all other case, we have no blending (also for unknown formats)
604 return true;
605}
606
Peiyong Linc2020ca2019-01-10 11:36:12 -0800607bool BufferLayer::needsFiltering() const {
608 const auto displayFrame = getBE().compositionInfo.hwc.displayFrame;
609 const auto sourceCrop = getBE().compositionInfo.hwc.sourceCrop;
610 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
611 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700612}
613
David Sodman0c69cad2017-08-21 12:12:51 -0700614uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800615 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700616 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700617 } else {
618 return mCurrentFrameNumber;
619 }
620}
621
Vishnu Nair60356342018-11-13 13:00:45 -0800622Rect BufferLayer::getBufferSize(const State& s) const {
623 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
624 // we cannot determine the buffer size.
625 if ((s.sidebandStream != nullptr) ||
626 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
627 return Rect(getActiveWidth(s), getActiveHeight(s));
628 }
629
630 if (mActiveBuffer == nullptr) {
631 return Rect::INVALID_RECT;
632 }
633
634 uint32_t bufWidth = mActiveBuffer->getWidth();
635 uint32_t bufHeight = mActiveBuffer->getHeight();
636
637 // Undo any transformations on the buffer and return the result.
638 if (mCurrentTransform & ui::Transform::ROT_90) {
639 std::swap(bufWidth, bufHeight);
640 }
641
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800642 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800643 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
644 if (invTransform & ui::Transform::ROT_90) {
645 std::swap(bufWidth, bufHeight);
646 }
647 }
648
649 return Rect(bufWidth, bufHeight);
650}
651
David Sodman0c69cad2017-08-21 12:12:51 -0700652} // namespace android
653
654#if defined(__gl_h_)
655#error "don't include gl/gl.h in this file"
656#endif
657
658#if defined(__gl2_h_)
659#error "don't include gl2/gl2.h in this file"
660#endif