blob: 4885c5fc460b2fac3bb7338e260f7ae365b8d94f [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 Agopiandb89edc2013-08-02 01:40:18 -070053FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
54 const sp<IGraphicBufferConsumer>& consumer) :
55 ConsumerBase(consumer),
Mathias Agopianf5a33922012-09-19 18:16:22 -070056 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070057 mCurrentBufferSlot(-1),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070058 mCurrentBuffer(0),
59 mHwc(hwc)
Mathias Agopian3e876012012-06-07 17:52:54 -070060{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070061 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070062 mConsumer->setConsumerName(mName);
63 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070064 GRALLOC_USAGE_HW_RENDER |
65 GRALLOC_USAGE_HW_COMPOSER);
Mathias Agopiandb89edc2013-08-02 01:40:18 -070066 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
67 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
68 mConsumer->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069}
70
Jesse Hall7cd85972014-08-07 22:48:06 -070071status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070072 return NO_ERROR;
73}
74
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070075status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070076 return NO_ERROR;
77}
78
Jesse Hall99c7dbb2013-03-14 14:29:29 -070079status_t FramebufferSurface::advanceFrame() {
80 // Once we remove FB HAL support, we can call nextBuffer() from here
81 // instead of using onFrameAvailable(). No real benefit, except it'll be
82 // more like VirtualDisplaySurface.
83 return NO_ERROR;
84}
85
Mathias Agopianda27af92012-09-13 18:17:13 -070086status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070087 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -070088
Jamie Gennis1a4d8832012-08-02 20:11:05 -070089 BufferQueue::BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -070090 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070091 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -070092 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070093 return NO_ERROR;
94 } else if (err != NO_ERROR) {
95 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
96 return err;
97 }
98
99 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
100 // then we may have acquired the slot we already own. If we had released
101 // our current buffer before we call acquireBuffer then that release call
102 // would have returned STALE_BUFFER_SLOT, and we would have called
103 // freeBufferLocked on that slot. Because the buffer slot has already
104 // been overwritten with the new buffer all we have to do is skip the
105 // releaseBuffer call and we should be in the same state we'd be in if we
106 // had released the old buffer first.
107 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
108 item.mBuf != mCurrentBufferSlot) {
109 // Release the previous buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700110 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
111 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700112 if (err < NO_ERROR) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700113 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
114 return err;
115 }
116 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700117 mCurrentBufferSlot = item.mBuf;
118 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Mathias Agopianda27af92012-09-13 18:17:13 -0700119 outFence = item.mFence;
120 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700121 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700122}
123
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700124// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
Dan Stoza8dc55392014-11-04 11:37:46 -0800125void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700126 sp<GraphicBuffer> buf;
Mathias Agopianda27af92012-09-13 18:17:13 -0700127 sp<Fence> acquireFence;
128 status_t err = nextBuffer(buf, acquireFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700129 if (err != NO_ERROR) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700130 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700131 strerror(-err), err);
132 return;
133 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700134 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700135 if (err != NO_ERROR) {
136 ALOGE("error posting framebuffer: %d", err);
137 }
138}
139
140void FramebufferSurface::freeBufferLocked(int slotIndex) {
141 ConsumerBase::freeBufferLocked(slotIndex);
142 if (slotIndex == mCurrentBufferSlot) {
143 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
144 }
145}
146
Jesse Hall851cfe82013-03-20 13:44:00 -0700147void FramebufferSurface::onFrameCommitted() {
148 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700149 if (fence->isValid() &&
150 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700151 status_t err = addReleaseFence(mCurrentBufferSlot,
152 mCurrentBuffer, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700153 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
154 strerror(-err), err);
Mathias Agopianda27af92012-09-13 18:17:13 -0700155 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700156}
157
Mathias Agopian3e876012012-06-07 17:52:54 -0700158status_t FramebufferSurface::compositionComplete()
159{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700160 return mHwc.fbCompositionComplete();
Mathias Agopian3e876012012-06-07 17:52:54 -0700161}
162
Dan Stoza01049c82014-11-11 10:32:31 -0800163void FramebufferSurface::dumpAsString(String8& result) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700164 ConsumerBase::dump(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700165}
166
Mathias Agopian74d211a2013-04-22 16:55:35 +0200167void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800168{
169 mHwc.fbDump(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200170 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800171}
172
Mathias Agopian3e876012012-06-07 17:52:54 -0700173// ----------------------------------------------------------------------------
174}; // namespace android
175// ----------------------------------------------------------------------------