blob: 52fbd440fffde58249ba93ac6cb1da635cf0b609 [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
Peiyong Lincbc184f2018-08-22 13:24:10 -070027#include <renderengine/RenderEngine.h>
David Sodman0c69cad2017-08-21 12:12:51 -070028
29#include <gui/BufferItem.h>
30#include <gui/BufferQueue.h>
31#include <gui/LayerDebugInfo.h>
32#include <gui/Surface.h>
33
34#include <ui/DebugUtils.h>
35
36#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/NativeHandle.h>
39#include <utils/StopWatch.h>
40#include <utils/Trace.h>
41
42#include <cutils/compiler.h>
43#include <cutils/native_handle.h>
44#include <cutils/properties.h>
45
46#include <math.h>
47#include <stdlib.h>
48#include <mutex>
49
50namespace android {
51
Lloyd Pique42ab75e2018-09-12 20:46:03 -070052BufferLayer::BufferLayer(const LayerCreationArgs& args)
53 : Layer(args), mTextureName(args.flinger->getNewTexture()) {
54 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070055
Peiyong Lin833074a2018-08-28 11:53:54 -070056 mTexture.init(renderengine::Texture::TEXTURE_EXTERNAL, mTextureName);
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());
71 destroyAllHwcLayers();
72 }
Yiwei Zhangdc224042018-10-18 15:34:00 -070073
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070074 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
128/*
129 * onDraw will draw the current layer onto the presentable buffer
130 */
131void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
Marissa Wall61c58622018-07-18 10:12:20 -0700132 bool useIdentityTransform) {
David Sodman0c69cad2017-08-21 12:12:51 -0700133 ATRACE_CALL();
134
David Sodman0cf8f8d2017-12-20 18:19:45 -0800135 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700136 // the texture has not been created yet, this Layer has
137 // in fact never been drawn into. This happens frequently with
138 // SurfaceView because the WindowManager can't know when the client
139 // has drawn the first time.
140
141 // If there is nothing under us, we paint the screen in black, otherwise
142 // we just skip this update.
143
144 // figure out if there is something below us
145 Region under;
146 bool finished = false;
147 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
148 if (finished || layer == static_cast<BufferLayer const*>(this)) {
149 finished = true;
150 return;
151 }
152 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
153 });
154 // if not everything below us is covered, we plug the holes!
155 Region holes(clip.subtract(under));
156 if (!holes.isEmpty()) {
157 clearWithOpenGL(renderArea, 0, 0, 0, 1);
158 }
159 return;
160 }
161
162 // Bind the current buffer to the GL texture, and wait for it to be
163 // ready for us to draw into.
Marissa Wallfd668622018-05-10 10:21:13 -0700164 status_t err = bindTextureImage();
David Sodman0c69cad2017-08-21 12:12:51 -0700165 if (err != NO_ERROR) {
166 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
167 // Go ahead and draw the buffer anyway; no matter what we do the screen
168 // is probably going to have something visibly wrong.
169 }
170
171 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
172
Lloyd Pique144e1162017-12-20 16:44:52 -0800173 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700174
175 if (!blackOutLayer) {
176 // TODO: we could be more subtle with isFixedSize()
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700177 const bool useFiltering = needsFiltering(renderArea) || 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
212 // Set things up for texturing.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800213 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700214 mTexture.setFiltering(useFiltering);
215 mTexture.setMatrix(textureMatrix);
216
217 engine.setupLayerTexturing(mTexture);
218 } else {
219 engine.setupLayerBlackedOut();
220 }
221 drawWithOpenGL(renderArea, useIdentityTransform);
222 engine.disableTexturing();
223}
224
Marissa Wallfd668622018-05-10 10:21:13 -0700225bool BufferLayer::isHdrY410() const {
226 // pixel format is HDR Y410 masquerading as RGBA_1010102
227 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
228 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
229 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700230}
231
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700232void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& display) {
David Sodman0c69cad2017-08-21 12:12:51 -0700233 // Apply this display's projection's viewport to the visible region
234 // before giving it to the HWC HAL.
Peiyong Linefefaac2018-08-17 12:27:51 -0700235 const ui::Transform& tr = display->getTransform();
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700236 const auto& viewport = display->getViewport();
David Sodman0c69cad2017-08-21 12:12:51 -0700237 Region visible = tr.transform(visibleRegion.intersect(viewport));
Dominik Laskowski7e045462018-05-30 13:02:02 -0700238 const auto displayId = display->getId();
David Sodman15094112018-10-11 09:39:37 -0700239 if (!hasHwcLayer(displayId)) {
240 ALOGE("[%s] failed to setPerFrameData: no HWC layer found (%d)",
241 mName.string(), displayId);
242 return;
243 }
244 auto& hwcInfo = getBE().mHwcLayers[displayId];
245 auto& hwcLayer = hwcInfo.layer;
246 auto error = hwcLayer->setVisibleRegion(visible);
247 if (error != HWC2::Error::None) {
248 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
249 to_string(error).c_str(), static_cast<int32_t>(error));
250 visible.dump(LOG_TAG);
251 }
David Sodmanba340492018-08-05 21:51:33 -0700252 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700253
254 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
255 if (error != HWC2::Error::None) {
256 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
257 to_string(error).c_str(), static_cast<int32_t>(error));
258 surfaceDamageRegion.dump(LOG_TAG);
259 }
David Sodmanba340492018-08-05 21:51:33 -0700260 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700261
262 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800263 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700264 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodman15094112018-10-11 09:39:37 -0700265 ALOGV("[%s] Requesting Sideband composition", mName.string());
266 error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
267 if (error != HWC2::Error::None) {
268 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
269 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
270 static_cast<int32_t>(error));
271 }
David Sodmanba340492018-08-05 21:51:33 -0700272 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700273 return;
274 }
275
David Sodman15094112018-10-11 09:39:37 -0700276 // Device or Cursor layers
277 if (mPotentialCursor) {
278 ALOGV("[%s] Requesting Cursor composition", mName.string());
279 setCompositionType(displayId, HWC2::Composition::Cursor);
280 } else {
281 ALOGV("[%s] Requesting Device composition", mName.string());
282 setCompositionType(displayId, HWC2::Composition::Device);
David Sodman0c69cad2017-08-21 12:12:51 -0700283 }
284
David Sodman15094112018-10-11 09:39:37 -0700285 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
286 error = hwcLayer->setDataspace(mCurrentDataSpace);
287 if (error != HWC2::Error::None) {
288 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
289 to_string(error).c_str(), static_cast<int32_t>(error));
290 }
291
292 const HdrMetadata& metadata = getDrawingHdrMetadata();
293 error = hwcLayer->setPerFrameMetadata(display->getSupportedPerFrameMetadata(), metadata);
294 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
295 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
296 to_string(error).c_str(), static_cast<int32_t>(error));
297 }
298
299 error = hwcLayer->setColorTransform(getColorTransform());
300 if (error != HWC2::Error::None) {
301 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
302 to_string(error).c_str(), static_cast<int32_t>(error));
303 }
David Sodmanba340492018-08-05 21:51:33 -0700304 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
305 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
306 getBE().compositionInfo.hwc.supportedPerFrameMetadata = display->getSupportedPerFrameMetadata();
Peiyong Lind3788632018-09-18 16:01:31 -0700307 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700308
Marissa Wallfd668622018-05-10 10:21:13 -0700309 setHwcLayerBuffer(display);
David Sodman0c69cad2017-08-21 12:12:51 -0700310}
311
Marissa Wallfd668622018-05-10 10:21:13 -0700312bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
313 if (mBufferLatched) {
314 Mutex::Autolock lock(mFrameEventHistoryMutex);
315 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700316 }
Marissa Wallfd668622018-05-10 10:21:13 -0700317 mRefreshPending = false;
318 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700319}
320
Marissa Wallfd668622018-05-10 10:21:13 -0700321bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
322 const std::shared_ptr<FenceTime>& presentFence,
323 const CompositorTiming& compositorTiming) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800324
Marissa Wallfd668622018-05-10 10:21:13 -0700325 // mFrameLatencyNeeded is true when a new frame was latched for the
326 // composition.
327 if (!mFrameLatencyNeeded) return false;
328
329 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700330 {
Marissa Wallfd668622018-05-10 10:21:13 -0700331 Mutex::Autolock lock(mFrameEventHistoryMutex);
332 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
333 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700334 }
335
Marissa Wallfd668622018-05-10 10:21:13 -0700336 // Update mFrameTracker.
337 nsecs_t desiredPresentTime = getDesiredPresentTime();
338 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
339
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700340 const int32_t layerID = getSequence();
341 mTimeStats.setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700342
343 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
344 if (frameReadyFence->isValid()) {
345 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
346 } else {
347 // There was no fence for this frame, so assume that it was ready
348 // to be presented at the desired present time.
349 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700350 }
Marissa Wallfd668622018-05-10 10:21:13 -0700351
352 if (presentFence->isValid()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700353 mTimeStats.setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700354 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
355 } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) {
356 // The HWC doesn't support present fences, so use the refresh
357 // timestamp instead.
358 const nsecs_t actualPresentTime =
359 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700360 mTimeStats.setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700361 mFrameTracker.setActualPresentTime(actualPresentTime);
362 }
363
364 mFrameTracker.advanceFrame();
365 mFrameLatencyNeeded = false;
366 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700367}
368
Alec Mouri86770e52018-09-24 22:40:58 +0000369Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
370 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700371 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700372
Marissa Wallfd668622018-05-10 10:21:13 -0700373 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700374
Marissa Wallfd668622018-05-10 10:21:13 -0700375 if (sidebandStreamDirtyRegion) {
376 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700377 }
378
Marissa Wallfd668622018-05-10 10:21:13 -0700379 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700380
Marissa Wallfd668622018-05-10 10:21:13 -0700381 if (!hasReadyFrame()) {
382 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700383 }
David Sodman0c69cad2017-08-21 12:12:51 -0700384
Marissa Wallfd668622018-05-10 10:21:13 -0700385 // if we've already called updateTexImage() without going through
386 // a composition step, we have to skip this layer at this point
387 // because we cannot call updateTeximage() without a corresponding
388 // compositionComplete() call.
389 // we'll trigger an update in onPreComposition().
390 if (mRefreshPending) {
391 return dirtyRegion;
392 }
393
394 // If the head buffer's acquire fence hasn't signaled yet, return and
395 // try again later
396 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700397 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700398 return dirtyRegion;
399 }
400
401 // Capture the old state of the layer for comparisons later
402 const State& s(getDrawingState());
403 const bool oldOpacity = isOpaque(s);
404 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
405
406 if (!allTransactionsSignaled()) {
407 mFlinger->signalLayerUpdate();
408 return dirtyRegion;
409 }
410
Alec Mouri86770e52018-09-24 22:40:58 +0000411 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700412 if (err != NO_ERROR) {
413 return dirtyRegion;
414 }
415
416 err = updateActiveBuffer();
417 if (err != NO_ERROR) {
418 return dirtyRegion;
419 }
420
421 mBufferLatched = true;
422
423 err = updateFrameNumber(latchTime);
424 if (err != NO_ERROR) {
425 return dirtyRegion;
426 }
427
428 mRefreshPending = true;
429 mFrameLatencyNeeded = true;
430 if (oldBuffer == nullptr) {
431 // the first time we receive a buffer, we need to trigger a
432 // geometry invalidation.
433 recomputeVisibleRegions = true;
434 }
435
436 ui::Dataspace dataSpace = getDrawingDataSpace();
437 // treat modern dataspaces as legacy dataspaces whenever possible, until
438 // we can trust the buffer producers
439 switch (dataSpace) {
440 case ui::Dataspace::V0_SRGB:
441 dataSpace = ui::Dataspace::SRGB;
442 break;
443 case ui::Dataspace::V0_SRGB_LINEAR:
444 dataSpace = ui::Dataspace::SRGB_LINEAR;
445 break;
446 case ui::Dataspace::V0_JFIF:
447 dataSpace = ui::Dataspace::JFIF;
448 break;
449 case ui::Dataspace::V0_BT601_625:
450 dataSpace = ui::Dataspace::BT601_625;
451 break;
452 case ui::Dataspace::V0_BT601_525:
453 dataSpace = ui::Dataspace::BT601_525;
454 break;
455 case ui::Dataspace::V0_BT709:
456 dataSpace = ui::Dataspace::BT709;
457 break;
458 default:
459 break;
460 }
461 mCurrentDataSpace = dataSpace;
462
463 Rect crop(getDrawingCrop());
464 const uint32_t transform(getDrawingTransform());
465 const uint32_t scalingMode(getDrawingScalingMode());
466 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
467 (scalingMode != mCurrentScalingMode)) {
468 mCurrentCrop = crop;
469 mCurrentTransform = transform;
470 mCurrentScalingMode = scalingMode;
471 recomputeVisibleRegions = true;
472 }
473
474 if (oldBuffer != nullptr) {
475 uint32_t bufWidth = mActiveBuffer->getWidth();
476 uint32_t bufHeight = mActiveBuffer->getHeight();
477 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
478 recomputeVisibleRegions = true;
479 }
480 }
481
482 if (oldOpacity != isOpaque(s)) {
483 recomputeVisibleRegions = true;
484 }
485
486 // Remove any sync points corresponding to the buffer which was just
487 // latched
488 {
489 Mutex::Autolock lock(mLocalSyncPointMutex);
490 auto point = mLocalSyncPoints.begin();
491 while (point != mLocalSyncPoints.end()) {
492 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
493 // This sync point must have been added since we started
494 // latching. Don't drop it yet.
495 ++point;
496 continue;
497 }
498
499 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
500 point = mLocalSyncPoints.erase(point);
501 } else {
502 ++point;
503 }
504 }
505 }
506
507 // FIXME: postedRegion should be dirty & bounds
508 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700509 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700510}
511
512// transaction
513void BufferLayer::notifyAvailableFrames() {
514 auto headFrameNumber = getHeadFrameNumber();
515 bool headFenceSignaled = fenceHasSignaled();
516 Mutex::Autolock lock(mLocalSyncPointMutex);
517 for (auto& point : mLocalSyncPoints) {
518 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
519 point->setFrameAvailable();
520 }
David Sodman0c69cad2017-08-21 12:12:51 -0700521 }
522}
523
Marissa Wallfd668622018-05-10 10:21:13 -0700524bool BufferLayer::hasReadyFrame() const {
525 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
526}
527
528uint32_t BufferLayer::getEffectiveScalingMode() const {
529 if (mOverrideScalingMode >= 0) {
530 return mOverrideScalingMode;
531 }
532
533 return mCurrentScalingMode;
534}
535
536bool BufferLayer::isProtected() const {
537 const sp<GraphicBuffer>& buffer(mActiveBuffer);
538 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
539}
540
541bool BufferLayer::latchUnsignaledBuffers() {
542 static bool propertyLoaded = false;
543 static bool latch = false;
544 static std::mutex mutex;
545 std::lock_guard<std::mutex> lock(mutex);
546 if (!propertyLoaded) {
547 char value[PROPERTY_VALUE_MAX] = {};
548 property_get("debug.sf.latch_unsignaled", value, "0");
549 latch = atoi(value);
550 propertyLoaded = true;
551 }
552 return latch;
553}
554
555// h/w composer set-up
556bool BufferLayer::allTransactionsSignaled() {
557 auto headFrameNumber = getHeadFrameNumber();
558 bool matchingFramesFound = false;
559 bool allTransactionsApplied = true;
560 Mutex::Autolock lock(mLocalSyncPointMutex);
561
562 for (auto& point : mLocalSyncPoints) {
563 if (point->getFrameNumber() > headFrameNumber) {
564 break;
565 }
566 matchingFramesFound = true;
567
568 if (!point->frameIsAvailable()) {
569 // We haven't notified the remote layer that the frame for
570 // this point is available yet. Notify it now, and then
571 // abort this attempt to latch.
572 point->setFrameAvailable();
573 allTransactionsApplied = false;
574 break;
575 }
576
577 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
578 }
579 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700580}
581
582// As documented in libhardware header, formats in the range
583// 0x100 - 0x1FF are specific to the HAL implementation, and
584// are known to have no alpha channel
585// TODO: move definition for device-specific range into
586// hardware.h, instead of using hard-coded values here.
587#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
588
589bool BufferLayer::getOpacityForFormat(uint32_t format) {
590 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
591 return true;
592 }
593 switch (format) {
594 case HAL_PIXEL_FORMAT_RGBA_8888:
595 case HAL_PIXEL_FORMAT_BGRA_8888:
596 case HAL_PIXEL_FORMAT_RGBA_FP16:
597 case HAL_PIXEL_FORMAT_RGBA_1010102:
598 return false;
599 }
600 // in all other case, we have no blending (also for unknown formats)
601 return true;
602}
603
Marissa Wallfd668622018-05-10 10:21:13 -0700604bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
605 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700606}
607
David Sodman41fdfc92017-11-06 16:09:56 -0800608void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700609 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700610 const State& s(getDrawingState());
611
David Sodman9eeae692017-11-02 10:53:32 -0700612 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700613
614 /*
615 * NOTE: the way we compute the texture coordinates here produces
616 * different results than when we take the HWC path -- in the later case
617 * the "source crop" is rounded to texel boundaries.
618 * This can produce significantly different results when the texture
619 * is scaled by a large amount.
620 *
621 * The GL code below is more logical (imho), and the difference with
622 * HWC is due to a limitation of the HWC API to integers -- a question
623 * is suspend is whether we should ignore this problem or revert to
624 * GL composition when a buffer scaling is applied (maybe with some
625 * minimal value)? Or, we could make GL behave like HWC -- but this feel
626 * like more of a hack.
627 */
Dan Stoza80d61162017-12-20 15:57:52 -0800628 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700629
Peiyong Linefefaac2018-08-17 12:27:51 -0700630 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800631 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700632
Marissa Wall61c58622018-07-18 10:12:20 -0700633 float left = float(win.left) / float(getActiveWidth(s));
634 float top = float(win.top) / float(getActiveHeight(s));
635 float right = float(win.right) / float(getActiveWidth(s));
636 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700637
638 // TODO: we probably want to generate the texture coords with the mesh
639 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700640 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
Chia-I Wu1be50b52018-08-29 10:44:48 -0700641 // flip texcoords vertically because BufferLayerConsumer expects them to be in GL convention
David Sodman0c69cad2017-08-21 12:12:51 -0700642 texCoords[0] = vec2(left, 1.0f - top);
643 texCoords[1] = vec2(left, 1.0f - bottom);
644 texCoords[2] = vec2(right, 1.0f - bottom);
645 texCoords[3] = vec2(right, 1.0f - top);
646
bohu21566132018-03-27 14:36:34 -0700647 auto& engine(mFlinger->getRenderEngine());
648 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
649 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700650 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800651
Chia-I Wu692e0832018-06-05 15:46:58 -0700652 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700653 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800654 }
bohu21566132018-03-27 14:36:34 -0700655
656 engine.drawMesh(getBE().mMesh);
657 engine.disableBlending();
658
659 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700660}
661
David Sodman0c69cad2017-08-21 12:12:51 -0700662uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700663 if (hasDrawingBuffer()) {
664 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700665 } else {
666 return mCurrentFrameNumber;
667 }
668}
669
Vishnu Nair60356342018-11-13 13:00:45 -0800670Rect BufferLayer::getBufferSize(const State& s) const {
671 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
672 // we cannot determine the buffer size.
673 if ((s.sidebandStream != nullptr) ||
674 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
675 return Rect(getActiveWidth(s), getActiveHeight(s));
676 }
677
678 if (mActiveBuffer == nullptr) {
679 return Rect::INVALID_RECT;
680 }
681
682 uint32_t bufWidth = mActiveBuffer->getWidth();
683 uint32_t bufHeight = mActiveBuffer->getHeight();
684
685 // Undo any transformations on the buffer and return the result.
686 if (mCurrentTransform & ui::Transform::ROT_90) {
687 std::swap(bufWidth, bufHeight);
688 }
689
690 if (getTransformToDisplayInverse()) {
691 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
692 if (invTransform & ui::Transform::ROT_90) {
693 std::swap(bufWidth, bufHeight);
694 }
695 }
696
697 return Rect(bufWidth, bufHeight);
698}
699
David Sodman0c69cad2017-08-21 12:12:51 -0700700} // namespace android
701
702#if defined(__gl_h_)
703#error "don't include gl/gl.h in this file"
704#endif
705
706#if defined(__gl2_h_)
707#error "don't include gl2/gl2.h in this file"
708#endif