blob: bd2f5f36e7ecb2b6111c02a652c7fc0e2e944ea6 [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>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080032#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080033#include <gui/GraphicBufferAlloc.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070034#include <ui/GraphicBuffer.h>
35
Mathias Agopian33ceeb32013-04-01 16:54:58 -070036#include "FramebufferSurface.h"
37#include "HWComposer.h"
Mathias Agopian3e876012012-06-07 17:52:54 -070038
Jamie Genniscdbaecb2012-10-12 14:18:10 -070039#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
40#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
41#endif
42
Mathias Agopian3e876012012-06-07 17:52:54 -070043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
Mathias Agopian3e876012012-06-07 17:52:54 -070047/*
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
Mathias Agopianf5a33922012-09-19 18:16:22 -070053FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp) :
Mathias Agopian595264f2013-07-16 22:56:09 -070054 ConsumerBase(new BufferQueue(new GraphicBufferAlloc())),
Mathias Agopianf5a33922012-09-19 18:16:22 -070055 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070056 mCurrentBufferSlot(-1),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070057 mCurrentBuffer(0),
58 mHwc(hwc)
Mathias Agopian3e876012012-06-07 17:52:54 -070059{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070060 mName = "FramebufferSurface";
61 mBufferQueue->setConsumerName(mName);
62 mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070063 GRALLOC_USAGE_HW_RENDER |
64 GRALLOC_USAGE_HW_COMPOSER);
65 mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(disp));
66 mBufferQueue->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
Jamie Genniscdbaecb2012-10-12 14:18:10 -070067 mBufferQueue->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
Mathias Agopian3e876012012-06-07 17:52:54 -070068}
69
Jesse Hall99c7dbb2013-03-14 14:29:29 -070070sp<IGraphicBufferProducer> FramebufferSurface::getIGraphicBufferProducer() const {
71 return getBufferQueue();
72}
73
Jesse Hall38efe862013-04-06 23:12:29 -070074status_t FramebufferSurface::prepareFrame(CompositionType compositionType) {
75 return NO_ERROR;
76}
77
Jesse Hall99c7dbb2013-03-14 14:29:29 -070078status_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
Mathias Agopianda27af92012-09-13 18:17:13 -070085status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070086 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -070087
Jamie Gennis1a4d8832012-08-02 20:11:05 -070088 BufferQueue::BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -070089 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070090 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -070091 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070092 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.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700109 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
110 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111 if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) {
112 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
113 return err;
114 }
115 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700116 mCurrentBufferSlot = item.mBuf;
117 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Mathias Agopianda27af92012-09-13 18:17:13 -0700118 outFence = item.mFence;
119 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700120 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700121}
122
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700123// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124void FramebufferSurface::onFrameAvailable() {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700125 sp<GraphicBuffer> buf;
Mathias Agopianda27af92012-09-13 18:17:13 -0700126 sp<Fence> acquireFence;
127 status_t err = nextBuffer(buf, acquireFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700128 if (err != NO_ERROR) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700129 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700130 strerror(-err), err);
131 return;
132 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700133 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134 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
Jesse Hall851cfe82013-03-20 13:44:00 -0700146void FramebufferSurface::onFrameCommitted() {
147 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700148 if (fence->isValid() &&
149 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700150 status_t err = addReleaseFence(mCurrentBufferSlot,
151 mCurrentBuffer, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700152 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
153 strerror(-err), err);
Mathias Agopianda27af92012-09-13 18:17:13 -0700154 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700155}
156
Mathias Agopian3e876012012-06-07 17:52:54 -0700157status_t FramebufferSurface::compositionComplete()
158{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700159 return mHwc.fbCompositionComplete();
Mathias Agopian3e876012012-06-07 17:52:54 -0700160}
161
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700162// 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 {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700179 ConsumerBase::dump(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700180}
181
Mathias Agopian74d211a2013-04-22 16:55:35 +0200182void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800183{
184 mHwc.fbDump(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200185 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800186}
187
Mathias Agopian3e876012012-06-07 17:52:54 -0700188// ----------------------------------------------------------------------------
189}; // namespace android
190// ----------------------------------------------------------------------------