Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 3 | ** Copyright 2012 The Android Open Source Project |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 4 | ** |
| 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 18 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 19 | #pragma clang diagnostic push |
| 20 | #pragma clang diagnostic ignored "-Wconversion" |
| 21 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 22 | // #define LOG_NDEBUG 0 |
| 23 | #undef LOG_TAG |
| 24 | #define LOG_TAG "FramebufferSurface" |
| 25 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 30 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 31 | #include <utils/String8.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 33 | |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 34 | #include <com_android_graphics_libgui_flags.h> |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 35 | #include <gui/BufferItem.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 36 | #include <gui/BufferQueue.h> |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 37 | #include <gui/Surface.h> |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 38 | #include <hardware/hardware.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 39 | |
Courtney Goeltzenleuchter | 19987a8 | 2017-07-14 12:16:56 -0600 | [diff] [blame] | 40 | #include <ui/DebugUtils.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 41 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 42 | #include <ui/Rect.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 43 | |
Mathias Agopian | 33ceeb3 | 2013-04-01 16:54:58 -0700 | [diff] [blame] | 44 | #include "FramebufferSurface.h" |
| 45 | #include "HWComposer.h" |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 46 | #include "../SurfaceFlinger.h" |
Jamie Gennis | cdbaecb | 2012-10-12 14:18:10 -0700 | [diff] [blame] | 47 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 48 | namespace android { |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 49 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 50 | using ui::Dataspace; |
| 51 | |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 52 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 53 | FramebufferSurface::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 Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 59 | FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId, |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 60 | const sp<IGraphicBufferConsumer>& consumer, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 61 | const ui::Size& size, const ui::Size& maxSize) |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 62 | : ConsumerBase(consumer), |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 63 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 64 | mDisplayId(displayId), |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 65 | mMaxSize(maxSize), |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 66 | mCurrentBufferSlot(-1), |
| 67 | mCurrentBuffer(), |
| 68 | mCurrentFence(Fence::NO_FENCE), |
| 69 | mHwc(hwc), |
| 70 | mHasPendingRelease(false), |
| 71 | mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT), |
| 72 | mPreviousBuffer() { |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 73 | ALOGV("Creating for display %s", to_string(displayId).c_str()); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 74 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 75 | mName = "FramebufferSurface"; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 76 | mConsumer->setConsumerName(mName); |
| 77 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 78 | GRALLOC_USAGE_HW_RENDER | |
| 79 | GRALLOC_USAGE_HW_COMPOSER); |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 80 | const auto limitedSize = limitSize(size); |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 81 | mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 82 | mConsumer->setMaxAcquiredBufferCount( |
| 83 | SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1); |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 84 | |
| 85 | for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) { |
| 86 | mHwcBufferIds[i] = UINT64_MAX; |
| 87 | } |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 90 | void FramebufferSurface::resizeBuffers(const ui::Size& newSize) { |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 91 | const auto limitedSize = limitSize(newSize); |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 92 | mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height); |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jesse Hall | 7cd8597 | 2014-08-07 22:48:06 -0700 | [diff] [blame] | 95 | status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) { |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 99 | status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 100 | return NO_ERROR; |
| 101 | } |
| 102 | |
Alec Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 103 | status_t FramebufferSurface::advanceFrame(float hdrSdrRatio) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 104 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 105 | |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 106 | BufferItem item; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 107 | status_t err = acquireBufferLocked(&item, 0); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 108 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 109 | mDataspace = Dataspace::UNKNOWN; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 110 | return NO_ERROR; |
| 111 | } else if (err != NO_ERROR) { |
| 112 | ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 113 | mDataspace = Dataspace::UNKNOWN; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 114 | 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 Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 126 | item.mSlot != mCurrentBufferSlot) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 127 | mHasPendingRelease = true; |
| 128 | mPreviousBufferSlot = mCurrentBufferSlot; |
| 129 | mPreviousBuffer = mCurrentBuffer; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 130 | } |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 131 | mCurrentBufferSlot = item.mSlot; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 132 | mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 133 | mCurrentFence = item.mFence; |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 134 | mDataspace = static_cast<Dataspace>(item.mDataSpace); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 135 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 136 | // 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 Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 143 | mDataspace, hdrSdrRatio); |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 144 | if (result != NO_ERROR) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 145 | ALOGE("error posting framebuffer: %s (%d)", strerror(-result), result); |
Courtney Goeltzenleuchter | cd939aa | 2017-07-25 10:34:27 -0600 | [diff] [blame] | 146 | return result; |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 147 | } |
Courtney Goeltzenleuchter | 281e811 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 148 | |
Courtney Goeltzenleuchter | 6461806 | 2017-08-02 15:55:06 -0600 | [diff] [blame] | 149 | return NO_ERROR; |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 152 | void FramebufferSurface::freeBufferLocked(int slotIndex) { |
| 153 | ConsumerBase::freeBufferLocked(slotIndex); |
| 154 | if (slotIndex == mCurrentBufferSlot) { |
| 155 | mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; |
| 156 | } |
| 157 | } |
| 158 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 159 | void FramebufferSurface::onFrameCommitted() { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 160 | if (mHasPendingRelease) { |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 161 | sp<Fence> fence = mHwc.getPresentFence(mDisplayId); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 162 | 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 Wu | 1e24cce | 2017-11-10 10:36:01 -0800 | [diff] [blame] | 168 | status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 169 | ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:" |
| 170 | " %s (%d)", strerror(-result), result); |
| 171 | |
| 172 | mPreviousBuffer.clear(); |
| 173 | mHasPendingRelease = false; |
| 174 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 177 | ui::Size FramebufferSurface::limitSize(const ui::Size& size) { |
| 178 | return limitSizeInternal(size, mMaxSize); |
| 179 | } |
| 180 | |
| 181 | ui::Size FramebufferSurface::limitSizeInternal(const ui::Size& size, const ui::Size& maxSize) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 182 | ui::Size limitedSize = size; |
| 183 | bool wasLimited = false; |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 184 | if (size.width > maxSize.width && maxSize.width != 0) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 185 | const float aspectRatio = static_cast<float>(size.width) / size.height; |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 186 | limitedSize.height = maxSize.width / aspectRatio; |
| 187 | limitedSize.width = maxSize.width; |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 188 | wasLimited = true; |
| 189 | } |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 190 | if (limitedSize.height > maxSize.height && maxSize.height != 0) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 191 | const float aspectRatio = static_cast<float>(size.width) / size.height; |
Marin Shalamanov | 4a92f55 | 2021-03-29 18:38:27 +0200 | [diff] [blame] | 192 | limitedSize.height = maxSize.height; |
| 193 | limitedSize.width = maxSize.height * aspectRatio; |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 194 | wasLimited = true; |
| 195 | } |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 196 | ALOGI_IF(wasLimited, "Framebuffer size has been limited to [%dx%d] from [%dx%d]", |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 197 | limitedSize.width, limitedSize.height, size.width, size.height); |
| 198 | return limitedSize; |
Brian Lindahl | a13f2d5 | 2020-03-05 11:54:17 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 201 | void FramebufferSurface::dumpAsString(String8& result) const { |
Courtney Goeltzenleuchter | 19987a8 | 2017-07-14 12:16:56 -0600 | [diff] [blame] | 202 | Mutex::Autolock lock(mMutex); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 203 | result.append(" FramebufferSurface\n"); |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 204 | result.appendFormat(" mDataspace=%s (%d)\n", |
| 205 | dataspaceDetails(static_cast<android_dataspace>(mDataspace)).c_str(), |
| 206 | mDataspace); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 207 | ConsumerBase::dumpLocked(result, " "); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 210 | void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 211 | ConsumerBase::dumpLocked(result, prefix); |
Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 214 | const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const { |
| 215 | return mCurrentFence; |
| 216 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 217 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 218 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 219 | |
| 220 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 221 | #pragma clang diagnostic pop // ignored "-Wconversion" |