framebuffer_driver.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Change Logs:
  3. * Date Author Notes
  4. * 2012-01-24 onelife fix a bug in framebuffer_draw_raw_hline
  5. */
  6. #include <rtgui/rtgui_system.h>
  7. #include <rtgui/driver.h>
  8. #define GET_PIXEL(dst, x, y, type) \
  9. (type *)((rt_uint8_t*)((dst)->framebuffer) + (y) * (dst)->pitch + (x) * ((dst)->bits_per_pixel/8))
  10. static void _rgb565_set_pixel(rtgui_color_t *c, int x, int y)
  11. {
  12. *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565(*c);
  13. }
  14. static void _rgb565_get_pixel(rtgui_color_t *c, int x, int y)
  15. {
  16. rt_uint16_t pixel;
  17. pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);
  18. /* get pixel from color */
  19. *c = rtgui_color_from_565(pixel);
  20. }
  21. static void _rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  22. {
  23. rt_ubase_t index;
  24. rt_uint16_t pixel;
  25. rt_uint16_t *pixel_ptr;
  26. /* get pixel from color */
  27. pixel = rtgui_color_to_565(*c);
  28. /* get pixel pointer in framebuffer */
  29. pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
  30. for (index = x1; index < x2; index ++)
  31. {
  32. *pixel_ptr = pixel;
  33. pixel_ptr ++;
  34. }
  35. }
  36. static void _rgb565_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  37. {
  38. rt_uint8_t *dst;
  39. rt_uint16_t pixel;
  40. rt_ubase_t index;
  41. pixel = rtgui_color_to_565(*c);
  42. dst = GET_PIXEL(rtgui_graphic_get_device(), x, y1, rt_uint8_t);
  43. for (index = y1; index < y2; index ++)
  44. {
  45. *(rt_uint16_t*)dst = pixel;
  46. dst += rtgui_graphic_get_device()->pitch;
  47. }
  48. }
  49. static void _rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
  50. {
  51. *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565p(*c);
  52. }
  53. static void _rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
  54. {
  55. rt_uint16_t pixel;
  56. pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);
  57. /* get pixel from color */
  58. *c = rtgui_color_from_565p(pixel);
  59. }
  60. static void _rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  61. {
  62. rt_ubase_t index;
  63. rt_uint16_t pixel;
  64. rt_uint16_t *pixel_ptr;
  65. /* get pixel from color */
  66. pixel = rtgui_color_to_565p(*c);
  67. /* get pixel pointer in framebuffer */
  68. pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
  69. for (index = x1; index < x2; index ++)
  70. {
  71. *pixel_ptr = pixel;
  72. pixel_ptr ++;
  73. }
  74. }
  75. static void _rgb565p_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  76. {
  77. rt_uint8_t *dst;
  78. rt_uint16_t pixel;
  79. rt_ubase_t index;
  80. pixel = rtgui_color_to_565p(*c);
  81. dst = GET_PIXEL(rtgui_graphic_get_device(), x, y1, rt_uint8_t);
  82. for (index = y1; index < y2; index ++)
  83. {
  84. *(rt_uint16_t*)dst = pixel;
  85. dst += rtgui_graphic_get_device()->pitch;
  86. }
  87. }
  88. /* draw raw hline */
  89. static void framebuffer_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
  90. {
  91. rt_uint8_t *dst;
  92. dst = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint8_t);
  93. rt_memcpy(dst, pixels, (x2 - x1 + 1) * (rtgui_graphic_get_device()->bits_per_pixel/8));
  94. }
  95. const struct rtgui_graphic_driver_ops _framebuffer_rgb565_ops =
  96. {
  97. _rgb565_set_pixel,
  98. _rgb565_get_pixel,
  99. _rgb565_draw_hline,
  100. _rgb565_draw_vline,
  101. framebuffer_draw_raw_hline,
  102. };
  103. const struct rtgui_graphic_driver_ops _framebuffer_rgb565p_ops =
  104. {
  105. _rgb565p_set_pixel,
  106. _rgb565p_get_pixel,
  107. _rgb565p_draw_hline,
  108. _rgb565p_draw_vline,
  109. framebuffer_draw_raw_hline,
  110. };
  111. #define FRAMEBUFFER (rtgui_graphic_get_device()->framebuffer)
  112. #define MONO_PIXEL(framebuffer, x, y) \
  113. ((rt_uint8_t**)(framebuffer))[y/8][x]
  114. static void _mono_set_pixel(rtgui_color_t *c, int x, int y)
  115. {
  116. if (*c == white)
  117. MONO_PIXEL(FRAMEBUFFER, x, y) &= ~(1 << (y%8));
  118. else
  119. MONO_PIXEL(FRAMEBUFFER, x, y) |= (1 << (y%8));
  120. }
  121. static void _mono_get_pixel(rtgui_color_t *c, int x, int y)
  122. {
  123. if (MONO_PIXEL(FRAMEBUFFER, x, y) & (1 << (y%8)))
  124. *c = black;
  125. else
  126. *c = white;
  127. }
  128. static void _mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  129. {
  130. rt_ubase_t index;
  131. if (*c == white)
  132. for (index = x1; index < x2; index ++)
  133. {
  134. MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y%8));
  135. }
  136. else
  137. for (index = x1; index < x2; index ++)
  138. {
  139. MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y%8));
  140. }
  141. }
  142. static void _mono_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  143. {
  144. rt_ubase_t index;
  145. if (*c == white)
  146. for (index = y1; index < y2; index ++)
  147. {
  148. MONO_PIXEL(FRAMEBUFFER, x, index) &= ~(1 << (index%8));
  149. }
  150. else
  151. for (index = y1; index < y2; index ++)
  152. {
  153. MONO_PIXEL(FRAMEBUFFER, x, index) |= (1 << (index%8));
  154. }
  155. }
  156. /* draw raw hline */
  157. static void _mono_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
  158. {
  159. rt_ubase_t index;
  160. for (index = x1; index < x2; index ++)
  161. {
  162. if (pixels[index/8] && (1 << (index % 8)))
  163. MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y%8));
  164. else
  165. MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y%8));
  166. }
  167. }
  168. const struct rtgui_graphic_driver_ops _framebuffer_mono_ops =
  169. {
  170. _mono_set_pixel,
  171. _mono_get_pixel,
  172. _mono_draw_hline,
  173. _mono_draw_vline,
  174. _mono_draw_raw_hline,
  175. };
  176. const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format)
  177. {
  178. switch (pixel_format)
  179. {
  180. case RTGRAPHIC_PIXEL_FORMAT_MONO:
  181. return &_framebuffer_mono_ops;
  182. case RTGRAPHIC_PIXEL_FORMAT_GRAY4:
  183. break;
  184. case RTGRAPHIC_PIXEL_FORMAT_GRAY16:
  185. break;
  186. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  187. return &_framebuffer_rgb565_ops;
  188. case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
  189. return &_framebuffer_rgb565p_ops;
  190. }
  191. return RT_NULL;
  192. }