dc_hw.c 12 KB

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