1
0

dc_hw.c 12 KB

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