dc_buffer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 GUIENGINE_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 GUIENGINE_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 GUIENGINE_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. #ifdef PKG_USING_RGB888_PIXEL_BITS_32
  258. VLINE(rt_uint32_t, DRAW_SETPIXEL_RGB888, 0);
  259. #else
  260. VLINE(rt_uint8_t, DRAW_SETPIXEL_RGB888, 0);
  261. #endif
  262. break;
  263. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  264. VLINE(rt_uint32_t, DRAW_SETPIXEL_ARGB8888, 0);
  265. break;
  266. }
  267. }
  268. static void rtgui_dc_buffer_draw_hline(struct rtgui_dc *self, int x1, int x2, int y1)
  269. {
  270. struct rtgui_dc_buffer *dst;
  271. unsigned r, g, b, a;
  272. dst = (struct rtgui_dc_buffer *)self;
  273. /* parameter checking */
  274. if (y1 < 0 || y1 >= dst->height) return;
  275. if (x1 >= dst->width) return;
  276. if (x1 < 0) x1 = 0;
  277. if (x2 > dst->width) x2 = dst->width;
  278. r = RTGUI_RGB_R(dst->gc.foreground);
  279. g = RTGUI_RGB_G(dst->gc.foreground);
  280. b = RTGUI_RGB_B(dst->gc.foreground);
  281. a = RTGUI_RGB_A(dst->gc.foreground);
  282. switch (dst->pixel_format)
  283. {
  284. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  285. HLINE(rt_uint16_t, DRAW_SETPIXEL_RGB565, 0);
  286. break;
  287. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  288. HLINE(rt_uint16_t, DRAW_SETPIXEL_BGR565, 0);
  289. break;
  290. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  291. #ifdef PKG_USING_RGB888_PIXEL_BITS_32
  292. HLINE(rt_uint32_t, DRAW_SETPIXEL_RGB888, 0);
  293. #else
  294. HLINE(rt_uint8_t, DRAW_SETPIXEL_RGB888, 0);
  295. #endif
  296. break;
  297. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  298. HLINE(rt_uint32_t, DRAW_SETPIXEL_ARGB8888, 0);
  299. break;
  300. }
  301. }
  302. static void rtgui_dc_buffer_fill_rect(struct rtgui_dc *self, struct rtgui_rect *dst_rect)
  303. {
  304. struct rtgui_dc_buffer *dst;
  305. unsigned r, g, b, a;
  306. rtgui_rect_t _r, *rect;
  307. RT_ASSERT(self);
  308. if (dst_rect == RT_NULL) rtgui_dc_get_rect(self, &_r);
  309. else _r = *dst_rect;
  310. dst = (struct rtgui_dc_buffer *)self;
  311. if (_r.x2 < 0 || _r.y2 < 0) return; /* out of rect */
  312. /* parameter checking */
  313. if (_r.x1 >= dst->width)
  314. return;
  315. else if (_r.x1 < 0)
  316. _r.x1 = 0;
  317. if (_r.x2 > dst->width)
  318. _r.x2 = dst->width;
  319. if (_r.y1 >= dst->height)
  320. return;
  321. else if (_r.y1 < 0)
  322. _r.y1 = 0;
  323. if (_r.y2 > dst->height)
  324. _r.y2 = dst->height;
  325. rect = &_r;
  326. r = RTGUI_RGB_R(dst->gc.background);
  327. g = RTGUI_RGB_G(dst->gc.background);
  328. b = RTGUI_RGB_B(dst->gc.background);
  329. a = RTGUI_RGB_A(dst->gc.background);
  330. switch (dst->pixel_format)
  331. {
  332. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  333. FILLRECT(rt_uint16_t, DRAW_SETPIXEL_RGB565);
  334. break;
  335. case RTGRAPHIC_PIXEL_FORMAT_BGR565:
  336. FILLRECT(rt_uint16_t, DRAW_SETPIXEL_BGR565);
  337. break;
  338. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  339. #ifdef PKG_USING_RGB888_PIXEL_BITS_32
  340. FILLRECT(rt_uint32_t, DRAW_SETPIXEL_RGB888);
  341. #else
  342. FILLRECT(rt_uint8_t, DRAW_SETPIXEL_RGB888);
  343. #endif
  344. break;
  345. case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
  346. FILLRECT(rt_uint32_t, DRAW_SETPIXEL_ARGB8888);
  347. break;
  348. }
  349. }
  350. /* blit a dc to another dc */
  351. static void rtgui_dc_buffer_blit(struct rtgui_dc *self,
  352. struct rtgui_point *dc_pt,
  353. struct rtgui_dc *dest,
  354. rtgui_rect_t *rect)
  355. {
  356. int pitch;
  357. rt_uint16_t rect_width, rect_height;
  358. struct rtgui_rect _rect, *dest_rect;
  359. struct rtgui_point dc_point;
  360. struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer *)self;
  361. if (rtgui_dc_get_visible(dest) == RT_FALSE)
  362. return;
  363. /* use the (0,0) origin point */
  364. if (dc_pt == RT_NULL)
  365. dc_point = rtgui_empty_point;
  366. else
  367. {
  368. dc_point = *dc_pt;
  369. }
  370. rtgui_dc_get_rect(dest, &_rect);
  371. /* use the rect of dest dc */
  372. if (rect == RT_NULL)
  373. {
  374. dest_rect = &_rect;
  375. }
  376. else
  377. {
  378. dest_rect = rect;
  379. if (dest_rect->x1 >= _rect.x2 || dest_rect->y1 >= _rect.y2)
  380. return;
  381. if (dest_rect->x1 < 0)
  382. {
  383. if (-dest_rect->x1 >= dc->width)
  384. return;
  385. dc_point.x += -dest_rect->x1;
  386. dest_rect->x1 = 0;
  387. }
  388. if (dest_rect->y1 < 0)
  389. {
  390. if (-dest_rect->y1 >= dc->height)
  391. return;
  392. dc_point.y += -dest_rect->y1;
  393. dest_rect->y1 = 0;
  394. }
  395. if (dest_rect->x2 > _rect.x2)
  396. dest_rect->x2 = _rect.x2;
  397. if (dest_rect->y2 > _rect.y2)
  398. dest_rect->y2 = _rect.y2;
  399. }
  400. if (dest_rect->x2 < dest_rect->x1 || dest_rect->y2 < dest_rect->y1) return;
  401. if (dc_point.x >= dc->width || dc_point.y >= dc->height) return;
  402. /* get the minimal width and height */
  403. rect_width = _UI_MIN(rtgui_rect_width(*dest_rect), dc->width - dc_point.x);
  404. rect_height = _UI_MIN(rtgui_rect_height(*dest_rect), dc->height - dc_point.y);
  405. if ((dest->type == RTGUI_DC_HW) || (dest->type == RTGUI_DC_CLIENT))
  406. {
  407. int index;
  408. rt_uint8_t *line_ptr, *pixels;
  409. rtgui_blit_line_func blit_line;
  410. struct rtgui_graphic_driver *hw_driver;
  411. hw_driver = rtgui_graphic_driver_get_default();
  412. /* prepare pixel line */
  413. pixels = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  414. if (dest->type == RTGUI_DC_HW && hw_driver->framebuffer != RT_NULL)
  415. {
  416. /* use rtgui_blit */
  417. struct rtgui_blit_info info = { 0 };
  418. struct rtgui_widget *owner;
  419. if (self->type == RTGUI_DC_BUFFER)
  420. info.a = dc->pixel_alpha;
  421. else
  422. info.a = 255;
  423. /* blit source */
  424. info.src = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  425. info.src_fmt = dc->pixel_format;
  426. info.src_h = rect_height;
  427. info.src_w = rect_width;
  428. info.src_pitch = dc->pitch;
  429. info.src_skip = info.src_pitch - info.src_w * rtgui_color_get_bpp(dc->pixel_format);
  430. owner = ((struct rtgui_dc_hw*)dest)->owner;
  431. /* blit destination */
  432. info.dst = (rt_uint8_t*)hw_driver->framebuffer;
  433. info.dst = info.dst + (owner->extent.y1 + dest_rect->y1) * hw_driver->pitch +
  434. (owner->extent.x1 + dest_rect->x1) * rtgui_color_get_bpp(hw_driver->pixel_format);
  435. info.dst_fmt = hw_driver->pixel_format;
  436. info.dst_h = rect_height;
  437. info.dst_w = rect_width;
  438. info.dst_pitch = hw_driver->pitch;
  439. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(hw_driver->pixel_format);
  440. rtgui_blit(&info);
  441. }
  442. else if (dest->type == RTGUI_DC_CLIENT && hw_driver->framebuffer != RT_NULL)
  443. {
  444. /* use rtgui_blit */
  445. rt_uint8_t bpp, hw_bpp;
  446. struct rtgui_blit_info info = { 0 };
  447. struct rtgui_widget *owner;
  448. struct rtgui_region dest_region;
  449. struct rtgui_rect dest_extent;
  450. int num_rects;
  451. struct rtgui_rect *rects;
  452. /* get owner */
  453. owner = RTGUI_CONTAINER_OF(dest, struct rtgui_widget, dc_type);
  454. dest_extent = *dest_rect;
  455. rtgui_widget_rect_to_device(owner, &dest_extent);
  456. rtgui_region_init_with_extents(&dest_region, &dest_extent);
  457. rtgui_region_intersect_rect(&dest_region, &(owner->clip), &dest_extent);
  458. bpp = rtgui_color_get_bpp(dc->pixel_format);
  459. hw_bpp = rtgui_color_get_bpp(hw_driver->pixel_format);
  460. num_rects = rtgui_region_num_rects(&dest_region);
  461. rects = rtgui_region_rects(&dest_region);
  462. /* common info */
  463. if (self->type == RTGUI_DC_BUFFER)
  464. info.a = dc->pixel_alpha;
  465. else
  466. info.a = 255;
  467. info.src_fmt = dc->pixel_format;
  468. info.src_pitch = dc->pitch;
  469. info.dst_fmt = hw_driver->pixel_format;
  470. info.dst_pitch = hw_driver->pitch;
  471. for (index = 0; index < num_rects; index ++)
  472. {
  473. struct rtgui_rect *r = &rects[index];
  474. rt_uint16_t blit_width, blit_height;
  475. blit_width = rtgui_rect_width(*r) >(hw_driver->width - r->x1) ? (hw_driver->width - r->x1) : rtgui_rect_width(*r);
  476. blit_height = rtgui_rect_height(*r) > (hw_driver->height - r->y1) ? (hw_driver->height - r->y1) : rtgui_rect_height(*r);
  477. /* blit source */
  478. info.src = _dc_get_pixel(dc, dc_point.x + (r->x1 - dest_extent.x1),
  479. dc_point.y + (r->y1 - dest_extent.y1));
  480. info.src_h = blit_height;
  481. info.src_w = blit_width;
  482. info.src_skip = info.src_pitch - info.src_w * bpp;
  483. /* blit destination */
  484. info.dst = (rt_uint8_t*)hw_driver->framebuffer + r->y1 * hw_driver->pitch +
  485. r->x1 * hw_bpp;
  486. info.dst_h = blit_height;
  487. info.dst_w = blit_width;
  488. info.dst_skip = info.dst_pitch - info.dst_w * hw_bpp;
  489. rtgui_blit(&info);
  490. }
  491. rtgui_region_fini(&dest_region);
  492. }
  493. else
  494. {
  495. /* calculate pitch */
  496. pitch = rect_width * rtgui_color_get_bpp(dc->pixel_format);
  497. /* get blit line function */
  498. blit_line = rtgui_blit_line_get(_UI_BITBYTES(hw_driver->bits_per_pixel),
  499. rtgui_color_get_bpp(dc->pixel_format));
  500. if (hw_driver->framebuffer != RT_NULL)
  501. {
  502. struct rtgui_widget* owner = RT_NULL;
  503. if (dest->type == RTGUI_DC_HW)
  504. owner = ((struct rtgui_dc_hw*) dest)->owner;
  505. else if (dest->type == RTGUI_DC_CLIENT)
  506. owner = RTGUI_CONTAINER_OF(dest, struct rtgui_widget, dc_type);
  507. else
  508. RT_ASSERT(0);
  509. /* change the logic coordinate to the device coordinate */
  510. rtgui_rect_move(dest_rect, owner->extent.x1, owner->extent.y1);
  511. for (index = dest_rect->y1; index < dest_rect->y1 + rect_height; index ++)
  512. {
  513. line_ptr = _hw_get_pixel(hw_driver, dest_rect->x1, index, rt_uint8_t);
  514. blit_line(line_ptr, (rt_uint8_t*)pixels, pitch);
  515. pixels += dc->pitch;
  516. }
  517. }
  518. else
  519. {
  520. /* calculate pitch */
  521. pitch = rect_width * rtgui_color_get_bpp(dc->pixel_format);
  522. /* create line buffer */
  523. line_ptr = (rt_uint8_t *) rtgui_malloc(rect_width * _UI_BITBYTES(hw_driver->bits_per_pixel));
  524. /* draw each line */
  525. for (index = dest_rect->y1; index < dest_rect->y1 + rect_height; index ++)
  526. {
  527. /* blit on line buffer */
  528. blit_line(line_ptr, (rt_uint8_t *)pixels, pitch);
  529. pixels += dc->pitch;
  530. /* draw on hardware dc */
  531. dest->engine->blit_line(dest, dest_rect->x1, dest_rect->x1 + rect_width,
  532. index, line_ptr);
  533. }
  534. /* release line buffer */
  535. rtgui_free(line_ptr);
  536. }
  537. }
  538. }
  539. else if (dest->type == RTGUI_DC_BUFFER)
  540. {
  541. struct rtgui_dc_buffer *dest_dc = (struct rtgui_dc_buffer*)dest;
  542. /* use rtgui_blit to handle buffer blit */
  543. struct rtgui_blit_info info = { 0 };
  544. if (self->type == RTGUI_DC_BUFFER)
  545. info.a = dc->pixel_alpha;
  546. else
  547. info.a = 255;
  548. /* blit source */
  549. info.src = _dc_get_pixel(dc, dc_point.x, dc_point.y);
  550. info.src_fmt = dc->pixel_format;
  551. info.src_h = rect_height;
  552. info.src_w = rect_width;
  553. info.src_pitch = dc->pitch;
  554. info.src_skip = info.src_pitch - info.src_w * rtgui_color_get_bpp(dc->pixel_format);
  555. /* blit destination */
  556. info.dst = _dc_get_pixel(dest_dc, dest_rect->x1, dest_rect->y1);
  557. info.dst_fmt = dest_dc->pixel_format;
  558. info.dst_h = rect_height;
  559. info.dst_w = rect_width;
  560. info.dst_pitch = dest_dc->pitch;
  561. info.dst_skip = info.dst_pitch - info.dst_w * rtgui_color_get_bpp(dest_dc->pixel_format);
  562. rtgui_blit(&info);
  563. }
  564. }
  565. static void rtgui_dc_buffer_blit_line(struct rtgui_dc *self, int x1, int x2, int y, rt_uint8_t *line_data)
  566. {
  567. rt_uint8_t *pixel;
  568. struct rtgui_dc_buffer *dc = (struct rtgui_dc_buffer *)self;
  569. RT_ASSERT(dc != RT_NULL);
  570. RT_ASSERT(line_data != RT_NULL);
  571. /* out of range */
  572. if ((x1 >= dc->width) || (y >= dc->height) || y < 0 || x1 == x2)
  573. return;
  574. /* check range */
  575. if (x1 < 0)
  576. x1 = 0;
  577. if (x2 >= dc->width)
  578. x2 = dc->width-1;
  579. pixel = _dc_get_pixel(dc,x1,y);
  580. memcpy(pixel, line_data, (x2 - x1) * rtgui_color_get_bpp(dc->pixel_format));
  581. }
  582. #ifdef RT_USING_DFS
  583. #include <dfs_posix.h>
  584. void rtgui_dc_buffer_dump(struct rtgui_dc *self, char *fn)
  585. {
  586. struct dc_file_header
  587. {
  588. int w, h;
  589. int format;
  590. } header;
  591. struct rtgui_dc_buffer *buffer;
  592. int fd;
  593. if (self->type != RTGUI_DC_BUFFER) return; /* only support DC buffer */
  594. buffer = (struct rtgui_dc_buffer*)self;
  595. header.w = buffer->width;
  596. header.h = buffer->height;
  597. header.format = buffer->pixel_format;
  598. fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, 0);
  599. if (fd >= 0)
  600. {
  601. write(fd, &header, sizeof(header));
  602. write(fd, buffer->pixel, header.w * header.h * rtgui_color_get_bpp(header.format));
  603. close(fd);
  604. }
  605. }
  606. #endif