dc_hw.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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
  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
  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
  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
  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. #if (0) /* Please yourself to decide whether to use it*/
  202. if(x < dc->owner->extent.x1 || x >= dc->owner->extent.x2) return;
  203. #endif
  204. y = y + dc->owner->extent.y1;
  205. #if (0)
  206. if(y < dc->owner->extent.y1 || y >= dc->owner->extent.y2) return;
  207. #endif
  208. /* draw this point */
  209. dc->hw_driver->ops->set_pixel(&(dc->owner->gc.foreground), x, y);
  210. }
  211. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
  212. {
  213. struct rtgui_dc_hw* dc;
  214. RT_ASSERT(self != RT_NULL);
  215. dc = (struct rtgui_dc_hw*) self;
  216. x = x + dc->owner->extent.x1;
  217. y = y + dc->owner->extent.y1;
  218. /* draw this point */
  219. dc->hw_driver->ops->set_pixel(&color, x, y);
  220. }
  221. /*
  222. * draw a logic vertical line on device
  223. */
  224. static void rtgui_dc_hw_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  225. {
  226. struct rtgui_dc_hw* dc;
  227. RT_ASSERT(self != RT_NULL);
  228. dc = (struct rtgui_dc_hw*) self;
  229. x = x + dc->owner->extent.x1;
  230. y1 = y1 + dc->owner->extent.y1;
  231. y2 = y2 + dc->owner->extent.y1;
  232. if (y1 > y2) _int_swap(y1, y2);
  233. /* draw vline */
  234. dc->hw_driver->ops->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
  235. }
  236. /*
  237. * draw a logic horizontal line on device
  238. */
  239. static void rtgui_dc_hw_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  240. {
  241. struct rtgui_dc_hw* dc;
  242. RT_ASSERT(self != RT_NULL);
  243. dc = (struct rtgui_dc_hw*) self;
  244. /* convert logic to device */
  245. x1 = x1 + dc->owner->extent.x1;
  246. x2 = x2 + dc->owner->extent.x1;
  247. if (x1 > x2) _int_swap(x1, x2);
  248. y = y + dc->owner->extent.y1;
  249. /* draw hline */
  250. dc->hw_driver->ops->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
  251. }
  252. static void rtgui_dc_hw_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  253. {
  254. rtgui_color_t color;
  255. register rt_base_t index, x1, x2;
  256. struct rtgui_dc_hw* dc;
  257. RT_ASSERT(self != RT_NULL);
  258. dc = (struct rtgui_dc_hw*) self;
  259. /* get background color */
  260. color = dc->owner->gc.background;
  261. /* convert logic to device */
  262. x1 = rect->x1 + dc->owner->extent.x1;
  263. x2 = rect->x2 + dc->owner->extent.x1;
  264. /* fill rect */
  265. for (index = dc->owner->extent.y1 + rect->y1; index < dc->owner->extent.y1 + rect->y2; index ++)
  266. {
  267. dc->hw_driver->ops->draw_hline(&color, x1, x2, index);
  268. }
  269. }
  270. static void rtgui_dc_hw_blit_line (struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data)
  271. {
  272. struct rtgui_dc_hw* dc;
  273. RT_ASSERT(self != RT_NULL);
  274. dc = (struct rtgui_dc_hw*) self;
  275. /* convert logic to device */
  276. x1 = x1 + dc->owner->extent.x1;
  277. x2 = x2 + dc->owner->extent.x1;
  278. if (x1 > x2) _int_swap(x1, x2);
  279. y = y + dc->owner->extent.y1;
  280. dc->hw_driver->ops->draw_raw_hline(line_data, x1, x2, y);
  281. }
  282. static void rtgui_dc_hw_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  283. {
  284. /* not blit in hardware dc */
  285. return ;
  286. }
  287. static void rtgui_dc_hw_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
  288. {
  289. struct rtgui_dc_hw* dc;
  290. RT_ASSERT(self != RT_NULL);
  291. dc = (struct rtgui_dc_hw*) self;
  292. /* set gc */
  293. dc->owner->gc = *gc;
  294. }
  295. static rtgui_gc_t* rtgui_dc_hw_get_gc(struct rtgui_dc* self)
  296. {
  297. struct rtgui_dc_hw* dc;
  298. RT_ASSERT(self != RT_NULL);
  299. dc = (struct rtgui_dc_hw*) self;
  300. return &(dc->owner->gc);
  301. }
  302. static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* self)
  303. {
  304. struct rtgui_dc_hw* dc;
  305. RT_ASSERT(self != RT_NULL);
  306. dc = (struct rtgui_dc_hw*) self;
  307. if (!RTGUI_WIDGET_IS_DC_VISIBLE(dc->owner)) return RT_FALSE;
  308. return RT_TRUE;
  309. }
  310. static void rtgui_dc_hw_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  311. {
  312. struct rtgui_dc_hw* dc;
  313. RT_ASSERT(self != RT_NULL);
  314. dc = (struct rtgui_dc_hw*) self;
  315. /* get owner */
  316. rtgui_widget_get_rect(dc->owner, rect);
  317. }