dc_hw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * File : dc_hw.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/dc.h>
  15. #include <rtgui/dc_hw.h>
  16. #include <rtgui/driver.h>
  17. #include <rtgui/rtgui_system.h>
  18. #include <rtgui/rtgui_app.h>
  19. #include <rtgui/rtgui_server.h>
  20. #include <rtgui/widgets/container.h>
  21. #include <rtgui/widgets/window.h>
  22. #include <rtgui/widgets/title.h>
  23. #define _int_swap(x, y) do {x ^= y; y ^= x; x ^= y;} while (0)
  24. static void rtgui_dc_hw_draw_point(struct rtgui_dc *dc, int x, int y);
  25. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *dc, int x, int y, rtgui_color_t color);
  26. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *dc, int x1, int x2, int y);
  27. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *dc, int x, int y1, int y2);
  28. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *dc, rtgui_rect_t *rect);
  29. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data);
  30. static void rtgui_dc_hw_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect);
  31. static void rtgui_dc_hw_set_gc(struct rtgui_dc *dc, rtgui_gc_t *gc);
  32. static rtgui_gc_t *rtgui_dc_hw_get_gc(struct rtgui_dc *dc);
  33. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc);
  34. static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc *dc);
  35. static void rtgui_dc_hw_get_rect(struct rtgui_dc *dc, rtgui_rect_t *rect);
  36. struct rtgui_dc_hw
  37. {
  38. struct rtgui_dc parent;
  39. rtgui_widget_t *owner;
  40. const struct rtgui_graphic_driver *hw_driver;
  41. };
  42. const struct rtgui_dc_engine dc_hw_engine =
  43. {
  44. rtgui_dc_hw_draw_point,
  45. rtgui_dc_hw_draw_color_point,
  46. rtgui_dc_hw_draw_vline,
  47. rtgui_dc_hw_draw_hline,
  48. rtgui_dc_hw_fill_rect,
  49. rtgui_dc_hw_blit_line,
  50. rtgui_dc_hw_blit,
  51. rtgui_dc_hw_set_gc,
  52. rtgui_dc_hw_get_gc,
  53. rtgui_dc_hw_get_visible,
  54. rtgui_dc_hw_get_rect,
  55. rtgui_dc_hw_fini,
  56. };
  57. extern struct rt_mutex cursor_mutex;
  58. extern void rtgui_mouse_show_cursor(void);
  59. extern void rtgui_mouse_hide_cursor(void);
  60. struct rtgui_dc *rtgui_dc_hw_create(rtgui_widget_t *owner)
  61. {
  62. struct rtgui_dc_hw *dc;
  63. rtgui_widget_t *widget;
  64. /* adjudge owner */
  65. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  66. if (!RTGUI_IS_WIN(owner->toplevel)) return RT_NULL;
  67. /* set init visible as true */
  68. RTGUI_WIDGET_DC_SET_VISIBLE(owner);
  69. /* check widget visible */
  70. widget = owner;
  71. while (widget != RT_NULL)
  72. {
  73. if (RTGUI_WIDGET_IS_HIDE(widget))
  74. {
  75. RTGUI_WIDGET_DC_SET_UNVISIBLE(owner);
  76. return RT_NULL;
  77. }
  78. widget = widget->parent;
  79. }
  80. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return RT_NULL;
  81. /* create DC */
  82. dc = (struct rtgui_dc_hw *) rtgui_malloc(sizeof(struct rtgui_dc_hw));
  83. dc->parent.type = RTGUI_DC_HW;
  84. dc->parent.engine = &dc_hw_engine;
  85. dc->owner = owner;
  86. dc->hw_driver = rtgui_graphic_driver_get_default();
  87. if (RTGUI_IS_WINTITLE(owner->toplevel))
  88. {
  89. struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
  90. top->drawing ++;
  91. if (top->drawing == 1)
  92. {
  93. #ifdef RTGUI_USING_MOUSE_CURSOR
  94. #ifdef _WIN32_NATIVE
  95. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  96. rt_kprintf("hide cursor\n");
  97. rtgui_mouse_hide_cursor();
  98. #else
  99. /* hide cursor */
  100. rtgui_mouse_hide_cursor();
  101. #endif
  102. #endif
  103. }
  104. }
  105. else if (RTGUI_IS_APP(owner->toplevel) ||
  106. RTGUI_IS_WIN(owner->toplevel))
  107. {
  108. struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
  109. top->drawing ++;
  110. if (top->drawing == 1)
  111. {
  112. #ifdef _WIN32_NATIVE
  113. #ifdef RTGUI_USING_MOUSE_CURSOR
  114. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  115. rt_kprintf("hide cursor\n");
  116. rtgui_mouse_hide_cursor();
  117. #endif
  118. #else
  119. /* send draw begin to server */
  120. struct rtgui_event_update_begin eupdate;
  121. RTGUI_EVENT_UPDATE_BEGIN_INIT(&(eupdate));
  122. eupdate.rect = RTGUI_WIDGET(top)->extent;
  123. rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
  124. #endif
  125. }
  126. }
  127. return &(dc->parent);
  128. }
  129. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc)
  130. {
  131. rtgui_widget_t *owner;
  132. struct rtgui_dc_hw *self;
  133. if (dc == RT_NULL || dc->type != RTGUI_DC_HW) return RT_FALSE;
  134. self = (struct rtgui_dc_hw *)dc;
  135. /* get owner */
  136. owner = self->owner;
  137. if (RTGUI_IS_WINTITLE(owner->toplevel))
  138. {
  139. /* update title extent */
  140. struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
  141. top->drawing --;
  142. if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
  143. {
  144. #ifdef _WIN32_NATIVE
  145. #ifdef RTGUI_USING_MOUSE_CURSOR
  146. rt_mutex_release(&cursor_mutex);
  147. /* show cursor */
  148. rtgui_mouse_show_cursor();
  149. rt_kprintf("show cursor\n");
  150. #endif
  151. /* update screen */
  152. rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
  153. #else
  154. #ifdef RTGUI_USING_MOUSE_CURSOR
  155. /* show cursor */
  156. rtgui_mouse_show_cursor();
  157. #endif
  158. /* update screen */
  159. rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
  160. #endif
  161. }
  162. }
  163. else if (RTGUI_IS_APP(owner->toplevel) ||
  164. RTGUI_IS_WIN(owner->toplevel))
  165. {
  166. struct rtgui_win *top = RTGUI_WIN(owner->toplevel);
  167. top->drawing --;
  168. if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
  169. {
  170. #ifdef _WIN32_NATIVE
  171. #ifdef RTGUI_USING_MOUSE_CURSOR
  172. rt_mutex_release(&cursor_mutex);
  173. /* show cursor */
  174. rtgui_mouse_show_cursor();
  175. rt_kprintf("show cursor\n");
  176. #endif
  177. /* update screen */
  178. rtgui_graphic_driver_screen_update(self->hw_driver, &(owner->extent));
  179. #else
  180. /* send to server to end drawing */
  181. struct rtgui_event_update_end eupdate;
  182. RTGUI_EVENT_UPDATE_END_INIT(&(eupdate));
  183. eupdate.rect = owner->extent;
  184. rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
  185. #endif
  186. }
  187. }
  188. /* release hardware dc */
  189. rtgui_free(self);
  190. return RT_TRUE;
  191. }
  192. /*
  193. * draw a logic point on device
  194. */
  195. static void rtgui_dc_hw_draw_point(struct rtgui_dc *self, int x, int y)
  196. {
  197. struct rtgui_dc_hw *dc;
  198. RT_ASSERT(self != RT_NULL);
  199. dc = (struct rtgui_dc_hw *) self;
  200. x = x + dc->owner->extent.x1;
  201. y = y + dc->owner->extent.y1;
  202. /* draw this point */
  203. dc->hw_driver->ops->set_pixel(&(dc->owner->gc.foreground), x, y);
  204. }
  205. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *self, int x, int y, rtgui_color_t color)
  206. {
  207. struct rtgui_dc_hw *dc;
  208. RT_ASSERT(self != RT_NULL);
  209. dc = (struct rtgui_dc_hw *) self;
  210. x = x + dc->owner->extent.x1;
  211. y = y + dc->owner->extent.y1;
  212. /* draw this point */
  213. dc->hw_driver->ops->set_pixel(&color, x, y);
  214. }
  215. /*
  216. * draw a logic vertical line on device
  217. */
  218. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *self, int x, int y1, int y2)
  219. {
  220. struct rtgui_dc_hw *dc;
  221. RT_ASSERT(self != RT_NULL);
  222. dc = (struct rtgui_dc_hw *) self;
  223. x = x + dc->owner->extent.x1;
  224. y1 = y1 + dc->owner->extent.y1;
  225. y2 = y2 + dc->owner->extent.y1;
  226. if (y1 > y2) _int_swap(y1, y2);
  227. /* draw vline */
  228. dc->hw_driver->ops->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
  229. }
  230. /*
  231. * draw a logic horizontal line on device
  232. */
  233. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *self, int x1, int x2, int y)
  234. {
  235. struct rtgui_dc_hw *dc;
  236. RT_ASSERT(self != RT_NULL);
  237. dc = (struct rtgui_dc_hw *) self;
  238. /* convert logic to device */
  239. x1 = x1 + dc->owner->extent.x1;
  240. x2 = x2 + dc->owner->extent.x1;
  241. if (x1 > x2) _int_swap(x1, x2);
  242. y = y + dc->owner->extent.y1;
  243. /* draw hline */
  244. dc->hw_driver->ops->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
  245. }
  246. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect)
  247. {
  248. rtgui_color_t color;
  249. register rt_base_t index, x1, x2;
  250. struct rtgui_dc_hw *dc;
  251. RT_ASSERT(self != RT_NULL);
  252. dc = (struct rtgui_dc_hw *) self;
  253. /* get background color */
  254. color = dc->owner->gc.background;
  255. /* convert logic to device */
  256. x1 = rect->x1 + dc->owner->extent.x1;
  257. x2 = rect->x2 + dc->owner->extent.x1;
  258. /* fill rect */
  259. for (index = dc->owner->extent.y1 + rect->y1; index < dc->owner->extent.y1 + rect->y2; index ++)
  260. {
  261. dc->hw_driver->ops->draw_hline(&color, x1, x2, index);
  262. }
  263. }
  264. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  265. {
  266. struct rtgui_dc_hw *dc;
  267. RT_ASSERT(self != RT_NULL);
  268. dc = (struct rtgui_dc_hw *) self;
  269. /* convert logic to device */
  270. x1 = x1 + dc->owner->extent.x1;
  271. x2 = x2 + dc->owner->extent.x1;
  272. if (x1 > x2) _int_swap(x1, x2);
  273. y = y + dc->owner->extent.y1;
  274. dc->hw_driver->ops->draw_raw_hline(line_data, x1, x2, y);
  275. }
  276. static void rtgui_dc_hw_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect)
  277. {
  278. /* not blit in hardware dc */
  279. return ;
  280. }
  281. static void rtgui_dc_hw_set_gc(struct rtgui_dc *self, rtgui_gc_t *gc)
  282. {
  283. struct rtgui_dc_hw *dc;
  284. RT_ASSERT(self != RT_NULL);
  285. dc = (struct rtgui_dc_hw *) self;
  286. /* set gc */
  287. dc->owner->gc = *gc;
  288. }
  289. static rtgui_gc_t *rtgui_dc_hw_get_gc(struct rtgui_dc *self)
  290. {
  291. struct rtgui_dc_hw *dc;
  292. RT_ASSERT(self != RT_NULL);
  293. dc = (struct rtgui_dc_hw *) self;
  294. return &(dc->owner->gc);
  295. }
  296. static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc *self)
  297. {
  298. struct rtgui_dc_hw *dc;
  299. RT_ASSERT(self != RT_NULL);
  300. dc = (struct rtgui_dc_hw *) self;
  301. if (!RTGUI_WIDGET_IS_DC_VISIBLE(dc->owner)) return RT_FALSE;
  302. return RT_TRUE;
  303. }
  304. static void rtgui_dc_hw_get_rect(struct rtgui_dc *self, rtgui_rect_t *rect)
  305. {
  306. struct rtgui_dc_hw *dc;
  307. RT_ASSERT(self != RT_NULL);
  308. dc = (struct rtgui_dc_hw *) self;
  309. /* get owner */
  310. rtgui_widget_get_rect(dc->owner, rect);
  311. }