dc_hw.c 13 KB

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