FramebufferNativeWindow.cpp revision 5221271375f361b84a6eeec3d7086f223997fbb3
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#include <utils/RefBase.h>
29
30#include <ui/SurfaceComposerClient.h>
31#include <ui/Rect.h>
32#include <ui/FramebufferNativeWindow.h>
33
34#include <EGL/egl.h>
35
36#include <pixelflinger/format.h>
37#include <pixelflinger/pixelflinger.h>
38
39#include <hardware/hardware.h>
40#include <hardware/gralloc.h>
41
42#include <private/ui/android_natives_priv.h>
43
44// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
48class NativeBuffer
49    : public EGLNativeBase<
50        android_native_buffer_t,
51        NativeBuffer,
52        LightRefBase<NativeBuffer> >
53{
54public:
55    NativeBuffer(int w, int h, int f, int u) : BASE() {
56        android_native_buffer_t::width  = w;
57        android_native_buffer_t::height = h;
58        android_native_buffer_t::format = f;
59        android_native_buffer_t::usage  = u;
60    }
61private:
62    friend class LightRefBase<NativeBuffer>;
63    ~NativeBuffer() { }; // this class cannot be overloaded
64};
65
66
67/*
68 * This implements the (main) framebuffer management. This class is used
69 * mostly by SurfaceFlinger, but also by command line GL application.
70 *
71 * In fact this is an implementation of android_native_window_t on top of
72 * the framebuffer.
73 *
74 * Currently it is pretty simple, it manages only two buffers (the front and
75 * back buffer).
76 *
77 */
78
79FramebufferNativeWindow::FramebufferNativeWindow()
80    : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
81{
82    hw_module_t const* module;
83    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
84        int stride;
85        int err;
86        err = framebuffer_open(module, &fbDev);
87        LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
88
89        err = gralloc_open(module, &grDev);
90        LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
91
92        // bail out if we can't initialize the modules
93        if (!fbDev || !grDev)
94            return;
95
96        mUpdateOnDemand = (fbDev->setUpdateRect != 0);
97
98        // initialize the buffer FIFO
99        mNumBuffers = 2;
100        mNumFreeBuffers = 2;
101        mBufferHead = mNumBuffers-1;
102        buffers[0] = new NativeBuffer(
103                fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
104        buffers[1] = new NativeBuffer(
105                fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
106
107        err = grDev->alloc(grDev,
108                fbDev->width, fbDev->height, fbDev->format,
109                GRALLOC_USAGE_HW_FB, &buffers[0]->handle, &buffers[0]->stride);
110
111        LOGE_IF(err, "fb buffer 0 allocation failed w=%d, h=%d, err=%s",
112                fbDev->width, fbDev->height, strerror(-err));
113
114        err = grDev->alloc(grDev,
115                fbDev->width, fbDev->height, fbDev->format,
116                GRALLOC_USAGE_HW_FB, &buffers[1]->handle, &buffers[1]->stride);
117
118        LOGE_IF(err, "fb buffer 1 allocation failed w=%d, h=%d, err=%s",
119                fbDev->width, fbDev->height, strerror(-err));
120    }
121
122    const_cast<uint32_t&>(android_native_window_t::flags) = fbDev->flags;
123    const_cast<float&>(android_native_window_t::xdpi) = fbDev->xdpi;
124    const_cast<float&>(android_native_window_t::ydpi) = fbDev->ydpi;
125    const_cast<int&>(android_native_window_t::minSwapInterval) =
126        fbDev->minSwapInterval;
127    const_cast<int&>(android_native_window_t::maxSwapInterval) =
128        fbDev->maxSwapInterval;
129
130    android_native_window_t::setSwapInterval = setSwapInterval;
131    android_native_window_t::dequeueBuffer = dequeueBuffer;
132    android_native_window_t::lockBuffer = lockBuffer;
133    android_native_window_t::queueBuffer = queueBuffer;
134    android_native_window_t::query = query;
135    android_native_window_t::perform = perform;
136}
137
138FramebufferNativeWindow::~FramebufferNativeWindow()
139{
140    if (grDev) {
141        if (buffers[0] != NULL)
142            grDev->free(grDev, buffers[0]->handle);
143        if (buffers[1] != NULL)
144            grDev->free(grDev, buffers[1]->handle);
145        gralloc_close(grDev);
146    }
147
148    if (fbDev) {
149        framebuffer_close(fbDev);
150    }
151}
152
153status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
154{
155    if (!mUpdateOnDemand) {
156        return INVALID_OPERATION;
157    }
158    return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
159}
160
161int FramebufferNativeWindow::setSwapInterval(
162        android_native_window_t* window, int interval)
163{
164    framebuffer_device_t* fb = getSelf(window)->fbDev;
165    return fb->setSwapInterval(fb, interval);
166}
167
168int FramebufferNativeWindow::dequeueBuffer(android_native_window_t* window,
169        android_native_buffer_t** buffer)
170{
171    FramebufferNativeWindow* self = getSelf(window);
172    Mutex::Autolock _l(self->mutex);
173    framebuffer_device_t* fb = self->fbDev;
174
175    // wait for a free buffer
176    while (!self->mNumFreeBuffers) {
177        self->mCondition.wait(self->mutex);
178    }
179    // get this buffer
180    self->mNumFreeBuffers--;
181    int index = self->mBufferHead++;
182    if (self->mBufferHead >= self->mNumBuffers)
183        self->mBufferHead = 0;
184
185    *buffer = self->buffers[index].get();
186
187    return 0;
188}
189
190int FramebufferNativeWindow::lockBuffer(android_native_window_t* window,
191        android_native_buffer_t* buffer)
192{
193    FramebufferNativeWindow* self = getSelf(window);
194    Mutex::Autolock _l(self->mutex);
195
196    // wait that the buffer we're locking is not front anymore
197    while (self->front == buffer) {
198        self->mCondition.wait(self->mutex);
199    }
200
201    return NO_ERROR;
202}
203
204int FramebufferNativeWindow::queueBuffer(android_native_window_t* window,
205        android_native_buffer_t* buffer)
206{
207    FramebufferNativeWindow* self = getSelf(window);
208    Mutex::Autolock _l(self->mutex);
209    framebuffer_device_t* fb = self->fbDev;
210    buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
211    int res = fb->post(fb, handle);
212    self->front = static_cast<NativeBuffer*>(buffer);
213    self->mNumFreeBuffers++;
214    self->mCondition.broadcast();
215    return res;
216}
217
218int FramebufferNativeWindow::query(android_native_window_t* window,
219        int what, int* value)
220{
221    FramebufferNativeWindow* self = getSelf(window);
222    Mutex::Autolock _l(self->mutex);
223    framebuffer_device_t* fb = self->fbDev;
224    switch (what) {
225        case NATIVE_WINDOW_WIDTH:
226            *value = fb->width;
227            return NO_ERROR;
228        case NATIVE_WINDOW_HEIGHT:
229            *value = fb->height;
230            return NO_ERROR;
231        case NATIVE_WINDOW_FORMAT:
232            *value = fb->format;
233            return NO_ERROR;
234    }
235    *value = 0;
236    return BAD_VALUE;
237}
238
239int FramebufferNativeWindow::perform(android_native_window_t* window,
240        int operation, ...)
241{
242    switch (operation) {
243        case NATIVE_WINDOW_SET_USAGE:
244            break;
245        default:
246            return NAME_NOT_FOUND;
247    }
248    return NO_ERROR;
249}
250
251// ----------------------------------------------------------------------------
252}; // namespace android
253// ----------------------------------------------------------------------------
254
255using namespace android;
256
257EGLNativeWindowType android_createDisplaySurface(void)
258{
259    FramebufferNativeWindow* w;
260    w = new FramebufferNativeWindow();
261    if (w->getDevice() == NULL) {
262        // get a ref so it can be destroyed when we exit this block
263        sp<FramebufferNativeWindow> ref(w);
264        return NULL;
265    }
266    return (EGLNativeWindowType)w;
267}
268