FramebufferNativeWindow.cpp revision af1097658321bfdfaf0cc65214a94bfcdee372ce
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/Rect.h>
31#include <ui/FramebufferNativeWindow.h>
32#include <ui/GraphicLog.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 ANativeWindow 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        int i;
87        err = framebuffer_open(module, &fbDev);
88        LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
89
90        err = gralloc_open(module, &grDev);
91        LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
92
93        // bail out if we can't initialize the modules
94        if (!fbDev || !grDev)
95            return;
96
97        mUpdateOnDemand = (fbDev->setUpdateRect != 0);
98
99        // initialize the buffer FIFO
100        mNumBuffers = NUM_FRAME_BUFFERS;
101        mNumFreeBuffers = NUM_FRAME_BUFFERS;
102        mBufferHead = mNumBuffers-1;
103
104        for (i = 0; i < mNumBuffers; i++)
105        {
106                buffers[i] = new NativeBuffer(
107                        fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
108        }
109
110        for (i = 0; i < mNumBuffers; i++)
111        {
112                err = grDev->alloc(grDev,
113                        fbDev->width, fbDev->height, fbDev->format,
114                        GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
115
116                LOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
117                        i, fbDev->width, fbDev->height, strerror(-err));
118
119                if (err)
120                {
121                        mNumBuffers = i;
122                        mNumFreeBuffers = i;
123                        mBufferHead = mNumBuffers-1;
124                        break;
125                }
126        }
127
128        const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
129        const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
130        const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
131        const_cast<int&>(ANativeWindow::minSwapInterval) =
132            fbDev->minSwapInterval;
133        const_cast<int&>(ANativeWindow::maxSwapInterval) =
134            fbDev->maxSwapInterval;
135    } else {
136        LOGE("Couldn't get gralloc module");
137    }
138
139    ANativeWindow::setSwapInterval = setSwapInterval;
140    ANativeWindow::dequeueBuffer = dequeueBuffer;
141    ANativeWindow::lockBuffer = lockBuffer;
142    ANativeWindow::queueBuffer = queueBuffer;
143    ANativeWindow::query = query;
144    ANativeWindow::perform = perform;
145}
146
147FramebufferNativeWindow::~FramebufferNativeWindow()
148{
149    if (grDev) {
150        if (buffers[0] != NULL)
151            grDev->free(grDev, buffers[0]->handle);
152        if (buffers[1] != NULL)
153            grDev->free(grDev, buffers[1]->handle);
154        gralloc_close(grDev);
155    }
156
157    if (fbDev) {
158        framebuffer_close(fbDev);
159    }
160}
161
162status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
163{
164    if (!mUpdateOnDemand) {
165        return INVALID_OPERATION;
166    }
167    return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
168}
169
170status_t FramebufferNativeWindow::compositionComplete()
171{
172    if (fbDev->compositionComplete) {
173        return fbDev->compositionComplete(fbDev);
174    }
175    return INVALID_OPERATION;
176}
177
178int FramebufferNativeWindow::setSwapInterval(
179        ANativeWindow* window, int interval)
180{
181    framebuffer_device_t* fb = getSelf(window)->fbDev;
182    return fb->setSwapInterval(fb, interval);
183}
184
185// only for debugging / logging
186int FramebufferNativeWindow::getCurrentBufferIndex() const
187{
188    Mutex::Autolock _l(mutex);
189    const int index = mCurrentBufferIndex;
190    return index;
191}
192
193int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* 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
200    int index = self->mBufferHead++;
201    if (self->mBufferHead >= self->mNumBuffers)
202        self->mBufferHead = 0;
203
204    GraphicLog& logger(GraphicLog::getInstance());
205    logger.log(GraphicLog::SF_FB_DEQUEUE_BEFORE, index);
206
207    // wait for a free buffer
208    while (!self->mNumFreeBuffers) {
209        self->mCondition.wait(self->mutex);
210    }
211    // get this buffer
212    self->mNumFreeBuffers--;
213    self->mCurrentBufferIndex = index;
214
215    *buffer = self->buffers[index].get();
216
217    logger.log(GraphicLog::SF_FB_DEQUEUE_AFTER, index);
218    return 0;
219}
220
221int FramebufferNativeWindow::lockBuffer(ANativeWindow* window,
222        android_native_buffer_t* buffer)
223{
224    FramebufferNativeWindow* self = getSelf(window);
225    Mutex::Autolock _l(self->mutex);
226
227    const int index = self->mCurrentBufferIndex;
228    GraphicLog& logger(GraphicLog::getInstance());
229    logger.log(GraphicLog::SF_FB_LOCK_BEFORE, index);
230
231    // wait that the buffer we're locking is not front anymore
232    while (self->front == buffer) {
233        self->mCondition.wait(self->mutex);
234    }
235
236    logger.log(GraphicLog::SF_FB_LOCK_AFTER, index);
237
238    return NO_ERROR;
239}
240
241int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
242        android_native_buffer_t* buffer)
243{
244    FramebufferNativeWindow* self = getSelf(window);
245    Mutex::Autolock _l(self->mutex);
246    framebuffer_device_t* fb = self->fbDev;
247    buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
248
249    const int index = self->mCurrentBufferIndex;
250    GraphicLog& logger(GraphicLog::getInstance());
251    logger.log(GraphicLog::SF_FB_POST_BEFORE, index);
252
253    int res = fb->post(fb, handle);
254
255    logger.log(GraphicLog::SF_FB_POST_AFTER, index);
256
257    self->front = static_cast<NativeBuffer*>(buffer);
258    self->mNumFreeBuffers++;
259    self->mCondition.broadcast();
260    return res;
261}
262
263int FramebufferNativeWindow::query(ANativeWindow* window,
264        int what, int* value)
265{
266    FramebufferNativeWindow* self = getSelf(window);
267    Mutex::Autolock _l(self->mutex);
268    framebuffer_device_t* fb = self->fbDev;
269    switch (what) {
270        case NATIVE_WINDOW_WIDTH:
271            *value = fb->width;
272            return NO_ERROR;
273        case NATIVE_WINDOW_HEIGHT:
274            *value = fb->height;
275            return NO_ERROR;
276        case NATIVE_WINDOW_FORMAT:
277            *value = fb->format;
278            return NO_ERROR;
279    }
280    *value = 0;
281    return BAD_VALUE;
282}
283
284int FramebufferNativeWindow::perform(ANativeWindow* window,
285        int operation, ...)
286{
287    switch (operation) {
288        case NATIVE_WINDOW_SET_USAGE:
289        case NATIVE_WINDOW_CONNECT:
290        case NATIVE_WINDOW_DISCONNECT:
291            break;
292        default:
293            return NAME_NOT_FOUND;
294    }
295    return NO_ERROR;
296}
297
298// ----------------------------------------------------------------------------
299}; // namespace android
300// ----------------------------------------------------------------------------
301
302using namespace android;
303
304EGLNativeWindowType android_createDisplaySurface(void)
305{
306    FramebufferNativeWindow* w;
307    w = new FramebufferNativeWindow();
308    if (w->getDevice() == NULL) {
309        // get a ref so it can be destroyed when we exit this block
310        sp<FramebufferNativeWindow> ref(w);
311        return NULL;
312    }
313    return (EGLNativeWindowType)w;
314}
315