summaryrefslogtreecommitdiffstats
path: root/exynos4/hal/libgralloc_ump/framebuffer_device.cpp
blob: 814e6689b4616fc3f851ae16ef076318e51cd0b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Copyright (C) 2010 ARM Limited. All rights reserved.
 *
 * Portions of this code have been modified from the original.
 * These modifications are:
 *    * includes
 *    * enums
 *    * fb_set_swap_interval()
 *    * fb_post()
 *    * init_frame_buffer_locked()
 *    * init_frame_buffer()
 *    * fb_close()
 *    * framebuffer_device_open()
 *
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <stdlib.h>

#include <cutils/log.h>
#include <cutils/atomic.h>
#include <cutils/properties.h>
#include <hardware/hardware.h>
#include <hardware/gralloc.h>

#include <GLES/gl.h>

#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
#include "gralloc_vsync_report.h"
#endif

#include "gralloc_priv.h"
#include "gralloc_helper.h"

#include "linux/fb.h"

/* numbers of buffers for page flipping */
#define NUM_BUFFERS 2

enum {
    PAGE_FLIP = 0x00000001,
};

static int fb_set_swap_interval(struct framebuffer_device_t* dev, int interval)
{
    if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
        return -EINVAL;

    /* Currently not implemented */
    return 0;
}

static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
{
    if (private_handle_t::validate(buffer) < 0)
        return -EINVAL;

    private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);

    if (m->currentBuffer) {
        m->base.unlock(&m->base, m->currentBuffer);
        m->currentBuffer = 0;
    }

    if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
        m->base.lock(&m->base, buffer, private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
                0, 0, m->info.xres, m->info.yres, NULL);

        const size_t offset = hnd->base - m->framebuffer->base;
        int interrupt;
        m->info.activate = FB_ACTIVATE_VBL;
        m->info.yoffset = offset / m->finfo.line_length;

#ifdef STANDARD_LINUX_SCREEN
#define FBIO_WAITFORVSYNC       _IOW('F', 0x20, __u32)
#define S3CFB_SET_VSYNC_INT     _IOW('F', 206, unsigned int)
        if (ioctl(m->framebuffer->fd, FBIOPAN_DISPLAY, &m->info) == -1) {
            ALOGE("FBIOPAN_DISPLAY failed");
            m->base.unlock(&m->base, buffer);
            return 0;
        }

        if (m->enableVSync) {
            /* enable VSYNC */
            interrupt = 1;
            if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0) {
                ALOGE("S3CFB_SET_VSYNC_INT enable failed");
                return 0;
            }
            /* wait for VSYNC */

#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
            gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT);
#endif
            int crtc;
            crtc = 0;
            if (ioctl(m->framebuffer->fd, FBIO_WAITFORVSYNC, &crtc) < 0) {
                ALOGE("FBIO_WAITFORVSYNC failed");
#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
                gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
#endif
                return 0;
            }
#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
            gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
#endif
            // disable VSYNC
            interrupt = 0;
            if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0) {
                ALOGE("S3CFB_SET_VSYNC_INT disable failed");
                return 0;
            }
        }
#else
        /*Standard Android way*/
#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
        gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT);
#endif
        if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
            ALOGE("FBIOPUT_VSCREENINFO failed");
#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
            gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
#endif
            m->base.unlock(&m->base, buffer);
            return -errno;
        }
#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
        gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
#endif
#endif

        m->currentBuffer = buffer;
    } else {
        /*
         * If we can't do the page_flip, just copy the buffer to the front
         * FIXME: use copybit HAL instead of memcpy
         */
        void* fb_vaddr;
        void* buffer_vaddr;

        m->base.lock(&m->base, m->framebuffer,  GRALLOC_USAGE_SW_WRITE_RARELY,
                     0, 0, m->info.xres, m->info.yres, &fb_vaddr);

        m->base.lock(&m->base, buffer,  GRALLOC_USAGE_SW_READ_RARELY,
                     0, 0, m->info.xres, m->info.yres, &buffer_vaddr);

        memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);

        m->base.unlock(&m->base, buffer);
        m->base.unlock(&m->base, m->framebuffer);
    }
    return 0;
}

