dc_hw.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * File : dc_hw.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. */
  24. #include <rtgui/dc.h>
  25. #include <rtgui/driver.h>
  26. #include <rtgui/rtgui_system.h>
  27. #include <rtgui/rtgui_app.h>
  28. #include <rtgui/rtgui_server.h>
  29. #include <rtgui/widgets/container.h>
  30. #include <rtgui/widgets/window.h>
  31. #define _int_swap(x, y) do {x ^= y; y ^= x; x ^= y;} while (0)
  32. static void rtgui_dc_hw_draw_point(struct rtgui_dc *dc, int x, int y);
  33. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *dc, int x, int y, rtgui_color_t color);
  34. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *dc, int x1, int x2, int y);
  35. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *dc, int x, int y1, int y2);
  36. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *dc, rtgui_rect_t *rect);
  37. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data);
  38. static void rtgui_dc_hw_blit(struct rtgui_dc *dc, struct rtgui_point *dc_point, struct rtgui_dc *dest, rtgui_rect_t *rect);
  39. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc);
  40. const struct rtgui_dc_engine dc_hw_engine =
  41. {
  42. rtgui_dc_hw_draw_point,
  43. rtgui_dc_hw_draw_color_point,
  44. rtgui_dc_hw_draw_vline,
  45. rtgui_dc_hw_draw_hline,
  46. rtgui_dc_hw_fill_rect,
  47. rtgui_dc_hw_blit_line,
  48. rtgui_dc_hw_blit,
  49. rtgui_dc_hw_fini,
  50. };
  51. struct rtgui_dc *rtgui_dc_hw_create(rtgui_widget_t *owner)
  52. {
  53. struct rtgui_dc_hw *dc;
  54. /* adjudge owner */
  55. if (owner == RT_NULL || owner->toplevel == RT_NULL) return RT_NULL;
  56. /* create DC */
  57. dc = (struct rtgui_dc_hw *) rtgui_malloc(sizeof(struct rtgui_dc_hw));
  58. if (dc)
  59. {
  60. dc->parent.type = RTGUI_DC_HW;
  61. dc->parent.engine = &dc_hw_engine;
  62. dc->owner = owner;
  63. dc->hw_driver = rtgui_graphic_driver_get_default();
  64. return &(dc->parent);
  65. }
  66. return RT_NULL;
  67. }
  68. static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc *dc)
  69. {
  70. if (dc == RT_NULL || dc->type != RTGUI_DC_HW) return RT_FALSE;
  71. /* release hardware dc */
  72. rtgui_free(dc);
  73. return RT_TRUE;
  74. }
  75. /*
  76. * draw a logic point on device
  77. */
  78. static void rtgui_dc_hw_draw_point(struct rtgui_dc *self, int x, int y)
  79. {
  80. struct rtgui_dc_hw *dc;
  81. RT_ASSERT(self != RT_NULL);
  82. dc = (struct rtgui_dc_hw *) self;
  83. if (x < 0 || y < 0)
  84. return;
  85. x = x + dc->owner->extent.x1;
  86. if (x >= dc->owner->extent.x2)
  87. return;
  88. y = y + dc->owner->extent.y1;
  89. if (y >= dc->owner->extent.y2)
  90. return;
  91. /* draw this point */
  92. dc->hw_driver->ops->set_pixel(&(dc->owner->gc.foreground), x, y);
  93. }
  94. static void rtgui_dc_hw_draw_color_point(struct rtgui_dc *self, int x, int y, rtgui_color_t color)
  95. {
  96. struct rtgui_dc_hw *dc;
  97. RT_ASSERT(self != RT_NULL);
  98. dc = (struct rtgui_dc_hw *) self;
  99. if (x < 0 || y < 0)
  100. return;
  101. x = x + dc->owner->extent.x1;
  102. if (x >= dc->owner->extent.x2)
  103. return;
  104. y = y + dc->owner->extent.y1;
  105. if (y >= dc->owner->extent.y2)
  106. return;
  107. /* draw this point */
  108. dc->hw_driver->ops->set_pixel(&color, x, y);
  109. }
  110. /*
  111. * draw a logic vertical line on device
  112. */
  113. static void rtgui_dc_hw_draw_vline(struct rtgui_dc *self, int x, int y1, int y2)
  114. {
  115. struct rtgui_dc_hw *dc;
  116. RT_ASSERT(self != RT_NULL);
  117. dc = (struct rtgui_dc_hw *) self;
  118. if (x < 0)
  119. return;
  120. x = x + dc->owner->extent.x1;
  121. if (x >= dc->owner->extent.x2)
  122. return;
  123. y1 = y1 + dc->owner->extent.y1;
  124. y2 = y2 + dc->owner->extent.y1;
  125. if (y1 > y2)
  126. _int_swap(y1, y2);
  127. if (y1 > dc->owner->extent.y2 || y2 < dc->owner->extent.y1)
  128. return;
  129. if (y1 < dc->owner->extent.y1)
  130. y1 = dc->owner->extent.y1;
  131. if (y2 > dc->owner->extent.y2)
  132. y2 = dc->owner->extent.y2;
  133. /* draw vline */
  134. dc->hw_driver->ops->draw_vline(&(dc->owner->gc.foreground), x, y1, y2);
  135. }
  136. /*
  137. * draw a logic horizontal line on device
  138. */
  139. static void rtgui_dc_hw_draw_hline(struct rtgui_dc *self, int x1, int x2, int y)
  140. {
  141. struct rtgui_dc_hw *dc;
  142. RT_ASSERT(self != RT_NULL);
  143. dc = (struct rtgui_dc_hw *) self;
  144. if (y < 0)
  145. return;
  146. y = y + dc->owner->extent.y1;
  147. if (y >= dc->owner->extent.y2)
  148. return;
  149. /* convert logic to device */
  150. x1 = x1 + dc->owner->extent.x1;
  151. x2 = x2 + dc->owner->extent.x1;
  152. if (x1 > x2)
  153. _int_swap(x1, x2);
  154. if (x1 > dc->owner->extent.x2 || x2 < dc->owner->extent.x1)
  155. return;
  156. if (x1 < dc->owner->extent.x1)
  157. x1 = dc->owner->extent.x1;
  158. if (x2 > dc->owner->extent.x2)
  159. x2 = dc->owner->extent.x2;
  160. /* draw hline */
  161. dc->hw_driver->ops->draw_hline(&(dc->owner->gc.foreground), x1, x2, y);
  162. }
  163. static void rtgui_dc_hw_fill_rect(struct rtgui_dc *self, struct rtgui_rect *rect)
  164. {
  165. rtgui_color_t color;
  166. register rt_base_t y1, y2, x1, x2;
  167. struct rtgui_dc_hw *dc;
  168. RT_ASSERT(self != RT_NULL);
  169. RT_ASSERT(rect);
  170. dc = (struct rtgui_dc_hw *) self;
  171. /* get background color */
  172. color = dc->owner->gc.background;
  173. /* convert logic to device */
  174. x1 = rect->x1 + dc->owner->extent.x1;
  175. if (x1 > dc->owner->extent.x2)
  176. return;
  177. if (x1 < dc->owner->extent.x1)
  178. x1 = dc->owner->extent.x1;
  179. x2 = rect->x2 + dc->owner->extent.x1;
  180. if (x2 < dc->owner->extent.x1)
  181. return;
  182. if (x2 > dc->owner->extent.x2)
  183. x2 = dc->owner->extent.x2;
  184. y1 = rect->y1 + dc->owner->extent.y1;
  185. if (y1 > dc->owner->extent.y2)
  186. return;
  187. if (y1 < dc->owner->extent.y1)
  188. y1 = dc->owner->extent.y1;
  189. y2 = rect->y2 + dc->owner->extent.y1;
  190. if (y2 < dc->owner->extent.y1)
  191. return;
  192. if (y2 > dc->owner->extent.y2)
  193. y2 = dc->owner->extent.y2;
  194. /* fill rect */
  195. for (; y1 < y2; y1++)
  196. {
  197. dc->hw_driver->ops->draw_hline(&color, x1, x2, y1);
  198. }
  199. }
  200. static void rtgui_dc_hw_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  201. {
  202. struct rtgui_dc_hw *dc;
  203. RT_ASSERT(self != RT_NULL);
  204. dc = (struct rtgui_dc_hw *) self;
  205. /* convert logic to device */
  206. if (y < 0)
  207. return;
  208. y = y + dc->owner->extent.y1;
  209. if (y > dc->owner->extent.y2)
  210. return;
  211. x1 = x1 + dc->owner->extent.x1;
  212. x2 = x2 + dc->owner->extent.x1;
  213. if (x1 > x2)
  214. _int_swap(x1, x2);
  215. if (x1 > dc->owner->extent.x2 || x2 < dc->owner->extent.x1)
  216. return;
  217. if (x1 < dc->owner->extent.x1)
  218. x1 = dc->owner->extent.x1;
  219. if (x2 > dc->owner->extent.x2)
  220. x2 = dc->owner->extent.x2;
  221. dc->hw_driver->ops->draw_raw_hline(line_data, x1, x2, y);
  222. }
  223. static void rtgui_dc_hw_blit(struct rtgui_dc *dc,
  224. struct rtgui_point *dc_point,
  225. struct rtgui_dc *dest,
  226. rtgui_rect_t *rect)
  227. {
  228. /* not blit in hardware dc */
  229. return ;
  230. }