dc_client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * File : dc_client.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. * 2010-08-09 Bernard rename hardware dc to client dc
  14. */
  15. #include <rtgui/dc.h>
  16. #include <rtgui/dc_hw.h>
  17. #include <rtgui/dc_client.h>
  18. #include <rtgui/driver.h>
  19. #include <rtgui/rtgui_system.h>
  20. #include <rtgui/widgets/view.h>
  21. #include <rtgui/widgets/window.h>
  22. #include <rtgui/widgets/workbench.h>
  23. #include <rtgui/widgets/title.h>
  24. static void rtgui_dc_client_draw_point(struct rtgui_dc* dc, int x, int y);
  25. static void rtgui_dc_client_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color);
  26. static void rtgui_dc_client_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y);
  27. static void rtgui_dc_client_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2);
  28. static void rtgui_dc_client_fill_rect (struct rtgui_dc* dc, rtgui_rect_t* rect);
  29. static void rtgui_dc_client_blit_line (struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data);
  30. static void rtgui_dc_client_blit (struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect);
  31. static void rtgui_dc_client_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc);
  32. static rtgui_gc_t *rtgui_dc_client_get_gc (struct rtgui_dc* dc);
  33. static rt_bool_t rtgui_dc_client_fini(struct rtgui_dc* dc);
  34. static rt_bool_t rtgui_dc_client_get_visible(struct rtgui_dc* dc);
  35. static void rtgui_dc_client_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect);
  36. #define hw_driver (rtgui_graphic_driver_get_default())
  37. #define dc_set_foreground(c) dc->gc.foreground = c
  38. #define dc_set_background(c) dc->gc.background = c
  39. struct rtgui_dc* rtgui_dc_begin_drawing(rtgui_widget_t* owner)
  40. {
  41. if (rtgui_region_is_flat(&owner->clip) == RT_EOK)
  42. {
  43. /* use hardware DC */
  44. return rtgui_dc_hw_create(owner);
  45. }
  46. return rtgui_dc_client_create(owner);
  47. }
  48. void rtgui_dc_end_drawing(struct rtgui_dc* dc)
  49. {
  50. dc->engine->fini(dc);
  51. }
  52. const struct rtgui_dc_engine dc_client_engine =
  53. {
  54. rtgui_dc_client_draw_point,
  55. rtgui_dc_client_draw_color_point,
  56. rtgui_dc_client_draw_vline,
  57. rtgui_dc_client_draw_hline,
  58. rtgui_dc_client_fill_rect,
  59. rtgui_dc_client_blit_line,
  60. rtgui_dc_client_blit,
  61. rtgui_dc_client_set_gc,
  62. rtgui_dc_client_get_gc,
  63. rtgui_dc_client_get_visible,
  64. rtgui_dc_client_get_rect,
  65. rtgui_dc_client_fini,
  66. };
  67. void rtgui_dc_client_init(rtgui_widget_t* owner)
  68. {
  69. struct rtgui_dc* dc;
  70. RT_ASSERT(owner != RT_NULL);
  71. dc = RTGUI_WIDGET_DC(owner);
  72. dc->type = RTGUI_DC_CLIENT;
  73. dc->engine = &dc_client_engine;
  74. }
  75. extern struct rt_mutex cursor_mutex;
  76. extern void rtgui_mouse_show_cursor(void);
  77. extern void rtgui_mouse_hide_cursor(void);
  78. struct rtgui_dc* rtgui_dc_client_create(rtgui_widget_t* owner)
  79. {
  80. struct rtgui_dc* dc;
  81. rtgui_widget_t* widget;
  82. /* adjudge owner */
  83. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  84. if (!RTGUI_IS_TOPLEVEL(owner->toplevel)) return RT_NULL;
  85. dc = RTGUI_WIDGET_DC(owner);
  86. /* set init visible as true */
  87. RTGUI_WIDGET_DC_SET_VISIBLE(owner);
  88. /* check widget visible */
  89. widget = owner;
  90. while (widget != RT_NULL)
  91. {
  92. if (RTGUI_WIDGET_IS_HIDE(widget))
  93. {
  94. RTGUI_WIDGET_DC_SET_UNVISIBLE(owner);
  95. break;
  96. }
  97. widget = widget->parent;
  98. }
  99. if (RTGUI_IS_WINTITLE(owner->toplevel))
  100. {
  101. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(owner->toplevel);
  102. top->drawing ++;
  103. if (top->drawing == 1)
  104. {
  105. #ifdef RTGUI_USING_MOUSE_CURSOR
  106. #ifdef __WIN32__
  107. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  108. rt_kprintf("hide cursor\n");
  109. rtgui_mouse_hide_cursor();
  110. #else
  111. /* hide cursor */
  112. rtgui_mouse_hide_cursor();
  113. #endif
  114. #endif
  115. }
  116. }
  117. else if (RTGUI_IS_WORKBENCH(owner->toplevel) ||
  118. RTGUI_IS_WIN(owner->toplevel))
  119. {
  120. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(owner->toplevel);
  121. top->drawing ++;
  122. if (top->drawing == 1)
  123. {
  124. #ifdef __WIN32__
  125. #ifdef RTGUI_USING_MOUSE_CURSOR
  126. rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
  127. rt_kprintf("hide cursor\n");
  128. rtgui_mouse_hide_cursor();
  129. #endif
  130. #else
  131. /* send draw begin to server */
  132. struct rtgui_event_update_begin eupdate;
  133. RTGUI_EVENT_UPDATE_BEGIN_INIT(&(eupdate));
  134. eupdate.rect = RTGUI_WIDGET(top)->extent;
  135. rtgui_thread_send(top->server, (struct rtgui_event*)&eupdate, sizeof(eupdate));
  136. #endif
  137. }
  138. }
  139. return dc;
  140. }
  141. static rt_bool_t rtgui_dc_client_fini(struct rtgui_dc* dc)
  142. {
  143. rtgui_widget_t* owner;
  144. if (dc == RT_NULL || dc->type != RTGUI_DC_CLIENT) return RT_FALSE;
  145. /* get owner */
  146. owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
  147. if (RTGUI_IS_WINTITLE(owner->toplevel))
  148. {
  149. /* update title extent */
  150. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(owner->toplevel);
  151. top->drawing --;
  152. if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
  153. {
  154. #ifdef __WIN32__
  155. #ifdef RTGUI_USING_MOUSE_CURSOR
  156. rt_mutex_release(&cursor_mutex);
  157. /* show cursor */
  158. rtgui_mouse_show_cursor();
  159. rt_kprintf("show cursor\n");
  160. #endif
  161. /* update screen */
  162. hw_driver->screen_update(&(owner->extent));
  163. #else
  164. #ifdef RTGUI_USING_MOUSE_CURSOR
  165. /* show cursor */
  166. rtgui_mouse_show_cursor();
  167. #endif
  168. /* update screen */
  169. hw_driver->screen_update(&(owner->extent));
  170. #endif
  171. }
  172. }
  173. else if (RTGUI_IS_WORKBENCH(owner->toplevel) ||
  174. RTGUI_IS_WIN(owner->toplevel))
  175. {
  176. rtgui_toplevel_t* top = RTGUI_TOPLEVEL(owner->toplevel);
  177. top->drawing --;
  178. if ((top->drawing == 0) && RTGUI_WIDGET_IS_DC_VISIBLE(owner))
  179. {
  180. #ifdef __WIN32__
  181. #ifdef RTGUI_USING_MOUSE_CURSOR
  182. rt_mutex_release(&cursor_mutex);
  183. /* show cursor */
  184. rtgui_mouse_show_cursor();
  185. rt_kprintf("show cursor\n");
  186. #endif
  187. /* update screen */
  188. hw_driver->screen_update(&(owner->extent));
  189. #else
  190. /* send to server to end drawing */
  191. struct rtgui_event_update_end eupdate;
  192. RTGUI_EVENT_UPDATE_END_INIT(&(eupdate));
  193. eupdate.rect = owner->extent;
  194. rtgui_thread_send(top->server, (struct rtgui_event*)&eupdate, sizeof(eupdate));
  195. #endif
  196. }
  197. }
  198. return RT_TRUE;
  199. }
  200. /*
  201. * draw a logic point on device
  202. */
  203. static void rtgui_dc_client_draw_point(struct rtgui_dc* self, int x, int y)
  204. {
  205. rtgui_rect_t rect;
  206. rtgui_widget_t *owner;
  207. if (self == RT_NULL) return;
  208. /* get owner */
  209. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  210. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  211. x = x + owner->extent.x1;
  212. y = y + owner->extent.y1;
  213. if (rtgui_region_contains_point(&(owner->clip), x, y, &rect) == RT_EOK)
  214. {
  215. /* draw this point */
  216. hw_driver->set_pixel(&(owner->gc.foreground), x, y);
  217. }
  218. }
  219. static void rtgui_dc_client_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color)
  220. {
  221. rtgui_rect_t rect;
  222. rtgui_widget_t *owner;
  223. if (self == RT_NULL) return;
  224. /* get owner */
  225. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  226. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  227. x = x + owner->extent.x1;
  228. y = y + owner->extent.y1;
  229. if (rtgui_region_contains_point(&(owner->clip), x, y, &rect) == RT_EOK)
  230. {
  231. /* draw this point */
  232. hw_driver->set_pixel(&color, x, y);
  233. }
  234. }
  235. /*
  236. * draw a logic vertical line on device
  237. */
  238. static void rtgui_dc_client_draw_vline(struct rtgui_dc* self, int x, int y1, int y2)
  239. {
  240. register rt_base_t index;
  241. rtgui_widget_t *owner;
  242. if (self == RT_NULL) return;
  243. /* get owner */
  244. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  245. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  246. x = x + owner->extent.x1;
  247. y1 = y1 + owner->extent.y1;
  248. y2 = y2 + owner->extent.y1;
  249. if (owner->clip.data == RT_NULL)
  250. {
  251. rtgui_rect_t* prect;
  252. prect = &(owner->clip.extents);
  253. /* calculate vline intersect */
  254. if (prect->x1 > x || prect->x2 <= x) return;
  255. if (prect->y2 <= y1 || prect->y1 > y2) return;
  256. if (prect->y1 > y1) y1 = prect->y1;
  257. if (prect->y2 < y2) y2 = prect->y2;
  258. /* draw vline */
  259. hw_driver->draw_vline(&(owner->gc.foreground), x, y1, y2);
  260. }
  261. else for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  262. {
  263. rtgui_rect_t* prect;
  264. register rt_base_t draw_y1, draw_y2;
  265. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  266. draw_y1 = y1;
  267. draw_y2 = y2;
  268. /* calculate vline clip */
  269. if (prect->x1 > x || prect->x2 <= x) continue;
  270. if (prect->y2 <= y1 || prect->y1 > y2) continue;
  271. if (prect->y1 > y1) draw_y1 = prect->y1;
  272. if (prect->y2 < y2) draw_y2 = prect->y2;
  273. /* draw vline */
  274. hw_driver->draw_vline(&(owner->gc.foreground), x, draw_y1, draw_y2);
  275. }
  276. }
  277. /*
  278. * draw a logic horizontal line on device
  279. */
  280. static void rtgui_dc_client_draw_hline(struct rtgui_dc* self, int x1, int x2, int y)
  281. {
  282. register rt_base_t index;
  283. rtgui_widget_t *owner;
  284. if (self == RT_NULL) return;
  285. /* get owner */
  286. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  287. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  288. /* convert logic to device */
  289. x1 = x1 + owner->extent.x1;
  290. x2 = x2 + owner->extent.x1;
  291. y = y + owner->extent.y1;
  292. if (owner->clip.data == RT_NULL)
  293. {
  294. rtgui_rect_t* prect;
  295. prect = &(owner->clip.extents);
  296. /* calculate vline intersect */
  297. if (prect->y1 > y || prect->y2 <= y ) return;
  298. if (prect->x2 <= x1 || prect->x1 > x2) return;
  299. if (prect->x1 > x1) x1 = prect->x1;
  300. if (prect->x2 < x2) x2 = prect->x2;
  301. /* draw hline */
  302. hw_driver->draw_hline(&(owner->gc.foreground), x1, x2, y);
  303. }
  304. else for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  305. {
  306. rtgui_rect_t* prect;
  307. register rt_base_t draw_x1, draw_x2;
  308. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  309. draw_x1 = x1;
  310. draw_x2 = x2;
  311. /* calculate hline clip */
  312. if (prect->y1 > y || prect->y2 <= y ) continue;
  313. if (prect->x2 <= x1 || prect->x1 > x2) continue;
  314. if (prect->x1 > x1) draw_x1 = prect->x1;
  315. if (prect->x2 < x2) draw_x2 = prect->x2;
  316. /* draw hline */
  317. hw_driver->draw_hline(&(owner->gc.foreground), draw_x1, draw_x2, y);
  318. }
  319. }
  320. static void rtgui_dc_client_fill_rect (struct rtgui_dc* self, struct rtgui_rect* rect)
  321. {
  322. rtgui_color_t foreground;
  323. register rt_base_t index;
  324. rtgui_widget_t *owner;
  325. if (self == RT_NULL) return;
  326. /* get owner */
  327. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  328. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  329. /* save foreground color */
  330. foreground = owner->gc.foreground;
  331. /* set background color as foreground color */
  332. owner->gc.foreground = owner->gc.background;
  333. /* fill rect */
  334. for (index = rect->y1; index < rect->y2; index ++)
  335. {
  336. rtgui_dc_client_draw_hline(self, rect->x1, rect->x2, index);
  337. }
  338. /* restore foreground color */
  339. owner->gc.foreground = foreground;
  340. }
  341. static void rtgui_dc_client_blit_line (struct rtgui_dc* self, int x1, int x2, int y, rt_uint8_t* line_data)
  342. {
  343. register rt_base_t index;
  344. rtgui_widget_t *owner;
  345. if (self == RT_NULL) return;
  346. /* get owner */
  347. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  348. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return;
  349. /* convert logic to device */
  350. x1 = x1 + owner->extent.x1;
  351. x2 = x2 + owner->extent.x1;
  352. y = y + owner->extent.y1;
  353. if (rtgui_region_is_flat(&(owner->clip)) == RT_EOK)
  354. {
  355. rtgui_rect_t* prect;
  356. prect = &(owner->clip.extents);
  357. /* calculate vline intersect */
  358. if (prect->y1 > y || prect->y2 <= y ) return;
  359. if (prect->x2 <= x1 || prect->x1 > x2) return;
  360. if (prect->x1 > x1) x1 = prect->x1;
  361. if (prect->x2 < x2) x2 = prect->x2;
  362. /* draw hline */
  363. hw_driver->draw_raw_hline(line_data, x1, x2, y);
  364. }
  365. else for (index = 0; index < rtgui_region_num_rects(&(owner->clip)); index ++)
  366. {
  367. rtgui_rect_t* prect;
  368. register rt_base_t draw_x1, draw_x2;
  369. prect = ((rtgui_rect_t *)(owner->clip.data + index + 1));
  370. draw_x1 = x1;
  371. draw_x2 = x2;
  372. /* calculate hline clip */
  373. if (prect->y1 > y || prect->y2 <= y ) continue;
  374. if (prect->x2 <= x1 || prect->x1 > x2) continue;
  375. if (prect->x1 > x1) draw_x1 = prect->x1;
  376. if (prect->x2 < x2) draw_x2 = prect->x2;
  377. /* draw hline */
  378. hw_driver->draw_raw_hline(line_data, draw_x1, draw_x2, y);
  379. }
  380. }
  381. static void rtgui_dc_client_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect)
  382. {
  383. /* not blit in hardware dc */
  384. return ;
  385. }
  386. static void rtgui_dc_client_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc)
  387. {
  388. rtgui_widget_t *owner;
  389. if (self == RT_NULL) return;
  390. /* get owner */
  391. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  392. owner->gc = *gc;
  393. }
  394. static rtgui_gc_t* rtgui_dc_client_get_gc(struct rtgui_dc* self)
  395. {
  396. rtgui_widget_t *owner;
  397. if (self == RT_NULL)
  398. {
  399. rt_kprintf("why!!\n");
  400. return RT_NULL;
  401. }
  402. /* get owner */
  403. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  404. return &(owner->gc);
  405. }
  406. static rt_bool_t rtgui_dc_client_get_visible(struct rtgui_dc* self)
  407. {
  408. rtgui_widget_t *owner;
  409. if (self == RT_NULL) return RT_FALSE;
  410. /* get owner */
  411. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  412. if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) return RT_FALSE;
  413. return RT_TRUE;
  414. }
  415. static void rtgui_dc_client_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect)
  416. {
  417. rtgui_widget_t *owner;
  418. if (self == RT_NULL) return;
  419. /* get owner */
  420. owner = RTGUI_CONTAINER_OF(self, struct rtgui_widget, dc_type);
  421. rtgui_widget_get_rect(owner, rect);
  422. }