FramebufferSurface.cpp revision 19e3e06e3c65a7c001a6fe0971744ba5ff536515
1/* 2 ** 3 ** Copyright 2012 The Android Open Source Project 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> 32#include <gui/BufferItem.h> 33#include <gui/GraphicBufferAlloc.h> 34#include <gui/Surface.h> 35#include <ui/GraphicBuffer.h> 36 37#include "FramebufferSurface.h" 38#include "HWComposer.h" 39 40#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS 41#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2) 42#endif 43 44// ---------------------------------------------------------------------------- 45namespace android { 46// ---------------------------------------------------------------------------- 47 48/* 49 * This implements the (main) framebuffer management. This class is used 50 * mostly by SurfaceFlinger, but also by command line GL application. 51 * 52 */ 53 54FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp, 55 const sp<IGraphicBufferConsumer>& consumer) : 56 ConsumerBase(consumer), 57 mDisplayType(disp), 58 mCurrentBufferSlot(-1), 59 mCurrentBuffer(0), 60 mHwc(hwc) 61{ 62 mName = "FramebufferSurface"; 63 mConsumer->setConsumerName(mName); 64 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | 65 GRALLOC_USAGE_HW_RENDER | 66 GRALLOC_USAGE_HW_COMPOSER); 67 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp)); 68 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp)); 69 mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1); 70} 71 72status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) { 73 return NO_ERROR; 74} 75 76status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) { 77 return NO_ERROR; 78} 79 80status_t FramebufferSurface::advanceFrame() { 81 // Once we remove FB HAL support, we can call nextBuffer() from here 82 // instead of using onFrameAvailable(). No real benefit, except it'll be 83 // more like VirtualDisplaySurface. 84 return NO_ERROR; 85} 86 87status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) { 88 Mutex::Autolock lock(mMutex); 89 90 BufferItem item; 91 status_t err = acquireBufferLocked(&item, 0); 92 if (err == BufferQueue::NO_BUFFER_AVAILABLE) { 93 outBuffer = mCurrentBuffer; 94 return NO_ERROR; 95 } else if (err != NO_ERROR) { 96 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); 97 return err; 98 } 99 100 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot 101 // then we may have acquired the slot we already own. If we had released 102 // our current buffer before we call acquireBuffer then that release call 103 // would have returned STALE_BUFFER_SLOT, and we would have called 104 // freeBufferLocked on that slot. Because the buffer slot has already 105 // been overwritten with the new buffer all we have to do is skip the 106 // releaseBuffer call and we should be in the same state we'd be in if we 107 // had released the old buffer first. 108 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && 109 item.mBuf != mCurrentBufferSlot) { 110 // Release the previous buffer. 111 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer, 112 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); 113 if (err < NO_ERROR) { 114 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err); 115 return err; 116 } 117 } 118 mCurrentBufferSlot = item.mBuf; 119 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; 120 outFence = item.mFence; 121 outBuffer = mCurrentBuffer; 122 return NO_ERROR; 123} 124 125// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl. 126void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) { 127 sp<GraphicBuffer> buf; 128 sp<Fence> acquireFence; 129 status_t err = nextBuffer(buf, acquireFence); 130 if (err != NO_ERROR) { 131 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)", 132 strerror(-err), err); 133 return; 134 } 135 err = mHwc.fbPost(mDisplayType, acquireFence, buf); 136 if (err != NO_ERROR) { 137 ALOGE("error posting framebuffer: %d", err); 138 } 139} 140 141void FramebufferSurface::freeBufferLocked(int slotIndex) { 142 ConsumerBase::freeBufferLocked(slotIndex); 143 if (slotIndex == mCurrentBufferSlot) { 144 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; 145 } 146} 147 148void FramebufferSurface::onFrameCommitted() { 149 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType); 150 if (fence->isValid() && 151 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) { 152 status_t err = addReleaseFence(mCurrentBufferSlot, 153 mCurrentBuffer, fence); 154 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)", 155 strerror(-err), err); 156 } 157} 158 159status_t FramebufferSurface::compositionComplete() 160{ 161 return mHwc.fbCompositionComplete(); 162} 163 164void FramebufferSurface::dumpAsString(String8& result) const { 165 ConsumerBase::dump(result); 166} 167 168void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const 169{ 170 mHwc.fbDump(result); 171 ConsumerBase::dumpLocked(result, prefix); 172} 173 174// ---------------------------------------------------------------------------- 175}; // namespace android 176// ---------------------------------------------------------------------------- 177