dc_buffer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/blit.h>
  17. #include <rtgui/dc_hw.h>
  18. #include <rtgui/color.h>
  19. #include <rtgui/rtgui_system.h>
  20. #define hw_driver (rtgui_graphic_driver_get_default())
  21. struct rtgui_dc_buffer
  22. {
  23. struct rtgui_dc parent;
  24. /* graphic context */
  25. rtgui_gc_t gc;
  26. /* width and height */
  27. rt_uint16_t width, height;
  28. rt_uint16_t pitch;
  29. /* blit info */
  30. rtgui_region_t clip;
  31. /* pixel data */
  32. rt_uint8_t* pixel;
  33. };
  34. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc);
  35. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y);
  36. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color);
  37. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
  38. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
  39. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect);
  40. static void rtgui_dc_buffer_blit_line(struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data);
  41. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point,
  42. struct rtgui_dc* dest, rtgui_rect_t* rect);
  43. static void rtgui_dc_buffer_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc);
  44. static rtgui_gc_t* rtgui_dc_buffer_get_gc(struct rtgui_dc* dc);
  45. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc);
  46. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);
  47. const static struct rtgui_dc_engine dc_buffer_engine =
  48. {
  49. rtgui_dc_buffer_draw_point,
  50. rtgui_dc_buffer_draw_color_point,
  51. rtgui_dc_buffer_draw_vline,
  52. rtgui_dc_buffer_draw_hline,
  53. rtgui_dc_buffer_fill_rect,
  54. rtgui_dc_buffer_blit_line,
  55. rtgui_dc_buffer_blit,
  56. rtgui_dc_buffer_set_gc,
  57. rtgui_dc_buffer_get_gc,
  58. rtgui_dc_buffer_get_visible,
  59. rtgui_dc_buffer_get_rect,
  60. rtgui_dc_buffer_fini,
  61. };
  62. struct rtgui_dc* rtgui_dc_buffer_create(int w, int h)
  63. {
  64. struct rtgui_dc_buffer* dc;
  65. dc = (struct rtgui_dc_buffer*)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  66. dc->parent.type = RTGUI_DC_BUFFER;
  67. dc->parent.engine = &dc_buffer_engine;
  68. dc->gc.foreground = default_foreground;
  69. dc->gc.background = default_background;
  70. dc->gc.font = rtgui_font_default();
  71. dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  72. dc->width = w;
  73. dc->height = h;
  74. dc->pitch = w * sizeof(rtgui_color_t);
  75. rtgui_region_init(&(dc->clip));
  76. dc->pixel = rtgui_malloc(h * dc->pitch);
  77. rt_memset(dc->pixel, 0, h * dc->pitch);
  78. return &(dc->parent);
  79. }
  80. rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc)
  81. {
  82. struct rtgui_dc_buffer* dc_buffer;
  83. dc_buffer = (struct rtgui_dc_buffer*)dc;
  84. return dc_buffer->pixel;
  85. }
  86. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc)
  87. {
  88. struct rtgui_dc_buffer* buffer = (struct rtgui_dc_buffer*)dc;
  89. if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
  90. rtgui_free(buffer->pixel);
  91. buffer->pixel = RT_NULL;
  92. return RT_TRUE;
  93. }
  94. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y)
  95. {
  96. rtgui_color_t* ptr;
  97. struct rtgui_dc_buffer* dc;
  98. dc = (struct rtgui_dc_buffer*)self;
  99. /* note: there is no parameter check in this function */
  100. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  101. *ptr = dc->gc.foreground;
  102. }
  103. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
  104. {
  105. rtgui_color_t* ptr;
  106. struct rtgui_dc_buffer* dc;
  107. dc = (struct rtgui_dc_buffer*)self;
  108. /* note: there is no parameter check in this function */
  109. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  110. *ptr = color;
  111. }
  112. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  113. {
  114. rtgui_color_t* ptr;
  115. register rt_base_t index;
  116. struct rtgui_dc_buffer* dc;
  117. dc = (struct rtgui_dc_buffer*)self;
  118. if (x >= dc->width) return;
  119. if (y1 > dc->height) y1 = dc->height;
  120. if (y2 > dc->height) y2 = dc->height;
  121. ptr = (rtgui_color_t*)(dc->pixel + y1 * dc->pitch + x * sizeof(rtgui_color_t));
  122. for (index = y1; index < y2; index ++)
  123. {
  124. /* draw this point */
  125. *ptr = dc->gc.foreground;
  126. ptr += dc->width;
  127. }
  128. }
  129. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  130. {
  131. rtgui_color_t* ptr;
  132. register rt_base_t index;
  133. struct rtgui_dc_buffer* dc;
  134. dc = (struct rtgui_dc_buffer*)self;
  135. if (y >= dc->height) return;
  136. if (x1 > dc->width) x1 = dc->width;
  137. if (x2 > dc->width) x2 = dc->width;
  138. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
  139. for (index = x1; index < x2; index ++)
  140. {
  141. /* draw this point */
  142. *ptr++ = dc->gc.foreground;
  143. }
  144. }
  145. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  146. {
  147. rtgui_rect_t r;
  148. struct rtgui_dc_buffer* dc;
  149. r = *rect;
  150. dc = (struct rtgui_dc_buffer*)self;
  151. if (r.x1 > dc->width) r.x1 = dc->width;
  152. if (r.x2 > dc->width) r.x2 = dc->width;
  153. if (r.y1 > dc->height) r.y1 = dc->height;
  154. if (r.y2 > dc->height) r.y2 = dc->height;
  155. /* fill first line */
  156. rtgui_dc_buffer_draw_hline(&(dc->parent), r.x1, r.x2, r.y1);
  157. /* memory copy other lines */
  158. if (r.y2 > r.y1)
  159. {
  160. register rt_base_t index;
  161. for (index = r.y1 + 1; index < r.y2; index ++)
  162. {
  163. rt_memcpy(dc->pixel + index * dc->pitch,
  164. dc->pixel + r.y1 * dc->pitch,
  165. (r.x2 - r.x1) * sizeof(rtgui_color_t));
  166. }
  167. }
  168. }
  169. /* blit a dc to a hardware dc */
  170. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  171. {
  172. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  173. struct rtgui_dc* hw = dest;
  174. if (dc_point == RT_NULL) dc_point = &rtgui_empty_point;
  175. if (rtgui_dc_get_visible(dest) == RT_FALSE) return;
  176. if ((dest->type == RTGUI_DC_HW) || (dest->type == RTGUI_DC_CLIENT))
  177. {
  178. rt_uint8_t *line_ptr, *pixels;
  179. rt_uint16_t rect_width, rect_height, index, pitch;
  180. rtgui_blit_line_func blit_line;
  181. /* calculate correct width and height */
  182. if (rtgui_rect_width(*rect) > (dc->width - dc_point->x))
  183. rect_width = dc->width - dc_point->x;
  184. else
  185. rect_width = rtgui_rect_width(*rect);
  186. if (rtgui_rect_height(*rect) > (dc->height - dc_point->y))
  187. rect_height = dc->height - dc_point->y;
  188. else
  189. rect_height = rtgui_rect_height(*rect);
  190. /* prepare pixel line */
  191. pixels = dc->pixel + dc_point->y * dc->pitch + dc_point->x * sizeof(rtgui_color_t);
  192. if (hw_driver->byte_per_pixel == sizeof(rtgui_color_t))
  193. {
  194. /* it's the same byte per pixel, draw it directly */
  195. for (index = rect->y1; index < rect->y1 + rect_height; index++)
  196. {
  197. dest->engine->blit_line(dest, rect->x1, rect->x1 + rect_width, index, pixels);
  198. pixels += dc->width * sizeof(rtgui_color_t);
  199. }
  200. }
  201. else
  202. {
  203. /* get blit line function */
  204. blit_line = rtgui_blit_line_get(hw_driver->byte_per_pixel, 4);
  205. /* calculate pitch */
  206. pitch = rect_width * sizeof(rtgui_color_t);
  207. /* create line buffer */
  208. line_ptr = (rt_uint8_t*) rtgui_malloc(rect_width * hw_driver->byte_per_pixel);
  209. /* draw each line */
  210. for (index = rect->y1; index < rect->y1 + rect_height; index ++)
  211. {
  212. /* blit on line buffer */
  213. blit_line(line_ptr, (rt_uint8_t*)pixels, pitch);
  214. pixels += dc->width * sizeof(rtgui_color_t);
  215. /* draw on hardware dc */
  216. dest->engine->blit_line(dest, rect->x1, rect->x1 + rect_width, index, line_ptr);
  217. }
  218. /* release line buffer */
  219. rtgui_free(line_ptr);
  220. }
  221. }
  222. }
  223. static void rtgui_dc_buffer_blit_line(struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data)
  224. {
  225. rtgui_color_t* color_ptr;
  226. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  227. RT_ASSERT(dc != RT_NULL);
  228. RT_ASSERT(line_data != RT_NULL);
  229. /* out of range */
  230. if ((x1 > dc->width) || (y > dc->height)) return;
  231. /* check range */
  232. if (x2 > dc->width) x2 = dc->width;
  233. color_ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
  234. rt_memcpy(color_ptr, line_data, (x2 - x1) * sizeof(rtgui_color_t));
  235. }
  236. static void rtgui_dc_buffer_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
  237. {
  238. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  239. dc->gc = *gc;
  240. }
  241. static rtgui_gc_t *rtgui_dc_buffer_get_gc(struct rtgui_dc* self)
  242. {
  243. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  244. return &dc->gc;
  245. }
  246. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
  247. {
  248. return RT_TRUE;
  249. }
  250. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  251. {
  252. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  253. rect->x1 = rect->y1 = 0;
  254. rect->x2 = dc->width;
  255. rect->y2 = dc->height;
  256. }