blob: 397540a5323c9623f105fb26ea3b0bf91557f6a7 [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 Sodmanba340492018-08-05 21:51:33 -0700239 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodmanba340492018-08-05 21:51:33 -0700240 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700241
242 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800243 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700244 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodmanba340492018-08-05 21:51:33 -0700245 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700246 return;
247 }
248
David Sodman10a41ff2018-08-05 12:14:17 -0700249 if (getBE().compositionInfo.hwc.skipGeometry) {
250 // Device or Cursor layers
251 if (mPotentialCursor) {
252 ALOGV("[%s] Requesting Cursor composition", mName.string());
253 setCompositionType(displayId, HWC2::Composition::Cursor);
254 } else {
255 ALOGV("[%s] Requesting Device composition", mName.string());
256 setCompositionType(displayId, HWC2::Composition::Device);
257 }
David Sodman0c69cad2017-08-21 12:12:51 -0700258 }
259
David Sodmanba340492018-08-05 21:51:33 -0700260 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
261 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
262 getBE().compositionInfo.hwc.supportedPerFrameMetadata = display->getSupportedPerFrameMetadata();
Peiyong Lind3788632018-09-18 16:01:31 -0700263 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700264
Marissa Wallfd668622018-05-10 10:21:13 -0700265 setHwcLayerBuffer(display);
David Sodman0c69cad2017-08-21 12:12:51 -0700266}
267
Marissa Wallfd668622018-05-10 10:21:13 -0700268bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
269 if (mBufferLatched) {
270 Mutex::Autolock lock(mFrameEventHistoryMutex);
271 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700272 }
Marissa Wallfd668622018-05-10 10:21:13 -0700273 mRefreshPending = false;
274 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700275}
276
Marissa Wallfd668622018-05-10 10:21:13 -0700277bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
278 const std::shared_ptr<FenceTime>& presentFence,
279 const CompositorTiming& compositorTiming) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800280
Marissa Wallfd668622018-05-10 10:21:13 -0700281 // mFrameLatencyNeeded is true when a new frame was latched for the
282 // composition.
283 if (!mFrameLatencyNeeded) return false;
284
285 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700286 {
Marissa Wallfd668622018-05-10 10:21:13 -0700287 Mutex::Autolock lock(mFrameEventHistoryMutex);
288 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
289 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700290 }
291
Marissa Wallfd668622018-05-10 10:21:13 -0700292 // Update mFrameTracker.
293 nsecs_t desiredPresentTime = getDesiredPresentTime();
294 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
295
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700296 const int32_t layerID = getSequence();
297 mTimeStats.setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700298
299 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
300 if (frameReadyFence->isValid()) {
301 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
302 } else {
303 // There was no fence for this frame, so assume that it was ready
304 // to be presented at the desired present time.
305 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700306 }
Marissa Wallfd668622018-05-10 10:21:13 -0700307
308 if (presentFence->isValid()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700309 mTimeStats.setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700310 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
311 } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) {
312 // The HWC doesn't support present fences, so use the refresh
313 // timestamp instead.
314 const nsecs_t actualPresentTime =
315 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700316 mTimeStats.setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700317 mFrameTracker.setActualPresentTime(actualPresentTime);
318 }
319
320 mFrameTracker.advanceFrame();
321 mFrameLatencyNeeded = false;
322 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700323}
324
Alec Mouri86770e52018-09-24 22:40:58 +0000325Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
326 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700327 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700328
Marissa Wallfd668622018-05-10 10:21:13 -0700329 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700330
Marissa Wallfd668622018-05-10 10:21:13 -0700331 if (sidebandStreamDirtyRegion) {
332 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700333 }
334
Marissa Wallfd668622018-05-10 10:21:13 -0700335 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700336
Marissa Wallfd668622018-05-10 10:21:13 -0700337 if (!hasReadyFrame()) {
338 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700339 }
David Sodman0c69cad2017-08-21 12:12:51 -0700340
Marissa Wallfd668622018-05-10 10:21:13 -0700341 // if we've already called updateTexImage() without going through
342 // a composition step, we have to skip this layer at this point
343 // because we cannot call updateTeximage() without a corresponding
344 // compositionComplete() call.
345 // we'll trigger an update in onPreComposition().
346 if (mRefreshPending) {
347 return dirtyRegion;
348 }
349
350 // If the head buffer's acquire fence hasn't signaled yet, return and
351 // try again later
352 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700353 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700354 return dirtyRegion;
355 }
356
357 // Capture the old state of the layer for comparisons later
358 const State& s(getDrawingState());
359 const bool oldOpacity = isOpaque(s);
360 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
361
362 if (!allTransactionsSignaled()) {
363 mFlinger->signalLayerUpdate();
364 return dirtyRegion;
365 }
366
Alec Mouri86770e52018-09-24 22:40:58 +0000367 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700368 if (err != NO_ERROR) {
369 return dirtyRegion;
370 }
371
372 err = updateActiveBuffer();
373 if (err != NO_ERROR) {
374 return dirtyRegion;
375 }
376
377 mBufferLatched = true;
378
379 err = updateFrameNumber(latchTime);
380 if (err != NO_ERROR) {
381 return dirtyRegion;
382 }
383
384 mRefreshPending = true;
385 mFrameLatencyNeeded = true;
386 if (oldBuffer == nullptr) {
387 // the first time we receive a buffer, we need to trigger a
388 // geometry invalidation.
389 recomputeVisibleRegions = true;
390 }
391
392 ui::Dataspace dataSpace = getDrawingDataSpace();
393 // treat modern dataspaces as legacy dataspaces whenever possible, until
394 // we can trust the buffer producers
395 switch (dataSpace) {
396 case ui::Dataspace::V0_SRGB:
397 dataSpace = ui::Dataspace::SRGB;
398 break;
399 case ui::Dataspace::V0_SRGB_LINEAR:
400 dataSpace = ui::Dataspace::SRGB_LINEAR;
401 break;
402 case ui::Dataspace::V0_JFIF:
403 dataSpace = ui::Dataspace::JFIF;
404 break;
405 case ui::Dataspace::V0_BT601_625:
406 dataSpace = ui::Dataspace::BT601_625;
407 break;
408 case ui::Dataspace::V0_BT601_525:
409 dataSpace = ui::Dataspace::BT601_525;
410 break;
411 case ui::Dataspace::V0_BT709:
412 dataSpace = ui::Dataspace::BT709;
413 break;
414 default:
415 break;
416 }
417 mCurrentDataSpace = dataSpace;
418
419 Rect crop(getDrawingCrop());
420 const uint32_t transform(getDrawingTransform());
421 const uint32_t scalingMode(getDrawingScalingMode());
422 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
423 (scalingMode != mCurrentScalingMode)) {
424 mCurrentCrop = crop;
425 mCurrentTransform = transform;
426 mCurrentScalingMode = scalingMode;
427 recomputeVisibleRegions = true;
428 }
429
430 if (oldBuffer != nullptr) {
431 uint32_t bufWidth = mActiveBuffer->getWidth();
432 uint32_t bufHeight = mActiveBuffer->getHeight();
433 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
434 recomputeVisibleRegions = true;
435 }
436 }
437
438 if (oldOpacity != isOpaque(s)) {
439 recomputeVisibleRegions = true;
440 }
441
442 // Remove any sync points corresponding to the buffer which was just
443 // latched
444 {
445 Mutex::Autolock lock(mLocalSyncPointMutex);
446 auto point = mLocalSyncPoints.begin();
447 while (point != mLocalSyncPoints.end()) {
448 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
449 // This sync point must have been added since we started
450 // latching. Don't drop it yet.
451 ++point;
452 continue;
453 }
454
455 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
456 point = mLocalSyncPoints.erase(point);
457 } else {
458 ++point;
459 }
460 }
461 }
462
463 // FIXME: postedRegion should be dirty & bounds
464 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700465 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700466}
467
468// transaction
469void BufferLayer::notifyAvailableFrames() {
470 auto headFrameNumber = getHeadFrameNumber();
471 bool headFenceSignaled = fenceHasSignaled();
472 Mutex::Autolock lock(mLocalSyncPointMutex);
473 for (auto& point : mLocalSyncPoints) {
474 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
475 point->setFrameAvailable();
476 }
David Sodman0c69cad2017-08-21 12:12:51 -0700477 }
478}
479
Marissa Wallfd668622018-05-10 10:21:13 -0700480bool BufferLayer::hasReadyFrame() const {
481 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
482}
483
484uint32_t BufferLayer::getEffectiveScalingMode() const {
485 if (mOverrideScalingMode >= 0) {
486 return mOverrideScalingMode;
487 }
488
489 return mCurrentScalingMode;
490}
491
492bool BufferLayer::isProtected() const {
493 const sp<GraphicBuffer>& buffer(mActiveBuffer);
494 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
495}
496
497bool BufferLayer::latchUnsignaledBuffers() {
498 static bool propertyLoaded = false;
499 static bool latch = false;
500 static std::mutex mutex;
501 std::lock_guard<std::mutex> lock(mutex);
502 if (!propertyLoaded) {
503 char value[PROPERTY_VALUE_MAX] = {};
504 property_get("debug.sf.latch_unsignaled", value, "0");
505 latch = atoi(value);
506 propertyLoaded = true;
507 }
508 return latch;
509}
510
511// h/w composer set-up
512bool BufferLayer::allTransactionsSignaled() {
513 auto headFrameNumber = getHeadFrameNumber();
514 bool matchingFramesFound = false;
515 bool allTransactionsApplied = true;
516 Mutex::Autolock lock(mLocalSyncPointMutex);
517
518 for (auto& point : mLocalSyncPoints) {
519 if (point->getFrameNumber() > headFrameNumber) {
520 break;
521 }
522 matchingFramesFound = true;
523
524 if (!point->frameIsAvailable()) {
525 // We haven't notified the remote layer that the frame for
526 // this point is available yet. Notify it now, and then
527 // abort this attempt to latch.
528 point->setFrameAvailable();
529 allTransactionsApplied = false;
530 break;
531 }
532
533 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
534 }
535 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700536}
537
538// As documented in libhardware header, formats in the range
539// 0x100 - 0x1FF are specific to the HAL implementation, and
540// are known to have no alpha channel
541// TODO: move definition for device-specific range into
542// hardware.h, instead of using hard-coded values here.
543#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
544
545bool BufferLayer::getOpacityForFormat(uint32_t format) {
546 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
547 return true;
548 }
549 switch (format) {
550 case HAL_PIXEL_FORMAT_RGBA_8888:
551 case HAL_PIXEL_FORMAT_BGRA_8888:
552 case HAL_PIXEL_FORMAT_RGBA_FP16:
553 case HAL_PIXEL_FORMAT_RGBA_1010102:
554 return false;
555 }
556 // in all other case, we have no blending (also for unknown formats)
557 return true;
558}
559
Marissa Wallfd668622018-05-10 10:21:13 -0700560bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
561 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700562}
563
David Sodman41fdfc92017-11-06 16:09:56 -0800564void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700565 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700566 const State& s(getDrawingState());
567
David Sodman9eeae692017-11-02 10:53:32 -0700568 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700569
570 /*
571 * NOTE: the way we compute the texture coordinates here produces
572 * different results than when we take the HWC path -- in the later case
573 * the "source crop" is rounded to texel boundaries.
574 * This can produce significantly different results when the texture
575 * is scaled by a large amount.
576 *
577 * The GL code below is more logical (imho), and the difference with
578 * HWC is due to a limitation of the HWC API to integers -- a question
579 * is suspend is whether we should ignore this problem or revert to
580 * GL composition when a buffer scaling is applied (maybe with some
581 * minimal value)? Or, we could make GL behave like HWC -- but this feel
582 * like more of a hack.
583 */
Dan Stoza80d61162017-12-20 15:57:52 -0800584 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700585
Peiyong Linefefaac2018-08-17 12:27:51 -0700586 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800587 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700588
Marissa Wall61c58622018-07-18 10:12:20 -0700589 float left = float(win.left) / float(getActiveWidth(s));
590 float top = float(win.top) / float(getActiveHeight(s));
591 float right = float(win.right) / float(getActiveWidth(s));
592 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700593
594 // TODO: we probably want to generate the texture coords with the mesh
595 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700596 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
Chia-I Wu1be50b52018-08-29 10:44:48 -0700597 // flip texcoords vertically because BufferLayerConsumer expects them to be in GL convention
David Sodman0c69cad2017-08-21 12:12:51 -0700598 texCoords[0] = vec2(left, 1.0f - top);
599 texCoords[1] = vec2(left, 1.0f - bottom);
600 texCoords[2] = vec2(right, 1.0f - bottom);
601 texCoords[3] = vec2(right, 1.0f - top);
602
bohu21566132018-03-27 14:36:34 -0700603 auto& engine(mFlinger->getRenderEngine());
604 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
605 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700606 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800607
Chia-I Wu692e0832018-06-05 15:46:58 -0700608 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700609 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800610 }
bohu21566132018-03-27 14:36:34 -0700611
612 engine.drawMesh(getBE().mMesh);
613 engine.disableBlending();
614
615 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700616}
617
David Sodman0c69cad2017-08-21 12:12:51 -0700618uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700619 if (hasDrawingBuffer()) {
620 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700621 } else {
622 return mCurrentFrameNumber;
623 }
624}
625
David Sodman0c69cad2017-08-21 12:12:51 -0700626} // namespace android
627
628#if defined(__gl_h_)
629#error "don't include gl/gl.h in this file"
630#endif
631
632#if defined(__gl2_h_)
633#error "don't include gl2/gl2.h in this file"
634#endif