FramebufferSurface.cpp revision 595264f1af12e25dce57d7c5b1d52ed86ac0d0c9
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#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22
23#include <cutils/log.h>
24
25#include <utils/String8.h>
26
27#include <ui/Rect.h>
28
29#include <EGL/egl.h>
30
31#include <hardware/hardware.h>
32#include <gui/Surface.h>
33#include <gui/GraphicBufferAlloc.h>
34#include <ui/GraphicBuffer.h>
35
36#include "FramebufferSurface.h"
37#include "HWComposer.h"
38
39#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
40#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
41#endif
42
43// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
47/*
48 * This implements the (main) framebuffer management. This class is used
49 * mostly by SurfaceFlinger, but also by command line GL application.
50 *
51 */
52
53FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp) :
54    ConsumerBase(new BufferQueue(new GraphicBufferAlloc())),
55    mDisplayType(disp),
56    mCurrentBufferSlot(-1),
57    mCurrentBuffer(0),
58    mHwc(hwc)
59{
60    mName = "FramebufferSurface";
61    mBufferQueue->setConsumerName(mName);
62    mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
63                                       GRALLOC_USAGE_HW_RENDER |
64                                       GRALLOC_USAGE_HW_COMPOSER);
65    mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(disp));
66    mBufferQueue->setDefaultBufferSize(mHwc.getWidth(disp),  mHwc.getHeight(disp));
67    mBufferQueue->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
68}
69
70sp<IGraphicBufferProducer> FramebufferSurface::getIGraphicBufferProducer() const {
71    return getBufferQueue();
72}
73
74status_t FramebufferSurface::prepareFrame(CompositionType compositionType) {
75    return NO_ERROR;
76}
77
78status_t FramebufferSurface::advanceFrame() {
79    // Once we remove FB HAL support, we can call nextBuffer() from here
80    // instead of using onFrameAvailable(). No real benefit, except it'll be
81    // more like VirtualDisplaySurface.
82    return NO_ERROR;
83}
84
85status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
86    Mutex::Autolock lock(mMutex);
87
88    BufferQueue::BufferItem item;
89    status_t err = acquireBufferLocked(&item, 0);
90    if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
91        outBuffer = mCurrentBuffer;
92        return NO_ERROR;
93    } else if (err != NO_ERROR) {
94        ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
95        return err;
96    }
97
98    // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
99    // then we may have acquired the slot we already own.  If we had released
100    // our current buffer before we call acquireBuffer then that release call
101    // would have returned STALE_BUFFER_SLOT, and we would have called
102    // freeBufferLocked on that slot.  Because the buffer slot has already
103    // been overwritten with the new buffer all we have to do is skip the
104    // releaseBuffer call and we should be in the same state we'd be in if we
105    // had released the old buffer first.
106    if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
107        item.mBuf != mCurrentBufferSlot) {
108        // Release the previous buffer.
109        err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
110                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
111        if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) {
112            ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
113            return err;
114        }
115    }
116    mCurrentBufferSlot = item.mBuf;
117    mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
118    outFence = item.mFence;
119    outBuffer = mCurrentBuffer;
120    return NO_ERROR;
121}
122
123// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
124void FramebufferSurface::onFrameAvailable() {
125    sp<GraphicBuffer> buf;
126    sp<Fence> acquireFence;
127    status_t err = nextBuffer(buf, acquireFence);
128    if (err != NO_ERROR) {
129        ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
130                strerror(-err), err);
131        return;
132    }
133    err = mHwc.fbPost(mDisplayType, acquireFence, buf);
134    if (err != NO_ERROR) {
135        ALOGE("error posting framebuffer: %d", err);
136    }
137}
138
139void FramebufferSurface::freeBufferLocked(int slotIndex) {
140    ConsumerBase::freeBufferLocked(slotIndex);
141    if (slotIndex == mCurrentBufferSlot) {
142        mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
143    }
144}
145
146void FramebufferSurface::onFrameCommitted() {
147    sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
148    if (fence->isValid() &&
149            mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
150        status_t err = addReleaseFence(mCurrentBufferSlot,
151                mCurrentBuffer, fence);
152        ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
153                strerror(-err), err);
154    }
155}
156
157status_t FramebufferSurface::compositionComplete()
158{
159    return mHwc.fbCompositionComplete();
160}
161
162// Since DisplaySurface and ConsumerBase both have a method with this
163// signature, results will vary based on the static pointer type the caller is
164// using:
165//   void dump(FrameBufferSurface* fbs, String8& s) {
166//       // calls FramebufferSurface::dump()
167//       fbs->dump(s);
168//
169//       // calls ConsumerBase::dump() since it is non-virtual
170//       static_cast<ConsumerBase*>(fbs)->dump(s);
171//
172//       // calls FramebufferSurface::dump() since it is virtual
173//       static_cast<DisplaySurface*>(fbs)->dump(s);
174//   }
175// To make sure that all of these end up doing the same thing, we just redirect
176// to ConsumerBase::dump() here. It will take the internal lock, and then call
177// virtual dumpLocked(), which is where the real work happens.
178void FramebufferSurface::dump(String8& result) const {
179    ConsumerBase::dump(result);
180}
181
182void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
183{
184    mHwc.fbDump(result);
185    ConsumerBase::dumpLocked(result, prefix);
186}
187
188// ----------------------------------------------------------------------------
189}; // namespace android
190// ----------------------------------------------------------------------------
191