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 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include <utils/String8.h> |
| 26 | |
| 27 | #include <ui/Rect.h> |
| 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | |
| 31 | #include <hardware/hardware.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 32 | #include <gui/Surface.h> |
Jamie Gennis | 392edd8 | 2012-11-29 23:26:29 -0800 | [diff] [blame] | 33 | #include <gui/GraphicBufferAlloc.h> |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 34 | #include <ui/GraphicBuffer.h> |
| 35 | |
Mathias Agopian | 33ceeb3 | 2013-04-01 16:54:58 -0700 | [diff] [blame] | 36 | #include "FramebufferSurface.h" |
| 37 | #include "HWComposer.h" |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 38 | |
Jamie Gennis | cdbaecb | 2012-10-12 14:18:10 -0700 | [diff] [blame] | 39 | #ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS |
| 40 | #define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2) |
| 41 | #endif |
| 42 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
| 44 | namespace android { |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 47 | /* |
| 48 | * This implements the (main) framebuffer management. This class is used |
| 49 | * mostly by SurfaceFlinger, but also by command line GL application. |
| 50 | * |
| 51 | */ |
| 52 | |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 53 | FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp, |
| 54 | const sp<IGraphicBufferConsumer>& consumer) : |
| 55 | ConsumerBase(consumer), |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 56 | mDisplayType(disp), |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 57 | mCurrentBufferSlot(-1), |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 58 | mCurrentBuffer(0), |
| 59 | mHwc(hwc) |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 60 | { |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 61 | mName = "FramebufferSurface"; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 62 | mConsumer->setConsumerName(mName); |
| 63 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 64 | GRALLOC_USAGE_HW_RENDER | |
| 65 | GRALLOC_USAGE_HW_COMPOSER); |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 66 | mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp)); |
| 67 | mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp)); |
| 68 | mConsumer->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS); |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jesse Hall | 7cd8597 | 2014-08-07 22:48:06 -0700 | [diff] [blame] | 71 | status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) { |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 72 | return NO_ERROR; |
| 73 | } |
| 74 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 75 | status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 76 | return NO_ERROR; |
| 77 | } |
| 78 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 79 | status_t FramebufferSurface::advanceFrame() { |
| 80 | // Once we remove FB HAL support, we can call nextBuffer() from here |
| 81 | // instead of using onFrameAvailable(). No real benefit, except it'll be |
| 82 | // more like VirtualDisplaySurface. |
| 83 | return NO_ERROR; |
| 84 | } |
| 85 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 86 | status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 87 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 88 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 89 | BufferQueue::BufferItem item; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 90 | status_t err = acquireBufferLocked(&item, 0); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 91 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 92 | outBuffer = mCurrentBuffer; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 93 | return NO_ERROR; |
| 94 | } else if (err != NO_ERROR) { |
| 95 | ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot |
| 100 | // then we may have acquired the slot we already own. If we had released |
| 101 | // our current buffer before we call acquireBuffer then that release call |
| 102 | // would have returned STALE_BUFFER_SLOT, and we would have called |
| 103 | // freeBufferLocked on that slot. Because the buffer slot has already |
| 104 | // been overwritten with the new buffer all we have to do is skip the |
| 105 | // releaseBuffer call and we should be in the same state we'd be in if we |
| 106 | // had released the old buffer first. |
| 107 | if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && |
| 108 | item.mBuf != mCurrentBufferSlot) { |
| 109 | // Release the previous buffer. |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 110 | err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer, |
| 111 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 112 | if (err < NO_ERROR) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 113 | ALOGE("error releasing buffer: %s (%d)", strerror(-err), err); |
| 114 | return err; |
| 115 | } |
| 116 | } |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 117 | mCurrentBufferSlot = item.mBuf; |
| 118 | mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 119 | outFence = item.mFence; |
| 120 | outBuffer = mCurrentBuffer; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 121 | return NO_ERROR; |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 124 | // Overrides ConsumerBase::onFrameAvailable(), does not call base class impl. |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 125 | void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 126 | sp<GraphicBuffer> buf; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 127 | sp<Fence> acquireFence; |
| 128 | status_t err = nextBuffer(buf, acquireFence); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 129 | if (err != NO_ERROR) { |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 130 | ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)", |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 131 | strerror(-err), err); |
| 132 | return; |
| 133 | } |
Mathias Agopian | f5a3392 | 2012-09-19 18:16:22 -0700 | [diff] [blame] | 134 | err = mHwc.fbPost(mDisplayType, acquireFence, buf); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 135 | if (err != NO_ERROR) { |
| 136 | ALOGE("error posting framebuffer: %d", err); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void FramebufferSurface::freeBufferLocked(int slotIndex) { |
| 141 | ConsumerBase::freeBufferLocked(slotIndex); |
| 142 | if (slotIndex == mCurrentBufferSlot) { |
| 143 | mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; |
| 144 | } |
| 145 | } |
| 146 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 147 | void FramebufferSurface::onFrameCommitted() { |
| 148 | sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType); |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 149 | if (fence->isValid() && |
| 150 | mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 151 | status_t err = addReleaseFence(mCurrentBufferSlot, |
| 152 | mCurrentBuffer, fence); |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 153 | ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)", |
| 154 | strerror(-err), err); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 155 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 158 | status_t FramebufferSurface::compositionComplete() |
| 159 | { |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 160 | return mHwc.fbCompositionComplete(); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 163 | void FramebufferSurface::dumpAsString(String8& result) const { |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 164 | ConsumerBase::dump(result); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 167 | void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const |
Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 168 | { |
| 169 | mHwc.fbDump(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 170 | ConsumerBase::dumpLocked(result, prefix); |
Jesse Hall | 7adb0f8 | 2013-03-06 16:13:49 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 173 | // ---------------------------------------------------------------------------- |
| 174 | }; // namespace android |
| 175 | // ---------------------------------------------------------------------------- |