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