1
0

dc_buffer.c 8.8 KB

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