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