blob: 4c3b3e502df69f6d6f2777cf67460237dc515d33 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080018// TODO(b/129481165): remove the #pragma below and fix conversion issues
19#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Wconversion"
21
Dan Stoza9e56aa02015-11-02 13:00:03 -080022// #define LOG_NDEBUG 0
23#undef LOG_TAG
24#define LOG_TAG "FramebufferSurface"
25
Mathias Agopian3e876012012-06-07 17:52:54 -070026#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070030
Mathias Agopian3e876012012-06-07 17:52:54 -070031#include <utils/String8.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070033
Mathias Agopian3e876012012-06-07 17:52:54 -070034#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070035#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <gui/BufferQueue.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070037#include <gui/Surface.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080038
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060039#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070040#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080041#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070042
Mathias Agopian33ceeb32013-04-01 16:54:58 -070043#include "FramebufferSurface.h"
44#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080045#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070046
Mathias Agopian3e876012012-06-07 17:52:54 -070047// ----------------------------------------------------------------------------
48namespace android {
49// ----------------------------------------------------------------------------
50
Peiyong Lin34beb7a2018-03-28 11:57:12 -070051using ui::Dataspace;
52
Mathias Agopian3e876012012-06-07 17:52:54 -070053/*
54 * This implements the (main) framebuffer management. This class is used
55 * mostly by SurfaceFlinger, but also by command line GL application.
56 *
57 */
58
Dominik Laskowski075d3172018-05-24 15:50:06 -070059FramebufferSurface::FramebufferSurface(HWComposer& hwc, DisplayId displayId,
Brian Lindahla13f2d52020-03-05 11:54:17 +010060 const sp<IGraphicBufferConsumer>& consumer,
61 uint32_t maxWidth, uint32_t maxHeight)
Dominik Laskowski075d3172018-05-24 15:50:06 -070062 : ConsumerBase(consumer),
63 mDisplayId(displayId),
Brian Lindahla13f2d52020-03-05 11:54:17 +010064 mMaxWidth(maxWidth),
65 mMaxHeight(maxHeight),
Dominik Laskowski075d3172018-05-24 15:50:06 -070066 mCurrentBufferSlot(-1),
67 mCurrentBuffer(),
68 mCurrentFence(Fence::NO_FENCE),
69 mHwc(hwc),
70 mHasPendingRelease(false),
71 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
72 mPreviousBuffer() {
Dominik Laskowski34157762018-10-31 13:07:19 -070073 ALOGV("Creating for display %s", to_string(displayId).c_str());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000074
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070075 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070076 mConsumer->setConsumerName(mName);
77 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070078 GRALLOC_USAGE_HW_RENDER |
79 GRALLOC_USAGE_HW_COMPOSER);
Dominik Laskowski075d3172018-05-24 15:50:06 -070080 const auto& activeConfig = mHwc.getActiveConfig(displayId);
Brian Lindahla13f2d52020-03-05 11:54:17 +010081 ui::Size limitedSize =
82 limitFramebufferSize(activeConfig->getWidth(), activeConfig->getHeight());
83 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Fabien Sanglard1971b632017-03-10 14:50:03 -080084 mConsumer->setMaxAcquiredBufferCount(
85 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070086}
87
Brian Lindahla13f2d52020-03-05 11:54:17 +010088void FramebufferSurface::resizeBuffers(uint32_t width, uint32_t height) {
89 ui::Size limitedSize = limitFramebufferSize(width, height);
90 mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
Lloyd Pique2ae2b3b2017-12-14 17:18:17 -080091}
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() {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800102 uint32_t slot = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103 sp<GraphicBuffer> buf;
104 sp<Fence> acquireFence(Fence::NO_FENCE);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700105 Dataspace dataspace = Dataspace::UNKNOWN;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800106 status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600107 mDataSpace = dataspace;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800108 if (result != NO_ERROR) {
109 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
110 strerror(-result), result);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111 }
112 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700113}
114
Chia-I Wu06d63de2017-01-04 14:58:51 +0800115status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
116 sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700117 Dataspace& outDataspace) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700118 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700119
Dan Stoza84493cd2015-03-12 15:12:44 -0700120 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700121 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700122 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700123 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124 return NO_ERROR;
125 } else if (err != NO_ERROR) {
126 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
127 return err;
128 }
129
130 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
131 // then we may have acquired the slot we already own. If we had released
132 // our current buffer before we call acquireBuffer then that release call
133 // would have returned STALE_BUFFER_SLOT, and we would have called
134 // freeBufferLocked on that slot. Because the buffer slot has already
135 // been overwritten with the new buffer all we have to do is skip the
136 // releaseBuffer call and we should be in the same state we'd be in if we
137 // had released the old buffer first.
138 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700139 item.mSlot != mCurrentBufferSlot) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140 mHasPendingRelease = true;
141 mPreviousBufferSlot = mCurrentBufferSlot;
142 mPreviousBuffer = mCurrentBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700143 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700144 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700145 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800146 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800147
Mathias Agopianda27af92012-09-13 18:17:13 -0700148 outFence = item.mFence;
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700149 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700150 outDataspace = static_cast<Dataspace>(item.mDataSpace);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700151 status_t result = mHwc.setClientTarget(mDisplayId, outSlot, outFence, outBuffer, outDataspace);
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600152 if (result != NO_ERROR) {
153 ALOGE("error posting framebuffer: %d", result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600154 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600155 }
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600156
Courtney Goeltzenleuchter64618062017-08-02 15:55:06 -0600157 return NO_ERROR;
Mathias Agopian3e876012012-06-07 17:52:54 -0700158}
159
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700160void FramebufferSurface::freeBufferLocked(int slotIndex) {
161 ConsumerBase::freeBufferLocked(slotIndex);
162 if (slotIndex == mCurrentBufferSlot) {
163 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
164 }
165}
166
Jesse Hall851cfe82013-03-20 13:44:00 -0700167void FramebufferSurface::onFrameCommitted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800168 if (mHasPendingRelease) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700169 sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170 if (fence->isValid()) {
171 status_t result = addReleaseFence(mPreviousBufferSlot,
172 mPreviousBuffer, fence);
173 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
174 " fence: %s (%d)", strerror(-result), result);
175 }
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800176 status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800177 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
178 " %s (%d)", strerror(-result), result);
179
180 mPreviousBuffer.clear();
181 mHasPendingRelease = false;
182 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700183}
184
Brian Lindahla13f2d52020-03-05 11:54:17 +0100185ui::Size FramebufferSurface::limitFramebufferSize(uint32_t width, uint32_t height) {
Lloyd Pique27ddcb52020-03-20 11:49:10 -0700186 ui::Size framebufferSize(width, height);
Brian Lindahla13f2d52020-03-05 11:54:17 +0100187 bool wasLimited = true;
188 if (width > mMaxWidth && mMaxWidth != 0) {
189 float aspectRatio = float(width) / float(height);
190 framebufferSize.height = mMaxWidth / aspectRatio;
191 framebufferSize.width = mMaxWidth;
192 wasLimited = true;
193 }
194 if (height > mMaxHeight && mMaxHeight != 0) {
195 float aspectRatio = float(width) / float(height);
196 framebufferSize.height = mMaxHeight;
197 framebufferSize.width = mMaxHeight * aspectRatio;
198 wasLimited = true;
199 }
200 ALOGI_IF(wasLimited, "framebuffer size has been limited to [%dx%d] from [%dx%d]",
201 framebufferSize.width, framebufferSize.height, width, height);
202 return framebufferSize;
203}
204
Dan Stozaf10c46e2014-11-11 10:32:31 -0800205void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600206 Mutex::Autolock lock(mMutex);
Chia-I Wu1e043612018-03-01 09:45:09 -0800207 result.appendFormat(" FramebufferSurface: dataspace: %s(%d)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700208 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
209 mDataSpace);
Chia-I Wu1e043612018-03-01 09:45:09 -0800210 ConsumerBase::dumpLocked(result, " ");
Mathias Agopian3e876012012-06-07 17:52:54 -0700211}
212
Mathias Agopian74d211a2013-04-22 16:55:35 +0200213void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800214{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200215 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800216}
217
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
219 return mCurrentFence;
220}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221
Mathias Agopian3e876012012-06-07 17:52:54 -0700222// ----------------------------------------------------------------------------
223}; // namespace android
224// ----------------------------------------------------------------------------
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800225
226// TODO(b/129481165): remove the #pragma below and fix conversion issues
227#pragma clang diagnostic pop // ignored "-Wconversion"