dc_hw.c 9.4 KB

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