dc_client.c 11 KB

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