blob: a50b5503d2e928a880d3982c9a211f76e0510f7c [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),
65 mHwc(hwc),
66 mHasPendingRelease(false),
67 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
68 mPreviousBuffer()
Mathias Agopian3e876012012-06-07 17:52:54 -070069{
Dan Stoza9e56aa02015-11-02 13:00:03 -080070 ALOGV("Creating for display %d", disp);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070071 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070072 mConsumer->setConsumerName(mName);
73 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070074 GRALLOC_USAGE_HW_RENDER |
75 GRALLOC_USAGE_HW_COMPOSER);
Dan Stoza9e56aa02015-11-02 13:00:03 -080076 const auto& activeConfig = mHwc.getActiveConfig(disp);
77 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
78 activeConfig->getHeight());
Pablo Ceballos19e3e062015-08-19 16:16:06 -070079 mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070080}
81
Jesse Hall7cd85972014-08-07 22:48:06 -070082status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070083 return NO_ERROR;
84}
85
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070086status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070087 return NO_ERROR;
88}
89
Jesse Hall99c7dbb2013-03-14 14:29:29 -070090status_t FramebufferSurface::advanceFrame() {
Dan Stoza9e56aa02015-11-02 13:00:03 -080091 sp<GraphicBuffer> buf;
92 sp<Fence> acquireFence(Fence::NO_FENCE);
93 android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
94 status_t result = nextBuffer(buf, acquireFence, dataspace);
95 if (result != NO_ERROR) {
96 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
97 strerror(-result), result);
98 return result;
99 }
100 result = mHwc.setClientTarget(mDisplayType, acquireFence, buf, dataspace);
101 if (result != NO_ERROR) {
102 ALOGE("error posting framebuffer: %d", result);
103 }
104 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700105}
106
Dan Stoza9e56aa02015-11-02 13:00:03 -0800107status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
108 sp<Fence>& outFence, android_dataspace_t& outDataspace) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700109 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700110
Dan Stoza84493cd2015-03-12 15:12:44 -0700111 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700112 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700113 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700114 outBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700115 return NO_ERROR;
116 } else if (err != NO_ERROR) {
117 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
118 return err;
119 }
120
121 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
122 // then we may have acquired the slot we already own. If we had released
123 // our current buffer before we call acquireBuffer then that release call
124 // would have returned STALE_BUFFER_SLOT, and we would have called
125 // freeBufferLocked on that slot. Because the buffer slot has already
126 // been overwritten with the new buffer all we have to do is skip the
127 // releaseBuffer call and we should be in the same state we'd be in if we
128 // had released the old buffer first.
129 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700130 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800131 mHasPendingRelease = true;
132 mPreviousBufferSlot = mCurrentBufferSlot;
133 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700135 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800137 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138
Mathias Agopianda27af92012-09-13 18:17:13 -0700139 outFence = item.mFence;
140 outBuffer = mCurrentBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800141 outDataspace = item.mDataSpace;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700142 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700143}
144
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700145void FramebufferSurface::freeBufferLocked(int slotIndex) {
146 ConsumerBase::freeBufferLocked(slotIndex);
147 if (slotIndex == mCurrentBufferSlot) {
148 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
149 }
150}
151
Jesse Hall851cfe82013-03-20 13:44:00 -0700152void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800153 if (mHasPendingRelease) {
154 sp<Fence> fence = mHwc.getRetireFence(mDisplayType);
155 if (fence->isValid()) {
156 status_t result = addReleaseFence(mPreviousBufferSlot,
157 mPreviousBuffer, fence);
158 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
159 " fence: %s (%d)", strerror(-result), result);
160 }
161 status_t result = releaseBufferLocked(mPreviousBufferSlot,
162 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
163 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
164 " %s (%d)", strerror(-result), result);
165
166 mPreviousBuffer.clear();
167 mHasPendingRelease = false;
168 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700169}
170
Dan Stozaf10c46e2014-11-11 10:32:31 -0800171void FramebufferSurface::dumpAsString(String8& result) const {
Colin Cross3d1d2802016-09-26 18:10:16 -0700172 ConsumerBase::dumpState(result);
Mathias Agopian3e876012012-06-07 17:52:54 -0700173}
174
Mathias Agopian74d211a2013-04-22 16:55:35 +0200175void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800176{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200177 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800178}
179
Dan Stoza9e56aa02015-11-02 13:00:03 -0800180const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
181 return mCurrentFence;
182}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800183
Mathias Agopian3e876012012-06-07 17:52:54 -0700184// ----------------------------------------------------------------------------
185}; // namespace android
186// ----------------------------------------------------------------------------