int init_frame_buffer_locked(struct private_module_t* module)
{
    /* Nothing to do, already initialized */
    if (module->framebuffer)
        return 0;

    char const * const device_template[] = {
        "/dev/graphics/fb%u",
        "/dev/fb%u",
        NULL
    };

    int fd = -1;
    int i = 0;
    char name[64];

    while ((fd == -1) && device_template[i]) {
        snprintf(name, 64, device_template[i], 0);
        fd = open(name, O_RDWR, 0);
        i++;
    }

    if (fd < 0)
        return -errno;

    struct fb_fix_screeninfo finfo;
    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
        return -errno;

    struct fb_var_screeninfo info;
    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
        return -errno;

    info.reserved[0] = 0;
    info.reserved[1] = 0;
    info.reserved[2] = 0;
    info.xoffset = 0;
    info.yoffset = 0;
    info.activate = FB_ACTIVATE_NOW;

#ifdef GRALLOC_16_BITS
    /*
     * Explicitly request 5/6/5
     */
    info.bits_per_pixel = 16;
    info.red.offset     = 11;
    info.red.length     = 5;
    info.green.offset   = 5;
    info.green.length   = 6;
    info.blue.offset    = 0;
    info.blue.length    = 5;
    info.transp.offset  = 0;
    info.transp.length  = 0;
#else
    /*
     * Explicitly request 8/8/8
     */
    info.bits_per_pixel = 32;
    info.red.offset     = 16;
    info.red.length     = 8;
    info.green.offset   = 8;
    info.green.length   = 8;
    info.blue.offset    = 0;
    info.blue.length    = 8;
    info.transp.offset  = 0;
    info.transp.length  = 0;
#endif

    /*
     * Request NUM_BUFFERS screens (at lest 2 for page flipping)
     */
    info.yres_virtual = info.yres * NUM_BUFFERS;

    uint32_t flags = PAGE_FLIP;
    if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
        info.yres_virtual = info.yres;
        flags &= ~PAGE_FLIP;
        ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
    }

    if (info.yres_virtual < info.yres * 2) {
        // we need at least 2 for page-flipping
        info.yres_virtual = info.yres;
        flags &= ~PAGE_FLIP;
        ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
                info.yres_virtual, info.yres * 2);
    }

    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
        return -errno;

    int refreshRate = 1000000000000000LLU /
    (
        uint64_t( info.upper_margin + info.lower_margin + info.vsync_len + info.yres )
        * ( info.left_margin  + info.right_margin + info.hsync_len + info.xres )
        * info.pixclock
    );

    if (refreshRate == 0)
        refreshRate = 60 * 1000;  /* 60 Hz */

    if (int(info.width) <= 0 || int(info.height) <= 0) {
        /* the driver doesn't return that information. default to 160 dpi */
        info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
        info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
    }

    float xdpi = (info.xres * 25.4f) / info.width;
    float ydpi = (info.yres * 25.4f) / info.height;
    float fps  = refreshRate / 1000.0f;

    ALOGI("using (fd=%d)\n"
         "id           = %s\n"
         "xres         = %d px\n"
         "yres         = %d px\n"
         "xres_virtual = %d px\n"
         "yres_virtual = %d px\n"
         "bpp          = %d\n"
         "r            = %2u:%u\n"
         "g            = %2u:%u\n"
         "b            = %2u:%u\n",
         fd,
         finfo.id,
         info.xres,
         info.yres,
         info.xres_virtual,
         info.yres_virtual,
         info.bits_per_pixel,
         info.red.offset, info.red.length,
         info.green.offset, info.green.length,
         info.blue.offset, info.blue.length);

    ALOGI("width        = %d mm (%f dpi)\n"
         "height       = %d mm (%f dpi)\n"
         "refresh rate = %.2f Hz\n",
         info.width,  xdpi,
         info.height, ydpi,
         fps);

    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
        return -errno;

    if (finfo.smem_len <= 0)
        return -errno;

    module->flags = flags;
    module->info = info;
    module->finfo = finfo;
    module->xdpi = xdpi;
    module->ydpi = ydpi;
    module->fps = fps;

    char value[PROPERTY_VALUE_MAX];
    property_get("debug.gralloc.vsync", value, "1");
    module->enableVSync = atoi(value);
    /*
     * map the framebuffer
     */
    size_t fbSize = round_up_to_page_size(finfo.line_length * info.yres_virtual);
    void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (vaddr == MAP_FAILED) {
        ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
        return -errno;
    }

    memset(vaddr, 0, fbSize);

    /*
     * Create a "fake" buffer object for the entire frame buffer memory,
     * and store it in the module
     */
    module->framebuffer = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
            fbSize, intptr_t(vaddr), 0, dup(fd), 0);

    module->numBuffers = info.yres_virtual / info.yres;
    module->bufferMask = 0;

    return 0;
}

