dc_buffer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #ifndef RTGUI_USING_SMALL_SIZE
  19. struct rtgui_dc_buffer
  20. {
  21. struct rtgui_dc parent;
  22. /* color and font */
  23. rtgui_color_t color;
  24. struct rtgui_font* font;
  25. /* text align */
  26. rt_int32_t align;
  27. /* width and height */
  28. rt_uint16_t width, height;
  29. rt_uint16_t pitch;
  30. /* blit info */
  31. rt_uint32_t clip_sync;
  32. rtgui_region_t clip;
  33. /* pixel data */
  34. rt_uint8_t* pixel;
  35. };
  36. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc);
  37. static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y);
  38. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
  39. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
  40. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect);
  41. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point,
  42. struct rtgui_dc* dest, rtgui_rect_t* rect);
  43. static void rtgui_dc_buffer_set_color (struct rtgui_dc* dc, rtgui_color_t color);
  44. static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* dc);
  45. static void rtgui_dc_buffer_set_font(struct rtgui_dc* dc, rtgui_font_t* font);
  46. static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* dc);
  47. static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* dc, rt_int32_t textalign);
  48. static rt_int32_t rtgui_dc_buffer_get_textalign(struct rtgui_dc* dc);
  49. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc);
  50. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);
  51. static void rtgui_dc_buffer_init(struct rtgui_dc_buffer* dc)
  52. {
  53. if (dc == RT_NULL) return;
  54. dc->parent.type = RTGUI_DC_BUFFER;
  55. dc->parent.draw_point = rtgui_dc_buffer_draw_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_color = rtgui_dc_buffer_set_color;
  61. dc->parent.get_color = rtgui_dc_buffer_get_color;
  62. dc->parent.set_font = rtgui_dc_buffer_set_font;
  63. dc->parent.get_font = rtgui_dc_buffer_get_font;
  64. dc->parent.set_textalign = rtgui_dc_buffer_set_textalign;
  65. dc->parent.get_textalign = rtgui_dc_buffer_get_textalign;
  66. dc->parent.get_visible= rtgui_dc_buffer_get_visible;
  67. dc->parent.get_rect = rtgui_dc_buffer_get_rect;
  68. dc->parent.fini = rtgui_dc_buffer_fini;
  69. }
  70. struct rtgui_dc* rtgui_dc_buffer_create(int w, int h)
  71. {
  72. struct rtgui_dc_buffer* dc;
  73. dc = (struct rtgui_dc_buffer*)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  74. rtgui_dc_buffer_init(dc);
  75. dc->color = 0;
  76. dc->font = RT_NULL;
  77. dc->align = 0;
  78. dc->width = w;
  79. dc->height = h;
  80. dc->pitch = w * sizeof(rtgui_color_t);
  81. dc->clip_sync = 0;
  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->color;
  109. }
  110. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  111. {
  112. rtgui_color_t* ptr;
  113. register rt_base_t index;
  114. struct rtgui_dc_buffer* dc;
  115. dc = (struct rtgui_dc_buffer*)self;
  116. if (x >= dc->width) return;
  117. if (y1 > dc->height) y1 = dc->height;
  118. if (y2 > dc->height) y2 = dc->height;
  119. ptr = (rtgui_color_t*)(dc->pixel + y1 * dc->pitch + x * sizeof(rtgui_color_t));
  120. for (index = y1; index < y2; index ++)
  121. {
  122. /* draw this point */
  123. *ptr = dc->color;
  124. ptr += dc->width;
  125. }
  126. }
  127. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  128. {
  129. rtgui_color_t* ptr;
  130. register rt_base_t index;
  131. struct rtgui_dc_buffer* dc;
  132. dc = (struct rtgui_dc_buffer*)self;
  133. if (y >= dc->height) return;
  134. if (x1 > dc->width) x1 = dc->width;
  135. if (x2 > dc->width) x2 = dc->width;
  136. ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x1 * sizeof(rtgui_color_t));
  137. for (index = x1; index < x2; index ++)
  138. {
  139. /* draw this point */
  140. *ptr++ = dc->color;
  141. }
  142. }
  143. static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  144. {
  145. rtgui_rect_t r;
  146. struct rtgui_dc_buffer* dc;
  147. r = *rect;
  148. dc = (struct rtgui_dc_buffer*)self;
  149. if (r.x1 > dc->width) r.x1 = dc->width;
  150. if (r.x2 > dc->width) r.x2 = dc->width;
  151. if (r.y1 > dc->height) r.y1 = dc->height;
  152. if (r.y2 > dc->height) r.y2 = dc->height;
  153. /* fill first line */
  154. rtgui_dc_buffer_draw_hline(&(dc->parent), r.x1, r.x2, r.y1);
  155. /* memory copy other lines */
  156. if (r.y2 > r.y1)
  157. {
  158. register rt_base_t index;
  159. for (index = r.y1 + 1; index < r.y2; index ++)
  160. {
  161. rt_memcpy(dc->pixel + index * dc->pitch,
  162. dc->pixel + r.y1 * dc->pitch,
  163. (r.x2 - r.x1) * sizeof(rtgui_color_t));
  164. }
  165. }
  166. }
  167. /* rtgui_color to RGB323 */
  168. rt_inline void rtgui_blit_line_1(rtgui_color_t* color, rt_uint8_t* dest, int line)
  169. {
  170. struct _color {rt_uint8_t r, g, b, a;} *c;
  171. c = (struct _color*)color;
  172. while (line-- > 0)
  173. {
  174. *dest = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
  175. c ++;
  176. dest ++;
  177. }
  178. }
  179. /* rtgui_color to RGB565 */
  180. rt_inline void rtgui_blit_line_2(rtgui_color_t* color, rt_uint8_t* dest, int line)
  181. {
  182. struct _color {rt_uint8_t r, g, b, a;} *c;
  183. rt_uint16_t* ptr;
  184. c = (struct _color*)color;
  185. ptr = (rt_uint16_t*)dest;
  186. while (line-- > 0)
  187. {
  188. *ptr = ((c->r & 0xf8) << 8) | ((c->g & 0xfc) << 3) | (c->b >> 3);
  189. c ++;
  190. ptr ++;
  191. }
  192. }
  193. /* rtgui_color to RGB888 */
  194. rt_inline void rtgui_blit_line_4(rtgui_color_t* color, rt_uint8_t* dest, int line)
  195. {
  196. rt_memcpy(dest, color, line * 4);
  197. }
  198. static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  199. {
  200. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  201. struct rtgui_dc_hw* hw = (struct rtgui_dc_hw*)dest;
  202. if (dest->type == RTGUI_DC_HW)
  203. {
  204. register int index;
  205. int fb_pitch;
  206. rtgui_rect_t abs_rect;
  207. void (*blit_line)(rtgui_color_t* color, rt_uint8_t* dest, int line);
  208. abs_rect.x1 = hw->owner->extent.x1 + rect->x1;
  209. abs_rect.y1 = hw->owner->extent.y1 + rect->y1;
  210. abs_rect.x2 = abs_rect.x1 + rtgui_rect_width(*rect);
  211. abs_rect.y2 = abs_rect.y1 + rtgui_rect_height(*rect);
  212. /* hw fb pitch */
  213. fb_pitch = hw->device->byte_per_pixel * hw->device->width;
  214. /* hardware dc blit */
  215. if (!rtgui_region_not_empty(&dc->clip) ||
  216. dc->clip_sync != hw->owner->clip_sync)
  217. {
  218. /* should re-calculate clip */
  219. rtgui_region_intersect_rect(&(dc->clip),
  220. &(hw->owner->clip), &abs_rect);
  221. }
  222. switch (hw->device->byte_per_pixel)
  223. {
  224. case 1:
  225. blit_line = rtgui_blit_line_1;
  226. break;
  227. case 2:
  228. blit_line = rtgui_blit_line_2;
  229. break;
  230. case 3:
  231. blit_line = rtgui_blit_line_4;
  232. break;
  233. default:
  234. /* can not blit */
  235. return;
  236. }
  237. /* blit each clip rect */
  238. if (dc->clip.data == RT_NULL)
  239. {
  240. int y;
  241. rtgui_color_t* pixel;
  242. rt_uint8_t* fb;
  243. pixel = (rtgui_color_t*)(dc->pixel + (dc_point->y + dc->clip.extents.y1 - abs_rect.y1) * dc->pitch +
  244. (dc_point->x + dc->clip.extents.x1 - abs_rect.x1) * sizeof(rtgui_color_t));
  245. fb = hw->device->get_framebuffer() + dc->clip.extents.y1 * fb_pitch +
  246. dc->clip.extents.x1 * hw->device->byte_per_pixel;
  247. for (y = dc->clip.extents.y1; y < dc->clip.extents.y2; y ++)
  248. {
  249. blit_line(pixel, fb, dc->clip.extents.x2 - dc->clip.extents.x1);
  250. fb += fb_pitch;
  251. pixel += dc->width;
  252. }
  253. }
  254. else for (index = 0; index < rtgui_region_num_rects(&(dc->clip)); index ++)
  255. {
  256. int y;
  257. rtgui_rect_t* prect;
  258. rtgui_color_t* pixel;
  259. rt_uint8_t* fb;
  260. prect = ((rtgui_rect_t *)(dc->clip.data + index + 1));
  261. pixel = (rtgui_color_t*)(dc->pixel + (dc_point->y + prect->y1 - abs_rect.y1) * dc->pitch +
  262. (dc_point->x + prect->x1 - abs_rect.x1) * sizeof(rtgui_color_t));
  263. fb = hw->device->get_framebuffer() + prect->y1 * fb_pitch +
  264. prect->x1 * hw->device->byte_per_pixel;
  265. for (y = prect->y1; y < prect->y2; y ++)
  266. {
  267. blit_line(pixel, fb, prect->x2 - prect->x1);
  268. fb += fb_pitch;
  269. pixel += dc->width;
  270. }
  271. }
  272. }
  273. }
  274. static void rtgui_dc_buffer_set_color (struct rtgui_dc* self, rtgui_color_t color)
  275. {
  276. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  277. dc->color = color;
  278. }
  279. static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* self)
  280. {
  281. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  282. return dc->color;
  283. }
  284. static void rtgui_dc_buffer_set_font(struct rtgui_dc* self, rtgui_font_t* font)
  285. {
  286. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  287. dc->font = font;
  288. }
  289. static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* self)
  290. {
  291. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  292. return dc->font;
  293. }
  294. static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* self, rt_int32_t textalign)
  295. {
  296. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  297. dc->align = textalign;
  298. }
  299. static rt_int32_t rtgui_dc_buffer_get_textalign(struct rtgui_dc* self)
  300. {
  301. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  302. return dc->align;
  303. }
  304. static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc)
  305. {
  306. return RT_TRUE;
  307. }
  308. static void rtgui_dc_buffer_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  309. {
  310. struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self;
  311. rect->x1 = rect->y1 = 0;
  312. rect->x2 = dc->width;
  313. rect->y2 = dc->height;
  314. }
  315. #endif