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