dc_buffer.c 22 KB

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