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 // #define LOG_NDEBUG 0
19 #undef LOG_TAG
20 #define LOG_TAG "FramebufferSurface"
21 
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <utils/String8.h>
28 #include <log/log.h>
29 
30 #include <hardware/hardware.h>
31 #include <gui/BufferItem.h>
32 #include <gui/BufferQueue.h>
33 #include <gui/Surface.h>
34 
35 #include <ui/DebugUtils.h>
36 #include <ui/GraphicBuffer.h>
37 #include <ui/Rect.h>
38 
39 #include "FramebufferSurface.h"
40 #include "HWComposer.h"
41 #include "../SurfaceFlinger.h"
42 
43 // ----------------------------------------------------------------------------
44 namespace android {
45 // ----------------------------------------------------------------------------
46 
47 using ui::Dataspace;
48 
49 /*
50  * This implements the (main) framebuffer management. This class is used
51  * mostly by SurfaceFlinger, but also by command line GL application.
52  *
53  */
54 
FramebufferSurface(HWComposer & hwc,int disp,const sp<IGraphicBufferConsumer> & consumer)55 FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
56         const sp<IGraphicBufferConsumer>& consumer) :
57     ConsumerBase(consumer),
58     mDisplayType(disp),
59     mCurrentBufferSlot(-1),
60     mCurrentBuffer(),
61     mCurrentFence(Fence::NO_FENCE),
62     mHwc(hwc),
63     mHasPendingRelease(false),
64     mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
65     mPreviousBuffer()
66 {
67     ALOGV("Creating for display %d", disp);
68 
69     mName = "FramebufferSurface";
70     mConsumer->setConsumerName(mName);
71     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
72                                        GRALLOC_USAGE_HW_RENDER |
73                                        GRALLOC_USAGE_HW_COMPOSER);
74     const auto& activeConfig = mHwc.getActiveConfig(disp);
75     mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
76             activeConfig->getHeight());
77     mConsumer->setMaxAcquiredBufferCount(
78             SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
79 }
80 
resizeBuffers(const uint32_t width,const uint32_t height)81 void FramebufferSurface::resizeBuffers(const uint32_t width, const uint32_t height) {
82     mConsumer->setDefaultBufferSize(width, height);
83 }
84 
beginFrame(bool)85 status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
86     return NO_ERROR;
87 }
88 
prepareFrame(CompositionType)89 status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
90     return NO_ERROR;
91 }
92 
advanceFrame()93 status_t FramebufferSurface::advanceFrame() {
94     uint32_t slot = 0;
95     sp<GraphicBuffer> buf;
96     sp<Fence> acquireFence(Fence::NO_FENCE);
97     Dataspace dataspace = Dataspace::UNKNOWN;
98     status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
99     mDataSpace = dataspace;
100     if (result != NO_ERROR) {
101         ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
102                 strerror(-result), result);
103     }
104     return result;
105 }
106 
nextBuffer(uint32_t & outSlot,sp<GraphicBuffer> & outBuffer,sp<Fence> & outFence,Dataspace & outDataspace)107 status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
108         sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
109         Dataspace& outDataspace) {
110     Mutex::Autolock lock(mMutex);
111 
112     BufferItem item;
113     status_t err = acquireBufferLocked(&item, 0);
114     if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
115         mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
116                 &outSlot, &outBuffer);
117         return NO_ERROR;
118     } else if (err != NO_ERROR) {
119         ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
120         return err;
121     }
122 
123     // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
124     // then we may have acquired the slot we already own.  If we had released
125     // our current buffer before we call acquireBuffer then that release call
126     // would have returned STALE_BUFFER_SLOT, and we would have called
127     // freeBufferLocked on that slot.  Because the buffer slot has already
128     // been overwritten with the new buffer all we have to do is skip the
129     // releaseBuffer call and we should be in the same state we'd be in if we
130     // had released the old buffer first.
131     if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
132         item.mSlot != mCurrentBufferSlot) {
133         mHasPendingRelease = true;
134         mPreviousBufferSlot = mCurrentBufferSlot;
135         mPreviousBuffer = mCurrentBuffer;
136     }
137     mCurrentBufferSlot = item.mSlot;
138     mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
139     mCurrentFence = item.mFence;
140 
141     outFence = item.mFence;
142     mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
143             &outSlot, &outBuffer);
144     outDataspace = static_cast<Dataspace>(item.mDataSpace);
145     status_t result =
146             mHwc.setClientTarget(mDisplayType, outSlot, outFence, outBuffer, outDataspace);
147     if (result != NO_ERROR) {
148         ALOGE("error posting framebuffer: %d", result);
149         return result;
150     }
151 
152     return NO_ERROR;
153 }
154 
freeBufferLocked(int slotIndex)155 void FramebufferSurface::freeBufferLocked(int slotIndex) {
156     ConsumerBase::freeBufferLocked(slotIndex);
157     if (slotIndex == mCurrentBufferSlot) {
158         mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
159     }
160 }
161 
onFrameCommitted()162 void FramebufferSurface::onFrameCommitted() {
163     if (mHasPendingRelease) {
164         sp<Fence> fence = mHwc.getPresentFence(mDisplayType);
165         if (fence->isValid()) {
166             status_t result = addReleaseFence(mPreviousBufferSlot,
167                     mPreviousBuffer, fence);
168             ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
169                     " fence: %s (%d)", strerror(-result), result);
170         }
171         status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
172         ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
173                 " %s (%d)", strerror(-result), result);
174 
175         mPreviousBuffer.clear();
176         mHasPendingRelease = false;
177     }
178 }
179 
dumpAsString(String8 & result) const180 void FramebufferSurface::dumpAsString(String8& result) const {
181     Mutex::Autolock lock(mMutex);
182     result.appendFormat("  FramebufferSurface: dataspace: %s(%d)\n",
183                         dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
184                         mDataSpace);
185     ConsumerBase::dumpLocked(result, "   ");
186 }
187 
dumpLocked(String8 & result,const char * prefix) const188 void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
189 {
190     ConsumerBase::dumpLocked(result, prefix);
191 }
192 
getClientTargetAcquireFence() const193 const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
194     return mCurrentFence;
195 }
196 
197 // ----------------------------------------------------------------------------
198 }; // namespace android
199 // ----------------------------------------------------------------------------
200