framebuffer_driver.c 5.4 KB

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