FramebufferNativeWindow.cpp revision 69029eb5abfaeb52716b84db89e32dc742551508
1/* 2** 3** Copyright 2007 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#define LOG_TAG "FramebufferNativeWindow" 19 20#include <stdlib.h> 21#include <stdio.h> 22#include <string.h> 23#include <errno.h> 24 25#include <cutils/log.h> 26#include <cutils/atomic.h> 27#include <utils/threads.h> 28 29#include <ui/SurfaceComposerClient.h> 30#include <ui/Rect.h> 31#include <ui/FramebufferNativeWindow.h> 32 33#include <EGL/egl.h> 34 35#include <pixelflinger/format.h> 36#include <pixelflinger/pixelflinger.h> 37 38#include <hardware/hardware.h> 39#include <hardware/gralloc.h> 40 41#include <private/ui/android_natives_priv.h> 42 43// ---------------------------------------------------------------------------- 44namespace android { 45// ---------------------------------------------------------------------------- 46 47class NativeBuffer 48 : public EGLNativeBase< 49 android_native_buffer_t, 50 NativeBuffer, 51 LightRefBase<NativeBuffer> > 52{ 53public: 54 NativeBuffer(int w, int h, int f, int u) : BASE() { 55 android_native_buffer_t::width = w; 56 android_native_buffer_t::height = h; 57 android_native_buffer_t::format = f; 58 android_native_buffer_t::usage = u; 59 } 60private: 61 friend class LightRefBase<NativeBuffer>; 62 ~NativeBuffer() { }; // this class cannot be overloaded 63}; 64 65 66android_native_buffer_t const* FramebufferNativeWindow::getBackbuffer() const { 67 return static_cast<android_native_buffer_t const*>(buffers[mLastDequeued].get()); 68} 69 70 71/* 72 * This implements the (main) framebuffer management. This class is used 73 * mostly by SurfaceFlinger, but also by command line GL application. 74 * 75 * In fact this is an implementation of android_native_window_t on top of 76 * the framebuffer. 77 * 78 * Currently it is pretty simple, it manages only two buffers (the front and 79 * back buffer). 80 * 81 */ 82 83FramebufferNativeWindow::FramebufferNativeWindow() 84 : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false) 85{ 86 hw_module_t const* module; 87 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { 88 int stride; 89 framebuffer_open(module, &fbDev); 90 gralloc_open(module, &grDev); 91 int err; 92 93 94 mUpdateOnDemand = (fbDev->setUpdateRect != 0); 95 96 // initialize the buffer FIFO 97 mNumBuffers = 2; 98 mNumFreeBuffers = 2; 99 mBufferHead = mNumBuffers-1; 100 buffers[0] = new NativeBuffer( 101 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); 102 buffers[1] = new NativeBuffer( 103 fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); 104 105 err = grDev->alloc(grDev, 106 fbDev->width, fbDev->height, fbDev->format, 107 GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride); 108 109 LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s", 110 fbDev->width, fbDev->height, strerror(-err)); 111 112 err = grDev->alloc(grDev, 113 fbDev->width, fbDev->height, fbDev->format, 114 GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride); 115 116 LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s", 117 fbDev->width, fbDev->height, strerror(-err)); 118 } 119 120 const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags; 121 const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi; 122 const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi; 123 const_cast<int&>(android_native_window_t::minSwapInterval) = 124 fbDev->minSwapInterval; 125 const_cast<int&>(android_native_window_t::maxSwapInterval) = 126 fbDev->maxSwapInterval; 127 128 android_native_window_t::setSwapInterval = setSwapInterval; 129 android_native_window_t::dequeueBuffer = dequeueBuffer; 130 android_native_window_t::lockBuffer = lockBuffer; 131 android_native_window_t::queueBuffer = queueBuffer; 132} 133 134FramebufferNativeWindow::~FramebufferNativeWindow() { 135 grDev->free(grDev, buffers[0]->handle); 136 grDev->free(grDev, buffers[1]->handle); 137 gralloc_close(grDev); 138 framebuffer_close(fbDev); 139} 140 141status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r) 142{ 143 if (!mUpdateOnDemand) { 144 return INVALID_OPERATION; 145 } 146 return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height()); 147} 148 149int FramebufferNativeWindow::setSwapInterval( 150 android_native_window_t* window, int interval) 151{ 152 framebuffer_device_t* fb = getSelf(window)->fbDev; 153 return fb->setSwapInterval(fb, interval); 154} 155 156int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window, 157 android_native_buffer_t** buffer) 158{ 159 FramebufferNativeWindow* self = getSelf(window); 160 Mutex::Autolock _l(self->mutex); 161 framebuffer_device_t* fb = self->fbDev; 162 163 // wait for a free buffer 164 while (!self->mNumFreeBuffers) { 165 self->mCondition.wait(self->mutex); 166 } 167 // get this buffer 168 self->mNumFreeBuffers--; 169 int index = self->mBufferHead++; 170 if (self->mBufferHead >= self->mNumBuffers) 171 self->mBufferHead = 0; 172 173 self->mLastDequeued = index; 174 *buffer = self->buffers[index].get(); 175 176 return 0; 177} 178 179int FramebufferNativeWindow::lockBuffer(android_native_window_t* window, 180 android_native_buffer_t* buffer) 181{ 182 FramebufferNativeWindow* self = getSelf(window); 183 Mutex::Autolock _l(self->mutex); 184 185 // wait that the buffer we're locking is not front anymore 186 while (self->front == buffer) { 187 self->mCondition.wait(self->mutex); 188 } 189 190 return NO_ERROR; 191} 192 193int FramebufferNativeWindow::queueBuffer(android_native_window_t* window, 194 android_native_buffer_t* buffer) 195{ 196 FramebufferNativeWindow* self = getSelf(window); 197 Mutex::Autolock _l(self->mutex); 198 framebuffer_device_t* fb = self->fbDev; 199 buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle; 200 int res = fb->post(fb, handle); 201 self->front = static_cast<NativeBuffer*>(buffer); 202 self->mNumFreeBuffers++; 203 self->mCondition.broadcast(); 204 return res; 205} 206 207// ---------------------------------------------------------------------------- 208}; // namespace android 209// ---------------------------------------------------------------------------- 210 211 212EGLNativeWindowType android_createDisplaySurface(void) 213{ 214 return new android::FramebufferNativeWindow(); 215} 216 217