dc_hw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/driver.h>
  16. #include <rtgui/rtgui_system.h>
  17. #include <rtgui/widgets/view.h>
  18. #include <rtgui/widgets/window.h>
  19. #include <rtgui/widgets/workbench.h>
  20. #include <rtgui/widgets/title.h>
  21. static void rtgui_dc_hw_draw_point(struct rtgui_dc* dc, int x, int y);
  22. static void rtgui_dc_hw_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
  23. static void rtgui_dc_hw_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
  24. static void rtgui_dc_hw_fill_rect (struct rtgui_dc* dc, rtgui_rect_t* rect);
  25. static void rtgui_dc_hw_blit (struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect);
  26. static void rtgui_dc_hw_set_color (struct rtgui_dc* dc, rtgui_color_t color);
  27. static rtgui_color_t rtgui_dc_hw_get_color (struct rtgui_dc* dc);
  28. static void rtgui_dc_hw_set_font(struct rtgui_dc* dc, rtgui_font_t* font);
  29. static rtgui_font_t* rtgui_dc_hw_get_font(struct rtgui_dc* dc);
  30. static void rtgui_dc_hw_set_textalign(struct rtgui_dc* dc, rt_int32_t textalign);
  31. static rt_int32_t rtgui_dc_hw_get_textalign(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* rtgui_dc_begin_drawing(rtgui_widget_t* owner)
  36. {
  37. return rtgui_dc_hw_create(owner);
  38. }
  39. void rtgui_dc_end_drawing(struct rtgui_dc* dc)
  40. {
  41. if (rtgui_dc_hw_fini(dc) == RT_TRUE)
  42. {
  43. rtgui_free(dc);
  44. }
  45. }
  46. void rtgui_dc_hw_init(struct rtgui_dc_hw* dc)
  47. {
  48. if (dc == RT_NULL) return;
  49. dc->parent.type = RTGUI_DC_HW;
  50. dc->parent.draw_point = rtgui_dc_hw_draw_point;
  51. dc->parent.draw_hline = rtgui_dc_hw_draw_hline;
  52. dc->parent.draw_vline = rtgui_dc_hw_draw_vline;
  53. dc->parent.fill_rect = rtgui_dc_hw_fill_rect ;
  54. dc->parent.blit = rtgui_dc_hw_blit;
  55. dc->parent.set_color = rtgui_dc_hw_set_color;
  56. dc->parent.get_color = rtgui_dc_hw_get_color;
  57. dc->parent.set_font = rtgui_dc_hw_set_font;
  58. dc->parent.get_font = rtgui_dc_hw_get_font;
  59. dc->parent.set_textalign = rtgui_dc_hw_set_textalign;
  60. dc->parent.get_textalign = rtgui_dc_hw_get_textalign;
  61. dc->parent.get_visible= rtgui_dc_hw_get_visible;
  62. dc->parent.get_rect = rtgui_dc_hw_get_rect;
  63. dc->parent.fini = rtgui_dc_hw_fini;
  64. }
  65. extern struct rt_mutex cursor_mutex;
  66. #define dc_set_foreground(c) dc->owner->gc.foreground = c
  67. #define dc_set_background(c) dc->owner->gc.background = c
  68. extern void rtgui_mouse_show_cursor(void);
  69. extern void rtgui_mouse_hide_cursor(void);
  70. struct rtgui_dc* rtgui_dc_hw_create(rtgui_widget_t* owner)
  71. {
  72. struct rtgui_dc_hw* dc;
  73. rtgui_widget_t* widget;
  74. /* adjudge owner */
  75. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  76. if (!RTGUI_IS_TOPLEVEL(owner->toplevel)) return RT_NULL;
  77. /* malloc a dc object */
  78. dc = (struct rtgui_dc_hw*) rtgui_malloc(sizeof(struct rtgui_dc_hw));
  79. rtgui_dc_hw_init(dc);
  80. dc->owner = owner;
  81. dc->visible = RT_TRUE;
  82. dc->device = rtgui_graphic_driver_get_default();
  83. /* set visible */
  84. widget = owner;
  85. while (widget != RT_NULL)
  86. {
  87. if (RTGUI_WIDGET_IS_HIDE(widget))
  88. {
  89. dc->visible = RT_FALSE;
  90. break;
  91. }
  92. widget = widget->parent;
  93. }
  94. if (RTGUI_IS_WINTITLE(dc->owner->toplevel))
  95. {
  96. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(dc->owner->toplevel);
  97. top->drawing ++;
  98. if (top->drawing == 1)
  99. {
  100. #ifdef RTGUI_USING_MOUSE_CURSOR
  101. #ifdef __WIN32__
  102. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  103. rt_kprintf("hide cursor\n");
  104. rtgui_mouse_hide_cursor();
  105. #else
  106. /* hide cursor */
  107. rtgui_mouse_hide_cursor();
  108. #endif
  109. #endif
  110. }
  111. }
  112. else if (RTGUI_IS_WORKBENCH(dc->owner->toplevel) ||
  113. RTGUI_IS_WIN(dc->owner->toplevel))
  114. {
  115. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(dc->owner->toplevel);
  116. top->drawing ++;
  117. if (top->drawing == 1)
  118. {
  119. #ifdef __WIN32__
  120. #ifdef RTGUI_USING_MOUSE_CURSOR
  121. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  122. rt_kprintf("hide cursor\n");
  123. rtgui_mouse_hide_cursor();
  124. #endif
  125. #else
  126. /* send draw begin to server */
  127. struct rtgui_event_update_begin eupdate;
  128. RTGUI_EVENT_UPDATE_BEGIN_INIT(&(eupdate));
  129. eupdate.rect = RTGUI_WIDGET(top)->extent;
  130. rtgui_thread_send(top->server, (struct rtgui_event*)&eupdate, sizeof(eupdate));
  131. #endif
  132. }
  133. }
  134. return &(dc->parent);
  135. }
  136. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc* dc)
  137. {
  138. struct rtgui_dc_hw* hw = (struct rtgui_dc_hw*)dc;
  139. if (dc == RT_NULL || hw->parent.type != RTGUI_DC_HW) return RT_FALSE;
  140. if (RTGUI_IS_WINTITLE(hw->owner->toplevel))
  141. {
  142. /* update title extent */
  143. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(hw->owner->toplevel);
  144. top->drawing --;
  145. if (top->drawing == 0)
  146. {
  147. #ifdef __WIN32__
  148. #ifdef RTGUI_USING_MOUSE_CURSOR
  149. rt_mutex_release(&cursor_mutex);
  150. /* show cursor */
  151. rtgui_mouse_show_cursor();
  152. rt_kprintf("show cursor\n");
  153. #endif
  154. /* update screen */
  155. hw->device->screen_update(&(hw->owner->extent));
  156. #else
  157. #ifdef RTGUI_USING_MOUSE_CURSOR
  158. /* show cursor */
  159. rtgui_mouse_show_cursor();
  160. #endif
  161. /* update screen */
  162. hw->device->screen_update(&(hw->owner->extent));
  163. #endif
  164. }
  165. }
  166. else if (RTGUI_IS_WORKBENCH(hw->owner->toplevel) ||
  167. RTGUI_IS_WIN(hw->owner->toplevel))
  168. {
  169. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(hw->owner->toplevel);
  170. top->drawing --;
  171. if (top->drawing == 0)
  172. {
  173. #ifdef __WIN32__
  174. #ifdef RTGUI_USING_MOUSE_CURSOR
  175. rt_mutex_release(&cursor_mutex);
  176. /* show cursor */
  177. rtgui_mouse_show_cursor();
  178. rt_kprintf("show cursor\n");
  179. #endif
  180. /* update screen */
  181. hw->device->screen_update(&(hw->owner->extent));
  182. #else
  183. /* send to server to end drawing */
  184. struct rtgui_event_update_end eupdate;
  185. RTGUI_EVENT_UPDATE_END_INIT(&(eupdate));
  186. eupdate.rect = RTGUI_WIDGET(top)->extent;
  187. rtgui_thread_send(top->server, (struct rtgui_event*)&eupdate, sizeof(eupdate));
  188. #endif
  189. }
  190. }
  191. return RT_TRUE;
  192. }
  193. /*
  194. * draw a logic point on device
  195. */
  196. static void rtgui_dc_hw_draw_point(struct rtgui_dc* self, int x, int y)
  197. {
  198. struct rtgui_dc_hw* dc;
  199. rtgui_rect_t rect;
  200. dc = (struct rtgui_dc_hw*)self;
  201. if (dc == RT_NULL || dc->visible != RT_TRUE) return;
  202. x = x + dc->owner->extent.x1;
  203. y = y + dc->owner->extent.y1;
  204. if (rtgui_region_contains_point(&(dc->owner->clip), x, y, &rect) == RT_EOK)
  205. {
  206. /* draw this point */
  207. dc->device->set_pixel(&(dc->owner->gc.foreground), x, y);
  208. }
  209. }
  210. /*
  211. * draw a logic vertical line on device
  212. */
  213. static void rtgui_dc_hw_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  214. {
  215. register rt_base_t index;
  216. struct rtgui_dc_hw* dc;
  217. dc = (struct rtgui_dc_hw*)self;
  218. if (dc == RT_NULL || dc->visible != RT_TRUE) return;
  219. x = x + dc->owner->extent.x1;
  220. y1 = y1 + dc->owner->extent.y1;
  221. y2 = y2 + dc->owner->extent.y1;
  222. if (dc->owner->clip.data == RT_NULL)
  223. {
  224. rtgui_rect_t* prect;
  225. prect = &(dc->owner->clip.extents);
  226. /* calculate vline intersect */
  227. if (prect->x1 > x || prect->x2 <= x) return;
  228. if (prect->y2 <= y1 || prect->y1 > y2) return;
  229. if (prect->y1 > y1) y1 = prect->y1;
  230. if (prect->y2 < y2) y2 = prect->y2;
  231. /* draw vline */
  232. dc->device->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
  233. }
  234. else for (index = 0; index < rtgui_region_num_rects(&(dc->owner->clip)); index ++)
  235. {
  236. rtgui_rect_t* prect;
  237. register rt_base_t draw_y1, draw_y2;
  238. prect = ((rtgui_rect_t *)(dc->owner->clip.data + index + 1));
  239. draw_y1 = y1;
  240. draw_y2 = y2;
  241. /* calculate vline clip */
  242. if (prect->x1 > x || prect->x2 <= x) continue;
  243. if (prect->y2 <= y1 || prect->y1 > y2) continue;
  244. if (prect->y1 > y1) draw_y1 = prect->y1;
  245. if (prect->y2 < y2) draw_y2 = prect->y2;
  246. /* draw vline */
  247. dc->device->draw_vline(&(dc->owner->gc.foreground), x, draw_y1, draw_y2);
  248. }
  249. }
  250. /*
  251. * draw a logic horizontal line on device
  252. */
  253. static void rtgui_dc_hw_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  254. {
  255. register rt_base_t index;
  256. struct rtgui_dc_hw* dc;
  257. dc = (struct rtgui_dc_hw*)self;
  258. if (dc == RT_NULL || dc->visible != RT_TRUE) return;
  259. /* convert logic to device */
  260. x1 = x1 + dc->owner->extent.x1;
  261. x2 = x2 + dc->owner->extent.x1;
  262. y = y + dc->owner->extent.y1;
  263. if (dc->owner->clip.data == RT_NULL)
  264. {
  265. rtgui_rect_t* prect;
  266. prect = &(dc->owner->clip.extents);
  267. /* calculate vline intersect */
  268. if (prect->y1 > y || prect->y2 <= y ) return;
  269. if (prect->x2 <= x1 || prect->x1 > x2) return;
  270. if (prect->x1 > x1) x1 = prect->x1;
  271. if (prect->x2 < x2) x2 = prect->x2;
  272. /* draw hline */
  273. dc->device->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
  274. }
  275. else for (index = 0; index < rtgui_region_num_rects(&(dc->owner->clip)); index ++)
  276. {
  277. rtgui_rect_t* prect;
  278. register rt_base_t draw_x1, draw_x2;
  279. prect = ((rtgui_rect_t *)(dc->owner->clip.data + index + 1));
  280. draw_x1 = x1;
  281. draw_x2 = x2;
  282. /* calculate hline clip */
  283. if (prect->y1 > y || prect->y2 <= y ) continue;
  284. if (prect->x2 <= x1 || prect->x1 > x2) continue;
  285. if (prect->x1 > x1) draw_x1 = prect->x1;
  286. if (prect->x2 < x2) draw_x2 = prect->x2;
  287. /* draw hline */
  288. dc->device->draw_hline(&(dc->owner->gc.foreground), draw_x1, draw_x2, y);
  289. }
  290. }
  291. static void rtgui_dc_hw_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  292. {
  293. rtgui_color_t foreground;
  294. register rt_base_t index;
  295. struct rtgui_dc_hw* dc;
  296. dc = (struct rtgui_dc_hw*)self;
  297. if (dc == RT_NULL || dc->visible != RT_TRUE) return;
  298. /* save foreground color */
  299. foreground = dc->owner->gc.foreground;
  300. /* set background color as foreground color */
  301. dc->owner->gc.foreground = dc->owner->gc.background;
  302. /* fill rect */
  303. for (index = rect->y1; index < rect->y2; index ++)
  304. {
  305. rtgui_dc_hw_draw_hline(self, rect->x1, rect->x2, index);
  306. }
  307. /* restore foreground color */
  308. dc->owner->gc.foreground = foreground;
  309. }
  310. static void rtgui_dc_hw_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  311. {
  312. /* not blit in hardware dc */
  313. return ;
  314. }
  315. static void rtgui_dc_hw_set_color(struct rtgui_dc* self, rtgui_color_t color)
  316. {
  317. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  318. if (self != RT_NULL)
  319. {
  320. dc->owner->gc.foreground = color;
  321. }
  322. }
  323. static rtgui_color_t rtgui_dc_hw_get_color(struct rtgui_dc* self)
  324. {
  325. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  326. return self != RT_NULL? dc->owner->gc.foreground : white;
  327. }
  328. static void rtgui_dc_hw_set_font(struct rtgui_dc* self, rtgui_font_t* font)
  329. {
  330. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  331. if (self != RT_NULL)
  332. {
  333. dc->owner->gc.font = font;
  334. }
  335. }
  336. static rtgui_font_t* rtgui_dc_hw_get_font(struct rtgui_dc* self)
  337. {
  338. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  339. return self != RT_NULL? dc->owner->gc.font : RT_NULL;
  340. }
  341. static void rtgui_dc_hw_set_textalign(struct rtgui_dc* self, rt_int32_t textalign)
  342. {
  343. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  344. dc->owner->gc.textalign = textalign;
  345. }
  346. static rt_int32_t rtgui_dc_hw_get_textalign(struct rtgui_dc* self)
  347. {
  348. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  349. return dc->owner->gc.textalign;
  350. }
  351. static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* self)
  352. {
  353. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  354. return dc->visible;
  355. }
  356. static void rtgui_dc_hw_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  357. {
  358. struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self;
  359. rtgui_widget_get_rect(dc->owner, rect);
  360. }