dc_buffer.c 21 KB

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