blob: 3e2fb2e408606f3cf77359aa22f100b434dd0261 [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
52BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name,
53 uint32_t w, uint32_t h, uint32_t flags)
54 : Layer(flinger, client, name, w, h, flags),
Marissa Wallfd668622018-05-10 10:21:13 -070055 mTextureName(mFlinger->getNewTexture()),
David Sodman0c69cad2017-08-21 12:12:51 -070056 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
57 mBufferLatched(false),
David Sodman0c69cad2017-08-21 12:12:51 -070058 mRefreshPending(false) {
David Sodman0c69cad2017-08-21 12:12:51 -070059 ALOGV("Creating Layer %s", name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070060
Peiyong Lin833074a2018-08-28 11:53:54 -070061 mTexture.init(renderengine::Texture::TEXTURE_EXTERNAL, mTextureName);
David Sodman0c69cad2017-08-21 12:12:51 -070062
Marissa Wallfd668622018-05-10 10:21:13 -070063 mPremultipliedAlpha = !(flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070064
Marissa Wallfd668622018-05-10 10:21:13 -070065 mPotentialCursor = flags & ISurfaceComposerClient::eCursorWindow;
66 mProtectedByApp = flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070067
68 // drawing state & current state are identical
69 mDrawingState = mCurrentState;
70}
71
72BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070073 mFlinger->deleteTextureAsync(mTextureName);
74
David Sodman6f65f3e2017-11-03 14:28:09 -070075 if (!getBE().mHwcLayers.empty()) {
David Sodman0c69cad2017-08-21 12:12:51 -070076 ALOGE("Found stale hardware composer layers when destroying "
77 "surface flinger layer %s",
78 mName.string());
79 destroyAllHwcLayers();
80 }
David Sodman0c69cad2017-08-21 12:12:51 -070081}
82
David Sodmaneb085e02017-10-05 18:49:04 -070083void BufferLayer::useSurfaceDamage() {
84 if (mFlinger->mForceFullDamage) {
85 surfaceDamageRegion = Region::INVALID_REGION;
86 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070087 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070088 }
89}
90
91void BufferLayer::useEmptyDamage() {
92 surfaceDamageRegion.clear();
93}
94
Marissa Wallfd668622018-05-10 10:21:13 -070095bool BufferLayer::isOpaque(const Layer::State& s) const {
96 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
97 // layer's opaque flag.
98 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
99 return false;
100 }
101
102 // if the layer has the opaque flag, then we're always opaque,
103 // otherwise we use the current buffer's format.
104 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700105}
106
107bool BufferLayer::isVisible() const {
108 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800109 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700110}
111
112bool BufferLayer::isFixedSize() const {
113 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
114}
115
David Sodman0c69cad2017-08-21 12:12:51 -0700116static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800117 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
118 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
119 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 -0700120 mat4 tr;
121
122 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
123 tr = tr * rot90;
124 }
125 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
126 tr = tr * flipH;
127 }
128 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
129 tr = tr * flipV;
130 }
131 return inverse(tr);
132}
133
134/*
135 * onDraw will draw the current layer onto the presentable buffer
136 */
137void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
Marissa Wall61c58622018-07-18 10:12:20 -0700138 bool useIdentityTransform) {
David Sodman0c69cad2017-08-21 12:12:51 -0700139 ATRACE_CALL();
140
David Sodman0cf8f8d2017-12-20 18:19:45 -0800141 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700142 // the texture has not been created yet, this Layer has
143 // in fact never been drawn into. This happens frequently with
144 // SurfaceView because the WindowManager can't know when the client
145 // has drawn the first time.
146
147 // If there is nothing under us, we paint the screen in black, otherwise
148 // we just skip this update.
149
150 // figure out if there is something below us
151 Region under;
152 bool finished = false;
153 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
154 if (finished || layer == static_cast<BufferLayer const*>(this)) {
155 finished = true;
156 return;
157 }
158 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
159 });
160 // if not everything below us is covered, we plug the holes!
161 Region holes(clip.subtract(under));
162 if (!holes.isEmpty()) {
163 clearWithOpenGL(renderArea, 0, 0, 0, 1);
164 }
165 return;
166 }
167
168 // Bind the current buffer to the GL texture, and wait for it to be
169 // ready for us to draw into.
Marissa Wallfd668622018-05-10 10:21:13 -0700170 status_t err = bindTextureImage();
David Sodman0c69cad2017-08-21 12:12:51 -0700171 if (err != NO_ERROR) {
172 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
173 // Go ahead and draw the buffer anyway; no matter what we do the screen
174 // is probably going to have something visibly wrong.
175 }
176
177 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
178
Lloyd Pique144e1162017-12-20 16:44:52 -0800179 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700180
181 if (!blackOutLayer) {
182 // TODO: we could be more subtle with isFixedSize()
183 const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize();
184
185 // Query the texture matrix given our current filtering mode.
186 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700187 setFilteringEnabled(useFiltering);
188 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700189
190 if (getTransformToDisplayInverse()) {
191 /*
192 * the code below applies the primary display's inverse transform to
193 * the texture transform
194 */
195 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
196 mat4 tr = inverseOrientation(transform);
197
198 /**
199 * TODO(b/36727915): This is basically a hack.
200 *
201 * Ensure that regardless of the parent transformation,
202 * this buffer is always transformed from native display
203 * orientation to display orientation. For example, in the case
204 * of a camera where the buffer remains in native orientation,
205 * we want the pixels to always be upright.
206 */
207 sp<Layer> p = mDrawingParent.promote();
208 if (p != nullptr) {
209 const auto parentTransform = p->getTransform();
210 tr = tr * inverseOrientation(parentTransform.getOrientation());
211 }
212
213 // and finally apply it to the original texture matrix
214 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
215 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
216 }
217
218 // Set things up for texturing.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800219 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700220 mTexture.setFiltering(useFiltering);
221 mTexture.setMatrix(textureMatrix);
222
223 engine.setupLayerTexturing(mTexture);
224 } else {
225 engine.setupLayerBlackedOut();
226 }
227 drawWithOpenGL(renderArea, useIdentityTransform);
228 engine.disableTexturing();
229}
230
Marissa Wallfd668622018-05-10 10:21:13 -0700231bool BufferLayer::isHdrY410() const {
232 // pixel format is HDR Y410 masquerading as RGBA_1010102
233 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
234 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
235 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700236}
237
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700238void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& display) {
David Sodman0c69cad2017-08-21 12:12:51 -0700239 // Apply this display's projection's viewport to the visible region
240 // before giving it to the HWC HAL.
Peiyong Linefefaac2018-08-17 12:27:51 -0700241 const ui::Transform& tr = display->getTransform();
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700242 const auto& viewport = display->getViewport();
David Sodman0c69cad2017-08-21 12:12:51 -0700243 Region visible = tr.transform(visibleRegion.intersect(viewport));
Dominik Laskowski7e045462018-05-30 13:02:02 -0700244 const auto displayId = display->getId();
David Sodmanba340492018-08-05 21:51:33 -0700245 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodmanba340492018-08-05 21:51:33 -0700246 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700247
248 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800249 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700250 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodmanba340492018-08-05 21:51:33 -0700251 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700252 return;
253 }
254
David Sodman10a41ff2018-08-05 12:14:17 -0700255 if (getBE().compositionInfo.hwc.skipGeometry) {
256 // Device or Cursor layers
257 if (mPotentialCursor) {
258 ALOGV("[%s] Requesting Cursor composition", mName.string());
259 setCompositionType(displayId, HWC2::Composition::Cursor);
260 } else {
261 ALOGV("[%s] Requesting Device composition", mName.string());
262 setCompositionType(displayId, HWC2::Composition::Device);
263 }
David Sodman0c69cad2017-08-21 12:12:51 -0700264 }
265
David Sodmanba340492018-08-05 21:51:33 -0700266 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
267 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
268 getBE().compositionInfo.hwc.supportedPerFrameMetadata = display->getSupportedPerFrameMetadata();
Lloyd Pique074e8122018-07-26 12:57:23 -0700269
Marissa Wallfd668622018-05-10 10:21:13 -0700270 setHwcLayerBuffer(display);
David Sodman0c69cad2017-08-21 12:12:51 -0700271}
272
Marissa Wallfd668622018-05-10 10:21:13 -0700273bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
274 if (mBufferLatched) {
275 Mutex::Autolock lock(mFrameEventHistoryMutex);
276 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700277 }
Marissa Wallfd668622018-05-10 10:21:13 -0700278 mRefreshPending = false;
279 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700280}
281
Marissa Wallfd668622018-05-10 10:21:13 -0700282bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
283 const std::shared_ptr<FenceTime>& presentFence,
284 const CompositorTiming& compositorTiming) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800285
Marissa Wallfd668622018-05-10 10:21:13 -0700286 // mFrameLatencyNeeded is true when a new frame was latched for the
287 // composition.
288 if (!mFrameLatencyNeeded) return false;
289
290 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700291 {
Marissa Wallfd668622018-05-10 10:21:13 -0700292 Mutex::Autolock lock(mFrameEventHistoryMutex);
293 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
294 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700295 }
296
Marissa Wallfd668622018-05-10 10:21:13 -0700297 // Update mFrameTracker.
298 nsecs_t desiredPresentTime = getDesiredPresentTime();
299 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
300
301 const std::string layerName(getName().c_str());
302 mTimeStats.setDesiredTime(layerName, mCurrentFrameNumber, desiredPresentTime);
303
304 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
305 if (frameReadyFence->isValid()) {
306 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
307 } else {
308 // There was no fence for this frame, so assume that it was ready
309 // to be presented at the desired present time.
310 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700311 }
Marissa Wallfd668622018-05-10 10:21:13 -0700312
313 if (presentFence->isValid()) {
314 mTimeStats.setPresentFence(layerName, mCurrentFrameNumber, presentFence);
315 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
316 } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) {
317 // The HWC doesn't support present fences, so use the refresh
318 // timestamp instead.
319 const nsecs_t actualPresentTime =
320 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
321 mTimeStats.setPresentTime(layerName, mCurrentFrameNumber, actualPresentTime);
322 mFrameTracker.setActualPresentTime(actualPresentTime);
323 }
324
325 mFrameTracker.advanceFrame();
326 mFrameLatencyNeeded = false;
327 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700328}
329
Marissa Wallfd668622018-05-10 10:21:13 -0700330Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
331 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700332
Marissa Wallfd668622018-05-10 10:21:13 -0700333 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700334
Marissa Wallfd668622018-05-10 10:21:13 -0700335 if (sidebandStreamDirtyRegion) {
336 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700337 }
338
Marissa Wallfd668622018-05-10 10:21:13 -0700339 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700340
Marissa Wallfd668622018-05-10 10:21:13 -0700341 if (!hasReadyFrame()) {
342 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700343 }
David Sodman0c69cad2017-08-21 12:12:51 -0700344
Marissa Wallfd668622018-05-10 10:21:13 -0700345 // if we've already called updateTexImage() without going through
346 // a composition step, we have to skip this layer at this point
347 // because we cannot call updateTeximage() without a corresponding
348 // compositionComplete() call.
349 // we'll trigger an update in onPreComposition().
350 if (mRefreshPending) {
351 return dirtyRegion;
352 }
353
354 // If the head buffer's acquire fence hasn't signaled yet, return and
355 // try again later
356 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700357 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700358 return dirtyRegion;
359 }
360
361 // Capture the old state of the layer for comparisons later
362 const State& s(getDrawingState());
363 const bool oldOpacity = isOpaque(s);
364 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
365
366 if (!allTransactionsSignaled()) {
367 mFlinger->signalLayerUpdate();
368 return dirtyRegion;
369 }
370
371 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
372 if (err != NO_ERROR) {
373 return dirtyRegion;
374 }
375
376 err = updateActiveBuffer();
377 if (err != NO_ERROR) {
378 return dirtyRegion;
379 }
380
381 mBufferLatched = true;
382
383 err = updateFrameNumber(latchTime);
384 if (err != NO_ERROR) {
385 return dirtyRegion;
386 }
387
388 mRefreshPending = true;
389 mFrameLatencyNeeded = true;
390 if (oldBuffer == nullptr) {
391 // the first time we receive a buffer, we need to trigger a
392 // geometry invalidation.
393 recomputeVisibleRegions = true;
394 }
395
396 ui::Dataspace dataSpace = getDrawingDataSpace();
397 // treat modern dataspaces as legacy dataspaces whenever possible, until
398 // we can trust the buffer producers
399 switch (dataSpace) {
400 case ui::Dataspace::V0_SRGB:
401 dataSpace = ui::Dataspace::SRGB;
402 break;
403 case ui::Dataspace::V0_SRGB_LINEAR:
404 dataSpace = ui::Dataspace::SRGB_LINEAR;
405 break;
406 case ui::Dataspace::V0_JFIF:
407 dataSpace = ui::Dataspace::JFIF;
408 break;
409 case ui::Dataspace::V0_BT601_625:
410 dataSpace = ui::Dataspace::BT601_625;
411 break;
412 case ui::Dataspace::V0_BT601_525:
413 dataSpace = ui::Dataspace::BT601_525;
414 break;
415 case ui::Dataspace::V0_BT709:
416 dataSpace = ui::Dataspace::BT709;
417 break;
418 default:
419 break;
420 }
421 mCurrentDataSpace = dataSpace;
422
423 Rect crop(getDrawingCrop());
424 const uint32_t transform(getDrawingTransform());
425 const uint32_t scalingMode(getDrawingScalingMode());
426 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
427 (scalingMode != mCurrentScalingMode)) {
428 mCurrentCrop = crop;
429 mCurrentTransform = transform;
430 mCurrentScalingMode = scalingMode;
431 recomputeVisibleRegions = true;
432 }
433
434 if (oldBuffer != nullptr) {
435 uint32_t bufWidth = mActiveBuffer->getWidth();
436 uint32_t bufHeight = mActiveBuffer->getHeight();
437 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
438 recomputeVisibleRegions = true;
439 }
440 }
441
442 if (oldOpacity != isOpaque(s)) {
443 recomputeVisibleRegions = true;
444 }
445
446 // Remove any sync points corresponding to the buffer which was just
447 // latched
448 {
449 Mutex::Autolock lock(mLocalSyncPointMutex);
450 auto point = mLocalSyncPoints.begin();
451 while (point != mLocalSyncPoints.end()) {
452 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
453 // This sync point must have been added since we started
454 // latching. Don't drop it yet.
455 ++point;
456 continue;
457 }
458
459 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
460 point = mLocalSyncPoints.erase(point);
461 } else {
462 ++point;
463 }
464 }
465 }
466
467 // FIXME: postedRegion should be dirty & bounds
468 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700469 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700470}
471
472// transaction
473void BufferLayer::notifyAvailableFrames() {
474 auto headFrameNumber = getHeadFrameNumber();
475 bool headFenceSignaled = fenceHasSignaled();
476 Mutex::Autolock lock(mLocalSyncPointMutex);
477 for (auto& point : mLocalSyncPoints) {
478 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
479 point->setFrameAvailable();
480 }
David Sodman0c69cad2017-08-21 12:12:51 -0700481 }
482}
483
Marissa Wallfd668622018-05-10 10:21:13 -0700484bool BufferLayer::hasReadyFrame() const {
485 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
486}
487
488uint32_t BufferLayer::getEffectiveScalingMode() const {
489 if (mOverrideScalingMode >= 0) {
490 return mOverrideScalingMode;
491 }
492
493 return mCurrentScalingMode;
494}
495
496bool BufferLayer::isProtected() const {
497 const sp<GraphicBuffer>& buffer(mActiveBuffer);
498 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
499}
500
501bool BufferLayer::latchUnsignaledBuffers() {
502 static bool propertyLoaded = false;
503 static bool latch = false;
504 static std::mutex mutex;
505 std::lock_guard<std::mutex> lock(mutex);
506 if (!propertyLoaded) {
507 char value[PROPERTY_VALUE_MAX] = {};
508 property_get("debug.sf.latch_unsignaled", value, "0");
509 latch = atoi(value);
510 propertyLoaded = true;
511 }
512 return latch;
513}
514
515// h/w composer set-up
516bool BufferLayer::allTransactionsSignaled() {
517 auto headFrameNumber = getHeadFrameNumber();
518 bool matchingFramesFound = false;
519 bool allTransactionsApplied = true;
520 Mutex::Autolock lock(mLocalSyncPointMutex);
521
522 for (auto& point : mLocalSyncPoints) {
523 if (point->getFrameNumber() > headFrameNumber) {
524 break;
525 }
526 matchingFramesFound = true;
527
528 if (!point->frameIsAvailable()) {
529 // We haven't notified the remote layer that the frame for
530 // this point is available yet. Notify it now, and then
531 // abort this attempt to latch.
532 point->setFrameAvailable();
533 allTransactionsApplied = false;
534 break;
535 }
536
537 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
538 }
539 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700540}
541
542// As documented in libhardware header, formats in the range
543// 0x100 - 0x1FF are specific to the HAL implementation, and
544// are known to have no alpha channel
545// TODO: move definition for device-specific range into
546// hardware.h, instead of using hard-coded values here.
547#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
548
549bool BufferLayer::getOpacityForFormat(uint32_t format) {
550 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
551 return true;
552 }
553 switch (format) {
554 case HAL_PIXEL_FORMAT_RGBA_8888:
555 case HAL_PIXEL_FORMAT_BGRA_8888:
556 case HAL_PIXEL_FORMAT_RGBA_FP16:
557 case HAL_PIXEL_FORMAT_RGBA_1010102:
558 return false;
559 }
560 // in all other case, we have no blending (also for unknown formats)
561 return true;
562}
563
Marissa Wallfd668622018-05-10 10:21:13 -0700564bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
565 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700566}
567
David Sodman41fdfc92017-11-06 16:09:56 -0800568void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700569 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700570 const State& s(getDrawingState());
571
David Sodman9eeae692017-11-02 10:53:32 -0700572 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700573
574 /*
575 * NOTE: the way we compute the texture coordinates here produces
576 * different results than when we take the HWC path -- in the later case
577 * the "source crop" is rounded to texel boundaries.
578 * This can produce significantly different results when the texture
579 * is scaled by a large amount.
580 *
581 * The GL code below is more logical (imho), and the difference with
582 * HWC is due to a limitation of the HWC API to integers -- a question
583 * is suspend is whether we should ignore this problem or revert to
584 * GL composition when a buffer scaling is applied (maybe with some
585 * minimal value)? Or, we could make GL behave like HWC -- but this feel
586 * like more of a hack.
587 */
Dan Stoza80d61162017-12-20 15:57:52 -0800588 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700589
Peiyong Linefefaac2018-08-17 12:27:51 -0700590 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800591 Rect win = bounds;
Marissa Wall61c58622018-07-18 10:12:20 -0700592 Rect finalCrop = getFinalCrop(s);
593 if (!finalCrop.isEmpty()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700594 win = t.transform(win);
Marissa Wall61c58622018-07-18 10:12:20 -0700595 if (!win.intersect(finalCrop, &win)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700596 win.clear();
597 }
598 win = t.inverse().transform(win);
Dan Stoza80d61162017-12-20 15:57:52 -0800599 if (!win.intersect(bounds, &win)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700600 win.clear();
601 }
602 }
603
Marissa Wall61c58622018-07-18 10:12:20 -0700604 float left = float(win.left) / float(getActiveWidth(s));
605 float top = float(win.top) / float(getActiveHeight(s));
606 float right = float(win.right) / float(getActiveWidth(s));
607 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700608
609 // TODO: we probably want to generate the texture coords with the mesh
610 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700611 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
David Sodman0c69cad2017-08-21 12:12:51 -0700612 texCoords[0] = vec2(left, 1.0f - top);
613 texCoords[1] = vec2(left, 1.0f - bottom);
614 texCoords[2] = vec2(right, 1.0f - bottom);
615 texCoords[3] = vec2(right, 1.0f - top);
616
bohu21566132018-03-27 14:36:34 -0700617 auto& engine(mFlinger->getRenderEngine());
618 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
619 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700620 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800621
Chia-I Wu692e0832018-06-05 15:46:58 -0700622 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700623 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800624 }
bohu21566132018-03-27 14:36:34 -0700625
626 engine.drawMesh(getBE().mMesh);
627 engine.disableBlending();
628
629 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700630}
631
David Sodman0c69cad2017-08-21 12:12:51 -0700632uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700633 if (hasDrawingBuffer()) {
634 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700635 } else {
636 return mCurrentFrameNumber;
637 }
638}
639
David Sodman0c69cad2017-08-21 12:12:51 -0700640} // namespace android
641
642#if defined(__gl_h_)
643#error "don't include gl/gl.h in this file"
644#endif
645
646#if defined(__gl2_h_)
647#error "don't include gl2/gl2.h in this file"
648#endif