dc_client.c 13 KB

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