/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. * History:
  26. * ================================================================
  27. * 2011-05-15 Han.hui <sylixos@gmail.com> created
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include "ftk_allocator.h"
  35. #include "ftk_log.h"
  36. #include <mouse.h>
  37. #include <keyboard.h>
  38. #include <sys/mman.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. #include <sys/ioctl.h>
  42. #include "ftk_display_mem.h"
  43. #include "ftk_display_sylixos.h"
  44. struct FbInfo
  45. {
  46. int fd;
  47. void* bits;
  48. LW_GM_VARINFO varinfo;
  49. LW_GM_SCRINFO scrinfo;
  50. };
  51. #define fb_width(fb) ((fb)->varinfo.GMVI_ulXRes)
  52. #define fb_height(fb) ((fb)->varinfo.GMVI_ulYRes)
  53. #define fb_bpp(fb) ((fb)->varinfo.GMVI_ulBitsPerPixel)
  54. #define fb_size(fb) (fb_width(fb) * fb_height(fb) * fb_bpp(fb) >> 3)
  55. static int gmem_open(struct FbInfo *fb, const char* fbfilename)
  56. {
  57. fb->fd = open(fbfilename, O_RDWR);
  58. if (fb->fd < 0)
  59. {
  60. return -1;
  61. }
  62. if (ioctl(fb->fd, LW_GM_GET_VARINFO, &fb->varinfo) < 0)
  63. goto fail;
  64. if (ioctl(fb->fd, LW_GM_GET_SCRINFO, &fb->scrinfo) < 0)
  65. goto fail;
  66. fb->bits = mmap(0, fb_size(fb), PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0);
  67. if (fb->bits == MAP_FAILED)
  68. {
  69. ftk_logd("map framebuffer failed.\n");
  70. goto fail;
  71. }
  72. memset(fb->bits, 0xff, fb_size(fb));
  73. return 0;
  74. fail:
  75. ftk_logd("%s is not a framebuffer.\n", fbfilename);
  76. close(fb->fd);
  77. return -1;
  78. }
  79. static void gmem_close(void *user)
  80. {
  81. struct FbInfo *fb = (struct FbInfo *)user;
  82. if (fb != NULL)
  83. {
  84. munmap(fb->bits, fb_size(fb));
  85. close(fb->fd);
  86. FTK_FREE(fb);
  87. }
  88. return;
  89. }
  90. static void gmem_sync(void* ctx, FtkRect* rect)
  91. {
  92. return;
  93. }
  94. FtkDisplay* ftk_display_sylixos_create(const char* filename)
  95. {
  96. FtkDisplay* thiz = NULL;
  97. struct FbInfo* fb = NULL;
  98. return_val_if_fail(filename != NULL, NULL);
  99. fb = FTK_ZALLOC(sizeof(struct FbInfo));
  100. return_val_if_fail(fb != NULL, NULL);
  101. if (gmem_open(fb, filename) == 0)
  102. {
  103. FtkPixelFormat format = FTK_PIXEL_NONE;
  104. int bits_per_pixel = fb_bpp(fb);
  105. if (bits_per_pixel == 16)
  106. {
  107. format = FTK_PIXEL_RGB565;
  108. }
  109. else if (bits_per_pixel == 24)
  110. {
  111. format = FTK_PIXEL_BGR24;
  112. }
  113. else if (bits_per_pixel == 32)
  114. {
  115. format = FTK_PIXEL_BGRA32;
  116. }
  117. else
  118. {
  119. assert(!"not supported framebuffer format.");
  120. }
  121. thiz = ftk_display_mem_create(format,
  122. fb_width(fb),
  123. fb_height(fb),
  124. fb->bits,
  125. gmem_close,
  126. fb);
  127. ftk_display_mem_set_sync_func(thiz, gmem_sync, fb);
  128. }
  129. return thiz;
  130. }