dc_client.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * File : dc_client.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. * 2010-08-09 Bernard rename hardware dc to client dc
  14. * 2010-09-13 Bernard fix rtgui_dc_client_blit_line issue, which found
  15. * by appele
  16. * 2010-09-14 Bernard fix vline and hline coordinate issue
  17. */
  18. #include <rtgui/dc.h>
  19. #include <rtgui/dc_hw.h>
  20. #include <rtgui/dc_client.h>
  21. #include <rtgui/driver.h>
  22. #include <rtgui/rtgui_system.h>
  23. #include <rtgui/rtgui_app.h>
  24. #include <rtgui/rtgui_server.h>
  25. #include <rtgui/widgets/container.h>
  26. #include <rtgui/widgets/window.h>
  27. static void rtgui_dc_client_draw_point(struct rtgui_dc *dc, int x, int y);
  28. static void rtgui_dc_client_draw_color_point(struct rtgui_dc *dc, int x, int y, rtgui_color_t color);
  29. static void rtgui_dc_client_draw_hline(struct rtgui_dc *dc, int x1, int x2, int y);
  30. static void rtgui_dc_client_draw_vline(struct rtgui_dc *dc, int x, int y1, int y2);
  31. static void rtgui_dc_client_fill_rect(struct rtgui_dc *dc, rtgui_rect_t *rect);
  32. static void rtgui_dc_client_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data);
  33. static void rtgui_dc_client_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect);
  34. static rt_bool_t rtgui_dc_client_fini(struct rtgui_dc *dc);
  35. #define hw_driver (rtgui_graphic_driver_get_default())
  36. #define dc_set_foreground(c) dc->gc.foreground = c
  37. #define dc_set_background(c) dc->gc.background = c
  38. #define _int_swap(x, y) do {x ^= y; y ^= x; x ^= y;} while (0)
  39. const struct rtgui_dc_engine dc_client_engine =
  40. {
  41. rtgui_dc_client_draw_point,
  42. rtgui_dc_client_draw_color_point,
  43. rtgui_dc_client_draw_vline,
  44. rtgui_dc_client_draw_hline,
  45. rtgui_dc_client_fill_rect,
  46. rtgui_dc_client_blit_line,
  47. rtgui_dc_client_blit,
  48. rtgui_dc_client_fini,
  49. };
  50. void rtgui_dc_client_init(rtgui_widget_t *owner)
  51. {
  52. struct rtgui_dc *dc;
  53. RT_ASSERT(owner != RT_NULL);
  54. dc = RTGUI_WIDGET_DC(owner);
  55. dc->type = RTGUI_DC_CLIENT;
  56. dc->engine = &dc_client_engine;
  57. }
  58. struct rtgui_dc *rtgui_dc_client_create(rtgui_widget_t *owner)
  59. {
  60. /* adjudge owner */
  61. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  62. return RTGUI_WIDGET_DC(owner);
  63. }
  64. static rt_bool_t rtgui_dc_client_fini(struct rtgui_dc *dc)
  65. {
  66. if (dc == RT_NULL || dc->type != RTGUI_DC_CLIENT) return RT_FALSE;
  67. return RT_TRUE;
  68. }
  69. /*
  70. * draw a logic point on device
  71. */
  72. static void rtgui_dc_client_draw_point(struct rtgui_dc *self, int x, int y)
  73. {
  74. rtgui_rect_t rect;
  75. rtgui_widget_t *owner;
  76. if (self == RT_NULL) return;
  77. if (!rtgui_dc_get_visible(self)) return;
  78. /* get owner */
  79. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  80. x = x + owner->extent.x1;
  81. y = y + owner->extent.y1;
  82. if (rtgui_region_contains_point(&(owner->clip), x, y, &rect) == RT_EOK)
  83. {
  84. /* draw this point */
  85. hw_driver->ops->set_pixel(&(owner->gc.foreground), x, y);
  86. }
  87. }
  88. static void rtgui_dc_client_draw_color_point(struct rtgui_dc *self, int x, int y, rtgui_color_t color)
  89. {
  90. rtgui_rect_t rect;
  91. rtgui_widget_t *owner;
  92. if (self == RT_NULL) return;
  93. if (!rtgui_dc_get_visible(self)) return;
  94. /* get owner */
  95. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  96. x = x + owner->extent.x1;
  97. y = y + owner->extent.y1;
  98. if (rtgui_region_contains_point(&(owner->clip), x, y, &rect) == RT_EOK)
  99. {
  100. /* draw this point */
  101. hw_driver->ops->set_pixel(&color, x, y);
  102. }
  103. }
  104. /*
  105. * draw a logic vertical line on device
  106. */
  107. static void rtgui_dc_client_draw_vline(struct rtgui_dc *self, int x, int y1, int y2)
  108. {
  109. register rt_base_t index;
  110. rtgui_widget_t *owner;
  111. if (self == RT_NULL) return;
  112. if (!rtgui_dc_get_visible(self)) return;
  113. /* get owner */
  114. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  115. x = x + owner->extent.x1;
  116. y1 = y1 + owner->extent.y1;
  117. y2 = y2 + owner->extent.y1;
  118. if (y1 > y2) _int_swap(y1, y2);
  119. if (owner->clip.data == RT_NULL)
  120. {
  121. rtgui_rect_t *prect;
  122. prect = &(owner->clip.extents);
  123. /* calculate vline intersect */
  124. if (prect->x1 > x || prect->x2 <= x) return;
  125. if (prect->y2 <= y1 || prect->y1 > y2) return;
  126. if (prect->y1 > y1) y1 = prect->y1;
  127. if (prect->y2 < y2) y2 = prect->y2;
  128. /* draw vline */
  129. hw_driver->ops->draw_vline(&(owner->gc.foreground), x, y1, y2);
  130. }
  131. else
  132. {
  133. for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  134. {
  135. rtgui_rect_t *prect;
  136. register rt_base_t draw_y1, draw_y2;
  137. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  138. draw_y1 = y1;
  139. draw_y2 = y2;
  140. /* calculate vline clip */
  141. if (prect->x1 > x || prect->x2 <= x) continue;
  142. if (prect->y2 <= y1 || prect->y1 > y2) continue;
  143. if (prect->y1 > y1) draw_y1 = prect->y1;
  144. if (prect->y2 < y2) draw_y2 = prect->y2;
  145. /* draw vline */
  146. hw_driver->ops->draw_vline(&(owner->gc.foreground), x, draw_y1, draw_y2);
  147. }
  148. }
  149. }
  150. /*
  151. * draw a logic horizontal line on device
  152. */
  153. static void rtgui_dc_client_draw_hline(struct rtgui_dc *self, int x1, int x2, int y)
  154. {
  155. register rt_base_t index;
  156. rtgui_widget_t *owner;
  157. if (self == RT_NULL) return;
  158. if (!rtgui_dc_get_visible(self)) return;
  159. /* get owner */
  160. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  161. /* convert logic to device */
  162. x1 = x1 + owner->extent.x1;
  163. x2 = x2 + owner->extent.x1;
  164. if (x1 > x2) _int_swap(x1, x2);
  165. y = y + owner->extent.y1;
  166. if (owner->clip.data == RT_NULL)
  167. {
  168. rtgui_rect_t *prect;
  169. prect = &(owner->clip.extents);
  170. /* calculate vline intersect */
  171. if (prect->y1 > y || prect->y2 <= y) return;
  172. if (prect->x2 <= x1 || prect->x1 > x2) return;
  173. if (prect->x1 > x1) x1 = prect->x1;
  174. if (prect->x2 < x2) x2 = prect->x2;
  175. /* draw hline */
  176. hw_driver->ops->draw_hline(&(owner->gc.foreground), x1, x2, y);
  177. }
  178. else
  179. {
  180. for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  181. {
  182. rtgui_rect_t *prect;
  183. register rt_base_t draw_x1, draw_x2;
  184. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  185. draw_x1 = x1;
  186. draw_x2 = x2;
  187. /* calculate hline clip */
  188. if (prect->y1 > y || prect->y2 <= y) continue;
  189. if (prect->x2 <= x1 || prect->x1 > x2) continue;
  190. if (prect->x1 > x1) draw_x1 = prect->x1;
  191. if (prect->x2 < x2) draw_x2 = prect->x2;
  192. /* draw hline */
  193. hw_driver->ops->draw_hline(&(owner->gc.foreground), draw_x1, draw_x2, y);
  194. }
  195. }
  196. }
  197. static void rtgui_dc_client_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect)
  198. {
  199. rtgui_color_t foreground;
  200. register rt_base_t index;
  201. rtgui_widget_t *owner;
  202. RT_ASSERT(self);
  203. RT_ASSERT(rect);
  204. if (!rtgui_dc_get_visible(self)) return;
  205. /* get owner */
  206. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  207. /* save foreground color */
  208. foreground = owner->gc.foreground;
  209. /* set background color as foreground color */
  210. owner->gc.foreground = owner->gc.background;
  211. /* fill rect */
  212. for (index = rect->y1; index < rect->y2; index ++)
  213. {
  214. rtgui_dc_client_draw_hline(self, rect->x1, rect->x2, index);
  215. }
  216. /* restore foreground color */
  217. owner->gc.foreground = foreground;
  218. }
  219. static void rtgui_dc_client_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  220. {
  221. register rt_base_t index;
  222. rtgui_widget_t *owner;
  223. if (self == RT_NULL) return;
  224. if (!rtgui_dc_get_visible(self)) return;
  225. /* get owner */
  226. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  227. /* convert logic to device */
  228. x1 = x1 + owner->extent.x1;
  229. x2 = x2 + owner->extent.x1;
  230. if (x1 > x2) _int_swap(x1, x2);
  231. y = y + owner->extent.y1;
  232. if (rtgui_region_is_flat(&(owner->clip)) == RT_EOK)
  233. {
  234. rtgui_rect_t *prect;
  235. int offset = 0;
  236. prect = &(owner->clip.extents);
  237. /* calculate vline intersect */
  238. if (prect->y1 > y || prect->y2 <= y) return;
  239. if (prect->x2 <= x1 || prect->x1 > x2) return;
  240. if (prect->x1 > x1) x1 = prect->x1;
  241. if (prect->x2 < x2) x2 = prect->x2;
  242. /* patch note:
  243. * We need to adjust the offset when update widget clip!
  244. * Of course at ordinary times for 0. General */
  245. offset = owner->clip.extents.x1 - owner->extent.x1;
  246. offset = offset * _UI_BITBYTES(hw_driver->bits_per_pixel);
  247. /* draw hline */
  248. hw_driver->ops->draw_raw_hline(line_data + offset, x1, x2, y);
  249. }
  250. else
  251. {
  252. for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  253. {
  254. rtgui_rect_t *prect;
  255. register rt_base_t draw_x1, draw_x2;
  256. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  257. draw_x1 = x1;
  258. draw_x2 = x2;
  259. /* calculate hline clip */
  260. if (prect->y1 > y || prect->y2 <= y) continue;
  261. if (prect->x2 <= x1 || prect->x1 > x2) continue;
  262. if (prect->x1 > x1) draw_x1 = prect->x1;
  263. if (prect->x2 < x2) draw_x2 = prect->x2;
  264. /* draw hline */
  265. hw_driver->ops->draw_raw_hline(line_data + (draw_x1 - x1) * _UI_BITBYTES(hw_driver->bits_per_pixel), draw_x1, draw_x2, y);
  266. }
  267. }
  268. }
  269. static void rtgui_dc_client_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect)
  270. {
  271. /* not blit in hardware dc */
  272. return ;
  273. }