blob: c77cdd4432edc111ded4f601c21564d4902e03fc [file] [log] [blame]
Mathias Agopian3e876012012-06-07 17:52:54 -07001/*
2 **
Jamie Gennis1a4d8832012-08-02 20:11:05 -07003 ** Copyright 2012 The Android Open Source Project
Mathias Agopian3e876012012-06-07 17:52:54 -07004 **
5 ** Licensed under the Apache License Version 2.0(the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing software
12 ** distributed under the License is distributed on an "AS IS" BASIS
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080018// TODO(b/129481165): remove the #pragma below and fix conversion issues
19#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Wconversion"
21
Dan Stoza9e56aa02015-11-02 13:00:03 -080022// #define LOG_NDEBUG 0
23#undef LOG_TAG
24#define LOG_TAG "FramebufferSurface"
25
Mathias Agopian3e876012012-06-07 17:52:54 -070026#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070030
Mathias Agopian3e876012012-06-07 17:52:54 -070031#include <utils/String8.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070033
Mathias Agopian3e876012012-06-07 17:52:54 -070034#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070035#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <gui/BufferQueue.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070037#include <gui/Surface.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080038
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060039#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070040#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080041#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070042
Mathias Agopian33ceeb32013-04-01 16:54:58 -070043#include "FramebufferSurface.h"
44#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080045#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070046
Mathias Agopian3e876012012-06-07 17:52:54 -070047namespace android {
Mathias Agopian3e876012012-06-07 17:52:54 -070048
Peiyong Lin34beb7a2018-03-28 11:57:12 -070049using ui::Dataspace;
50
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020051FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
Brian Lindahla13f2d52020-03-05 11:54:17 +010052 const sp<IGraphicBufferConsumer>& consumer,
Lloyd Pique30db6402023-06-26 18:56:51 +000053 const ui::Size& size, const ui::Size& maxSize)
Dominik Laskowski075d3172018-05-24 15:50:06 -070054 : ConsumerBase(consumer),
55 mDisplayId(displayId),
Marin Shalamanov045b7002021-01-07 16:56:24 +010056 mMaxSize(maxSize),
Dominik Laskowski075d3172018-05-24 15:50:06 -070057 mCurrentBufferSlot(-1),
58 mCurrentBuffer(),
59 mCurrentFence(Fence::NO_FENCE),
60 mHwc(hwc),
61 mHasPendingRelease(false),
62 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
63 mPreviousBuffer() {
Dominik Laskowski34157762018-10-31 13:07:19 -070064 ALOGV("Creating for display %s", to_string(displayId).c_str());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000065
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070066 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070067 mConsumer->setConsumerName(mName);
68 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070069 GRALLOC_USAGE_HW_RENDER |
70 GRALLOC_USAGE_HW_COMPOSER);
Marin Shalamanov4a92f552021-03-29 18:38:27 +020071 const auto limitedSize = limitSize(size);
Brian Lindahla13f2d52020-03-05 11:54:17 +010072 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Lloyd Pique30db6402023-06-26 18:56:51 +000073 mConsumer->setMaxAcquiredBufferCount(
74 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Brian Lindahl439afad2022-11-14 11:16:55 -070075
76 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
77 mHwcBufferIds[i] = UINT64_MAX;
78 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -070079}
80
Marin Shalamanov045b7002021-01-07 16:56:24 +010081void FramebufferSurface::resizeBuffers(const ui::Size& newSize) {
Marin Shalamanov4a92f552021-03-29 18:38:27 +020082 const auto limitedSize = limitSize(newSize);
Brian Lindahla13f2d52020-03-05 11:54:17 +010083 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Lloyd Pique2ae2b3b2017-12-14 17:18:17 -080084}
85
Jesse Hall7cd85972014-08-07 22:48:06 -070086status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070087 return NO_ERROR;
88}
89
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070090status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070091 return NO_ERROR;
92}
93
Alec Mourif97df4d2023-09-06 02:10:05 +000094status_t FramebufferSurface::advanceFrame(float hdrSdrRatio) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070095 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -070096
Dan Stoza84493cd2015-03-12 15:12:44 -070097 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -070098 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070099 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700100 mDataspace = Dataspace::UNKNOWN;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700101 return NO_ERROR;
102 } else if (err != NO_ERROR) {
103 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
Brian Lindahl439afad2022-11-14 11:16:55 -0700104 mDataspace = Dataspace::UNKNOWN;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700105 return err;
106 }
107
108 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
109 // then we may have acquired the slot we already own. If we had released
110 // our current buffer before we call acquireBuffer then that release call
111 // would have returned STALE_BUFFER_SLOT, and we would have called
112 // freeBufferLocked on that slot. Because the buffer slot has already
113 // been overwritten with the new buffer all we have to do is skip the
114 // releaseBuffer call and we should be in the same state we'd be in if we
115 // had released the old buffer first.
116 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700117 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800118 mHasPendingRelease = true;
119 mPreviousBufferSlot = mCurrentBufferSlot;
120 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700121 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700122 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700123 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800124 mCurrentFence = item.mFence;
Brian Lindahl439afad2022-11-14 11:16:55 -0700125 mDataspace = static_cast<Dataspace>(item.mDataSpace);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800126
Brian Lindahl439afad2022-11-14 11:16:55 -0700127 // assume HWC has previously seen the buffer in this slot
128 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
129 if (mCurrentBuffer->getId() != mHwcBufferIds[mCurrentBufferSlot]) {
130 mHwcBufferIds[mCurrentBufferSlot] = mCurrentBuffer->getId();
131 hwcBuffer = mCurrentBuffer; // HWC hasn't previously seen this buffer in this slot
132 }
133 status_t result = mHwc.setClientTarget(mDisplayId, mCurrentBufferSlot, mCurrentFence, hwcBuffer,
Alec Mourif97df4d2023-09-06 02:10:05 +0000134 mDataspace, hdrSdrRatio);
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600135 if (result != NO_ERROR) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700136 ALOGE("error posting framebuffer: %s (%d)", strerror(-result), result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600137 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600138 }
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600139
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600140 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700141}
142
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700143void FramebufferSurface::freeBufferLocked(int slotIndex) {
144 ConsumerBase::freeBufferLocked(slotIndex);
145 if (slotIndex == mCurrentBufferSlot) {
146 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
147 }
148}
149
Jesse Hall851cfe82013-03-20 13:44:00 -0700150void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151 if (mHasPendingRelease) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700152 sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800153 if (fence->isValid()) {
154 status_t result = addReleaseFence(mPreviousBufferSlot,
155 mPreviousBuffer, fence);
156 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
157 " fence: %s (%d)", strerror(-result), result);
158 }
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800159 status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800160 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
161 " %s (%d)", strerror(-result), result);
162
163 mPreviousBuffer.clear();
164 mHasPendingRelease = false;
165 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700166}
167
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200168ui::Size FramebufferSurface::limitSize(const ui::Size& size) {
169 return limitSizeInternal(size, mMaxSize);
170}
171
172ui::Size FramebufferSurface::limitSizeInternal(const ui::Size& size, const ui::Size& maxSize) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100173 ui::Size limitedSize = size;
174 bool wasLimited = false;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200175 if (size.width > maxSize.width && maxSize.width != 0) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100176 const float aspectRatio = static_cast<float>(size.width) / size.height;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200177 limitedSize.height = maxSize.width / aspectRatio;
178 limitedSize.width = maxSize.width;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100179 wasLimited = true;
180 }
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200181 if (limitedSize.height > maxSize.height && maxSize.height != 0) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100182 const float aspectRatio = static_cast<float>(size.width) / size.height;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200183 limitedSize.height = maxSize.height;
184 limitedSize.width = maxSize.height * aspectRatio;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100185 wasLimited = true;
186 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700187 ALOGI_IF(wasLimited, "Framebuffer size has been limited to [%dx%d] from [%dx%d]",
Marin Shalamanov045b7002021-01-07 16:56:24 +0100188 limitedSize.width, limitedSize.height, size.width, size.height);
189 return limitedSize;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100190}
191
Dan Stozaf10c46e2014-11-11 10:32:31 -0800192void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600193 Mutex::Autolock lock(mMutex);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700194 result.append(" FramebufferSurface\n");
Brian Lindahl439afad2022-11-14 11:16:55 -0700195 result.appendFormat(" mDataspace=%s (%d)\n",
196 dataspaceDetails(static_cast<android_dataspace>(mDataspace)).c_str(),
197 mDataspace);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700198 ConsumerBase::dumpLocked(result, " ");
Mathias Agopian3e876012012-06-07 17:52:54 -0700199}
200
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700201void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200202 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800203}
204
Dan Stoza9e56aa02015-11-02 13:00:03 -0800205const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
206 return mCurrentFence;
207}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700209} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800210
211// TODO(b/129481165): remove the #pragma below and fix conversion issues
212#pragma clang diagnostic pop // ignored "-Wconversion"