dc_buffer.c 8.9 KB

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