FramebufferSurface.cpp revision db27621e22559a1b16414f890677ef04242fbc3b
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/HWComposer.h" 37 38// ---------------------------------------------------------------------------- 39namespace android { 40// ---------------------------------------------------------------------------- 41 42class GraphicBufferAlloc : public BnGraphicBufferAlloc { 43public: 44 GraphicBufferAlloc() { }; 45 virtual ~GraphicBufferAlloc() { }; 46 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, 47 PixelFormat format, uint32_t usage, status_t* error) { 48 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); 49 return graphicBuffer; 50 } 51}; 52 53 54/* 55 * This implements the (main) framebuffer management. This class is used 56 * mostly by SurfaceFlinger, but also by command line GL application. 57 * 58 */ 59 60FramebufferSurface::FramebufferSurface(HWComposer& hwc) : 61 ConsumerBase(new BufferQueue(true, new GraphicBufferAlloc())), 62 mCurrentBufferSlot(-1), 63 mCurrentBuffer(0), 64 mHwc(hwc) 65{ 66 mName = "FramebufferSurface"; 67 mBufferQueue->setConsumerName(mName); 68 mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB | 69 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER); 70 mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(HWC_DISPLAY_PRIMARY)); 71 mBufferQueue->setDefaultBufferSize(mHwc.getWidth(HWC_DISPLAY_PRIMARY), 72 mHwc.getHeight(HWC_DISPLAY_PRIMARY)); 73 mBufferQueue->setSynchronousMode(true); 74 mBufferQueue->setDefaultMaxBufferCount(NUM_FRAME_BUFFERS); 75} 76 77status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>* buffer) { 78 Mutex::Autolock lock(mMutex); 79 80 BufferQueue::BufferItem item; 81 status_t err = acquireBufferLocked(&item); 82 if (err == BufferQueue::NO_BUFFER_AVAILABLE) { 83 if (buffer != NULL) { 84 *buffer = mCurrentBuffer; 85 } 86 return NO_ERROR; 87 } else if (err != NO_ERROR) { 88 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err); 89 return err; 90 } 91 92 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot 93 // then we may have acquired the slot we already own. If we had released 94 // our current buffer before we call acquireBuffer then that release call 95 // would have returned STALE_BUFFER_SLOT, and we would have called 96 // freeBufferLocked on that slot. Because the buffer slot has already 97 // been overwritten with the new buffer all we have to do is skip the 98 // releaseBuffer call and we should be in the same state we'd be in if we 99 // had released the old buffer first. 100 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && 101 item.mBuf != mCurrentBufferSlot) { 102 // Release the previous buffer. 103 err = releaseBufferLocked(mCurrentBufferSlot, EGL_NO_DISPLAY, 104 EGL_NO_SYNC_KHR); 105 if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) { 106 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err); 107 return err; 108 } 109 } 110 111 mCurrentBufferSlot = item.mBuf; 112 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; 113 if (item.mFence != NULL) { 114 item.mFence->wait(Fence::TIMEOUT_NEVER); 115 } 116 117 if (buffer != NULL) { 118 *buffer = mCurrentBuffer; 119 } 120 121 return NO_ERROR; 122} 123 124// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl. 125void FramebufferSurface::onFrameAvailable() { 126 // XXX: The following code is here temporarily as part of the transition 127 // away from the framebuffer HAL. 128 sp<GraphicBuffer> buf; 129 status_t err = nextBuffer(&buf); 130 if (err != NO_ERROR) { 131 ALOGE("error latching next FramebufferSurface buffer: %s (%d)", 132 strerror(-err), err); 133 return; 134 } 135 err = mHwc.fbPost(buf->handle); 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 148status_t FramebufferSurface::setUpdateRectangle(const Rect& r) 149{ 150 return INVALID_OPERATION; 151} 152 153status_t FramebufferSurface::compositionComplete() 154{ 155 return mHwc.fbCompositionComplete(); 156} 157 158void FramebufferSurface::dump(String8& result) { 159 mHwc.fbDump(result); 160 ConsumerBase::dump(result); 161} 162 163// ---------------------------------------------------------------------------- 164}; // namespace android 165// ---------------------------------------------------------------------------- 166