blob: cdfe34aa6710bb2f54e267a3a0a77ba5e49427ca [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),
Pablo Ceballos40845df2016-01-25 17:41:15 -080064#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mHwc(hwc),
66 mHasPendingRelease(false),
67 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
68 mPreviousBuffer()
69#else
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070070 mHwc(hwc)
Dan Stoza9e56aa02015-11-02 13:00:03 -080071#endif
Mathias Agopian3e876012012-06-07 17:52:54 -070072{
Dan Stoza9e56aa02015-11-02 13:00:03 -080073#ifdef USE_HWC2
74 ALOGV("Creating for display %d", disp);
75#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);
Dan Stoza9e56aa02015-11-02 13:00:03 -080082#ifdef USE_HWC2
83 const auto& activeConfig = mHwc.getActiveConfig(disp);
84 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
85 activeConfig->getHeight());
86#else
Mathias Agopiandb89edc2013-08-02 01:40:18 -070087 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
Dan Stoza9e56aa02015-11-02 13:00:03 -080088 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() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102#ifdef USE_HWC2
103 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;
117#else
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700118 // 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;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800122#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700123}
124
Dan Stoza9e56aa02015-11-02 13:00:03 -0800125#ifdef USE_HWC2
126status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
127 sp<Fence>& outFence, android_dataspace_t& outDataspace) {
128#else
Mathias Agopianda27af92012-09-13 18:17:13 -0700129status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130#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) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800153#ifdef USE_HWC2
154 mHasPendingRelease = true;
155 mPreviousBufferSlot = mCurrentBufferSlot;
156 mPreviousBuffer = mCurrentBuffer;
157#else
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700158 // Release the previous buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700159 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
160 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700161 if (err < NO_ERROR) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700162 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
163 return err;
164 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800165#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;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800173#ifdef USE_HWC2
174 outDataspace = item.mDataSpace;
175#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700176 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700177}
178
Dan Stoza9e56aa02015-11-02 13:00:03 -0800179#ifndef USE_HWC2
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700180// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
Dan Stoza8dc55392014-11-04 11:37:46 -0800181void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700182 sp<GraphicBuffer> buf;
Mathias Agopianda27af92012-09-13 18:17:13 -0700183 sp<Fence> acquireFence;
184 status_t err = nextBuffer(buf, acquireFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700185 if (err != NO_ERROR) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700186 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700187 strerror(-err), err);
188 return;
189 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700190 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700191 if (err != NO_ERROR) {
192 ALOGE("error posting framebuffer: %d", err);
193 }
194}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700196
197void 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() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800205#ifdef USE_HWC2
206 if (mHasPendingRelease) {
207 sp<Fence> fence = mHwc.getRetireFence(mDisplayType);
208 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 }
222#else
Jesse Hall851cfe82013-03-20 13:44:00 -0700223 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700224 if (fence->isValid() &&
225 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700226 status_t err = addReleaseFence(mCurrentBufferSlot,
227 mCurrentBuffer, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700228 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
229 strerror(-err), err);
Mathias Agopianda27af92012-09-13 18:17:13 -0700230 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700232}
233
Dan Stoza9e56aa02015-11-02 13:00:03 -0800234#ifndef USE_HWC2
Mathias Agopian3e876012012-06-07 17:52:54 -0700235status_t FramebufferSurface::compositionComplete()
236{
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700237 return mHwc.fbCompositionComplete();
Mathias Agopian3e876012012-06-07 17:52:54 -0700238}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239#endif
Mathias Agopian3e876012012-06-07 17:52:54 -0700240
Dan Stozaf10c46e2014-11-11 10:32:31 -0800241void FramebufferSurface::dumpAsString(String8& result) const {
Colin Cross5fa12232016-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{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247#ifndef USE_HWC2
Jesse Hall7adb0f82013-03-06 16:13:49 -0800248 mHwc.fbDump(result);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800249#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// ----------------------------------------------------------------------------