int enableScreen(struct framebuffer_device_t* dev, int enable)
{
    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);

    if (enable == 1) {
        if (ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK) < 0) {
            ALOGE("%s: FBIOBLANK failed : (%d:%s)",
                    __func__, m->framebuffer->fd, strerror(errno));
            return -EINVAL;
        }
    } else if (enable == 0) {
        if (ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) {
            ALOGE("%s: FBIOBLANK failed : (%d:%s)",
                    __func__, m->framebuffer->fd, strerror(errno));
            return -EINVAL;
        }
    } else {
        return -EINVAL;
    }
    return 0;
}
static int init_frame_buffer(struct private_module_t* module)
{
    pthread_mutex_lock(&module->lock);
    int err = init_frame_buffer_locked(module);
    pthread_mutex_unlock(&module->lock);
    return err;
}

static int fb_close(struct hw_device_t *device)
{
    framebuffer_device_t* dev = reinterpret_cast<framebuffer_device_t*>(device);
    if (dev) {
        ump_close();
        delete dev;
    }
    return 0;
}

int compositionComplete(struct framebuffer_device_t* dev)
{
#ifndef HWC_HWOVERLAY
    unsigned char pixels[4];
    /* By doing a readpixel here we force the GL driver to start rendering
       all the drawcalls up to this point, and to wait for the rendering to be complete.
       Readpixel() also reads a dummy pixel, but this is not used. We only use this
       function here to flush the render pipeline. */
    glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    /* The rendering of the backbuffer is now completed.
       When SurfaceFlinger later does a call to eglSwapBuffer(), the swap will be done
       synchronously in the same thread, and not asynchronoulsy in a background thread later.
       The SurfaceFlinger requires this behaviour since it releases the lock on all the
       SourceBuffers (Layers) after the compositionComplete() function returns.
       However this "bad" behaviour by SurfaceFlinger should not affect performance,
       since the Applications that render the SourceBuffers (Layers) still get the
       full renderpipeline using asynchronouls rendering. So they perform at maximum speed,
       and because of their complexity compared to the Surface flinger jobs, the Surface flinger
       is normally faster even if it does everyhing synchronous and serial.
       */
#endif
    return 0;
}

int framebuffer_device_open(hw_module_t const* module, const char* name, hw_device_t** device)
{
    int status = -EINVAL;

    alloc_device_t* gralloc_device;
    status = gralloc_open(module, &gralloc_device);
    if (status < 0) {
        ALOGE("Fail to Open gralloc device");
        return status;
    }

    /* initialize our state here */
    framebuffer_device_t *dev = (framebuffer_device_t *)malloc(sizeof(framebuffer_device_t));
    if (dev == NULL) {
        ALOGE("Failed to allocate memory for dev");
        gralloc_close(gralloc_device);
        return status;
    }    

    private_module_t* m = (private_module_t*)module;
    status = init_frame_buffer(m);
    if (status < 0) {
        ALOGE("Fail to init framebuffer");
        free(dev);
        gralloc_close(gralloc_device);
        return status;
    }

    memset(dev, 0, sizeof(*dev));

    /* initialize the procs */
    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = 0;
    dev->common.module = const_cast<hw_module_t*>(module);
    dev->common.close = fb_close;
    dev->setSwapInterval = fb_set_swap_interval;
    dev->post = fb_post;
    dev->setUpdateRect = 0;
    dev->compositionComplete = &compositionComplete;
    dev->enableScreen = &enableScreen;

    int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
    const_cast<uint32_t&>(dev->flags) = 0;
    const_cast<uint32_t&>(dev->width) = m->info.xres;
    const_cast<uint32_t&>(dev->height) = m->info.yres;
    const_cast<int&>(dev->stride) = stride;
#ifdef GRALLOC_16_BITS
    const_cast<int&>(dev->format) = HAL_PIXEL_FORMAT_RGB_565;
#else
    const_cast<int&>(dev->format) = HAL_PIXEL_FORMAT_BGRA_8888;
#endif
    const_cast<float&>(dev->xdpi) = m->xdpi;
    const_cast<float&>(dev->ydpi) = m->ydpi;
    const_cast<float&>(dev->fps) = m->fps;
    const_cast<int&>(dev->minSwapInterval) = 1;
    const_cast<int&>(dev->maxSwapInterval) = 1;
    *device = &dev->common;
    status = 0;

    return status;
}