FramebufferSurface.cpp revision da27af9832a0170f1fc40ef3f21371c4d30d21b3
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/SurfaceTextureClient.h>
33#include <ui/GraphicBuffer.h>
34
35#include "DisplayHardware/FramebufferSurface.h"
36#include "DisplayHardware/HWComposer.h"
37
38// ----------------------------------------------------------------------------
39namespace android {
40// ----------------------------------------------------------------------------
41
42class GraphicBufferAlloc : public BnGraphicBufferAlloc {
43public:
44    GraphicBufferAlloc() { };
45    virtual ~GraphicBufferAlloc() { };
46    virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
47            PixelFormat format, uint32_t usage, status_t* error) {
48        sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
49        return graphicBuffer;
50    }
51};
52
53
54/*
55 * This implements the (main) framebuffer management. This class is used
56 * mostly by SurfaceFlinger, but also by command line GL application.
57 *
58 */
59
60FramebufferSurface::FramebufferSurface(HWComposer& hwc) :
61    ConsumerBase(new BufferQueue(true, new GraphicBufferAlloc())),
62    mCurrentBufferSlot(-1),
63    mCurrentBuffer(0),
64    mHwc(hwc)
65{
66    mName = "FramebufferSurface";
67    mBufferQueue->setConsumerName(mName);
68    mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
69                                       GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER);
70    mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(HWC_DISPLAY_PRIMARY));
71    mBufferQueue->setDefaultBufferSize(mHwc.getWidth(HWC_DISPLAY_PRIMARY),
72                                       mHwc.getHeight(HWC_DISPLAY_PRIMARY));
73    mBufferQueue->setSynchronousMode(true);
74    mBufferQueue->setDefaultMaxBufferCount(NUM_FRAME_BUFFERS);
75}
76
77status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
78    Mutex::Autolock lock(mMutex);
79
80    BufferQueue::BufferItem item;
81    status_t err = acquireBufferLocked(&item);
82    if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
83        outBuffer = mCurrentBuffer;
84        return NO_ERROR;
85    } else if (err != NO_ERROR) {
86        ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
87        return err;
88    }
89
90    // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
91    // then we may have acquired the slot we already own.  If we had released
92    // our current buffer before we call acquireBuffer then that release call
93    // would have returned STALE_BUFFER_SLOT, and we would have called
94    // freeBufferLocked on that slot.  Because the buffer slot has already
95    // been overwritten with the new buffer all we have to do is skip the
96    // releaseBuffer call and we should be in the same state we'd be in if we
97    // had released the old buffer first.
98    if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
99        item.mBuf != mCurrentBufferSlot) {
100        // Release the previous buffer.
101        err = releaseBufferLocked(mCurrentBufferSlot, EGL_NO_DISPLAY,
102                EGL_NO_SYNC_KHR);
103        if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) {
104            ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
105            return err;
106        }
107    }
108    mCurrentBufferSlot = item.mBuf;
109    mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
110    outFence = item.mFence;
111    outBuffer = mCurrentBuffer;
112    return NO_ERROR;
113}
114
115// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
116void FramebufferSurface::onFrameAvailable() {
117    sp<GraphicBuffer> buf;
118    sp<Fence> acquireFence;
119    status_t err = nextBuffer(buf, acquireFence);
120    if (err != NO_ERROR) {
121        ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
122                strerror(-err), err);
123        return;
124    }
125    err = mHwc.fbPost(0, acquireFence, buf); // FIXME: use real display id
126    if (err != NO_ERROR) {
127        ALOGE("error posting framebuffer: %d", err);
128    }
129}
130
131void FramebufferSurface::freeBufferLocked(int slotIndex) {
132    ConsumerBase::freeBufferLocked(slotIndex);
133    if (slotIndex == mCurrentBufferSlot) {
134        mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
135    }
136}
137
138status_t FramebufferSurface::setReleaseFenceFd(int fenceFd) {
139    status_t err = NO_ERROR;
140    if (fenceFd >= 0) {
141        sp<Fence> fence(new Fence(fenceFd));
142        if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
143            status_t err = addReleaseFence(mCurrentBufferSlot, fence);
144            ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
145                    strerror(-err), err);
146        }
147    }
148    return err;
149}
150
151status_t FramebufferSurface::setUpdateRectangle(const Rect& r)
152{
153    return INVALID_OPERATION;
154}
155
156status_t FramebufferSurface::compositionComplete()
157{
158    return mHwc.fbCompositionComplete();
159}
160
161void FramebufferSurface::dump(String8& result) {
162    mHwc.fbDump(result);
163    ConsumerBase::dump(result);
164}
165
166// ----------------------------------------------------------------------------
167}; // namespace android
168// ----------------------------------------------------------------------------
169