dc_buffer.c 7.7 KB

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