blob: 8c6ecc03a188789dc93899d31430e33ac8b86daa [file] [log] [blame]
Mathias Agopian3e876012012-06-07 17:52:54 -07001/*
2 **
Jamie Gennis1a4d8832012-08-02 20:11:05 -07003 ** Copyright 2012 The Android Open Source Project
Mathias Agopian3e876012012-06-07 17:52:54 -07004 **
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"
Mathias Agopianf33e4b62012-09-20 16:54:14 -070036#include "DisplayHardware/GraphicBufferAlloc.h"
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070037#include "DisplayHardware/HWComposer.h"
Mathias Agopian3e876012012-06-07 17:52:54 -070038
39// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
Mathias Agopian3e876012012-06-07 17:52:54 -070043/*
44 * This implements the (main) framebuffer management. This class is used
45 * mostly by SurfaceFlinger, but also by command line GL application.
46 *
47 */
48
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070049FramebufferSurface::FramebufferSurface(HWComposer& hwc) :
Jamie Gennis72f096f2012-08-27 18:48:37 -070050 ConsumerBase(new BufferQueue(true, new GraphicBufferAlloc())),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070051 mCurrentBufferSlot(-1),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070052 mCurrentBuffer(0),
53 mHwc(hwc)
Mathias Agopian3e876012012-06-07 17:52:54 -070054{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070055 mName = "FramebufferSurface";
56 mBufferQueue->setConsumerName(mName);
57 mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
58 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER);
59 mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(HWC_DISPLAY_PRIMARY));
Jesse Halldb276212012-09-07 11:20:56 -070060 mBufferQueue->setDefaultBufferSize(mHwc.getWidth(HWC_DISPLAY_PRIMARY),
61 mHwc.getHeight(HWC_DISPLAY_PRIMARY));
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070062 mBufferQueue->setSynchronousMode(true);
63 mBufferQueue->setDefaultMaxBufferCount(NUM_FRAME_BUFFERS);
Mathias Agopian3e876012012-06-07 17:52:54 -070064}
65
Mathias Agopianda27af92012-09-13 18:17:13 -070066status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070067 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -070068
Jamie Gennis1a4d8832012-08-02 20:11:05 -070069 BufferQueue::BufferItem item;
70 status_t err = acquireBufferLocked(&item);
71 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -070072 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073 return NO_ERROR;
74 } else if (err != NO_ERROR) {
75 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
76 return err;
77 }
78
79 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
80 // then we may have acquired the slot we already own. If we had released
81 // our current buffer before we call acquireBuffer then that release call
82 // would have returned STALE_BUFFER_SLOT, and we would have called
83 // freeBufferLocked on that slot. Because the buffer slot has already
84 // been overwritten with the new buffer all we have to do is skip the
85 // releaseBuffer call and we should be in the same state we'd be in if we
86 // had released the old buffer first.
87 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
88 item.mBuf != mCurrentBufferSlot) {
89 // Release the previous buffer.
90 err = releaseBufferLocked(mCurrentBufferSlot, EGL_NO_DISPLAY,
Jamie Gennisb2725412012-09-05 20:09:05 -070091 EGL_NO_SYNC_KHR);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070092 if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) {
93 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
94 return err;
95 }
96 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -070097 mCurrentBufferSlot = item.mBuf;
98 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Mathias Agopianda27af92012-09-13 18:17:13 -070099 outFence = item.mFence;
100 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700101 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700102}
103
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700104// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700105void FramebufferSurface::onFrameAvailable() {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700106 sp<GraphicBuffer> buf;
Mathias Agopianda27af92012-09-13 18:17:13 -0700107 sp<Fence> acquireFence;
108 status_t err = nextBuffer(buf, acquireFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700109 if (err != NO_ERROR) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700110 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111 strerror(-err), err);
112 return;
113 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700114 err = mHwc.fbPost(0, acquireFence, buf); // FIXME: use real display id
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700115 if (err != NO_ERROR) {
116 ALOGE("error posting framebuffer: %d", err);
117 }
118}
119
120void FramebufferSurface::freeBufferLocked(int slotIndex) {
121 ConsumerBase::freeBufferLocked(slotIndex);
122 if (slotIndex == mCurrentBufferSlot) {
123 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
124 }
125}
126
Mathias Agopianda27af92012-09-13 18:17:13 -0700127status_t FramebufferSurface::setReleaseFenceFd(int fenceFd) {
128 status_t err = NO_ERROR;
129 if (fenceFd >= 0) {
130 sp<Fence> fence(new Fence(fenceFd));
131 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
132 status_t err = addReleaseFence(mCurrentBufferSlot, fence);
133 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
134 strerror(-err), err);
135 }
136 }
137 return err;
138}
139
Mathias Agopian3e876012012-06-07 17:52:54 -0700140status_t FramebufferSurface::setUpdateRectangle(const Rect& r)
141{
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700142 return INVALID_OPERATION;
Mathias Agopian3e876012012-06-07 17:52:54 -0700143}
144
145status_t FramebufferSurface::compositionComplete()
146{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700147 return mHwc.fbCompositionComplete();
Mathias Agopian3e876012012-06-07 17:52:54 -0700148}
149
150void FramebufferSurface::dump(String8& result) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700151 mHwc.fbDump(result);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700152 ConsumerBase::dump(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700153}
154
155// ----------------------------------------------------------------------------
156}; // namespace android
157// ----------------------------------------------------------------------------