dc_buffer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * File : dc_buffer.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/rtgui.h>
  25. #include <rtgui/dc.h>
  26. #include <rtgui/blit.h>
  27. #include <rtgui/color.h>
  28. #include <rtgui/rtgui_system.h>
  29. #include <rtgui/dc_draw.h>
  30. #include <rtgui/image_container.h>
  31. #include <string.h>
  32. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc *dc);
  33. static void rtgui_dc_buffer_draw_point(struct rtgui_dc *dc, int x, int y);
  34. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc *dc, int x, int y, rtgui_color_t color);
  35. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc *dc, int x, int y1, int y2);
  36. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc *dc, int x1, int x2, int y);
  37. static void rtgui_dc_buffer_fill_rect(struct rtgui_dc *dc, struct rtgui_rect *rect);
  38. static void rtgui_dc_buffer_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data);
  39. static void rtgui_dc_buffer_blit(struct rtgui_dc *self, struct rtgui_point *dc_point,
  40. struct rtgui_dc *dest, rtgui_rect_t *rect);
  41. const struct rtgui_dc_engine dc_buffer_engine =
  42. {
  43. rtgui_dc_buffer_draw_point,
  44. rtgui_dc_buffer_draw_color_point,
  45. rtgui_dc_buffer_draw_vline,
  46. rtgui_dc_buffer_draw_hline,
  47. rtgui_dc_buffer_fill_rect,
  48. rtgui_dc_buffer_blit_line,
  49. rtgui_dc_buffer_blit,
  50. rtgui_dc_buffer_fini,
  51. };
  52. #define _dc_get_pitch(dc) \
  53. (dc->pitch)
  54. #define _dc_get_pixel(dc, x, y) \
  55. ((dc)->pixel + (y) * (dc)->pitch + (x) * rtgui_color_get_bpp((dc)->pixel_format))
  56. #define _dc_get_bits_per_pixel(dc) \
  57. rtgui_color_get_bits(dc->pixel_format)
  58. #define _hw_get_pixel(dst, x, y, type) \
  59. (type *)((rt_uint8_t*)((dst)->framebuffer) + (y) * (dst)->pitch + (x) * _UI_BITBYTES((dst)->bits_per_pixel))
  60. struct rtgui_dc *rtgui_dc_buffer_create(int w, int h)
  61. {
  62. rt_uint8_t pixel_format;
  63. pixel_format = rtgui_graphic_driver_get_default()->pixel_format;
  64. /* create a dc_buffer with hardware driver pixel format */
  65. return rtgui_dc_buffer_create_pixformat(pixel_format, w, h);
  66. }
  67. RTM_EXPORT(rtgui_dc_buffer_create);
  68. struct rtgui_dc *rtgui_dc_buffer_create_pixformat(rt_uint8_t pixel_format, int w, int h)
  69. {
  70. struct rtgui_dc_buffer *dc;
  71. dc = (struct rtgui_dc_buffer *)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  72. if (dc)
  73. {
  74. dc->parent.type = RTGUI_DC_BUFFER;
  75. dc->parent.engine = &dc_buffer_engine;
  76. dc->gc.foreground = default_foreground;
  77. dc->gc.background = default_background;
  78. dc->gc.font = rtgui_font_default();
  79. dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  80. dc->pixel_format = pixel_format;
  81. dc->pixel_alpha = 255;
  82. dc->width = w;
  83. dc->height = h;
  84. dc->pitch = w * rtgui_color_get_bpp(pixel_format);
  85. #ifdef RTGUI_IMAGE_CONTAINER
  86. dc->image_item = RT_NULL;
  87. #endif
  88. dc->pixel = rtgui_malloc(h * dc->pitch);
  89. if (!dc->pixel)
  90. {
  91. rtgui_free(dc);
  92. return RT_NULL;
  93. }
  94. rt_memset(dc->pixel, 0, h * dc->pitch);
  95. return &(dc->parent);
  96. }
  97. return RT_NULL;
  98. }
  99. RTM_EXPORT(rtgui_dc_buffer_create_pixformat);
  100. #ifdef RTGUI_IMAGE_CONTAINER
  101. struct rtgui_dc *rtgui_img_dc_create_pixformat(rt_uint8_t pixel_format,
  102. rt_uint8_t *pixel, struct rtgui_image_item *image_item)
  103. {
  104. struct rtgui_dc_buffer *dc;
  105. dc = (struct rtgui_dc_buffer *)rtgui_malloc(sizeof(struct rtgui_dc_buffer));
  106. if (dc)
  107. {
  108. dc->parent.type = RTGUI_DC_BUFFER;
  109. dc->parent.engine = &dc_buffer_engine;
  110. dc->gc.foreground = default_foreground;
  111. dc->gc.background = default_background;
  112. dc->gc.font = rtgui_font_default();
  113. dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  114. dc->pixel_format = pixel_format;
  115. dc->pixel_alpha = 255;
  116. dc->width = image_item->image->w;
  117. dc->height = image_item->image->h;
  118. dc->pitch = image_item->image->w * rtgui_color_get_bpp(pixel_format);
  119. dc->image_item = image_item;
  120. dc->pixel = pixel;
  121. return &(dc->parent);
  122. }
  123. return RT_NULL;
  124. }
  125. RTM_EXPORT(rtgui_img_dc_create_pixformat);
  126. #endif
  127. struct rtgui_dc *rtgui_dc_buffer_create_from_dc(struct rtgui_dc* dc)
  128. {
  129. struct rtgui_dc_buffer *buffer;
  130. if (dc == RT_NULL)
  131. return RT_NULL;
  132. if (dc->type == RTGUI_DC_BUFFER)
  133. {
  134. struct rtgui_dc_buffer *d = (struct rtgui_dc_buffer*) dc;
  135. /* buffer clone */
  136. buffer = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(d->pixel_format,
  137. d->width,
  138. d->height);
  139. if (buffer != RT_NULL)
  140. {
  141. memcpy(buffer->pixel, d->pixel, d->pitch * d->height);
  142. d->pixel_alpha = 255;
  143. return RTGUI_DC(buffer);
  144. }
  145. }
  146. return RT_NULL;
  147. }
  148. RTM_EXPORT(rtgui_dc_buffer_create_from_dc);
  149. void rtgui_dc_buffer_set_alpha(struct rtgui_dc* dc, rt_uint8_t pixel_alpha)
  150. {
  151. struct rtgui_dc_buffer *d = (struct rtgui_dc_buffer*) dc;
  152. if (d)
  153. {
  154. d->pixel_alpha = pixel_alpha;
  155. }
  156. }
  157. rt_uint8_t *rtgui_dc_buffer_get_pixel(struct rtgui_dc *dc)
  158. {
  159. struct rtgui_dc_buffer *dc_buffer;
  160. dc_buffer = (struct rtgui_dc_buffer *)dc;
  161. return dc_buffer->pixel;
  162. }
  163. RTM_EXPORT(rtgui_dc_buffer_get_pixel);
  164. static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc *dc)
  165. {
  166. struct rtgui_dc_buffer *buffer = (struct rtgui_dc_buffer *)dc;
  167. if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE;
  168. #ifdef RTGUI_IMAGE_CONTAINER
  169. if (buffer->image_item)
  170. {
  171. rtgui_image_container_put(buffer->image_item);
  172. buffer->pixel = RT_NULL;
  173. }
  174. #endif
  175. if (buffer->pixel)
  176. rtgui_free(buffer->pixel);
  177. return RT_TRUE;
  178. }
  179. static void rtgui_dc_buffer_draw_point(struct rtgui_dc *self, int x, int y)
  180. {
  181. struct rtgui_dc_buffer *dst;
  182. unsigned r, g, b, a;
  183. dst = (struct rtgui_dc_buffer *)self;
  184. /* does not draw point out of dc */
  185. if ((x >= dst->width) || (y >= dst->height)) return;
  186. if (x < 0 || y < 0) return;
  187. r = RTGUI_RGB_R(dst->gc.foreground);
  188. g = RTGUI_RGB_G(dst->gc.foreground);
  189. b = RTGUI_RGB_B(dst->gc.foreground);
  190. a = RTGUI_RGB_A(dst->gc.foreground);
  191. switch (dst->pixel_format)
  192. {
  193. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  194. DRAW_SETPIXELXY_RGB565(x, y);
  195. break;
  196. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  197. DRAW_SETPIXELXY_BGR565(x, y);
  198. break;
  199. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  200. DRAW_SETPIXELXY_RGB888(x, y);
  201. break;
  202. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  203. DRAW_SETPIXELXY_ARGB8888(x, y);
  204. break;
  205. }
  206. }
  207. static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc *self, int x, int y, rtgui_color_t color)
  208. {
  209. struct rtgui_dc_buffer *dst;
  210. unsigned r, g, b, a;
  211. dst = (struct rtgui_dc_buffer *)self;
  212. /* does not draw point out of dc */
  213. if ((x >= dst->width) || (y >= dst->height)) return;
  214. if (x < 0 || y < 0) return;
  215. r = RTGUI_RGB_R(color);
  216. g = RTGUI_RGB_G(color);
  217. b = RTGUI_RGB_B(color);
  218. a = RTGUI_RGB_A(color);
  219. switch (dst->pixel_format)
  220. {
  221. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  222. DRAW_SETPIXELXY_RGB565(x, y);
  223. break;
  224. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  225. DRAW_SETPIXELXY_BGR565(x, y);
  226. break;
  227. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  228. DRAW_SETPIXELXY_RGB888(x, y);
  229. break;
  230. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  231. DRAW_SETPIXELXY_ARGB8888(x, y);
  232. break;
  233. }
  234. }
  235. static void rtgui_dc_buffer_draw_vline(struct rtgui_dc *self, int x1, int y1, int y2)
  236. {
  237. struct rtgui_dc_buffer *dst;
  238. unsigned r, g, b, a;
  239. dst = (struct rtgui_dc_buffer *)self;
  240. if (x1 < 0 || x1 >= dst->width) return;
  241. if (y1 >= dst->height) return;
  242. if (y1 < 0) y1 = 0;
  243. if (y2 > dst->height) y2 = dst->height;
  244. r = RTGUI_RGB_R(dst->gc.foreground);
  245. g = RTGUI_RGB_G(dst->gc.foreground);
  246. b = RTGUI_RGB_B(dst->gc.foreground);
  247. a = RTGUI_RGB_A(dst->gc.foreground);
  248. switch (dst->pixel_format)
  249. {
  250. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  251. VLINE(rt_uint16_t, DRAW_SETPIXEL_RGB565, 0);
  252. break;
  253. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  254. VLINE(rt_uint16_t, DRAW_SETPIXEL_BGR565, 0);
  255. break;
  256. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  257. VLINE(rt_uint16_t, DRAW_SETPIXEL_RGB888, 0);
  258. break;
  259. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  260. VLINE(rt_uint32_t, DRAW_SETPIXEL_ARGB8888, 0);
  261. break;
  262. }
  263. }
  264. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc *self, int x1, int x2, int y1)
  265. {
  266. struct rtgui_dc_buffer *dst;
  267. unsigned r, g, b, a;
  268. dst = (struct rtgui_dc_buffer *)self;
  269. /* parameter checking */
  270. if (y1 < 0 || y1 >= dst->height) return;
  271. if (x1 >= dst->width) return;
  272. if (x1 < 0) x1 = 0;
  273. if (x2 > dst->width) x2 = dst->width;
  274. r = RTGUI_RGB_R(dst->gc.foreground);
  275. g = RTGUI_RGB_G(dst->gc.foreground);
  276. b = RTGUI_RGB_B(dst->gc.foreground);
  277. a = RTGUI_RGB_A(dst->gc.foreground);
  278. switch (dst->pixel_format)
  279. {
  280. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  281. HLINE(rt_uint16_t, DRAW_SETPIXEL_RGB565, 0);
  282. break;
  283. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  284. HLINE(rt_uint16_t, DRAW_SETPIXEL_BGR565, 0);
  285. break;
  286. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  287. HLINE(rt_uint16_t, DRAW_SETPIXEL_RGB888, 0);
  288. break;
  289. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  290. HLINE(rt_uint32_t, DRAW_SETPIXEL_ARGB8888, 0);
  291. break;
  292. }
  293. }
  294. static void rtgui_dc_buffer_fill_rect(struct rtgui_dc *self, struct rtgui_rect *dst_rect)
  295. {
  296. struct rtgui_dc_buffer *dst;
  297. unsigned r, g, b, a;
  298. rtgui_rect_t _r, *rect;
  299. RT_ASSERT(self);
  300. if (dst_rect == RT_NULL) rtgui_dc_get_rect(self, &_r);
  301. else _r = *dst_rect;
  302. dst = (struct rtgui_dc_buffer *)self;
  303. if (_r.x2 < 0 || _r.y2 < 0) return; /* out of rect */
  304. /* parameter checking */
  305. if (_r.x1 >= dst->width)
  306. return;
  307. else if (_r.x1 < 0)
  308. _r.x1 = 0;
  309. if (_r.x2 > dst->width)
  310. _r.x2 = dst->width;
  311. if (_r.y1 >= dst->height)
  312. return;
  313. else if (_r.y1 < 0)
  314. _r.y1 = 0;
  315. if (_r.y2 > dst->height)
  316. _r.y2 = dst->height;
  317. rect = &_r;
  318. r = RTGUI_RGB_R(dst->gc.background);
  319. g = RTGUI_RGB_G(dst->gc.background);
  320. b = RTGUI_RGB_B(dst->gc.background);
  321. a = RTGUI_RGB_A(dst->gc.background);
  322. switch (dst->pixel_format)
  323. {
  324. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  325. FILLRECT(rt_uint16_t, DRAW_SETPIXEL_RGB565);
  326. break;
  327. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  328. FILLRECT(rt_uint16_t, DRAW_SETPIXEL_BGR565);
  329. break;
  330. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  331. FILLRECT(rt_uint32_t, DRAW_SETPIXEL_RGB888);
  332. break;
  333. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  334. FILLRECT(rt_uint32_t, DRAW_SETPIXEL_ARGB8888);
  335. break;
  336. }
  337. }
  338. /* blit a dc to another dc */
  339. static void rtgui_dc_buffer_blit(struct rtgui_dc *self,
  340. struct rtgui_point *dc_pt,
  341. struct rtgui_dc *dest,
  342. rtgui_rect_t *rect)
  343. {
  344. int pitch;
  345. rt_uint16_t rect_width, rect_height;
  346. struct rtgui_rect _rect, *dest_rect;
  347. struct rtgui_point dc_point;
  348. struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer *)self;
  349. if (rtgui_dc_get_visible(dest) == RT_FALSE)
  350. return;
  351. /* use the (0,0) origin point */
  352. if (dc_pt == RT_NULL)
  353. dc_point = rtgui_empty_point;
  354. else
  355. {
  356. dc_point = *dc_pt;
  357. }
  358. rtgui_dc_get_rect(dest, &_rect);
  359. /* use the rect of dest dc */
  360. if (rect == RT_NULL)
  361. {
  362. dest_rect = &_rect;
  363. }
  364. else
  365. {
  366. dest_rect = rect;
  367. if (dest_rect->x1 >= _rect.x2 || dest_rect->y1 >= _rect.y2)
  368. return;
  369. if (dest_rect->x1 < 0)
  370. {
  371. if (-dest_rect->x1 >= dc->width)
  372. return;
  373. dc_point.x += -dest_rect->x1;
  374. dest_rect->x1 = 0;
  375. }
  376. if (dest_rect->y1 < 0)
  377. {
  378. if (-dest_rect->y1 >= dc->height)
  379. return;
  380. dc_point.y += -dest_rect->y1;
  381. dest_rect->y1 = 0;
  382. }
  383. if (dest_rect->x2 > _rect.x2)
  384. dest_rect->x2 = _rect.x2;
  385. if (dest_rect->y2 > _rect.y2)
  386. dest_rect->y2 = _rect.y2;
  387. }
  388. if (dest_rect->x2 < dest_rect->x1 || dest_rect->y2 < dest_rect->y1) return;
  389. if (dc_point.x >= dc->width || dc_point.y >= dc->height) return;
  390. /* get the minimal width and height */
  391. rect_width = _UI_MIN(rtgui_rect_width(*dest_rect), dc->width - dc_point.x);
  392. rect_height = _UI_MIN(rtgui_rect_height(*dest_rect), dc->height - dc_point.y);
  393. if ((dest->type == RTGUI_DC_HW) || (dest->type == RTGUI_DC_CLIENT))
  394. {
  395. int index;
  396. rt_uint8_t *line_ptr, *pixels;
  397. rtgui_blit_line_func blit_line;
  398. struct rtgui_graphic_driver *hw_driver;
  399. hw_driver = rtgui_graphic_driver_get_default();
  400. /* prepare pixel line */
  401. pixels = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  402. if (dest->type == RTGUI_DC_HW && hw_driver->framebuffer != RT_NULL)
  403. {
  404. /* use rtgui_blit */
  405. struct rtgui_blit_info info = { 0 };
  406. struct rtgui_widget *owner;
  407. if (self->type == RTGUI_DC_BUFFER)
  408. info.a = dc->pixel_alpha;
  409. else
  410. info.a = 255;
  411. /* blit source */
  412. info.src = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  413. info.src_fmt = dc->pixel_format;
  414. info.src_h = rect_height;
  415. info.src_w = rect_width;
  416. info.src_pitch = dc->pitch;
  417. info.src_skip = info.src_pitch - info.src_w * rtgui_color_get_bpp(dc->pixel_format);
  418. owner = ((struct rtgui_dc_hw*)dest)->owner;
  419. /* blit destination */
  420. info.dst = (rt_uint8_t*)hw_driver->framebuffer;
  421. info.dst = info.dst + (owner->extent.y1 + dest_rect->y1) * hw_driver->pitch +
  422. (owner->extent.x1 + dest_rect->x1) * rtgui_color_get_bpp(hw_driver->pixel_format);
  423. info.dst_fmt = hw_driver->pixel_format;
  424. info.dst_h = rect_height;
  425. info.dst_w = rect_width;
  426. info.dst_pitch = hw_driver->pitch;
  427. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(hw_driver->pixel_format);
  428. rtgui_blit(&info);
  429. }
  430. else if (dest->type == RTGUI_DC_CLIENT&& hw_driver->framebuffer != RT_NULL)
  431. {
  432. /* use rtgui_blit */
  433. rt_uint8_t bpp, hw_bpp;
  434. struct rtgui_blit_info info = { 0 };
  435. struct rtgui_widget *owner;
  436. struct rtgui_region dest_region;
  437. struct rtgui_rect dest_extent;
  438. int num_rects;
  439. struct rtgui_rect *rects;
  440. /* get owner */
  441. owner = RTGUI_CONTAINER_OF(dest, struct rtgui_widget, dc_type);
  442. dest_extent = *dest_rect;
  443. rtgui_widget_rect_to_device(owner, &dest_extent);
  444. rtgui_region_init_with_extents(&dest_region, &dest_extent);
  445. rtgui_region_intersect_rect(&dest_region, &(owner->clip), &dest_extent);
  446. bpp = rtgui_color_get_bpp(dc->pixel_format);
  447. hw_bpp = rtgui_color_get_bpp(hw_driver->pixel_format);
  448. num_rects = rtgui_region_num_rects(&dest_region);
  449. rects = rtgui_region_rects(&dest_region);
  450. /* common info */
  451. if (self->type == RTGUI_DC_BUFFER)
  452. info.a = dc->pixel_alpha;
  453. else
  454. info.a = 255;
  455. info.src_fmt = dc->pixel_format;
  456. info.src_pitch = dc->pitch;
  457. info.dst_fmt = hw_driver->pixel_format;
  458. info.dst_pitch = hw_driver->pitch;
  459. for (index = 0; index < num_rects; index ++)
  460. {
  461. struct rtgui_rect *r = &rects[index];
  462. /* blit source */
  463. info.src = _dc_get_pixel(dc, dc_point.x + (r->x1 - dest_extent.x1),
  464. dc_point.y + (r->y1 - dest_extent.y1));
  465. info.src_h = rtgui_rect_height(*r);
  466. info.src_w = rtgui_rect_width(*r);
  467. info.src_skip = info.src_pitch - info.src_w * bpp;
  468. /* blit destination */
  469. info.dst = (rt_uint8_t*)hw_driver->framebuffer + r->y1 * hw_driver->pitch +
  470. r->x1 * hw_bpp;
  471. info.dst_h = rtgui_rect_height(*r);
  472. info.dst_w = rtgui_rect_width(*r);
  473. info.dst_skip = info.dst_pitch - info.dst_w * hw_bpp;
  474. rtgui_blit(&info);
  475. }
  476. rtgui_region_fini(&dest_region);
  477. }
  478. else
  479. {
  480. /* calculate pitch */
  481. pitch = rect_width * rtgui_color_get_bpp(dc->pixel_format);
  482. /* get blit line function */
  483. blit_line = rtgui_blit_line_get(_UI_BITBYTES(hw_driver->bits_per_pixel),
  484. rtgui_color_get_bpp(dc->pixel_format));
  485. if (hw_driver->framebuffer != RT_NULL)
  486. {
  487. struct rtgui_widget* owner;
  488. if (dest->type == RTGUI_DC_HW) owner = ((struct rtgui_dc_hw*) dest)->owner;
  489. else if (dest->type == RTGUI_DC_CLIENT) owner = RTGUI_CONTAINER_OF(dest, struct rtgui_widget, dc_type);
  490. else RT_ASSERT(0);
  491. /* change the logic coordinate to the device coordinate */
  492. rtgui_rect_move(dest_rect, owner->extent.x1, owner->extent.y1);
  493. for (index = dest_rect->y1; index < dest_rect->y1 + rect_height; index ++)
  494. {
  495. line_ptr = _hw_get_pixel(hw_driver, dest_rect->x1, index, rt_uint8_t);
  496. blit_line(line_ptr, (rt_uint8_t*)pixels, pitch);
  497. pixels += dc->pitch;
  498. }
  499. }
  500. else
  501. {
  502. /* calculate pitch */
  503. pitch = rect_width * rtgui_color_get_bpp(dc->pixel_format);
  504. /* create line buffer */
  505. line_ptr = (rt_uint8_t *) rtgui_malloc(rect_width * _UI_BITBYTES(hw_driver->bits_per_pixel));
  506. /* draw each line */
  507. for (index = dest_rect->y1; index < dest_rect->y1 + rect_height; index ++)
  508. {
  509. /* blit on line buffer */
  510. blit_line(line_ptr, (rt_uint8_t *)pixels, pitch);
  511. pixels += dc->pitch;
  512. /* draw on hardware dc */
  513. dest->engine->blit_line(dest, dest_rect->x1, dest_rect->x1 + rect_width,
  514. index, line_ptr);
  515. }
  516. /* release line buffer */
  517. rtgui_free(line_ptr);
  518. }
  519. }
  520. }
  521. else if (dest->type == RTGUI_DC_BUFFER)
  522. {
  523. struct rtgui_dc_buffer *dest_dc = (struct rtgui_dc_buffer*)dest;
  524. /* use rtgui_blit to handle buffer blit */
  525. struct rtgui_blit_info info = { 0 };
  526. if (self->type == RTGUI_DC_BUFFER)
  527. info.a = dc->pixel_alpha;
  528. else
  529. info.a = 255;
  530. /* blit source */
  531. info.src = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  532. info.src_fmt = dc->pixel_format;
  533. info.src_h = rect_height;
  534. info.src_w = rect_width;
  535. info.src_pitch = dc->pitch;
  536. info.src_skip = info.src_pitch - info.src_w * rtgui_color_get_bpp(dc->pixel_format);
  537. /* blit destination */
  538. info.dst = _dc_get_pixel(dest_dc, dest_rect->x1, dest_rect->y1);
  539. info.dst_fmt = dest_dc->pixel_format;
  540. info.dst_h = rect_height;
  541. info.dst_w = rect_width;
  542. info.dst_pitch = dest_dc->pitch;
  543. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(dest_dc->pixel_format);
  544. rtgui_blit(&info);
  545. }
  546. }
  547. static void rtgui_dc_buffer_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  548. {
  549. rt_uint8_t *pixel;
  550. struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer *)self;
  551. RT_ASSERT(dc != RT_NULL);
  552. RT_ASSERT(line_data != RT_NULL);
  553. /* out of range */
  554. if ((x1 >= dc->width) || (y >= dc->height) || y < 0 || x1 == x2)
  555. return;
  556. /* check range */
  557. if (x1 < 0)
  558. x1 = 0;
  559. if (x2 >= dc->width)
  560. x2 = dc->width-1;
  561. pixel = _dc_get_pixel(dc,x1,y);
  562. memcpy(pixel, line_data, (x2 - x1) * rtgui_color_get_bpp(dc->pixel_format));
  563. }
  564. #ifdef RT_USING_DFS
  565. #include <dfs_posix.h>
  566. void rtgui_dc_buffer_dump(struct rtgui_dc *self, char *fn)
  567. {
  568. struct dc_file_header
  569. {
  570. int w, h;
  571. int format;
  572. } header;
  573. struct rtgui_dc_buffer *buffer;
  574. int fd;
  575. if (self->type != RTGUI_DC_BUFFER) return; /* only support DC buffer */
  576. buffer = (struct rtgui_dc_buffer*)self;
  577. header.w = buffer->width;
  578. header.h = buffer->height;
  579. header.format = buffer->pixel_format;
  580. fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, 0);
  581. if (fd >= 0)
  582. {
  583. write(fd, &header, sizeof(header));
  584. write(fd, buffer->pixel, header.w * header.h * rtgui_color_get_bpp(header.format));
  585. close(fd);
  586. }
  587. }
  588. #endif