dc_hw.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/rtgui_app.h>
  19. #include <rtgui/rtgui_server.h>
  20. #include <rtgui/widgets/container.h>
  21. #include <rtgui/widgets/window.h>
  22. #define _int_swap(x, y) do {x ^= y; y ^= x; x ^= y;} while (0)
  23. static void rtgui_dc_hw_draw_point(struct rtgui_dc *dc, int x, int y);
  24. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *dc, int x, int y, rtgui_color_t color);
  25. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *dc, int x1, int x2, int y);
  26. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *dc, int x, int y1, int y2);
  27. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *dc, rtgui_rect_t *rect);
  28. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data);
  29. static void rtgui_dc_hw_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect);
  30. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc);
  31. const struct rtgui_dc_engine dc_hw_engine =
  32. {
  33. rtgui_dc_hw_draw_point,
  34. rtgui_dc_hw_draw_color_point,
  35. rtgui_dc_hw_draw_vline,
  36. rtgui_dc_hw_draw_hline,
  37. rtgui_dc_hw_fill_rect,
  38. rtgui_dc_hw_blit_line,
  39. rtgui_dc_hw_blit,
  40. rtgui_dc_hw_fini,
  41. };
  42. struct rtgui_dc *rtgui_dc_hw_create(rtgui_widget_t *owner)
  43. {
  44. struct rtgui_dc_hw *dc;
  45. /* adjudge owner */
  46. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  47. /* create DC */
  48. dc = (struct rtgui_dc_hw *) rtgui_malloc(sizeof(struct rtgui_dc_hw));
  49. if (dc)
  50. {
  51. dc->parent.type = RTGUI_DC_HW;
  52. dc->parent.engine = &dc_hw_engine;
  53. dc->owner = owner;
  54. dc->hw_driver = rtgui_graphic_driver_get_default();
  55. return &(dc->parent);
  56. }
  57. return RT_NULL;
  58. }
  59. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc)
  60. {
  61. if (dc == RT_NULL || dc->type != RTGUI_DC_HW) return RT_FALSE;
  62. /* release hardware dc */
  63. rtgui_free(dc);
  64. return RT_TRUE;
  65. }
  66. /*
  67. * draw a logic point on device
  68. */
  69. static void rtgui_dc_hw_draw_point(struct rtgui_dc *self, int x, int y)
  70. {
  71. struct rtgui_dc_hw *dc;
  72. RT_ASSERT(self != RT_NULL);
  73. dc = (struct rtgui_dc_hw *) self;
  74. if (x < 0 || y < 0)
  75. return;
  76. x = x + dc->owner->extent.x1;
  77. if (x >= dc->owner->extent.x2)
  78. return;
  79. y = y + dc->owner->extent.y1;
  80. if (y >= dc->owner->extent.y2)
  81. return;
  82. /* draw this point */
  83. dc->hw_driver->ops->set_pixel(&(dc->owner->gc.foreground), x, y);
  84. }
  85. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *self, int x, int y, rtgui_color_t color)
  86. {
  87. struct rtgui_dc_hw *dc;
  88. RT_ASSERT(self != RT_NULL);
  89. dc = (struct rtgui_dc_hw *) self;
  90. if (x < 0 || y < 0)
  91. return;
  92. x = x + dc->owner->extent.x1;
  93. if (x >= dc->owner->extent.x2)
  94. return;
  95. y = y + dc->owner->extent.y1;
  96. if (y >= dc->owner->extent.y2)
  97. return;
  98. /* draw this point */
  99. dc->hw_driver->ops->set_pixel(&color, x, y);
  100. }
  101. /*
  102. * draw a logic vertical line on device
  103. */
  104. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *self, int x, int y1, int y2)
  105. {
  106. struct rtgui_dc_hw *dc;
  107. RT_ASSERT(self != RT_NULL);
  108. dc = (struct rtgui_dc_hw *) self;
  109. if (x < 0)
  110. return;
  111. x = x + dc->owner->extent.x1;
  112. if (x >= dc->owner->extent.x2)
  113. return;
  114. y1 = y1 + dc->owner->extent.y1;
  115. y2 = y2 + dc->owner->extent.y1;
  116. if (y1 > y2)
  117. _int_swap(y1, y2);
  118. if (y1 > dc->owner->extent.y2 || y2 < dc->owner->extent.y1)
  119. return;
  120. if (y1 < dc->owner->extent.y1)
  121. y1 = dc->owner->extent.y1;
  122. if (y2 > dc->owner->extent.y2)
  123. y2 = dc->owner->extent.y2;
  124. /* draw vline */
  125. dc->hw_driver->ops->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
  126. }
  127. /*
  128. * draw a logic horizontal line on device
  129. */
  130. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *self, int x1, int x2, int y)
  131. {
  132. struct rtgui_dc_hw *dc;
  133. RT_ASSERT(self != RT_NULL);
  134. dc = (struct rtgui_dc_hw *) self;
  135. if (y < 0)
  136. return;
  137. y = y + dc->owner->extent.y1;
  138. if (y >= dc->owner->extent.y2)
  139. return;
  140. /* convert logic to device */
  141. x1 = x1 + dc->owner->extent.x1;
  142. x2 = x2 + dc->owner->extent.x1;
  143. if (x1 > x2)
  144. _int_swap(x1, x2);
  145. if (x1 > dc->owner->extent.x2 || x2 < dc->owner->extent.x1)
  146. return;
  147. if (x1 < dc->owner->extent.x1)
  148. x1 = dc->owner->extent.x1;
  149. if (x2 > dc->owner->extent.x2)
  150. x2 = dc->owner->extent.x2;
  151. /* draw hline */
  152. dc->hw_driver->ops->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
  153. }
  154. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect)
  155. {
  156. rtgui_color_t color;
  157. register rt_base_t y1, y2, x1, x2;
  158. struct rtgui_dc_hw *dc;
  159. RT_ASSERT(self != RT_NULL);
  160. RT_ASSERT(rect);
  161. dc = (struct rtgui_dc_hw *) self;
  162. /* get background color */
  163. color = dc->owner->gc.background;
  164. /* convert logic to device */
  165. x1 = rect->x1 + dc->owner->extent.x1;
  166. if (x1 > dc->owner->extent.x2)
  167. return;
  168. if (x1 < dc->owner->extent.x1)
  169. x1 = dc->owner->extent.x1;
  170. x2 = rect->x2 + dc->owner->extent.x1;
  171. if (x2 < dc->owner->extent.x1)
  172. return;
  173. if (x2 > dc->owner->extent.x2)
  174. x2 = dc->owner->extent.x2;
  175. y1 = rect->y1 + dc->owner->extent.y1;
  176. if (y1 > dc->owner->extent.y2)
  177. return;
  178. if (y1 < dc->owner->extent.y1)
  179. y1 = dc->owner->extent.y1;
  180. y2 = rect->y2 + dc->owner->extent.y1;
  181. if (y2 < dc->owner->extent.y1)
  182. return;
  183. if (y2 > dc->owner->extent.y2)
  184. y2 = dc->owner->extent.y2;
  185. /* fill rect */
  186. for (; y1 < y2; y1++)
  187. {
  188. dc->hw_driver->ops->draw_hline(&color, x1, x2, y1);
  189. }
  190. }
  191. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  192. {
  193. struct rtgui_dc_hw *dc;
  194. RT_ASSERT(self != RT_NULL);
  195. dc = (struct rtgui_dc_hw *) self;
  196. /* convert logic to device */
  197. if (y < 0)
  198. return;
  199. y = y + dc->owner->extent.y1;
  200. if (y > dc->owner->extent.y2)
  201. return;
  202. x1 = x1 + dc->owner->extent.x1;
  203. x2 = x2 + dc->owner->extent.x1;
  204. if (x1 > x2)
  205. _int_swap(x1, x2);
  206. if (x1 > dc->owner->extent.x2 || x2 < dc->owner->extent.x1)
  207. return;
  208. if (x1 < dc->owner->extent.x1)
  209. x1 = dc->owner->extent.x1;
  210. if (x2 > dc->owner->extent.x2)
  211. x2 = dc->owner->extent.x2;
  212. dc->hw_driver->ops->draw_raw_hline(line_data, x1, x2, y);
  213. }
  214. static void rtgui_dc_hw_blit(struct rtgui_dc *dc,
  215. struct rtgui_point *dc_point,
  216. struct rtgui_dc *dest,
  217. rtgui_rect_t *rect)
  218. {
  219. /* not blit in hardware dc */
  220. return ;
  221. }