blob: 18c7945577a11356e7ea286e6edbcbc0b8b8eceb [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080018// #define LOG_NDEBUG 0
19#undef LOG_TAG
20#define LOG_TAG "FramebufferSurface"
21
Mathias Agopian3e876012012-06-07 17:52:54 -070022#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26
27#include <cutils/log.h>
28
29#include <utils/String8.h>
30
31#include <ui/Rect.h>
32
33#include <EGL/egl.h>
34
35#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070036#include <gui/BufferItem.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080037#include <gui/GraphicBufferAlloc.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070038#include <gui/Surface.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070039#include <ui/GraphicBuffer.h>
40
Mathias Agopian33ceeb32013-04-01 16:54:58 -070041#include "FramebufferSurface.h"
42#include "HWComposer.h"
Mathias Agopian3e876012012-06-07 17:52:54 -070043
Jamie Genniscdbaecb2012-10-12 14:18:10 -070044#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
45#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
46#endif
47
Mathias Agopian3e876012012-06-07 17:52:54 -070048// ----------------------------------------------------------------------------
49namespace android {
50// ----------------------------------------------------------------------------
51
Mathias Agopian3e876012012-06-07 17:52:54 -070052/*
53 * This implements the (main) framebuffer management. This class is used
54 * mostly by SurfaceFlinger, but also by command line GL application.
55 *
56 */
57
Mathias Agopiandb89edc2013-08-02 01:40:18 -070058FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
59 const sp<IGraphicBufferConsumer>& consumer) :
60 ConsumerBase(consumer),
Mathias Agopianf5a33922012-09-19 18:16:22 -070061 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070062 mCurrentBufferSlot(-1),
Dan Stoza9e56aa02015-11-02 13:00:03 -080063 mCurrentBuffer(),
64 mCurrentFence(Fence::NO_FENCE),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000065#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080066 mHwc(hwc),
67 mHasPendingRelease(false),
68 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
69 mPreviousBuffer()
Fabien Sanglard9d96de42016-10-11 00:15:18 +000070#else
71 mHwc(hwc)
72#endif
Mathias Agopian3e876012012-06-07 17:52:54 -070073{
Fabien Sanglard9d96de42016-10-11 00:15:18 +000074#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080075 ALOGV("Creating for display %d", disp);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000076#endif
77
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070078 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070079 mConsumer->setConsumerName(mName);
80 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070081 GRALLOC_USAGE_HW_RENDER |
82 GRALLOC_USAGE_HW_COMPOSER);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000083#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080084 const auto& activeConfig = mHwc.getActiveConfig(disp);
85 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
86 activeConfig->getHeight());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000087#else
88 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
89 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
90#endif
Pablo Ceballos19e3e062015-08-19 16:16:06 -070091 mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070092}
93
Jesse Hall7cd85972014-08-07 22:48:06 -070094status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070095 return NO_ERROR;
96}
97
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070098status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070099 return NO_ERROR;
100}
101
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700102status_t FramebufferSurface::advanceFrame() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000103#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800104 sp<GraphicBuffer> buf;
105 sp<Fence> acquireFence(Fence::NO_FENCE);
106 android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
107 status_t result = nextBuffer(buf, acquireFence, dataspace);
108 if (result != NO_ERROR) {
109 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
110 strerror(-result), result);
111 return result;
112 }
113 result = mHwc.setClientTarget(mDisplayType, acquireFence, buf, dataspace);
114 if (result != NO_ERROR) {
115 ALOGE("error posting framebuffer: %d", result);
116 }
117 return result;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000118#else
119 // Once we remove FB HAL support, we can call nextBuffer() from here
120 // instead of using onFrameAvailable(). No real benefit, except it'll be
121 // more like VirtualDisplaySurface.
122 return NO_ERROR;
123#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700124}
125
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000126#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
128 sp<Fence>& outFence, android_dataspace_t& outDataspace) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000129#else
130status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
131#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700133
Dan Stoza84493cd2015-03-12 15:12:44 -0700134 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700135 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700137 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700138 return NO_ERROR;
139 } else if (err != NO_ERROR) {
140 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
141 return err;
142 }
143
144 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
145 // then we may have acquired the slot we already own. If we had released
146 // our current buffer before we call acquireBuffer then that release call
147 // would have returned STALE_BUFFER_SLOT, and we would have called
148 // freeBufferLocked on that slot. Because the buffer slot has already
149 // been overwritten with the new buffer all we have to do is skip the
150 // releaseBuffer call and we should be in the same state we'd be in if we
151 // had released the old buffer first.
152 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700153 item.mSlot != mCurrentBufferSlot) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000154#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800155 mHasPendingRelease = true;
156 mPreviousBufferSlot = mCurrentBufferSlot;
157 mPreviousBuffer = mCurrentBuffer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000158#else
159 // Release the previous buffer.
160 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
161 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
162 if (err < NO_ERROR) {
163 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
164 return err;
165 }
166#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700167 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700168 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700169 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800171
Mathias Agopianda27af92012-09-13 18:17:13 -0700172 outFence = item.mFence;
173 outBuffer = mCurrentBuffer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000174#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800175 outDataspace = item.mDataSpace;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000176#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700177 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700178}
179
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000180#ifndef USE_HWC2
181// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
182void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
183 sp<GraphicBuffer> buf;
184 sp<Fence> acquireFence;
185 status_t err = nextBuffer(buf, acquireFence);
186 if (err != NO_ERROR) {
187 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
188 strerror(-err), err);
189 return;
190 }
191 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
192 if (err != NO_ERROR) {
193 ALOGE("error posting framebuffer: %d", err);
194 }
195}
196#endif
197
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700198void FramebufferSurface::freeBufferLocked(int slotIndex) {
199 ConsumerBase::freeBufferLocked(slotIndex);
200 if (slotIndex == mCurrentBufferSlot) {
201 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
202 }
203}
204
Jesse Hall851cfe82013-03-20 13:44:00 -0700205void FramebufferSurface::onFrameCommitted() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000206#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207 if (mHasPendingRelease) {
208 sp<Fence> fence = mHwc.getRetireFence(mDisplayType);
209 if (fence->isValid()) {
210 status_t result = addReleaseFence(mPreviousBufferSlot,
211 mPreviousBuffer, fence);
212 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
213 " fence: %s (%d)", strerror(-result), result);
214 }
215 status_t result = releaseBufferLocked(mPreviousBufferSlot,
216 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
217 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
218 " %s (%d)", strerror(-result), result);
219
220 mPreviousBuffer.clear();
221 mHasPendingRelease = false;
222 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000223#else
224 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
225 if (fence->isValid() &&
226 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
227 status_t err = addReleaseFence(mCurrentBufferSlot,
228 mCurrentBuffer, fence);
229 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
230 strerror(-err), err);
231 }
232#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700233}
234
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000235#ifndef USE_HWC2
236status_t FramebufferSurface::compositionComplete()
237{
238 return mHwc.fbCompositionComplete();
239}
240#endif
241
Dan Stozaf10c46e2014-11-11 10:32:31 -0800242void FramebufferSurface::dumpAsString(String8& result) const {
Colin Cross3d1d2802016-09-26 18:10:16 -0700243 ConsumerBase::dumpState(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700244}
245
Mathias Agopian74d211a2013-04-22 16:55:35 +0200246void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800247{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000248#ifndef USE_HWC2
249 mHwc.fbDump(result);
250#endif
Mathias Agopian74d211a2013-04-22 16:55:35 +0200251 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800252}
253
Dan Stoza9e56aa02015-11-02 13:00:03 -0800254const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
255 return mCurrentFence;
256}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800257
Mathias Agopian3e876012012-06-07 17:52:54 -0700258// ----------------------------------------------------------------------------
259}; // namespace android
260// ----------------------------------------------------------------------------