FramebufferSurface.cpp revision f33e4b6f13bc3ee2d2a4e1abd1ada171c70d3492
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/SurfaceTextureClient.h> 33#include <ui/GraphicBuffer.h> 34 35#include "DisplayHardware/FramebufferSurface.h" 36#include "DisplayHardware/GraphicBufferAlloc.h" 37#include "DisplayHardware/HWComposer.h" 38 39// ---------------------------------------------------------------------------- 40namespace android { 41// ---------------------------------------------------------------------------- 42 43/* 44 * This implements the (main) framebuffer management. This class is used 45 * mostly by SurfaceFlinger, but also by command line GL application. 46 * 47 */ 48 49FramebufferSurface::FramebufferSurface(HWComposer& hwc) : 50 ConsumerBase(new BufferQueue(true, new GraphicBufferAlloc())), 51 mCurrentBufferSlot(-1), 52 mCurrentBuffer(0), 53 mHwc(hwc) 54{ 55 mName = "FramebufferSurface"; 56 mBufferQueue->setConsumerName(mName); 57 mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | 58 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER); 59 mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(HWC_DISPLAY_PRIMARY)); 60 mBufferQueue->setDefaultBufferSize(mHwc.getWidth(HWC_DISPLAY_PRIMARY), 61 mHwc.getHeight(HWC_DISPLAY_PRIMARY)); 62 mBufferQueue->setSynchronousMode(true); 63 mBufferQueue->setDefaultMaxBufferCount(NUM_FRAME_BUFFERS); 64} 65 66status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) { 67 Mutex::Autolock lock(mMutex); 68 69 BufferQueue::BufferItem item; 70 status_t err = acquireBufferLocked(&item); 71 if (err == BufferQueue::NO_BUFFER_AVAILABLE) { 72 outBuffer = mCurrentBuffer; 73 return NO_ERROR; 74 } else if (err != NO_ERROR) { 75 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); 76 return err; 77 } 78 79 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot 80 // then we may have acquired the slot we already own. If we had released 81 // our current buffer before we call acquireBuffer then that release call 82 // would have returned STALE_BUFFER_SLOT, and we would have called 83 // freeBufferLocked on that slot. Because the buffer slot has already 84 // been overwritten with the new buffer all we have to do is skip the 85 // releaseBuffer call and we should be in the same state we'd be in if we 86 // had released the old buffer first. 87 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && 88 item.mBuf != mCurrentBufferSlot) { 89 // Release the previous buffer. 90 err = releaseBufferLocked(mCurrentBufferSlot, EGL_NO_DISPLAY, 91 EGL_NO_SYNC_KHR); 92 if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) { 93 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err); 94 return err; 95 } 96 } 97 mCurrentBufferSlot = item.mBuf; 98 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; 99 outFence = item.mFence; 100 outBuffer = mCurrentBuffer; 101 return NO_ERROR; 102} 103 104// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl. 105void FramebufferSurface::onFrameAvailable() { 106 sp<GraphicBuffer> buf; 107 sp<Fence> acquireFence; 108 status_t err = nextBuffer(buf, acquireFence); 109 if (err != NO_ERROR) { 110 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)", 111 strerror(-err), err); 112 return; 113 } 114 err = mHwc.fbPost(0, acquireFence, buf); // FIXME: use real display id 115 if (err != NO_ERROR) { 116 ALOGE("error posting framebuffer: %d", err); 117 } 118} 119 120void FramebufferSurface::freeBufferLocked(int slotIndex) { 121 ConsumerBase::freeBufferLocked(slotIndex); 122 if (slotIndex == mCurrentBufferSlot) { 123 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT; 124 } 125} 126 127status_t FramebufferSurface::setReleaseFenceFd(int fenceFd) { 128 status_t err = NO_ERROR; 129 if (fenceFd >= 0) { 130 sp<Fence> fence(new Fence(fenceFd)); 131 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) { 132 status_t err = addReleaseFence(mCurrentBufferSlot, fence); 133 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)", 134 strerror(-err), err); 135 } 136 } 137 return err; 138} 139 140status_t FramebufferSurface::setUpdateRectangle(const Rect& r) 141{ 142 return INVALID_OPERATION; 143} 144 145status_t FramebufferSurface::compositionComplete() 146{ 147 return mHwc.fbCompositionComplete(); 148} 149 150void FramebufferSurface::dump(String8& result) { 151 mHwc.fbDump(result); 152 ConsumerBase::dump(result); 153} 154 155// ---------------------------------------------------------------------------- 156}; // namespace android 157// ---------------------------------------------------------------------------- 158