dc_buffer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. rt_uint32_t clip_sync;
  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. static void rtgui_dc_buffer_init(struct rtgui_dc_buffer* dc)
  46. {
  47. if (dc == RT_NULL) return;
  48. dc->parent.type = RTGUI_DC_BUFFER;
  49. dc->parent.draw_point = rtgui_dc_buffer_draw_point;
  50. dc->parent.draw_color_point = rtgui_dc_buffer_draw_color_point;
  51. dc->parent.draw_hline = rtgui_dc_buffer_draw_hline;
  52. dc->parent.draw_vline = rtgui_dc_buffer_draw_vline;
  53. dc->parent.fill_rect = rtgui_dc_buffer_fill_rect;
  54. dc->parent.blit = rtgui_dc_buffer_blit;
  55. dc->parent.set_gc = rtgui_dc_buffer_set_gc;
  56. dc->parent.get_gc = rtgui_dc_buffer_get_gc;
  57. dc->parent.get_visible= rtgui_dc_buffer_get_visible;
  58. dc->parent.get_rect = rtgui_dc_buffer_get_rect;
  59. dc->parent.fini = rtgui_dc_buffer_fini;
  60. }
  61. struct rtgui_dc* rtgui_dc_buffer_create(int w, int h)
  62. {
  63. struct rtgui_dc_buffer* dc;
  64. dc = (struct rtgui_dc_buffer*)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  65. rtgui_dc_buffer_init(dc);
  66. dc->gc.foreground = default_foreground;
  67. dc->gc.background = default_background;
  68. dc->gc.font = rtgui_font_default();
  69. dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  70. dc->width = w;
  71. dc->height = h;
  72. dc->pitch = w * sizeof(rtgui_color_t);
  73. dc->clip_sync = 0;
  74. rtgui_region_init(&(dc->clip));
  75. dc->pixel = rtgui_malloc(h * dc->pitch);
  76. rt_memset(dc->pixel, 0, h * dc->pitch);
  77. return &(dc->parent);
  78. }
  79. rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc)
  80. {
  81. struct rtgui_dc_buffer* dc_buffer;
  82. dc_buffer = (struct rtgui_dc_buffer*)dc;
  83. return dc_buffer->pixel;
  84. }
  85. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc)
  86. {
  87. struct rtgui_dc_buffer* buffer = (struct rtgui_dc_buffer*)dc;
  88. if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
  89. rtgui_free(buffer->pixel);
  90. buffer->pixel = RT_NULL;
  91. return RT_TRUE;
  92. }
  93. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y)
  94. {
  95. rtgui_color_t* ptr;
  96. struct rtgui_dc_buffer* dc;
  97. dc = (struct rtgui_dc_buffer*)self;
  98. /* note: there is no parameter check in this function */
  99. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  100. *ptr = dc->gc.foreground;
  101. }
  102. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
  103. {
  104. rtgui_color_t* ptr;
  105. struct rtgui_dc_buffer* dc;
  106. dc = (struct rtgui_dc_buffer*)self;
  107. /* note: there is no parameter check in this function */
  108. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t));
  109. *ptr = color;
  110. }
  111. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  112. {
  113. rtgui_color_t* ptr;
  114. register rt_base_t index;
  115. struct rtgui_dc_buffer* dc;
  116. dc = (struct rtgui_dc_buffer*)self;
  117. if (x >= dc->width) return;
  118. if (y1 > dc->height) y1 = dc->height;
  119. if (y2 > dc->height) y2 = dc->height;
  120. ptr = (rtgui_color_t*)(dc->pixel + y1 * dc->pitch + x * sizeof(rtgui_color_t));
  121. for (index = y1; index < y2; index ++)
  122. {
  123. /* draw this point */
  124. *ptr = dc->gc.foreground;
  125. ptr += dc->width;
  126. }
  127. }
  128. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  129. {
  130. rtgui_color_t* ptr;
  131. register rt_base_t index;
  132. struct rtgui_dc_buffer* dc;
  133. dc = (struct rtgui_dc_buffer*)self;
  134. if (y >= dc->height) return;
  135. if (x1 > dc->width) x1 = dc->width;
  136. if (x2 > dc->width) x2 = dc->width;
  137. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
  138. for (index = x1; index < x2; index ++)
  139. {
  140. /* draw this point */
  141. *ptr++ = dc->gc.foreground;
  142. }
  143. }
  144. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  145. {
  146. rtgui_rect_t r;
  147. struct rtgui_dc_buffer* dc;
  148. r = *rect;
  149. dc = (struct rtgui_dc_buffer*)self;
  150. if (r.x1 > dc->width) r.x1 = dc->width;
  151. if (r.x2 > dc->width) r.x2 = dc->width;
  152. if (r.y1 > dc->height) r.y1 = dc->height;
  153. if (r.y2 > dc->height) r.y2 = dc->height;
  154. /* fill first line */
  155. rtgui_dc_buffer_draw_hline(&(dc->parent), r.x1, r.x2, r.y1);
  156. /* memory copy other lines */
  157. if (r.y2 > r.y1)
  158. {
  159. register rt_base_t index;
  160. for (index = r.y1 + 1; index < r.y2; index ++)
  161. {
  162. rt_memcpy(dc->pixel + index * dc->pitch,
  163. dc->pixel + r.y1 * dc->pitch,
  164. (r.x2 - r.x1) * sizeof(rtgui_color_t));
  165. }
  166. }
  167. }
  168. /* rtgui_color to RGB323 */
  169. rt_inline void rtgui_blit_line_1(rtgui_color_t* color, rt_uint8_t* dest, int line)
  170. {
  171. struct _color {rt_uint8_t r, g, b, a;} *c;
  172. c = (struct _color*)color;
  173. while (line-- > 0)
  174. {
  175. *dest = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
  176. c ++;
  177. dest ++;
  178. }
  179. }
  180. /* rtgui_color to RGB565 */
  181. rt_inline void rtgui_blit_line_2(rtgui_color_t* color, rt_uint8_t* dest, int line)
  182. {
  183. struct _color {rt_uint8_t r, g, b, a;} *c;
  184. rt_uint16_t* ptr;
  185. c = (struct _color*)color;
  186. ptr = (rt_uint16_t*)dest;
  187. while (line-- > 0)
  188. {
  189. *ptr = ((c->r & 0xf8) << 8) | ((c->g & 0xfc) << 3) | (c->b >> 3);
  190. c ++;
  191. ptr ++;
  192. }
  193. }
  194. /* rtgui_color to RGB888 */
  195. rt_inline void rtgui_blit_line_4(rtgui_color_t* color, rt_uint8_t* dest, int line)
  196. {
  197. rt_memcpy(dest, color, line * 4);
  198. }
  199. /* blit a dc to a hardware dc */
  200. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  201. {
  202. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  203. struct rtgui_dc_hw* hw = (struct rtgui_dc_hw*)dest;
  204. if (dc_point == RT_NULL) dc_point = &rtgui_empty_point;
  205. if (dest->type == RTGUI_DC_HW)
  206. {
  207. rtgui_color_t* pixel;
  208. rt_uint8_t *line_ptr;
  209. rt_uint16_t rect_width, rect_height, index;
  210. void (*blit_line)(rtgui_color_t* color, rt_uint8_t* dest, int line);
  211. /* calculate correct width and height */
  212. if (rtgui_rect_width(*rect) > (dc->width - dc_point->x))
  213. rect_width = dc->width - dc_point->x;
  214. else
  215. rect_width = rtgui_rect_width(*rect);
  216. if (rtgui_rect_height(*rect) > (dc->height - dc_point->y))
  217. rect_height = dc->height - dc_point->y;
  218. else
  219. rect_height = rtgui_rect_height(*rect);
  220. /* get blit line function */
  221. switch (hw->device->byte_per_pixel)
  222. {
  223. case 1:
  224. blit_line = rtgui_blit_line_1;
  225. break;
  226. case 2:
  227. blit_line = rtgui_blit_line_2;
  228. break;
  229. case 4:
  230. blit_line = rtgui_blit_line_4;
  231. break;
  232. default:
  233. /* not support byte per pixel */
  234. return;
  235. }
  236. /* create line buffer */
  237. line_ptr = (rt_uint8_t*) rtgui_malloc(rect_width * hw->device->byte_per_pixel);
  238. /* prepare pixel line */
  239. pixel = (rtgui_color_t*)(dc->pixel + dc_point->y * dc->pitch + dc_point->x * sizeof(rtgui_color_t));
  240. /* draw each line */
  241. for (index = rect->y1; index < rect->y1 + rect_height; index ++)
  242. {
  243. /* blit on line buffer */
  244. blit_line(pixel, line_ptr, rect_width);
  245. pixel += dc->width;
  246. /* draw on hardware dc */
  247. rtgui_dc_hw_draw_raw_hline(hw, line_ptr, rect->x1, rect->x1 + rect_width, index);
  248. }
  249. /* release line buffer */
  250. rtgui_free(line_ptr);
  251. }
  252. }
  253. static void rtgui_dc_buffer_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
  254. {
  255. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  256. dc->gc = *gc;
  257. }
  258. static rtgui_gc_t *rtgui_dc_buffer_get_gc(struct rtgui_dc* self)
  259. {
  260. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  261. return &dc->gc;
  262. }
  263. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
  264. {
  265. return RT_TRUE;
  266. }
  267. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  268. {
  269. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  270. rect->x1 = rect->y1 = 0;
  271. rect->x2 = dc->width;
  272. rect->y2 = dc->height;
  273. }