1
0

dc_client.c 14 KB

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