xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  **
3  ** Copyright 2012 The Android Open Source Project
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 
18 // TODO(b/129481165): remove the #pragma below and fix conversion issues
19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wconversion"
21 
22 // #define LOG_NDEBUG 0
23 #undef LOG_TAG
24 #define LOG_TAG "FramebufferSurface"
25 
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <utils/String8.h>
32 #include <log/log.h>
33 
34 #include <com_android_graphics_libgui_flags.h>
35 #include <gui/BufferItem.h>
36 #include <gui/BufferQueue.h>
37 #include <gui/Surface.h>
38 #include <hardware/hardware.h>
39 
40 #include <ui/DebugUtils.h>
41 #include <ui/GraphicBuffer.h>
42 #include <ui/Rect.h>
43 
44 #include "FramebufferSurface.h"
45 #include "HWComposer.h"
46 #include "../SurfaceFlinger.h"
47 
48 namespace android {
49 
50 using ui::Dataspace;
51 
52 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
FramebufferSurface(HWComposer & hwc,PhysicalDisplayId displayId,const sp<IGraphicBufferProducer> & producer,const sp<IGraphicBufferConsumer> & consumer,const ui::Size & size,const ui::Size & maxSize)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
59 FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
60                                        const sp<IGraphicBufferConsumer>& consumer,
61                                        const ui::Size& size, const ui::Size& maxSize)
62       : ConsumerBase(consumer),
63 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
64         mDisplayId(displayId),
65         mMaxSize(maxSize),
66         mCurrentBufferSlot(-1),
67         mCurrentBuffer(),
68         mCurrentFence(Fence::NO_FENCE),
69         mHwc(hwc),
70         mHasPendingRelease(false),
71         mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
72         mPreviousBuffer() {
73     ALOGV("Creating for display %s", to_string(displayId).c_str());
74 
75     mName = "FramebufferSurface";
76     mConsumer->setConsumerName(mName);
77     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
78                                        GRALLOC_USAGE_HW_RENDER |
79                                        GRALLOC_USAGE_HW_COMPOSER);
80     const auto limitedSize = limitSize(size);
81     mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
82     mConsumer->setMaxAcquiredBufferCount(
83             SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
84 
85     for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
86         mHwcBufferIds[i] = UINT64_MAX;
87     }
88 }
89 
resizeBuffers(const ui::Size & newSize)90 void FramebufferSurface::resizeBuffers(const ui::Size& newSize) {
91     const auto limitedSize = limitSize(newSize);
92     mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
93 }
94 
beginFrame(bool)95 status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
96     return NO_ERROR;
97 }
98 
prepareFrame(CompositionType)99 status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
100     return NO_ERROR;
101 }
102 
advanceFrame(float hdrSdrRatio)103 status_t FramebufferSurface::advanceFrame(float hdrSdrRatio) {
104     Mutex::Autolock lock(mMutex);
105 
106     BufferItem item;
107     status_t err = acquireBufferLocked(&item, 0);
108     if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
109         mDataspace = Dataspace::UNKNOWN;
110         return NO_ERROR;
111     } else if (err != NO_ERROR) {
112         ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
113         mDataspace = Dataspace::UNKNOWN;
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 &&
126         item.mSlot != mCurrentBufferSlot) {
127         mHasPendingRelease = true;
128         mPreviousBufferSlot = mCurrentBufferSlot;
129         mPreviousBuffer = mCurrentBuffer;
130     }
131     mCurrentBufferSlot = item.mSlot;
132     mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
133     mCurrentFence = item.mFence;
134     mDataspace = static_cast<Dataspace>(item.mDataSpace);
135 
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,
143                                            mDataspace, hdrSdrRatio);
144     if (result != NO_ERROR) {
145         ALOGE("error posting framebuffer: %s (%d)", strerror(-result), result);
146         return result;
147     }
148 
149     return NO_ERROR;
150 }
151 
freeBufferLocked(int slotIndex)152 void FramebufferSurface::freeBufferLocked(int slotIndex) {
153     ConsumerBase::freeBufferLocked(slotIndex);
154     if (slotIndex == mCurrentBufferSlot) {
155         mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
156     }
157 }
158 
onFrameCommitted()159 void FramebufferSurface::onFrameCommitted() {
160     if (mHasPendingRelease) {
161         sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
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         }
168         status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
169         ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
170                 " %s (%d)", strerror(-result), result);
171 
172         mPreviousBuffer.clear();
173         mHasPendingRelease = false;
174     }
175 }
176 
limitSize(const ui::Size & size)177 ui::Size FramebufferSurface::limitSize(const ui::Size& size) {
178     return limitSizeInternal(size, mMaxSize);
179 }
180 
limitSizeInternal(const ui::Size & size,const ui::Size & maxSize)181 ui::Size FramebufferSurface::limitSizeInternal(const ui::Size& size, const ui::Size& maxSize) {
182     ui::Size limitedSize = size;
183     bool wasLimited = false;
184     if (size.width > maxSize.width && maxSize.width != 0) {
185         const float aspectRatio = static_cast<float>(size.width) / size.height;
186         limitedSize.height = maxSize.width / aspectRatio;
187         limitedSize.width = maxSize.width;
188         wasLimited = true;
189     }
190     if (limitedSize.height > maxSize.height && maxSize.height != 0) {
191         const float aspectRatio = static_cast<float>(size.width) / size.height;
192         limitedSize.height = maxSize.height;
193         limitedSize.width = maxSize.height * aspectRatio;
194         wasLimited = true;
195     }
196     ALOGI_IF(wasLimited, "Framebuffer size has been limited to [%dx%d] from [%dx%d]",
197              limitedSize.width, limitedSize.height, size.width, size.height);
198     return limitedSize;
199 }
200 
dumpAsString(String8 & result) const201 void FramebufferSurface::dumpAsString(String8& result) const {
202     Mutex::Autolock lock(mMutex);
203     result.append("   FramebufferSurface\n");
204     result.appendFormat("      mDataspace=%s (%d)\n",
205                         dataspaceDetails(static_cast<android_dataspace>(mDataspace)).c_str(),
206                         mDataspace);
207     ConsumerBase::dumpLocked(result, "      ");
208 }
209 
dumpLocked(String8 & result,const char * prefix) const210 void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const {
211     ConsumerBase::dumpLocked(result, prefix);
212 }
213 
getClientTargetAcquireFence() const214 const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
215     return mCurrentFence;
216 }
217 
218 } // namespace android
219 
220 // TODO(b/129481165): remove the #pragma below and fix conversion issues
221 #pragma clang diagnostic pop // ignored "-Wconversion"
222