PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/backend/sylixos_gmem/ftk_display_sylixos.c

http://ftk.googlecode.com/
C | 153 lines | 101 code | 23 blank | 29 comment | 19 complexity | 0436e3729cb626e09ee8f3ea41f9e282 MD5 | raw file
  1/*
  2 * File: ftk_display_fb.c
  3 * Author:  Han.hui <sylixos@gmail.com>
  4 * Brief:   sylixos gmem implemented display.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2011-05-15 Han.hui <sylixos@gmail.com> created
 29 *
 30 */
 31
 32#include <stdio.h>

 33#include <stdlib.h>

 34#include <unistd.h>

 35#include <fcntl.h>

 36#include "ftk_allocator.h"

 37#include "ftk_log.h"

 38#include <mouse.h>

 39#include <keyboard.h>

 40#include <sys/mman.h>

 41#include <sys/stat.h>

 42#include <sys/types.h>

 43#include <sys/ioctl.h>

 44#include "ftk_display_mem.h"

 45#include "ftk_display_sylixos.h"

 46
 47struct FbInfo
 48{
 49    int             fd;
 50    void*           bits;
 51    LW_GM_VARINFO   varinfo;
 52    LW_GM_SCRINFO   scrinfo;
 53};
 54
 55#define fb_width(fb)    ((fb)->varinfo.GMVI_ulXRes)

 56#define fb_height(fb)   ((fb)->varinfo.GMVI_ulYRes)

 57#define fb_bpp(fb)      ((fb)->varinfo.GMVI_ulBitsPerPixel)

 58#define fb_size(fb)     (fb_width(fb) * fb_height(fb) * fb_bpp(fb) >> 3)

 59
 60static int gmem_open(struct FbInfo *fb, const char* fbfilename)
 61{
 62    fb->fd = open(fbfilename, O_RDWR);
 63    if (fb->fd < 0)
 64    {
 65        return -1;
 66    }
 67
 68    if (ioctl(fb->fd, LW_GM_GET_VARINFO, &fb->varinfo) < 0)
 69        goto fail;
 70
 71    if (ioctl(fb->fd, LW_GM_GET_SCRINFO, &fb->scrinfo) < 0)
 72        goto fail;
 73
 74    fb->bits = mmap(0, fb_size(fb), PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0);
 75    if (fb->bits == MAP_FAILED)
 76    {
 77        ftk_logd("map framebuffer failed.\n");
 78        goto fail;
 79    }
 80
 81    memset(fb->bits, 0xff, fb_size(fb));
 82
 83    return 0;
 84
 85fail:
 86    ftk_logd("%s is not a framebuffer.\n", fbfilename);
 87    close(fb->fd);
 88
 89    return -1;
 90}
 91
 92static void gmem_close(void *user)
 93{
 94    struct FbInfo *fb = (struct FbInfo *)user;
 95
 96    if (fb != NULL)
 97    {
 98        munmap(fb->bits, fb_size(fb));
 99        close(fb->fd);
100        FTK_FREE(fb);
101    }
102
103    return;
104}
105
106static void gmem_sync(void* ctx, FtkRect* rect)
107{
108    return;
109}
110
111FtkDisplay* ftk_display_sylixos_create(const char* filename)
112{
113    FtkDisplay*    thiz = NULL;
114    struct FbInfo* fb   = NULL;
115    return_val_if_fail(filename != NULL, NULL);
116
117    fb = FTK_ZALLOC(sizeof(struct FbInfo));
118    return_val_if_fail(fb != NULL, NULL);
119
120    if (gmem_open(fb, filename) == 0)
121    {
122        FtkPixelFormat format         = FTK_PIXEL_NONE;
123        int            bits_per_pixel = fb_bpp(fb);
124
125        if (bits_per_pixel == 16)
126        {
127            format = FTK_PIXEL_RGB565;
128        }
129        else if (bits_per_pixel == 24)
130        {
131            format = FTK_PIXEL_BGR24;
132        }
133        else if (bits_per_pixel == 32)
134        {
135            format = FTK_PIXEL_BGRA32;
136        }
137        else
138        {
139            assert(!"not supported framebuffer format.");
140        }
141
142        thiz = ftk_display_mem_create(format,
143                                      fb_width(fb),
144                                      fb_height(fb),
145                                      fb->bits,
146                                      gmem_close,
147                                      fb);
148
149        ftk_display_mem_set_sync_func(thiz, gmem_sync, fb);
150    }
151
152    return thiz;
153}