dc_hw.c 10 KB

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