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