dc_buffer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/color.h>
  17. #include <rtgui/rtgui_system.h>
  18. struct rtgui_dc_buffer
  19. {
  20. struct rtgui_dc parent;
  21. /* color and font */
  22. rtgui_color_t color;
  23. struct rtgui_font* font;
  24. /* text align */
  25. rt_int32_t align;
  26. /* width and height */
  27. rt_uint16_t width, height;
  28. rt_uint16_t pitch;
  29. /* blit info */
  30. rt_uint32_t clip_sync;
  31. rtgui_region_t clip;
  32. /* pixel data */
  33. rt_uint8_t* pixel;
  34. };
  35. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc);
  36. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y);
  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(struct rtgui_dc* self, struct rtgui_point* dc_point,
  41. struct rtgui_dc* dest, rtgui_rect_t* rect);
  42. static void rtgui_dc_buffer_set_color (struct rtgui_dc* dc, rtgui_color_t color);
  43. static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* dc);
  44. static void rtgui_dc_buffer_set_font(struct rtgui_dc* dc, rtgui_font_t* font);
  45. static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* dc);
  46. static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* dc, rt_int32_t textalign);
  47. static rt_int32_t rtgui_dc_buffer_get_textalign(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_hline = rtgui_dc_buffer_draw_hline;
  56. dc->parent.draw_vline = rtgui_dc_buffer_draw_vline;
  57. dc->parent.fill_rect = rtgui_dc_buffer_fill_rect;
  58. dc->parent.blit = rtgui_dc_buffer_blit;
  59. dc->parent.set_color = rtgui_dc_buffer_set_color;
  60. dc->parent.get_color = rtgui_dc_buffer_get_color;
  61. dc->parent.set_font = rtgui_dc_buffer_set_font;
  62. dc->parent.get_font = rtgui_dc_buffer_get_font;
  63. dc->parent.set_textalign = rtgui_dc_buffer_set_textalign;
  64. dc->parent.get_textalign = rtgui_dc_buffer_get_textalign;
  65. dc->parent.get_visible= rtgui_dc_buffer_get_visible;
  66. dc->parent.get_rect = rtgui_dc_buffer_get_rect;
  67. dc->parent.fini = 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. rtgui_dc_buffer_init(dc);
  74. dc->color = 0;
  75. dc->font = RT_NULL;
  76. dc->align = 0;
  77. dc->width = w;
  78. dc->height = h;
  79. dc->pitch = w * sizeof(rtgui_color_t);
  80. dc->clip_sync = 0;
  81. rtgui_region_init(&(dc->clip));
  82. dc->pixel = rtgui_malloc(h * dc->pitch);
  83. rt_memset(dc->pixel, 0, h * dc->pitch);
  84. return &(dc->parent);
  85. }
  86. rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc)
  87. {
  88. struct rtgui_dc_buffer* dc_buffer;
  89. dc_buffer = (struct rtgui_dc_buffer*)dc;
  90. return dc_buffer->pixel;
  91. }
  92. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc)
  93. {
  94. struct rtgui_dc_buffer* buffer = (struct rtgui_dc_buffer*)dc;
  95. if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
  96. rtgui_free(buffer->pixel);
  97. buffer->pixel = RT_NULL;
  98. return RT_TRUE;
  99. }
  100. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y)
  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 = dc->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->color;
  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->color;
  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. /* rtgui_color to RGB323 */
  167. rt_inline void rtgui_blit_line_1(rtgui_color_t* color, rt_uint8_t* dest, int line)
  168. {
  169. struct _color {rt_uint8_t r, g, b, a;} *c;
  170. c = (struct _color*)color;
  171. while (line-- > 0)
  172. {
  173. *dest = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
  174. c ++;
  175. dest ++;
  176. }
  177. }
  178. /* rtgui_color to RGB565 */
  179. rt_inline void rtgui_blit_line_2(rtgui_color_t* color, rt_uint8_t* dest, int line)
  180. {
  181. struct _color {rt_uint8_t r, g, b, a;} *c;
  182. rt_uint16_t* ptr;
  183. c = (struct _color*)color;
  184. ptr = (rt_uint16_t*)dest;
  185. while (line-- > 0)
  186. {
  187. *ptr = ((c->r & 0xf8) << 8) | ((c->g & 0xfc) << 3) | (c->b >> 3);
  188. c ++;
  189. ptr ++;
  190. }
  191. }
  192. /* rtgui_color to RGB888 */
  193. rt_inline void rtgui_blit_line_4(rtgui_color_t* color, rt_uint8_t* dest, int line)
  194. {
  195. rt_memcpy(dest, color, line * 4);
  196. }
  197. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  198. {
  199. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  200. struct rtgui_dc_hw* hw = (struct rtgui_dc_hw*)dest;
  201. if (dest->type == RTGUI_DC_HW)
  202. {
  203. register int index;
  204. int fb_pitch;
  205. rtgui_rect_t abs_rect;
  206. void (*blit_line)(rtgui_color_t* color, rt_uint8_t* dest, int line);
  207. abs_rect.x1 = hw->owner->extent.x1 + rect->x1;
  208. abs_rect.y1 = hw->owner->extent.y1 + rect->y1;
  209. abs_rect.x2 = abs_rect.x1 + rtgui_rect_width(*rect);
  210. abs_rect.y2 = abs_rect.y1 + rtgui_rect_height(*rect);
  211. /* hw fb pitch */
  212. fb_pitch = hw->device->byte_per_pixel * hw->device->width;
  213. /* hardware dc blit */
  214. if (!rtgui_region_not_empty(&dc->clip) ||
  215. dc->clip_sync != hw->owner->clip_sync)
  216. {
  217. /* should re-calculate clip */
  218. rtgui_region_intersect_rect(&(dc->clip),
  219. &(hw->owner->clip), &abs_rect);
  220. }
  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 3:
  230. blit_line = rtgui_blit_line_4;
  231. break;
  232. default:
  233. /* can not blit */
  234. return;
  235. }
  236. /* blit each clip rect */
  237. if (dc->clip.data == RT_NULL)
  238. {
  239. int y;
  240. rtgui_color_t* pixel;
  241. rt_uint8_t* fb;
  242. pixel = (rtgui_color_t*)(dc->pixel + (dc_point->y + dc->clip.extents.y1 - abs_rect.y1) * dc->pitch +
  243. (dc_point->x + dc->clip.extents.x1 - abs_rect.x1) * sizeof(rtgui_color_t));
  244. fb = hw->device->get_framebuffer() + dc->clip.extents.y1 * fb_pitch +
  245. dc->clip.extents.x1 * hw->device->byte_per_pixel;
  246. for (y = dc->clip.extents.y1; y < dc->clip.extents.y2; y ++)
  247. {
  248. blit_line(pixel, fb, dc->clip.extents.x2 - dc->clip.extents.x1);
  249. fb += fb_pitch;
  250. pixel += dc->width;
  251. }
  252. }
  253. else for (index = 0; index < rtgui_region_num_rects(&(dc->clip)); index ++)
  254. {
  255. int y;
  256. rtgui_rect_t* prect;
  257. rtgui_color_t* pixel;
  258. rt_uint8_t* fb;
  259. prect = ((rtgui_rect_t *)(dc->clip.data + index + 1));
  260. pixel = (rtgui_color_t*)(dc->pixel + (dc_point->y + prect->y1 - abs_rect.y1) * dc->pitch +
  261. (dc_point->x + prect->x1 - abs_rect.x1) * sizeof(rtgui_color_t));
  262. fb = hw->device->get_framebuffer() + prect->y1 * fb_pitch +
  263. prect->x1 * hw->device->byte_per_pixel;
  264. for (y = prect->y1; y < prect->y2; y ++)
  265. {
  266. blit_line(pixel, fb, prect->x2 - prect->x1);
  267. fb += fb_pitch;
  268. pixel += dc->width;
  269. }
  270. }
  271. }
  272. }
  273. static void rtgui_dc_buffer_set_color (struct rtgui_dc* self, rtgui_color_t color)
  274. {
  275. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  276. dc->color = color;
  277. }
  278. static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* self)
  279. {
  280. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  281. return dc->color;
  282. }
  283. static void rtgui_dc_buffer_set_font(struct rtgui_dc* self, rtgui_font_t* font)
  284. {
  285. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  286. dc->font = font;
  287. }
  288. static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* self)
  289. {
  290. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  291. return dc->font;
  292. }
  293. static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* self, rt_int32_t textalign)
  294. {
  295. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  296. dc->align = textalign;
  297. }
  298. static rt_int32_t rtgui_dc_buffer_get_textalign(struct rtgui_dc* self)
  299. {
  300. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  301. return dc->align;
  302. }
  303. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
  304. {
  305. return RT_TRUE;
  306. }
  307. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  308. {
  309. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  310. rect->x1 = rect->y1 = 0;
  311. rect->x2 = dc->width;
  312. rect->y2 = dc->height;
  313. }