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/Surface.h> 33#include <gui/GraphicBufferAlloc.h> 34#include <ui/GraphicBuffer.h> 35 36#include "FramebufferSurface.h" 37#include "HWComposer.h" 38 39#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS 40#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2) 41#endif 42 43// ---------------------------------------------------------------------------- 44namespace android { 45// ---------------------------------------------------------------------------- 46 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 53FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp, 54 const sp<IGraphicBufferConsumer>& consumer) : 55 ConsumerBase(consumer), 56 mDisplayType(disp), 57 mCurrentBufferSlot(-1), 58 mCurrentBuffer(0), 59 mHwc(hwc) 60{ 61 mName = "FramebufferSurface"; 62 mConsumer->setConsumerName(mName); 63 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | 64 GRALLOC_USAGE_HW_RENDER | 65 GRALLOC_USAGE_HW_COMPOSER); 66 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp)); 67 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp)); 68 mConsumer->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS); 69} 70 71status_t FramebufferSurface::beginFrame() { 72 return NO_ERROR; 73} 74 75status_t FramebufferSurface::prepareFrame(CompositionType compositionType) { 76 return NO_ERROR; 77} 78 79status_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 86status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) { 87 Mutex::Autolock lock(mMutex); 88 89 BufferQueue::BufferItem item; 90 status_t err = acquireBufferLocked(&item, 0); 91 if (err == BufferQueue::NO_BUFFER_AVAILABLE) { 92 outBuffer = mCurrentBuffer; 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. 110 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer, 111 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); 112 if (err < NO_ERROR) { 113 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err); 114 return err; 115 } 116 } 117 mCurrentBufferSlot = item.mBuf; 118 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; 119 outFence = item.mFence; 120 outBuffer = mCurrentBuffer; 121 return NO_ERROR; 122} 123 124// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl. 125void FramebufferSurface::onFrameAvailable() { 126 sp<GraphicBuffer> buf; 127 sp<Fence> acquireFence; 128 status_t err = nextBuffer(buf, acquireFence); 129 if (err != NO_ERROR) { 130 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)", 131 strerror(-err), err); 132 return; 133 } 134 err = mHwc.fbPost(mDisplayType, acquireFence, buf); 135 if (err != NO_ERROR) { 136 ALOGE("error posting framebuffer: %d", err); 137 } 138} 139 140void FramebufferSurface::freeBufferLocked(int slotIndex) { 141 ConsumerBase::freeBufferLocked(slotIndex); 142 if (slotIndex == mCurrentBufferSlot) { 143 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; 144 } 145} 146 147void FramebufferSurface::onFrameCommitted() { 148 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType); 149 if (fence->isValid() && 150 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) { 151 status_t err = addReleaseFence(mCurrentBufferSlot, 152 mCurrentBuffer, fence); 153 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)", 154 strerror(-err), err); 155 } 156} 157 158status_t FramebufferSurface::compositionComplete() 159{ 160 return mHwc.fbCompositionComplete(); 161} 162 163// Since DisplaySurface and ConsumerBase both have a method with this 164// signature, results will vary based on the static pointer type the caller is 165// using: 166// void dump(FrameBufferSurface* fbs, String8& s) { 167// // calls FramebufferSurface::dump() 168// fbs->dump(s); 169// 170// // calls ConsumerBase::dump() since it is non-virtual 171// static_cast<ConsumerBase*>(fbs)->dump(s); 172// 173// // calls FramebufferSurface::dump() since it is virtual 174// static_cast<DisplaySurface*>(fbs)->dump(s); 175// } 176// To make sure that all of these end up doing the same thing, we just redirect 177// to ConsumerBase::dump() here. It will take the internal lock, and then call 178// virtual dumpLocked(), which is where the real work happens. 179void FramebufferSurface::dump(String8& result) const { 180 ConsumerBase::dump(result); 181} 182 183void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const 184{ 185 mHwc.fbDump(result); 186 ConsumerBase::dumpLocked(result, prefix); 187} 188 189// ---------------------------------------------------------------------------- 190}; // namespace android 191// ---------------------------------------------------------------------------- 192