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 <hardware/hardware.h>
35 #include <gui/BufferItem.h>
36 #include <gui/BufferQueue.h>
37 #include <gui/Surface.h>
38 
39 #include <ui/DebugUtils.h>
40 #include <ui/GraphicBuffer.h>
41 #include <ui/Rect.h>
42 
43 #include "FramebufferSurface.h"
44 #include "HWComposer.h"
45 #include "../SurfaceFlinger.h"
46 
47 namespace android {
48 
49 using ui::Dataspace;
50 
FramebufferSurface(HWComposer & hwc,PhysicalDisplayId displayId,const sp<IGraphicBufferConsumer> & consumer,const ui::Size & size,const ui::Size & maxSize)51 FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
52                                        const sp<IGraphicBufferConsumer>& consumer,
53                                        const ui::Size& size, const ui::Size& maxSize)
54       : ConsumerBase(consumer),
55         mDisplayId(displayId),
56         mMaxSize(maxSize),
57         mCurrentBufferSlot(-1),
58         mCurrentBuffer(),
59         mCurrentFence(Fence::NO_FENCE),
60         mHwc(hwc),
61         mHasPendingRelease(false),
62         mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
63         mPreviousBuffer() {
64     ALOGV("Creating for display %s", to_string(displayId).c_str());
65 
66     mName = "FramebufferSurface";
67     mConsumer->setConsumerName(mName);
68     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
69                                        GRALLOC_USAGE_HW_RENDER |
70                                        GRALLOC_USAGE_HW_COMPOSER);
71     const auto limitedSize = limitSize(size);
72     mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
73     mConsumer->setMaxAcquiredBufferCount(
74             SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
75 
76     for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
77         mHwcBufferIds[i] = UINT64_MAX;
78     }
79 }
80 
resizeBuffers(const ui::Size & newSize)81 void FramebufferSurface::resizeBuffers(const ui::Size& newSize) {
82     const auto limitedSize = limitSize(newSize);
83     mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
84 }
85 
beginFrame(bool)86 status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
87     return NO_ERROR;
88 }
89 
prepareFrame(CompositionType)90 status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
91     return NO_ERROR;
92 }
93 
advanceFrame(float hdrSdrRatio)94 status_t FramebufferSurface::advanceFrame(float hdrSdrRatio) {
95     Mutex::Autolock lock(mMutex);
96 
97     BufferItem item;
98     status_t err = acquireBufferLocked(&item, 0);
99     if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
100         mDataspace = Dataspace::UNKNOWN;
101         return NO_ERROR;
102     } else if (err != NO_ERROR) {
103         ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
104         mDataspace = Dataspace::UNKNOWN;
105         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 &&
117         item.mSlot != mCurrentBufferSlot) {
118         mHasPendingRelease = true;
119         mPreviousBufferSlot = mCurrentBufferSlot;
120         mPreviousBuffer = mCurrentBuffer;
121     }
122     mCurrentBufferSlot = item.mSlot;
123     mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
124     mCurrentFence = item.mFence;
125     mDataspace = static_cast<Dataspace>(item.mDataSpace);
126 
127     // 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,
134                                            mDataspace, hdrSdrRatio);
135     if (result != NO_ERROR) {
136         ALOGE("error posting framebuffer: %s (%d)", strerror(-result), result);
137         return result;
138     }
139 
140     return NO_ERROR;
141 }
142 
freeBufferLocked(int slotIndex)143 void FramebufferSurface::freeBufferLocked(int slotIndex) {
144     ConsumerBase::freeBufferLocked(slotIndex);
145     if (slotIndex == mCurrentBufferSlot) {
146         mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
147     }
148 }
149 
onFrameCommitted()150 void FramebufferSurface::onFrameCommitted() {
151     if (mHasPendingRelease) {
152         sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
153         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         }
159         status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
160         ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
161                 " %s (%d)", strerror(-result), result);
162 
163         mPreviousBuffer.clear();
164         mHasPendingRelease = false;
165     }
166 }
167 
limitSize(const ui::Size & size)168 ui::Size FramebufferSurface::limitSize(const ui::Size& size) {
169     return limitSizeInternal(size, mMaxSize);
170 }
171 
limitSizeInternal(const ui::Size & size,const ui::Size & maxSize)172 ui::Size FramebufferSurface::limitSizeInternal(const ui::Size& size, const ui::Size& maxSize) {
173     ui::Size limitedSize = size;
174     bool wasLimited = false;
175     if (size.width > maxSize.width && maxSize.width != 0) {
176         const float aspectRatio = static_cast<float>(size.width) / size.height;
177         limitedSize.height = maxSize.width / aspectRatio;
178         limitedSize.width = maxSize.width;
179         wasLimited = true;
180     }
181     if (limitedSize.height > maxSize.height && maxSize.height != 0) {
182         const float aspectRatio = static_cast<float>(size.width) / size.height;
183         limitedSize.height = maxSize.height;
184         limitedSize.width = maxSize.height * aspectRatio;
185         wasLimited = true;
186     }
187     ALOGI_IF(wasLimited, "Framebuffer size has been limited to [%dx%d] from [%dx%d]",
188              limitedSize.width, limitedSize.height, size.width, size.height);
189     return limitedSize;
190 }
191 
dumpAsString(String8 & result) const192 void FramebufferSurface::dumpAsString(String8& result) const {
193     Mutex::Autolock lock(mMutex);
194     result.append("   FramebufferSurface\n");
195     result.appendFormat("      mDataspace=%s (%d)\n",
196                         dataspaceDetails(static_cast<android_dataspace>(mDataspace)).c_str(),
197                         mDataspace);
198     ConsumerBase::dumpLocked(result, "      ");
199 }
200 
dumpLocked(String8 & result,const char * prefix) const201 void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const {
202     ConsumerBase::dumpLocked(result, prefix);
203 }
204 
getClientTargetAcquireFence() const205 const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
206     return mCurrentFence;
207 }
208 
209 } // namespace android
210 
211 // TODO(b/129481165): remove the #pragma below and fix conversion issues
212 #pragma clang diagnostic pop // ignored "-Wconversion"
213