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 // ----------------------------------------------------------------------------
48 namespace android {
49 // ----------------------------------------------------------------------------
50
51 using ui::Dataspace;
52
53 /*
54 * This implements the (main) framebuffer management. This class is used
55 * mostly by SurfaceFlinger, but also by command line GL application.
56 *
57 */
58
FramebufferSurface(HWComposer & hwc,DisplayId displayId,const sp<IGraphicBufferConsumer> & consumer,uint32_t maxWidth,uint32_t maxHeight)59 FramebufferSurface::FramebufferSurface(HWComposer& hwc, DisplayId displayId,
60 const sp<IGraphicBufferConsumer>& consumer,
61 uint32_t maxWidth, uint32_t maxHeight)
62 : ConsumerBase(consumer),
63 mDisplayId(displayId),
64 mMaxWidth(maxWidth),
65 mMaxHeight(maxHeight),
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& activeConfig = mHwc.getActiveConfig(displayId);
81 ui::Size limitedSize =
82 limitFramebufferSize(activeConfig->getWidth(), activeConfig->getHeight());
83 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
84 mConsumer->setMaxAcquiredBufferCount(
85 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
86 }
87
resizeBuffers(uint32_t width,uint32_t height)88 void FramebufferSurface::resizeBuffers(uint32_t width, uint32_t height) {
89 ui::Size limitedSize = limitFramebufferSize(width, height);
90 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
91 }
92
beginFrame(bool)93 status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
94 return NO_ERROR;
95 }
96
prepareFrame(CompositionType)97 status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
98 return NO_ERROR;
99 }
100
advanceFrame()101 status_t FramebufferSurface::advanceFrame() {
102 uint32_t slot = 0;
103 sp<GraphicBuffer> buf;
104 sp<Fence> acquireFence(Fence::NO_FENCE);
105 Dataspace dataspace = Dataspace::UNKNOWN;
106 status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
107 mDataSpace = dataspace;
108 if (result != NO_ERROR) {
109 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
110 strerror(-result), result);
111 }
112 return result;
113 }
114
nextBuffer(uint32_t & outSlot,sp<GraphicBuffer> & outBuffer,sp<Fence> & outFence,Dataspace & outDataspace)115 status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
116 sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
117 Dataspace& outDataspace) {
118 Mutex::Autolock lock(mMutex);
119
120 BufferItem item;
121 status_t err = acquireBufferLocked(&item, 0);
122 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
123 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
124 return NO_ERROR;
125 } else if (err != NO_ERROR) {
126 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
127 return err;
128 }
129
130 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
131 // then we may have acquired the slot we already own. If we had released
132 // our current buffer before we call acquireBuffer then that release call
133 // would have returned STALE_BUFFER_SLOT, and we would have called
134 // freeBufferLocked on that slot. Because the buffer slot has already
135 // been overwritten with the new buffer all we have to do is skip the
136 // releaseBuffer call and we should be in the same state we'd be in if we
137 // had released the old buffer first.
138 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
139 item.mSlot != mCurrentBufferSlot) {
140 mHasPendingRelease = true;
141 mPreviousBufferSlot = mCurrentBufferSlot;
142 mPreviousBuffer = mCurrentBuffer;
143 }
144 mCurrentBufferSlot = item.mSlot;
145 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
146 mCurrentFence = item.mFence;
147
148 outFence = item.mFence;
149 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
150 outDataspace = static_cast<Dataspace>(item.mDataSpace);
151 status_t result = mHwc.setClientTarget(mDisplayId, outSlot, outFence, outBuffer, outDataspace);
152 if (result != NO_ERROR) {
153 ALOGE("error posting framebuffer: %d", result);
154 return result;
155 }
156
157 return NO_ERROR;
158 }
159
freeBufferLocked(int slotIndex)160 void FramebufferSurface::freeBufferLocked(int slotIndex) {
161 ConsumerBase::freeBufferLocked(slotIndex);
162 if (slotIndex == mCurrentBufferSlot) {
163 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
164 }
165 }
166
onFrameCommitted()167 void FramebufferSurface::onFrameCommitted() {
168 if (mHasPendingRelease) {
169 sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
170 if (fence->isValid()) {
171 status_t result = addReleaseFence(mPreviousBufferSlot,
172 mPreviousBuffer, fence);
173 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
174 " fence: %s (%d)", strerror(-result), result);
175 }
176 status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
177 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
178 " %s (%d)", strerror(-result), result);
179
180 mPreviousBuffer.clear();
181 mHasPendingRelease = false;
182 }
183 }
184
limitFramebufferSize(uint32_t width,uint32_t height)185 ui::Size FramebufferSurface::limitFramebufferSize(uint32_t width, uint32_t height) {
186 ui::Size framebufferSize(width, height);
187 bool wasLimited = true;
188 if (width > mMaxWidth && mMaxWidth != 0) {
189 float aspectRatio = float(width) / float(height);
190 framebufferSize.height = mMaxWidth / aspectRatio;
191 framebufferSize.width = mMaxWidth;
192 wasLimited = true;
193 }
194 if (height > mMaxHeight && mMaxHeight != 0) {
195 float aspectRatio = float(width) / float(height);
196 framebufferSize.height = mMaxHeight;
197 framebufferSize.width = mMaxHeight * aspectRatio;
198 wasLimited = true;
199 }
200 ALOGI_IF(wasLimited, "framebuffer size has been limited to [%dx%d] from [%dx%d]",
201 framebufferSize.width, framebufferSize.height, width, height);
202 return framebufferSize;
203 }
204
dumpAsString(String8 & result) const205 void FramebufferSurface::dumpAsString(String8& result) const {
206 Mutex::Autolock lock(mMutex);
207 result.appendFormat(" FramebufferSurface: dataspace: %s(%d)\n",
208 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
209 mDataSpace);
210 ConsumerBase::dumpLocked(result, " ");
211 }
212
dumpLocked(String8 & result,const char * prefix) const213 void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
214 {
215 ConsumerBase::dumpLocked(result, prefix);
216 }
217
getClientTargetAcquireFence() const218 const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
219 return mCurrentFence;
220 }
221
222 // ----------------------------------------------------------------------------
223 }; // namespace android
224 // ----------------------------------------------------------------------------
225
226 // TODO(b/129481165): remove the #pragma below and fix conversion issues
227 #pragma clang diagnostic pop // ignored "-Wconversion"
228