blob: 27812f7492dd40c8ac7b1e0fadee5c58dfb46d45 [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 <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070031#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080032#include <gui/BufferQueue.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070033#include <gui/Surface.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080034
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060035#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070036#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080037#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070038
Mathias Agopian33ceeb32013-04-01 16:54:58 -070039#include "FramebufferSurface.h"
40#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080041#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070042
Mathias Agopian3e876012012-06-07 17:52:54 -070043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
Peiyong Lin34beb7a2018-03-28 11:57:12 -070047using ui::Dataspace;
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
Dominik Laskowski075d3172018-05-24 15:50:06 -070055FramebufferSurface::FramebufferSurface(HWComposer& hwc, DisplayId displayId,
56 const sp<IGraphicBufferConsumer>& consumer)
57 : ConsumerBase(consumer),
58 mDisplayId(displayId),
59 mCurrentBufferSlot(-1),
60 mCurrentBuffer(),
61 mCurrentFence(Fence::NO_FENCE),
62 mHwc(hwc),
63 mHasPendingRelease(false),
64 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
65 mPreviousBuffer() {
Dominik Laskowski34157762018-10-31 13:07:19 -070066 ALOGV("Creating for display %s", to_string(displayId).c_str());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000067
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070068 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070069 mConsumer->setConsumerName(mName);
70 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070071 GRALLOC_USAGE_HW_RENDER |
72 GRALLOC_USAGE_HW_COMPOSER);
Dominik Laskowski075d3172018-05-24 15:50:06 -070073 const auto& activeConfig = mHwc.getActiveConfig(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080074 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
75 activeConfig->getHeight());
Fabien Sanglard1971b632017-03-10 14:50:03 -080076 mConsumer->setMaxAcquiredBufferCount(
77 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070078}
79
Lloyd Pique2ae2b3b2017-12-14 17:18:17 -080080void FramebufferSurface::resizeBuffers(const uint32_t width, const uint32_t height) {
81 mConsumer->setDefaultBufferSize(width, height);
82}
83
Jesse Hall7cd85972014-08-07 22:48:06 -070084status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070085 return NO_ERROR;
86}
87
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070088status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070089 return NO_ERROR;
90}
91
Jesse Hall99c7dbb2013-03-14 14:29:29 -070092status_t FramebufferSurface::advanceFrame() {
Chia-I Wu06d63de2017-01-04 14:58:51 +080093 uint32_t slot = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -080094 sp<GraphicBuffer> buf;
95 sp<Fence> acquireFence(Fence::NO_FENCE);
Peiyong Lin34beb7a2018-03-28 11:57:12 -070096 Dataspace dataspace = Dataspace::UNKNOWN;
Chia-I Wu06d63de2017-01-04 14:58:51 +080097 status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060098 mDataSpace = dataspace;
Dan Stoza9e56aa02015-11-02 13:00:03 -080099 if (result != NO_ERROR) {
100 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
101 strerror(-result), result);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102 }
103 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700104}
105
Chia-I Wu06d63de2017-01-04 14:58:51 +0800106status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
107 sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700108 Dataspace& 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) {
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800114 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800115 &outSlot, &outBuffer);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700116 return NO_ERROR;
117 } else if (err != NO_ERROR) {
118 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
119 return err;
120 }
121
122 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
123 // then we may have acquired the slot we already own. If we had released
124 // our current buffer before we call acquireBuffer then that release call
125 // would have returned STALE_BUFFER_SLOT, and we would have called
126 // freeBufferLocked on that slot. Because the buffer slot has already
127 // been overwritten with the new buffer all we have to do is skip the
128 // releaseBuffer call and we should be in the same state we'd be in if we
129 // had released the old buffer first.
130 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700131 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132 mHasPendingRelease = true;
133 mPreviousBufferSlot = mCurrentBufferSlot;
134 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700135 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700136 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700137 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800139
Mathias Agopianda27af92012-09-13 18:17:13 -0700140 outFence = item.mFence;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800141 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800142 &outSlot, &outBuffer);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700143 outDataspace = static_cast<Dataspace>(item.mDataSpace);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700144 status_t result = mHwc.setClientTarget(mDisplayId, outSlot, outFence, outBuffer, outDataspace);
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600145 if (result != NO_ERROR) {
146 ALOGE("error posting framebuffer: %d", result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600147 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600148 }
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600149
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600150 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700151}
152
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700153void FramebufferSurface::freeBufferLocked(int slotIndex) {
154 ConsumerBase::freeBufferLocked(slotIndex);
155 if (slotIndex == mCurrentBufferSlot) {
156 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
157 }
158}
159
Jesse Hall851cfe82013-03-20 13:44:00 -0700160void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800161 if (mHasPendingRelease) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700162 sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800163 if (fence->isValid()) {
164 status_t result = addReleaseFence(mPreviousBufferSlot,
165 mPreviousBuffer, fence);
166 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
167 " fence: %s (%d)", strerror(-result), result);
168 }
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800169 status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
171 " %s (%d)", strerror(-result), result);
172
173 mPreviousBuffer.clear();
174 mHasPendingRelease = false;
175 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700176}
177
Dan Stozaf10c46e2014-11-11 10:32:31 -0800178void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600179 Mutex::Autolock lock(mMutex);
Chia-I Wu1e043612018-03-01 09:45:09 -0800180 result.appendFormat(" FramebufferSurface: dataspace: %s(%d)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700181 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
182 mDataSpace);
Chia-I Wu1e043612018-03-01 09:45:09 -0800183 ConsumerBase::dumpLocked(result, " ");
Mathias Agopian3e876012012-06-07 17:52:54 -0700184}
185
Mathias Agopian74d211a2013-04-22 16:55:35 +0200186void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800187{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200188 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800189}
190
Dan Stoza9e56aa02015-11-02 13:00:03 -0800191const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
192 return mCurrentFence;
193}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194
Mathias Agopian3e876012012-06-07 17:52:54 -0700195// ----------------------------------------------------------------------------
196}; // namespace android
197// ----------------------------------------------------------------------------