Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 3 | ** Copyright 2012 The Android Open Source Project |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 4 | ** |
| 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 Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 18 | // #define LOG_NDEBUG 0 |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "FramebufferSurface" |
| 21 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 26 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 29 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 30 | #include <hardware/hardware.h> |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 31 | #include <gui/BufferItem.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 32 | #include <gui/BufferQueue.h> |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 33 | #include <gui/Surface.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 34 | |
Courtney Goeltzenleuchter | 19987a8 | 2017-07-14 12:16:56 -0600 | [diff] [blame] | 35 | #include <ui/DebugUtils.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 36 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 37 | #include <ui/Rect.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 38 | |
Mathias Agopian | 33ceeb3 | 2013-04-01 16:54:58 -0700 | [diff] [blame] | 39 | #include "FramebufferSurface.h" |
| 40 | #include "HWComposer.h" |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 41 | #include "../SurfaceFlinger.h" |
Jamie Gennis | cdbaecb | 2012-10-12 14:18:10 -0700 | [diff] [blame] | 42 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
| 44 | namespace android { |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 47 | using ui::Dataspace; |
| 48 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 49 | /* |
| 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 Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 55 | FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp, |
| 56 | const sp<IGraphicBufferConsumer>& consumer) : |
| 57 | ConsumerBase(consumer), |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 58 | mDisplayType(disp), |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 59 | mCurrentBufferSlot(-1), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 60 | mCurrentBuffer(), |
| 61 | mCurrentFence(Fence::NO_FENCE), |
| 62 | mHwc(hwc), |
| 63 | mHasPendingRelease(false), |
| 64 | mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT), |
| 65 | mPreviousBuffer() |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 66 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 67 | ALOGV("Creating for display %d", disp); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 68 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 69 | mName = "FramebufferSurface"; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 70 | mConsumer->setConsumerName(mName); |
| 71 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 72 | GRALLOC_USAGE_HW_RENDER | |
| 73 | GRALLOC_USAGE_HW_COMPOSER); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 74 | const auto& activeConfig = mHwc.getActiveConfig(disp); |
| 75 | mConsumer->setDefaultBufferSize(activeConfig->getWidth(), |
| 76 | activeConfig->getHeight()); |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 77 | mConsumer->setMaxAcquiredBufferCount( |
| 78 | SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1); |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 81 | void FramebufferSurface::resizeBuffers(const uint32_t width, const uint32_t height) { |
| 82 | mConsumer->setDefaultBufferSize(width, height); |
| 83 | } |
| 84 | |
Jesse Hall | 7cd8597 | 2014-08-07 22:48:06 -0700 | [diff] [blame] | 85 | status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) { |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 86 | return NO_ERROR; |
| 87 | } |
| 88 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 89 | status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 90 | return NO_ERROR; |
| 91 | } |
| 92 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 93 | status_t FramebufferSurface::advanceFrame() { |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 94 | uint32_t slot = 0; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 95 | sp<GraphicBuffer> buf; |
| 96 | sp<Fence> acquireFence(Fence::NO_FENCE); |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 97 | Dataspace dataspace = Dataspace::UNKNOWN; |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 98 | status_t result = nextBuffer(slot, buf, acquireFence, dataspace); |
Courtney Goeltzenleuchter | 19987a8 | 2017-07-14 12:16:56 -0600 | [diff] [blame] | 99 | mDataSpace = dataspace; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 100 | if (result != NO_ERROR) { |
| 101 | ALOGE("error latching next FramebufferSurface buffer: %s (%d)", |
| 102 | strerror(-result), result); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 103 | } |
| 104 | return result; |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 107 | status_t FramebufferSurface::nextBuffer(uint32_t& outSlot, |
| 108 | sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence, |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 109 | Dataspace& outDataspace) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 110 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 111 | |
Dan Stoza | 84493cd | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 112 | BufferItem item; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 113 | status_t err = acquireBufferLocked(&item, 0); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 114 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 115 | mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 116 | &outSlot, &outBuffer); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 117 | return NO_ERROR; |
| 118 | } else if (err != NO_ERROR) { |
| 119 | ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot |
| 124 | // then we may have acquired the slot we already own. If we had released |
| 125 | // our current buffer before we call acquireBuffer then that release call |
| 126 | // would have returned STALE_BUFFER_SLOT, and we would have called |
| 127 | // freeBufferLocked on that slot. Because the buffer slot has already |
| 128 | // been overwritten with the new buffer all we have to do is skip the |
| 129 | // releaseBuffer call and we should be in the same state we'd be in if we |
| 130 | // had released the old buffer first. |
| 131 | if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 132 | item.mSlot != mCurrentBufferSlot) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 133 | mHasPendingRelease = true; |
| 134 | mPreviousBufferSlot = mCurrentBufferSlot; |
| 135 | mPreviousBuffer = mCurrentBuffer; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 136 | } |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 137 | mCurrentBufferSlot = item.mSlot; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 138 | mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 139 | mCurrentFence = item.mFence; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 140 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 141 | outFence = item.mFence; |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 142 | mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 143 | &outSlot, &outBuffer); |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 144 | outDataspace = static_cast<Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 145 | status_t result = |
| 146 | mHwc.setClientTarget(mDisplayType, outSlot, outFence, outBuffer, outDataspace); |
| 147 | if (result != NO_ERROR) { |
| 148 | ALOGE("error posting framebuffer: %d", result); |
Courtney Goeltzenleuchter | cd939aa | 2017-07-25 10:34:27 -0600 | [diff] [blame] | 149 | return result; |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 150 | } |
Courtney Goeltzenleuchter | 281e811 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 151 | |
Courtney Goeltzenleuchter | 6461806 | 2017-08-02 15:55:06 -0600 | [diff] [blame] | 152 | return NO_ERROR; |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 155 | void FramebufferSurface::freeBufferLocked(int slotIndex) { |
| 156 | ConsumerBase::freeBufferLocked(slotIndex); |
| 157 | if (slotIndex == mCurrentBufferSlot) { |
| 158 | mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; |
| 159 | } |
| 160 | } |
| 161 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 162 | void FramebufferSurface::onFrameCommitted() { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 163 | if (mHasPendingRelease) { |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 164 | sp<Fence> fence = mHwc.getPresentFence(mDisplayType); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 165 | if (fence->isValid()) { |
| 166 | status_t result = addReleaseFence(mPreviousBufferSlot, |
| 167 | mPreviousBuffer, fence); |
| 168 | ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the" |
| 169 | " fence: %s (%d)", strerror(-result), result); |
| 170 | } |
Chia-I Wu | 1e24cce | 2017-11-10 10:36:01 -0800 | [diff] [blame] | 171 | status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 172 | ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:" |
| 173 | " %s (%d)", strerror(-result), result); |
| 174 | |
| 175 | mPreviousBuffer.clear(); |
| 176 | mHasPendingRelease = false; |
| 177 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 180 | void FramebufferSurface::dumpAsString(String8& result) const { |
Courtney Goeltzenleuchter | 19987a8 | 2017-07-14 12:16:56 -0600 | [diff] [blame] | 181 | Mutex::Autolock lock(mMutex); |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 182 | result.appendFormat(" FramebufferSurface: dataspace: %s(%d)\n", |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 183 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), |
| 184 | mDataSpace); |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 185 | ConsumerBase::dumpLocked(result, " "); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 188 | void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const |
Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 189 | { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 190 | ConsumerBase::dumpLocked(result, prefix); |
Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 193 | const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const { |
| 194 | return mCurrentFence; |
| 195 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 196 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 197 | // ---------------------------------------------------------------------------- |
| 198 | }; // namespace android |
| 199 | // ---------------------------------------------------------------------------- |