blob: 1998edfe7cf355863eba7147522f6e15a10d0b5d [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 <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070026
Mathias Agopian3e876012012-06-07 17:52:54 -070027#include <utils/String8.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070029
30#include <ui/Rect.h>
31
32#include <EGL/egl.h>
33
34#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070035#include <gui/BufferItem.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080036#include <gui/GraphicBufferAlloc.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070037#include <gui/Surface.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070038#include <ui/GraphicBuffer.h>
39
Mathias Agopian33ceeb32013-04-01 16:54:58 -070040#include "FramebufferSurface.h"
41#include "HWComposer.h"
Mathias Agopian3e876012012-06-07 17:52:54 -070042
Jamie Genniscdbaecb2012-10-12 14:18:10 -070043#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
44#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
45#endif
46
Mathias Agopian3e876012012-06-07 17:52:54 -070047// ----------------------------------------------------------------------------
48namespace android {
49// ----------------------------------------------------------------------------
50
Mathias Agopian3e876012012-06-07 17:52:54 -070051/*
52 * This implements the (main) framebuffer management. This class is used
53 * mostly by SurfaceFlinger, but also by command line GL application.
54 *
55 */
56
Mathias Agopiandb89edc2013-08-02 01:40:18 -070057FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
58 const sp<IGraphicBufferConsumer>& consumer) :
59 ConsumerBase(consumer),
Mathias Agopianf5a33922012-09-19 18:16:22 -070060 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070061 mCurrentBufferSlot(-1),
Dan Stoza9e56aa02015-11-02 13:00:03 -080062 mCurrentBuffer(),
63 mCurrentFence(Fence::NO_FENCE),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000064#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mHwc(hwc),
66 mHasPendingRelease(false),
67 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
68 mPreviousBuffer()
Fabien Sanglard9d96de42016-10-11 00:15:18 +000069#else
70 mHwc(hwc)
71#endif
Mathias Agopian3e876012012-06-07 17:52:54 -070072{
Fabien Sanglard9d96de42016-10-11 00:15:18 +000073#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080074 ALOGV("Creating for display %d", disp);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000075#endif
76
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070077 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070078 mConsumer->setConsumerName(mName);
79 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070080 GRALLOC_USAGE_HW_RENDER |
81 GRALLOC_USAGE_HW_COMPOSER);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000082#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080083 const auto& activeConfig = mHwc.getActiveConfig(disp);
84 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
85 activeConfig->getHeight());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000086#else
87 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
88 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
89#endif
Pablo Ceballos19e3e062015-08-19 16:16:06 -070090 mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070091}
92
Jesse Hall7cd85972014-08-07 22:48:06 -070093status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070094 return NO_ERROR;
95}
96
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070097status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070098 return NO_ERROR;
99}
100
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700101status_t FramebufferSurface::advanceFrame() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000102#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103 sp<GraphicBuffer> buf;
104 sp<Fence> acquireFence(Fence::NO_FENCE);
105 android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
106 status_t result = nextBuffer(buf, acquireFence, dataspace);
107 if (result != NO_ERROR) {
108 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
109 strerror(-result), result);
110 return result;
111 }
112 result = mHwc.setClientTarget(mDisplayType, acquireFence, buf, dataspace);
113 if (result != NO_ERROR) {
114 ALOGE("error posting framebuffer: %d", result);
115 }
116 return result;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000117#else
118 // Once we remove FB HAL support, we can call nextBuffer() from here
119 // instead of using onFrameAvailable(). No real benefit, except it'll be
120 // more like VirtualDisplaySurface.
121 return NO_ERROR;
122#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700123}
124
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000125#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800126status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
127 sp<Fence>& outFence, android_dataspace_t& outDataspace) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000128#else
129status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
130#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700131 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700132
Dan Stoza84493cd2015-03-12 15:12:44 -0700133 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700134 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700135 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700136 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700137 return NO_ERROR;
138 } else if (err != NO_ERROR) {
139 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
140 return err;
141 }
142
143 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
144 // then we may have acquired the slot we already own. If we had released
145 // our current buffer before we call acquireBuffer then that release call
146 // would have returned STALE_BUFFER_SLOT, and we would have called
147 // freeBufferLocked on that slot. Because the buffer slot has already
148 // been overwritten with the new buffer all we have to do is skip the
149 // releaseBuffer call and we should be in the same state we'd be in if we
150 // had released the old buffer first.
151 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700152 item.mSlot != mCurrentBufferSlot) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000153#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800154 mHasPendingRelease = true;
155 mPreviousBufferSlot = mCurrentBufferSlot;
156 mPreviousBuffer = mCurrentBuffer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000157#else
158 // Release the previous buffer.
159 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
160 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
161 if (err < NO_ERROR) {
162 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
163 return err;
164 }
165#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700166 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700167 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700168 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800169 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170
Mathias Agopianda27af92012-09-13 18:17:13 -0700171 outFence = item.mFence;
172 outBuffer = mCurrentBuffer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000173#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800174 outDataspace = item.mDataSpace;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000175#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700176 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700177}
178
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000179#ifndef USE_HWC2
180// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
181void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
182 sp<GraphicBuffer> buf;
183 sp<Fence> acquireFence;
184 status_t err = nextBuffer(buf, acquireFence);
185 if (err != NO_ERROR) {
186 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
187 strerror(-err), err);
188 return;
189 }
190 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
191 if (err != NO_ERROR) {
192 ALOGE("error posting framebuffer: %d", err);
193 }
194}
195#endif
196
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700197void FramebufferSurface::freeBufferLocked(int slotIndex) {
198 ConsumerBase::freeBufferLocked(slotIndex);
199 if (slotIndex == mCurrentBufferSlot) {
200 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
201 }
202}
203
Jesse Hall851cfe82013-03-20 13:44:00 -0700204void FramebufferSurface::onFrameCommitted() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000205#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206 if (mHasPendingRelease) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800207 sp<Fence> fence = mHwc.getPresentFence(mDisplayType);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 if (fence->isValid()) {
209 status_t result = addReleaseFence(mPreviousBufferSlot,
210 mPreviousBuffer, fence);
211 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
212 " fence: %s (%d)", strerror(-result), result);
213 }
214 status_t result = releaseBufferLocked(mPreviousBufferSlot,
215 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
216 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
217 " %s (%d)", strerror(-result), result);
218
219 mPreviousBuffer.clear();
220 mHasPendingRelease = false;
221 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000222#else
223 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
224 if (fence->isValid() &&
225 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
226 status_t err = addReleaseFence(mCurrentBufferSlot,
227 mCurrentBuffer, fence);
228 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
229 strerror(-err), err);
230 }
231#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700232}
233
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000234#ifndef USE_HWC2
235status_t FramebufferSurface::compositionComplete()
236{
237 return mHwc.fbCompositionComplete();
238}
239#endif
240
Dan Stozaf10c46e2014-11-11 10:32:31 -0800241void FramebufferSurface::dumpAsString(String8& result) const {
Colin Cross3d1d2802016-09-26 18:10:16 -0700242 ConsumerBase::dumpState(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700243}
244
Mathias Agopian74d211a2013-04-22 16:55:35 +0200245void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800246{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000247#ifndef USE_HWC2
248 mHwc.fbDump(result);
249#endif
Mathias Agopian74d211a2013-04-22 16:55:35 +0200250 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800251}
252
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
254 return mCurrentFence;
255}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256
Mathias Agopian3e876012012-06-07 17:52:54 -0700257// ----------------------------------------------------------------------------
258}; // namespace android
259// ----------------------------------------------------------------------------