dc_hw.c 12 KB

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