blob: 748765a4e451003aac394c920843f9f55dbc695e [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
Jim Shargo6ccc5e82024-07-27 03:42:08 +000034#include <com_android_graphics_libgui_flags.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>
Jim Shargo6ccc5e82024-07-27 03:42:08 +000038#include <hardware/hardware.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080039
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060040#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070041#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070043
Mathias Agopian33ceeb32013-04-01 16:54:58 -070044#include "FramebufferSurface.h"
45#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080046#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070047
Mathias Agopian3e876012012-06-07 17:52:54 -070048namespace android {
Mathias Agopian3e876012012-06-07 17:52:54 -070049
Peiyong Lin34beb7a2018-03-28 11:57:12 -070050using ui::Dataspace;
51
Jim Shargo6ccc5e82024-07-27 03:42:08 +000052#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
53FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
54 const sp<IGraphicBufferProducer>& producer,
55 const sp<IGraphicBufferConsumer>& consumer,
56 const ui::Size& size, const ui::Size& maxSize)
57 : ConsumerBase(producer, consumer),
58#else
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020059FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
Brian Lindahla13f2d52020-03-05 11:54:17 +010060 const sp<IGraphicBufferConsumer>& consumer,
Lloyd Pique30db6402023-06-26 18:56:51 +000061 const ui::Size& size, const ui::Size& maxSize)
Dominik Laskowski075d3172018-05-24 15:50:06 -070062 : ConsumerBase(consumer),
Jim Shargo6ccc5e82024-07-27 03:42:08 +000063#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Dominik Laskowski075d3172018-05-24 15:50:06 -070064 mDisplayId(displayId),
Marin Shalamanov045b7002021-01-07 16:56:24 +010065 mMaxSize(maxSize),
Dominik Laskowski075d3172018-05-24 15:50:06 -070066 mCurrentBufferSlot(-1),
67 mCurrentBuffer(),
68 mCurrentFence(Fence::NO_FENCE),
69 mHwc(hwc),
70 mHasPendingRelease(false),
71 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
72 mPreviousBuffer() {
Dominik Laskowski34157762018-10-31 13:07:19 -070073 ALOGV("Creating for display %s", to_string(displayId).c_str());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000074
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070075 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070076 mConsumer->setConsumerName(mName);
77 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070078 GRALLOC_USAGE_HW_RENDER |
79 GRALLOC_USAGE_HW_COMPOSER);
Marin Shalamanov4a92f552021-03-29 18:38:27 +020080 const auto limitedSize = limitSize(size);
Brian Lindahla13f2d52020-03-05 11:54:17 +010081 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Lloyd Pique30db6402023-06-26 18:56:51 +000082 mConsumer->setMaxAcquiredBufferCount(
83 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Brian Lindahl439afad2022-11-14 11:16:55 -070084
85 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
86 mHwcBufferIds[i] = UINT64_MAX;
87 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -070088}
89
Marin Shalamanov045b7002021-01-07 16:56:24 +010090void FramebufferSurface::resizeBuffers(const ui::Size& newSize) {
Marin Shalamanov4a92f552021-03-29 18:38:27 +020091 const auto limitedSize = limitSize(newSize);
Brian Lindahla13f2d52020-03-05 11:54:17 +010092 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Lloyd Pique2ae2b3b2017-12-14 17:18:17 -080093}
94
Jesse Hall7cd85972014-08-07 22:48:06 -070095status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070096 return NO_ERROR;
97}
98
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070099status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -0700100 return NO_ERROR;
101}
102
Alec Mourif97df4d2023-09-06 02:10:05 +0000103status_t FramebufferSurface::advanceFrame(float hdrSdrRatio) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700104 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700105
Dan Stoza84493cd2015-03-12 15:12:44 -0700106 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700107 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700108 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700109 mDataspace = Dataspace::UNKNOWN;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700110 return NO_ERROR;
111 } else if (err != NO_ERROR) {
112 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
Brian Lindahl439afad2022-11-14 11:16:55 -0700113 mDataspace = Dataspace::UNKNOWN;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114 return err;
115 }
116
117 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
118 // then we may have acquired the slot we already own. If we had released
119 // our current buffer before we call acquireBuffer then that release call
120 // would have returned STALE_BUFFER_SLOT, and we would have called
121 // freeBufferLocked on that slot. Because the buffer slot has already
122 // been overwritten with the new buffer all we have to do is skip the
123 // releaseBuffer call and we should be in the same state we'd be in if we
124 // had released the old buffer first.
125 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700126 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127 mHasPendingRelease = true;
128 mPreviousBufferSlot = mCurrentBufferSlot;
129 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700130 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700131 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 mCurrentFence = item.mFence;
Brian Lindahl439afad2022-11-14 11:16:55 -0700134 mDataspace = static_cast<Dataspace>(item.mDataSpace);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800135
Brian Lindahl439afad2022-11-14 11:16:55 -0700136 // assume HWC has previously seen the buffer in this slot
137 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
138 if (mCurrentBuffer->getId() != mHwcBufferIds[mCurrentBufferSlot]) {
139 mHwcBufferIds[mCurrentBufferSlot] = mCurrentBuffer->getId();
140 hwcBuffer = mCurrentBuffer; // HWC hasn't previously seen this buffer in this slot
141 }
142 status_t result = mHwc.setClientTarget(mDisplayId, mCurrentBufferSlot, mCurrentFence, hwcBuffer,
Alec Mourif97df4d2023-09-06 02:10:05 +0000143 mDataspace, hdrSdrRatio);
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600144 if (result != NO_ERROR) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700145 ALOGE("error posting framebuffer: %s (%d)", strerror(-result), result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600146 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600147 }
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600148
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600149 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700150}
151
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700152void FramebufferSurface::freeBufferLocked(int slotIndex) {
153 ConsumerBase::freeBufferLocked(slotIndex);
154 if (slotIndex == mCurrentBufferSlot) {
155 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
156 }
157}
158
Jesse Hall851cfe82013-03-20 13:44:00 -0700159void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800160 if (mHasPendingRelease) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700161 sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800162 if (fence->isValid()) {
163 status_t result = addReleaseFence(mPreviousBufferSlot,
164 mPreviousBuffer, fence);
165 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
166 " fence: %s (%d)", strerror(-result), result);
167 }
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800168 status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800169 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
170 " %s (%d)", strerror(-result), result);
171
172 mPreviousBuffer.clear();
173 mHasPendingRelease = false;
174 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700175}
176
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200177ui::Size FramebufferSurface::limitSize(const ui::Size& size) {
178 return limitSizeInternal(size, mMaxSize);
179}
180
181ui::Size FramebufferSurface::limitSizeInternal(const ui::Size& size, const ui::Size& maxSize) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100182 ui::Size limitedSize = size;
183 bool wasLimited = false;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200184 if (size.width > maxSize.width && maxSize.width != 0) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100185 const float aspectRatio = static_cast<float>(size.width) / size.height;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200186 limitedSize.height = maxSize.width / aspectRatio;
187 limitedSize.width = maxSize.width;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100188 wasLimited = true;
189 }
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200190 if (limitedSize.height > maxSize.height && maxSize.height != 0) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100191 const float aspectRatio = static_cast<float>(size.width) / size.height;
Marin Shalamanov4a92f552021-03-29 18:38:27 +0200192 limitedSize.height = maxSize.height;
193 limitedSize.width = maxSize.height * aspectRatio;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100194 wasLimited = true;
195 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700196 ALOGI_IF(wasLimited, "Framebuffer size has been limited to [%dx%d] from [%dx%d]",
Marin Shalamanov045b7002021-01-07 16:56:24 +0100197 limitedSize.width, limitedSize.height, size.width, size.height);
198 return limitedSize;
Brian Lindahla13f2d52020-03-05 11:54:17 +0100199}
200
Dan Stozaf10c46e2014-11-11 10:32:31 -0800201void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600202 Mutex::Autolock lock(mMutex);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700203 result.append(" FramebufferSurface\n");
Brian Lindahl439afad2022-11-14 11:16:55 -0700204 result.appendFormat(" mDataspace=%s (%d)\n",
205 dataspaceDetails(static_cast<android_dataspace>(mDataspace)).c_str(),
206 mDataspace);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700207 ConsumerBase::dumpLocked(result, " ");
Mathias Agopian3e876012012-06-07 17:52:54 -0700208}
209
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700210void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200211 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800212}
213
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
215 return mCurrentFence;
216}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700218} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800219
220// TODO(b/129481165): remove the #pragma below and fix conversion issues
221#pragma clang diagnostic pop // ignored "-Wconversion"