dc_buffer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * File : dc_buffer.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/rtgui.h>
  15. #include <rtgui/dc.h>
  16. #include <rtgui/dc_hw.h>
  17. #include <rtgui/color.h>
  18. #include <rtgui/rtgui_system.h>
  19. struct rtgui_dc_buffer
  20. {
  21. struct rtgui_dc parent;
  22. /* graphic context */
  23. rtgui_gc_t gc;
  24. /* width and height */
  25. rt_uint16_t width, height;
  26. rt_uint16_t pitch;
  27. /* blit info */
  28. rtgui_region_t clip;
  29. /* pixel data */
  30. rt_uint8_t* pixel;
  31. };
  32. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc);
  33. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y);
  34. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color);
  35. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
  36. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
  37. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect);
  38. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point,
  39. struct rtgui_dc* dest, rtgui_rect_t* rect);
  40. static void rtgui_dc_buffer_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc);
  41. static rtgui_gc_t* rtgui_dc_buffer_get_gc(struct rtgui_dc* dc);
  42. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc);
  43. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);
  44. const static struct rtgui_dc_engine dc_buffer_engine =
  45. {
  46. rtgui_dc_buffer_draw_point,
  47. rtgui_dc_buffer_draw_color_point,
  48. rtgui_dc_buffer_draw_vline,
  49. rtgui_dc_buffer_draw_hline,
  50. rtgui_dc_buffer_fill_rect,
  51. rtgui_dc_buffer_blit,
  52. rtgui_dc_buffer_set_gc,
  53. rtgui_dc_buffer_get_gc,
  54. rtgui_dc_buffer_get_visible,
  55. rtgui_dc_buffer_get_rect,
  56. rtgui_dc_buffer_fini,
  57. };
  58. struct rtgui_dc* rtgui_dc_buffer_create(int w, int h)
  59. {
  60. struct rtgui_dc_buffer* dc;
  61. dc = (struct rtgui_dc_buffer*)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  62. dc->parent.type = RTGUI_DC_BUFFER;
  63. dc->parent.engine = &dc_buffer_engine;
  64. dc->gc.foreground = default_foreground;
  65. dc->gc.background = default_background;
  66. dc->gc.font = rtgui_font_default();
  67. dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  68. dc->width = w;
  69. dc->height = h;
  70. dc->pitch = w * sizeof(rtgui_color_t);
  71. rtgui_region_init(&(dc->clip));
  72. dc->pixel = rtgui_malloc(h * dc->pitch);
  73. rt_memset(dc->pixel, 0, h * dc->pitch);
  74. return &(dc->parent);
  75. }
  76. rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc)
  77. {
  78. struct rtgui_dc_buffer* dc_buffer;
  79. dc_buffer = (struct rtgui_dc_buffer*)dc;
  80. return dc_buffer->pixel;
  81. }
  82. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc)
  83. {
  84. struct rtgui_dc_buffer* buffer = (struct rtgui_dc_buffer*)dc;
  85. if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
  86. rtgui_free(buffer->pixel);
  87. buffer->pixel = RT_NULL;
  88. return RT_TRUE;
  89. }
  90. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y)
  91. {
  92. rtgui_color_t* ptr;
  93. struct rtgui_dc_buffer* dc;
  94. dc = (struct rtgui_dc_buffer*)self;
  95. /* note: there is no parameter check in this function */
  96. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  97. *ptr = dc->gc.foreground;
  98. }
  99. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
  100. {
  101. rtgui_color_t* ptr;
  102. struct rtgui_dc_buffer* dc;
  103. dc = (struct rtgui_dc_buffer*)self;
  104. /* note: there is no parameter check in this function */
  105. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  106. *ptr = color;
  107. }
  108. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  109. {
  110. rtgui_color_t* ptr;
  111. register rt_base_t index;
  112. struct rtgui_dc_buffer* dc;
  113. dc = (struct rtgui_dc_buffer*)self;
  114. if (x >= dc->width) return;
  115. if (y1 > dc->height) y1 = dc->height;
  116. if (y2 > dc->height) y2 = dc->height;
  117. ptr = (rtgui_color_t*)(dc->pixel + y1 * dc->pitch + x * sizeof(rtgui_color_t));
  118. for (index = y1; index < y2; index ++)
  119. {
  120. /* draw this point */
  121. *ptr = dc->gc.foreground;
  122. ptr += dc->width;
  123. }
  124. }
  125. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  126. {
  127. rtgui_color_t* ptr;
  128. register rt_base_t index;
  129. struct rtgui_dc_buffer* dc;
  130. dc = (struct rtgui_dc_buffer*)self;
  131. if (y >= dc->height) return;
  132. if (x1 > dc->width) x1 = dc->width;
  133. if (x2 > dc->width) x2 = dc->width;
  134. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
  135. for (index = x1; index < x2; index ++)
  136. {
  137. /* draw this point */
  138. *ptr++ = dc->gc.foreground;
  139. }
  140. }
  141. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  142. {
  143. rtgui_rect_t r;
  144. struct rtgui_dc_buffer* dc;
  145. r = *rect;
  146. dc = (struct rtgui_dc_buffer*)self;
  147. if (r.x1 > dc->width) r.x1 = dc->width;
  148. if (r.x2 > dc->width) r.x2 = dc->width;
  149. if (r.y1 > dc->height) r.y1 = dc->height;
  150. if (r.y2 > dc->height) r.y2 = dc->height;
  151. /* fill first line */
  152. rtgui_dc_buffer_draw_hline(&(dc->parent), r.x1, r.x2, r.y1);
  153. /* memory copy other lines */
  154. if (r.y2 > r.y1)
  155. {
  156. register rt_base_t index;
  157. for (index = r.y1 + 1; index < r.y2; index ++)
  158. {
  159. rt_memcpy(dc->pixel + index * dc->pitch,
  160. dc->pixel + r.y1 * dc->pitch,
  161. (r.x2 - r.x1) * sizeof(rtgui_color_t));
  162. }
  163. }
  164. }
  165. /* rtgui_color to RGB323 */
  166. rt_inline void rtgui_blit_line_1(rtgui_color_t* color, rt_uint8_t* dest, int line)
  167. {
  168. struct _color {rt_uint8_t r, g, b, a;} *c;
  169. c = (struct _color*)color;
  170. while (line-- > 0)
  171. {
  172. *dest = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
  173. c ++;
  174. dest ++;
  175. }
  176. }
  177. /* rtgui_color to RGB565 */
  178. rt_inline void rtgui_blit_line_2(rtgui_color_t* color, rt_uint8_t* dest, int line)
  179. {
  180. struct _color {rt_uint8_t r, g, b, a;} *c;
  181. rt_uint16_t* ptr;
  182. c = (struct _color*)color;
  183. ptr = (rt_uint16_t*)dest;
  184. while (line-- > 0)
  185. {
  186. *ptr = ((c->r & 0xf8) << 8) | ((c->g & 0xfc) << 3) | (c->b >> 3);
  187. c ++;
  188. ptr ++;
  189. }
  190. }
  191. /* rtgui_color to RGB888 */
  192. rt_inline void rtgui_blit_line_4(rtgui_color_t* color, rt_uint8_t* dest, int line)
  193. {
  194. rt_memcpy(dest, color, line * 4);
  195. }
  196. /* blit a dc to a hardware dc */
  197. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  198. {
  199. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  200. struct rtgui_dc* hw = dest;
  201. if (dc_point == RT_NULL) dc_point = &rtgui_empty_point;
  202. if (dest->type == RTGUI_DC_HW)
  203. {
  204. rtgui_color_t* pixel;
  205. rt_uint8_t *line_ptr;
  206. rt_uint16_t rect_width, rect_height, index;
  207. void (*blit_line)(rtgui_color_t* color, rt_uint8_t* dest, int line);
  208. /* calculate correct width and height */
  209. if (rtgui_rect_width(*rect) > (dc->width - dc_point->x))
  210. rect_width = dc->width - dc_point->x;
  211. else
  212. rect_width = rtgui_rect_width(*rect);
  213. if (rtgui_rect_height(*rect) > (dc->height - dc_point->y))
  214. rect_height = dc->height - dc_point->y;
  215. else
  216. rect_height = rtgui_rect_height(*rect);
  217. /* get blit line function */
  218. switch (rtgui_graphic_driver_get_default()->byte_per_pixel)
  219. {
  220. case 1:
  221. blit_line = rtgui_blit_line_1;
  222. break;
  223. case 2:
  224. blit_line = rtgui_blit_line_2;
  225. break;
  226. case 4:
  227. blit_line = rtgui_blit_line_4;
  228. break;
  229. default:
  230. /* not support byte per pixel */
  231. return;
  232. }
  233. /* create line buffer */
  234. line_ptr = (rt_uint8_t*) rtgui_malloc(rect_width * rtgui_graphic_driver_get_default()->byte_per_pixel);
  235. /* prepare pixel line */
  236. pixel = (rtgui_color_t*)(dc->pixel + dc_point->y * dc->pitch + dc_point->x * sizeof(rtgui_color_t));
  237. /* draw each line */
  238. for (index = rect->y1; index < rect->y1 + rect_height; index ++)
  239. {
  240. /* blit on line buffer */
  241. blit_line(pixel, line_ptr, rect_width);
  242. pixel += dc->width;
  243. /* draw on hardware dc */
  244. rtgui_dc_hw_draw_raw_hline(hw, line_ptr, rect->x1, rect->x1 + rect_width, index);
  245. }
  246. /* release line buffer */
  247. rtgui_free(line_ptr);
  248. }
  249. }
  250. static void rtgui_dc_buffer_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
  251. {
  252. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  253. dc->gc = *gc;
  254. }
  255. static rtgui_gc_t *rtgui_dc_buffer_get_gc(struct rtgui_dc* self)
  256. {
  257. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  258. return &dc->gc;
  259. }
  260. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
  261. {
  262. return RT_TRUE;
  263. }
  264. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  265. {
  266. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  267. rect->x1 = rect->y1 = 0;
  268. rect->x2 = dc->width;
  269. rect->y2 = dc->height;
  270. }