blob: 182629c97e3ea1cbd6846d87028d8a1fc3a47cf6 [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
Mathias Agopian3e876012012-06-07 17:52:54 -070030#include <EGL/egl.h>
31
32#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070033#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080034#include <gui/BufferQueue.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070035#include <gui/Surface.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060037#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070038#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080039#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070040
Mathias Agopian33ceeb32013-04-01 16:54:58 -070041#include "FramebufferSurface.h"
42#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080043#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070044
Mathias Agopian3e876012012-06-07 17:52:54 -070045// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopian3e876012012-06-07 17:52:54 -070049/*
50 * This implements the (main) framebuffer management. This class is used
51 * mostly by SurfaceFlinger, but also by command line GL application.
52 *
53 */
54
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
56 const sp<IGraphicBufferConsumer>& consumer) :
57 ConsumerBase(consumer),
Mathias Agopianf5a33922012-09-19 18:16:22 -070058 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070059 mCurrentBufferSlot(-1),
Dan Stoza9e56aa02015-11-02 13:00:03 -080060 mCurrentBuffer(),
61 mCurrentFence(Fence::NO_FENCE),
62 mHwc(hwc),
63 mHasPendingRelease(false),
64 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
65 mPreviousBuffer()
Mathias Agopian3e876012012-06-07 17:52:54 -070066{
Dan Stoza9e56aa02015-11-02 13:00:03 -080067 ALOGV("Creating for display %d", disp);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000068
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070069 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070070 mConsumer->setConsumerName(mName);
71 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070072 GRALLOC_USAGE_HW_RENDER |
73 GRALLOC_USAGE_HW_COMPOSER);
Dan Stoza9e56aa02015-11-02 13:00:03 -080074 const auto& activeConfig = mHwc.getActiveConfig(disp);
75 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
76 activeConfig->getHeight());
Fabien Sanglard1971b632017-03-10 14:50:03 -080077 mConsumer->setMaxAcquiredBufferCount(
78 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070079}
80
Jesse Hall7cd85972014-08-07 22:48:06 -070081status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070082 return NO_ERROR;
83}
84
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070085status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070086 return NO_ERROR;
87}
88
Jesse Hall99c7dbb2013-03-14 14:29:29 -070089status_t FramebufferSurface::advanceFrame() {
Chia-I Wu06d63de2017-01-04 14:58:51 +080090 uint32_t slot = 0;
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;
Chia-I Wu06d63de2017-01-04 14:58:51 +080094 status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060095 mDataSpace = dataspace;
Dan Stoza9e56aa02015-11-02 13:00:03 -080096 if (result != NO_ERROR) {
97 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
98 strerror(-result), result);
Dan Stoza9e56aa02015-11-02 13:00:03 -080099 }
100 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700101}
102
Chia-I Wu06d63de2017-01-04 14:58:51 +0800103status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
104 sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
105 android_dataspace_t& outDataspace) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700106 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700107
Dan Stoza84493cd2015-03-12 15:12:44 -0700108 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700109 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700110 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800111 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800112 &outSlot, &outBuffer);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700113 return NO_ERROR;
114 } else if (err != NO_ERROR) {
115 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
116 return err;
117 }
118
119 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
120 // then we may have acquired the slot we already own. If we had released
121 // our current buffer before we call acquireBuffer then that release call
122 // would have returned STALE_BUFFER_SLOT, and we would have called
123 // freeBufferLocked on that slot. Because the buffer slot has already
124 // been overwritten with the new buffer all we have to do is skip the
125 // releaseBuffer call and we should be in the same state we'd be in if we
126 // had released the old buffer first.
127 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700128 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800129 mHasPendingRelease = true;
130 mPreviousBufferSlot = mCurrentBufferSlot;
131 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700133 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800135 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800136
Mathias Agopianda27af92012-09-13 18:17:13 -0700137 outFence = item.mFence;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800138 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800139 &outSlot, &outBuffer);
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600140 outDataspace = item.mDataSpace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600141 status_t result =
142 mHwc.setClientTarget(mDisplayType, outSlot, outFence, outBuffer, outDataspace);
143 if (result != NO_ERROR) {
144 ALOGE("error posting framebuffer: %d", result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600145 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600146 }
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600147
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600148 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700149}
150
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700151void FramebufferSurface::freeBufferLocked(int slotIndex) {
152 ConsumerBase::freeBufferLocked(slotIndex);
153 if (slotIndex == mCurrentBufferSlot) {
154 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
155 }
156}
157
Jesse Hall851cfe82013-03-20 13:44:00 -0700158void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800159 if (mHasPendingRelease) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800160 sp<Fence> fence = mHwc.getPresentFence(mDisplayType);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800161 if (fence->isValid()) {
162 status_t result = addReleaseFence(mPreviousBufferSlot,
163 mPreviousBuffer, fence);
164 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
165 " fence: %s (%d)", strerror(-result), result);
166 }
167 status_t result = releaseBufferLocked(mPreviousBufferSlot,
168 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
169 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
170 " %s (%d)", strerror(-result), result);
171
172 mPreviousBuffer.clear();
173 mHasPendingRelease = false;
174 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700175}
176
Dan Stozaf10c46e2014-11-11 10:32:31 -0800177void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600178 Mutex::Autolock lock(mMutex);
179 result.appendFormat("FramebufferSurface: dataspace: %s(%d)\n",
180 dataspaceDetails(mDataSpace).c_str(), mDataSpace);
181 ConsumerBase::dumpLocked(result, "");
Mathias Agopian3e876012012-06-07 17:52:54 -0700182}
183
Mathias Agopian74d211a2013-04-22 16:55:35 +0200184void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800185{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200186 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800187}
188
Dan Stoza9e56aa02015-11-02 13:00:03 -0800189const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
190 return mCurrentFence;
191}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192
Mathias Agopian3e876012012-06-07 17:52:54 -0700193// ----------------------------------------------------------------------------
194}; // namespace android
195// ----------------------------------------------------------------------------