blit.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * File : blit.h
  3. * This file is part of RT-Thread GUI
  4. * COPYRIGHT (C) 2006 - 2013, 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. * 2012-01-24 onelife add one more blit table which exchanges the
  23. * positions of R and B color components in output
  24. * 2013-10-04 Bernard porting SDL software render to RT-Thread GUI
  25. */
  26. /*
  27. Simple DirectMedia Layer
  28. Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
  29. This software is provided 'as-is', without any express or implied
  30. warranty. In no event will the authors be held liable for any damages
  31. arising from the use of this software.
  32. Permission is granted to anyone to use this software for any purpose,
  33. including commercial applications, and to alter it and redistribute it
  34. freely, subject to the following restrictions:
  35. 1. The origin of this software must not be misrepresented; you must not
  36. claim that you wrote the original software. If you use this software
  37. in a product, an acknowledgment in the product documentation would be
  38. appreciated but is not required.
  39. 2. Altered source versions must be plainly marked as such, and must not be
  40. misrepresented as being the original software.
  41. 3. This notice may not be removed or altered from any source distribution.
  42. */
  43. #include <rtgui/rtgui.h>
  44. #include <rtgui/blit.h>
  45. #include <rtgui/color.h>
  46. /* Lookup tables to expand partial bytes to the full 0..255 range */
  47. static const rt_uint8_t lookup_0[] = {
  48. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  49. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  50. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  51. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
  52. 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
  53. 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
  54. 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
  55. 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
  56. 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
  57. 247, 248, 249, 250, 251, 252, 253, 254, 255
  58. };
  59. static const rt_uint8_t lookup_1[] = {
  60. 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62,
  61. 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
  62. 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166,
  63. 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214,
  64. 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 255
  65. };
  66. static const rt_uint8_t lookup_2[] = {
  67. 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 85, 89, 93, 97, 101, 105, 109, 113, 117,
  68. 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, 161, 165, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218,
  69. 222, 226, 230, 234, 238, 242, 246, 250, 255
  70. };
  71. static const rt_uint8_t lookup_3[] = {
  72. 0, 8, 16, 24, 32, 41, 49, 57, 65, 74, 82, 90, 98, 106, 115, 123, 131, 139, 148, 156, 164, 172, 180, 189, 197, 205, 213, 222,
  73. 230, 238, 246, 255
  74. };
  75. static const rt_uint8_t lookup_4[] = {
  76. 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
  77. };
  78. static const rt_uint8_t lookup_5[] = {
  79. 0, 36, 72, 109, 145, 182, 218, 255
  80. };
  81. static const rt_uint8_t lookup_6[] = {
  82. 0, 85, 170, 255
  83. };
  84. static const rt_uint8_t lookup_7[] = {
  85. 0, 255
  86. };
  87. static const rt_uint8_t lookup_8[] = {
  88. 255
  89. };
  90. const rt_uint8_t* rtgui_blit_expand_byte[9] = {
  91. lookup_0,
  92. lookup_1,
  93. lookup_2,
  94. lookup_3,
  95. lookup_4,
  96. lookup_5,
  97. lookup_6,
  98. lookup_7,
  99. lookup_8
  100. };
  101. /* 2 bpp to 1 bpp */
  102. static void rtgui_blit_line_2_1(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  103. {
  104. return;
  105. }
  106. /* 3 bpp to 1 bpp */
  107. static void rtgui_blit_line_3_1(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  108. {
  109. line = line / 3;
  110. while (line)
  111. {
  112. *dst_ptr = (rt_uint8_t)(((*src_ptr & 0x00E00000) >> 16) |
  113. ((*(src_ptr + 1) & 0x0000E000) >> 11) |
  114. ((*(src_ptr + 2) & 0x000000C0) >> 6));
  115. src_ptr += 3;
  116. dst_ptr ++;
  117. line --;
  118. }
  119. return;
  120. }
  121. /* 4 bpp to 1 bpp */
  122. static void rtgui_blit_line_4_1(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  123. {
  124. struct _color
  125. {
  126. rt_uint8_t r, g, b, a;
  127. } *c;
  128. c = (struct _color *)src_ptr;
  129. while (line-- > 0)
  130. {
  131. *dst_ptr = (c->r & 0xe0) | (c->g & 0xc0) >> 3 | (c->b & 0xe0) >> 5 ;
  132. c ++;
  133. dst_ptr ++;
  134. }
  135. }
  136. /* 1 bpp to 2 bpp */
  137. static void rtgui_blit_line_1_2(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  138. {
  139. return;
  140. }
  141. /* 3 bpp to 2 bpp */
  142. static void rtgui_blit_line_3_2(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  143. {
  144. rt_uint16_t *dst;
  145. dst = (rt_uint16_t *)dst_ptr;
  146. line = line / 3;
  147. while (line)
  148. {
  149. *dst = (((*(src_ptr + 2) << 8) & 0x0000F800) |
  150. ((*(src_ptr + 1) << 3) & 0x000007E0) |
  151. ((*src_ptr >> 3) & 0x0000001F));
  152. src_ptr += 3;
  153. dst ++;
  154. line --;
  155. }
  156. return;
  157. }
  158. /* 4 bpp to 2 bpp (ARGB --> RGB565) */
  159. static void rtgui_blit_line_4_2(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  160. {
  161. int width = line/4;
  162. rt_uint32_t *srcp = (rt_uint32_t *) src_ptr;
  163. rt_uint16_t *dstp = (rt_uint16_t *) dst_ptr;
  164. /* *INDENT-OFF* */
  165. DUFFS_LOOP4({
  166. rt_uint32_t s = *srcp;
  167. unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
  168. /* FIXME: Here we special-case opaque alpha since the
  169. compositioning used (>>8 instead of /255) doesn't handle
  170. it correctly. Also special-case alpha=0 for speed?
  171. Benchmark this! */
  172. if(alpha) {
  173. if(alpha == (255 >> 3)) {
  174. *dstp = (rt_uint16_t)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3 & 0x1f));
  175. } else {
  176. rt_uint32_t d = *dstp;
  177. /*
  178. * convert source and destination to G0RAB65565
  179. * and blend all components at the same time
  180. */
  181. s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
  182. + (s >> 3 & 0x1f);
  183. d = (d | d << 16) & 0x07e0f81f;
  184. d += (s - d) * alpha >> 5;
  185. d &= 0x07e0f81f;
  186. *dstp = (rt_uint16_t)(d | d >> 16);
  187. }
  188. }
  189. srcp++;
  190. dstp++;
  191. }, width);
  192. }
  193. static void rtgui_blit_line_1_3(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  194. {
  195. return;
  196. }
  197. #define HI 1
  198. #define LO 0
  199. /* Special optimized blit for RGB 5-6-5 --> RGBA 8-8-8-8 */
  200. static const rt_uint32_t RGB565_RGBA8888_LUT[512] =
  201. {
  202. 0x000000ff, 0x00000000, 0x000008ff, 0x00200000,
  203. 0x000010ff, 0x00400000, 0x000018ff, 0x00610000,
  204. 0x000020ff, 0x00810000, 0x000029ff, 0x00a10000,
  205. 0x000031ff, 0x00c20000, 0x000039ff, 0x00e20000,
  206. 0x000041ff, 0x08000000, 0x00004aff, 0x08200000,
  207. 0x000052ff, 0x08400000, 0x00005aff, 0x08610000,
  208. 0x000062ff, 0x08810000, 0x00006aff, 0x08a10000,
  209. 0x000073ff, 0x08c20000, 0x00007bff, 0x08e20000,
  210. 0x000083ff, 0x10000000, 0x00008bff, 0x10200000,
  211. 0x000094ff, 0x10400000, 0x00009cff, 0x10610000,
  212. 0x0000a4ff, 0x10810000, 0x0000acff, 0x10a10000,
  213. 0x0000b4ff, 0x10c20000, 0x0000bdff, 0x10e20000,
  214. 0x0000c5ff, 0x18000000, 0x0000cdff, 0x18200000,
  215. 0x0000d5ff, 0x18400000, 0x0000deff, 0x18610000,
  216. 0x0000e6ff, 0x18810000, 0x0000eeff, 0x18a10000,
  217. 0x0000f6ff, 0x18c20000, 0x0000ffff, 0x18e20000,
  218. 0x000400ff, 0x20000000, 0x000408ff, 0x20200000,
  219. 0x000410ff, 0x20400000, 0x000418ff, 0x20610000,
  220. 0x000420ff, 0x20810000, 0x000429ff, 0x20a10000,
  221. 0x000431ff, 0x20c20000, 0x000439ff, 0x20e20000,
  222. 0x000441ff, 0x29000000, 0x00044aff, 0x29200000,
  223. 0x000452ff, 0x29400000, 0x00045aff, 0x29610000,
  224. 0x000462ff, 0x29810000, 0x00046aff, 0x29a10000,
  225. 0x000473ff, 0x29c20000, 0x00047bff, 0x29e20000,
  226. 0x000483ff, 0x31000000, 0x00048bff, 0x31200000,
  227. 0x000494ff, 0x31400000, 0x00049cff, 0x31610000,
  228. 0x0004a4ff, 0x31810000, 0x0004acff, 0x31a10000,
  229. 0x0004b4ff, 0x31c20000, 0x0004bdff, 0x31e20000,
  230. 0x0004c5ff, 0x39000000, 0x0004cdff, 0x39200000,
  231. 0x0004d5ff, 0x39400000, 0x0004deff, 0x39610000,
  232. 0x0004e6ff, 0x39810000, 0x0004eeff, 0x39a10000,
  233. 0x0004f6ff, 0x39c20000, 0x0004ffff, 0x39e20000,
  234. 0x000800ff, 0x41000000, 0x000808ff, 0x41200000,
  235. 0x000810ff, 0x41400000, 0x000818ff, 0x41610000,
  236. 0x000820ff, 0x41810000, 0x000829ff, 0x41a10000,
  237. 0x000831ff, 0x41c20000, 0x000839ff, 0x41e20000,
  238. 0x000841ff, 0x4a000000, 0x00084aff, 0x4a200000,
  239. 0x000852ff, 0x4a400000, 0x00085aff, 0x4a610000,
  240. 0x000862ff, 0x4a810000, 0x00086aff, 0x4aa10000,
  241. 0x000873ff, 0x4ac20000, 0x00087bff, 0x4ae20000,
  242. 0x000883ff, 0x52000000, 0x00088bff, 0x52200000,
  243. 0x000894ff, 0x52400000, 0x00089cff, 0x52610000,
  244. 0x0008a4ff, 0x52810000, 0x0008acff, 0x52a10000,
  245. 0x0008b4ff, 0x52c20000, 0x0008bdff, 0x52e20000,
  246. 0x0008c5ff, 0x5a000000, 0x0008cdff, 0x5a200000,
  247. 0x0008d5ff, 0x5a400000, 0x0008deff, 0x5a610000,
  248. 0x0008e6ff, 0x5a810000, 0x0008eeff, 0x5aa10000,
  249. 0x0008f6ff, 0x5ac20000, 0x0008ffff, 0x5ae20000,
  250. 0x000c00ff, 0x62000000, 0x000c08ff, 0x62200000,
  251. 0x000c10ff, 0x62400000, 0x000c18ff, 0x62610000,
  252. 0x000c20ff, 0x62810000, 0x000c29ff, 0x62a10000,
  253. 0x000c31ff, 0x62c20000, 0x000c39ff, 0x62e20000,
  254. 0x000c41ff, 0x6a000000, 0x000c4aff, 0x6a200000,
  255. 0x000c52ff, 0x6a400000, 0x000c5aff, 0x6a610000,
  256. 0x000c62ff, 0x6a810000, 0x000c6aff, 0x6aa10000,
  257. 0x000c73ff, 0x6ac20000, 0x000c7bff, 0x6ae20000,
  258. 0x000c83ff, 0x73000000, 0x000c8bff, 0x73200000,
  259. 0x000c94ff, 0x73400000, 0x000c9cff, 0x73610000,
  260. 0x000ca4ff, 0x73810000, 0x000cacff, 0x73a10000,
  261. 0x000cb4ff, 0x73c20000, 0x000cbdff, 0x73e20000,
  262. 0x000cc5ff, 0x7b000000, 0x000ccdff, 0x7b200000,
  263. 0x000cd5ff, 0x7b400000, 0x000cdeff, 0x7b610000,
  264. 0x000ce6ff, 0x7b810000, 0x000ceeff, 0x7ba10000,
  265. 0x000cf6ff, 0x7bc20000, 0x000cffff, 0x7be20000,
  266. 0x001000ff, 0x83000000, 0x001008ff, 0x83200000,
  267. 0x001010ff, 0x83400000, 0x001018ff, 0x83610000,
  268. 0x001020ff, 0x83810000, 0x001029ff, 0x83a10000,
  269. 0x001031ff, 0x83c20000, 0x001039ff, 0x83e20000,
  270. 0x001041ff, 0x8b000000, 0x00104aff, 0x8b200000,
  271. 0x001052ff, 0x8b400000, 0x00105aff, 0x8b610000,
  272. 0x001062ff, 0x8b810000, 0x00106aff, 0x8ba10000,
  273. 0x001073ff, 0x8bc20000, 0x00107bff, 0x8be20000,
  274. 0x001083ff, 0x94000000, 0x00108bff, 0x94200000,
  275. 0x001094ff, 0x94400000, 0x00109cff, 0x94610000,
  276. 0x0010a4ff, 0x94810000, 0x0010acff, 0x94a10000,
  277. 0x0010b4ff, 0x94c20000, 0x0010bdff, 0x94e20000,
  278. 0x0010c5ff, 0x9c000000, 0x0010cdff, 0x9c200000,
  279. 0x0010d5ff, 0x9c400000, 0x0010deff, 0x9c610000,
  280. 0x0010e6ff, 0x9c810000, 0x0010eeff, 0x9ca10000,
  281. 0x0010f6ff, 0x9cc20000, 0x0010ffff, 0x9ce20000,
  282. 0x001400ff, 0xa4000000, 0x001408ff, 0xa4200000,
  283. 0x001410ff, 0xa4400000, 0x001418ff, 0xa4610000,
  284. 0x001420ff, 0xa4810000, 0x001429ff, 0xa4a10000,
  285. 0x001431ff, 0xa4c20000, 0x001439ff, 0xa4e20000,
  286. 0x001441ff, 0xac000000, 0x00144aff, 0xac200000,
  287. 0x001452ff, 0xac400000, 0x00145aff, 0xac610000,
  288. 0x001462ff, 0xac810000, 0x00146aff, 0xaca10000,
  289. 0x001473ff, 0xacc20000, 0x00147bff, 0xace20000,
  290. 0x001483ff, 0xb4000000, 0x00148bff, 0xb4200000,
  291. 0x001494ff, 0xb4400000, 0x00149cff, 0xb4610000,
  292. 0x0014a4ff, 0xb4810000, 0x0014acff, 0xb4a10000,
  293. 0x0014b4ff, 0xb4c20000, 0x0014bdff, 0xb4e20000,
  294. 0x0014c5ff, 0xbd000000, 0x0014cdff, 0xbd200000,
  295. 0x0014d5ff, 0xbd400000, 0x0014deff, 0xbd610000,
  296. 0x0014e6ff, 0xbd810000, 0x0014eeff, 0xbda10000,
  297. 0x0014f6ff, 0xbdc20000, 0x0014ffff, 0xbde20000,
  298. 0x001800ff, 0xc5000000, 0x001808ff, 0xc5200000,
  299. 0x001810ff, 0xc5400000, 0x001818ff, 0xc5610000,
  300. 0x001820ff, 0xc5810000, 0x001829ff, 0xc5a10000,
  301. 0x001831ff, 0xc5c20000, 0x001839ff, 0xc5e20000,
  302. 0x001841ff, 0xcd000000, 0x00184aff, 0xcd200000,
  303. 0x001852ff, 0xcd400000, 0x00185aff, 0xcd610000,
  304. 0x001862ff, 0xcd810000, 0x00186aff, 0xcda10000,
  305. 0x001873ff, 0xcdc20000, 0x00187bff, 0xcde20000,
  306. 0x001883ff, 0xd5000000, 0x00188bff, 0xd5200000,
  307. 0x001894ff, 0xd5400000, 0x00189cff, 0xd5610000,
  308. 0x0018a4ff, 0xd5810000, 0x0018acff, 0xd5a10000,
  309. 0x0018b4ff, 0xd5c20000, 0x0018bdff, 0xd5e20000,
  310. 0x0018c5ff, 0xde000000, 0x0018cdff, 0xde200000,
  311. 0x0018d5ff, 0xde400000, 0x0018deff, 0xde610000,
  312. 0x0018e6ff, 0xde810000, 0x0018eeff, 0xdea10000,
  313. 0x0018f6ff, 0xdec20000, 0x0018ffff, 0xdee20000,
  314. 0x001c00ff, 0xe6000000, 0x001c08ff, 0xe6200000,
  315. 0x001c10ff, 0xe6400000, 0x001c18ff, 0xe6610000,
  316. 0x001c20ff, 0xe6810000, 0x001c29ff, 0xe6a10000,
  317. 0x001c31ff, 0xe6c20000, 0x001c39ff, 0xe6e20000,
  318. 0x001c41ff, 0xee000000, 0x001c4aff, 0xee200000,
  319. 0x001c52ff, 0xee400000, 0x001c5aff, 0xee610000,
  320. 0x001c62ff, 0xee810000, 0x001c6aff, 0xeea10000,
  321. 0x001c73ff, 0xeec20000, 0x001c7bff, 0xeee20000,
  322. 0x001c83ff, 0xf6000000, 0x001c8bff, 0xf6200000,
  323. 0x001c94ff, 0xf6400000, 0x001c9cff, 0xf6610000,
  324. 0x001ca4ff, 0xf6810000, 0x001cacff, 0xf6a10000,
  325. 0x001cb4ff, 0xf6c20000, 0x001cbdff, 0xf6e20000,
  326. 0x001cc5ff, 0xff000000, 0x001ccdff, 0xff200000,
  327. 0x001cd5ff, 0xff400000, 0x001cdeff, 0xff610000,
  328. 0x001ce6ff, 0xff810000, 0x001ceeff, 0xffa10000,
  329. 0x001cf6ff, 0xffc20000, 0x001cffff, 0xffe20000,
  330. };
  331. /* Special optimized blit for RGB 5-6-5 --> ARGB 8-8-8-8 */
  332. static const rt_uint32_t RGB565_ARGB8888_LUT[512] = {
  333. 0x00000000, 0xff000000, 0x00000008, 0xff002000,
  334. 0x00000010, 0xff004000, 0x00000018, 0xff006100,
  335. 0x00000020, 0xff008100, 0x00000029, 0xff00a100,
  336. 0x00000031, 0xff00c200, 0x00000039, 0xff00e200,
  337. 0x00000041, 0xff080000, 0x0000004a, 0xff082000,
  338. 0x00000052, 0xff084000, 0x0000005a, 0xff086100,
  339. 0x00000062, 0xff088100, 0x0000006a, 0xff08a100,
  340. 0x00000073, 0xff08c200, 0x0000007b, 0xff08e200,
  341. 0x00000083, 0xff100000, 0x0000008b, 0xff102000,
  342. 0x00000094, 0xff104000, 0x0000009c, 0xff106100,
  343. 0x000000a4, 0xff108100, 0x000000ac, 0xff10a100,
  344. 0x000000b4, 0xff10c200, 0x000000bd, 0xff10e200,
  345. 0x000000c5, 0xff180000, 0x000000cd, 0xff182000,
  346. 0x000000d5, 0xff184000, 0x000000de, 0xff186100,
  347. 0x000000e6, 0xff188100, 0x000000ee, 0xff18a100,
  348. 0x000000f6, 0xff18c200, 0x000000ff, 0xff18e200,
  349. 0x00000400, 0xff200000, 0x00000408, 0xff202000,
  350. 0x00000410, 0xff204000, 0x00000418, 0xff206100,
  351. 0x00000420, 0xff208100, 0x00000429, 0xff20a100,
  352. 0x00000431, 0xff20c200, 0x00000439, 0xff20e200,
  353. 0x00000441, 0xff290000, 0x0000044a, 0xff292000,
  354. 0x00000452, 0xff294000, 0x0000045a, 0xff296100,
  355. 0x00000462, 0xff298100, 0x0000046a, 0xff29a100,
  356. 0x00000473, 0xff29c200, 0x0000047b, 0xff29e200,
  357. 0x00000483, 0xff310000, 0x0000048b, 0xff312000,
  358. 0x00000494, 0xff314000, 0x0000049c, 0xff316100,
  359. 0x000004a4, 0xff318100, 0x000004ac, 0xff31a100,
  360. 0x000004b4, 0xff31c200, 0x000004bd, 0xff31e200,
  361. 0x000004c5, 0xff390000, 0x000004cd, 0xff392000,
  362. 0x000004d5, 0xff394000, 0x000004de, 0xff396100,
  363. 0x000004e6, 0xff398100, 0x000004ee, 0xff39a100,
  364. 0x000004f6, 0xff39c200, 0x000004ff, 0xff39e200,
  365. 0x00000800, 0xff410000, 0x00000808, 0xff412000,
  366. 0x00000810, 0xff414000, 0x00000818, 0xff416100,
  367. 0x00000820, 0xff418100, 0x00000829, 0xff41a100,
  368. 0x00000831, 0xff41c200, 0x00000839, 0xff41e200,
  369. 0x00000841, 0xff4a0000, 0x0000084a, 0xff4a2000,
  370. 0x00000852, 0xff4a4000, 0x0000085a, 0xff4a6100,
  371. 0x00000862, 0xff4a8100, 0x0000086a, 0xff4aa100,
  372. 0x00000873, 0xff4ac200, 0x0000087b, 0xff4ae200,
  373. 0x00000883, 0xff520000, 0x0000088b, 0xff522000,
  374. 0x00000894, 0xff524000, 0x0000089c, 0xff526100,
  375. 0x000008a4, 0xff528100, 0x000008ac, 0xff52a100,
  376. 0x000008b4, 0xff52c200, 0x000008bd, 0xff52e200,
  377. 0x000008c5, 0xff5a0000, 0x000008cd, 0xff5a2000,
  378. 0x000008d5, 0xff5a4000, 0x000008de, 0xff5a6100,
  379. 0x000008e6, 0xff5a8100, 0x000008ee, 0xff5aa100,
  380. 0x000008f6, 0xff5ac200, 0x000008ff, 0xff5ae200,
  381. 0x00000c00, 0xff620000, 0x00000c08, 0xff622000,
  382. 0x00000c10, 0xff624000, 0x00000c18, 0xff626100,
  383. 0x00000c20, 0xff628100, 0x00000c29, 0xff62a100,
  384. 0x00000c31, 0xff62c200, 0x00000c39, 0xff62e200,
  385. 0x00000c41, 0xff6a0000, 0x00000c4a, 0xff6a2000,
  386. 0x00000c52, 0xff6a4000, 0x00000c5a, 0xff6a6100,
  387. 0x00000c62, 0xff6a8100, 0x00000c6a, 0xff6aa100,
  388. 0x00000c73, 0xff6ac200, 0x00000c7b, 0xff6ae200,
  389. 0x00000c83, 0xff730000, 0x00000c8b, 0xff732000,
  390. 0x00000c94, 0xff734000, 0x00000c9c, 0xff736100,
  391. 0x00000ca4, 0xff738100, 0x00000cac, 0xff73a100,
  392. 0x00000cb4, 0xff73c200, 0x00000cbd, 0xff73e200,
  393. 0x00000cc5, 0xff7b0000, 0x00000ccd, 0xff7b2000,
  394. 0x00000cd5, 0xff7b4000, 0x00000cde, 0xff7b6100,
  395. 0x00000ce6, 0xff7b8100, 0x00000cee, 0xff7ba100,
  396. 0x00000cf6, 0xff7bc200, 0x00000cff, 0xff7be200,
  397. 0x00001000, 0xff830000, 0x00001008, 0xff832000,
  398. 0x00001010, 0xff834000, 0x00001018, 0xff836100,
  399. 0x00001020, 0xff838100, 0x00001029, 0xff83a100,
  400. 0x00001031, 0xff83c200, 0x00001039, 0xff83e200,
  401. 0x00001041, 0xff8b0000, 0x0000104a, 0xff8b2000,
  402. 0x00001052, 0xff8b4000, 0x0000105a, 0xff8b6100,
  403. 0x00001062, 0xff8b8100, 0x0000106a, 0xff8ba100,
  404. 0x00001073, 0xff8bc200, 0x0000107b, 0xff8be200,
  405. 0x00001083, 0xff940000, 0x0000108b, 0xff942000,
  406. 0x00001094, 0xff944000, 0x0000109c, 0xff946100,
  407. 0x000010a4, 0xff948100, 0x000010ac, 0xff94a100,
  408. 0x000010b4, 0xff94c200, 0x000010bd, 0xff94e200,
  409. 0x000010c5, 0xff9c0000, 0x000010cd, 0xff9c2000,
  410. 0x000010d5, 0xff9c4000, 0x000010de, 0xff9c6100,
  411. 0x000010e6, 0xff9c8100, 0x000010ee, 0xff9ca100,
  412. 0x000010f6, 0xff9cc200, 0x000010ff, 0xff9ce200,
  413. 0x00001400, 0xffa40000, 0x00001408, 0xffa42000,
  414. 0x00001410, 0xffa44000, 0x00001418, 0xffa46100,
  415. 0x00001420, 0xffa48100, 0x00001429, 0xffa4a100,
  416. 0x00001431, 0xffa4c200, 0x00001439, 0xffa4e200,
  417. 0x00001441, 0xffac0000, 0x0000144a, 0xffac2000,
  418. 0x00001452, 0xffac4000, 0x0000145a, 0xffac6100,
  419. 0x00001462, 0xffac8100, 0x0000146a, 0xffaca100,
  420. 0x00001473, 0xffacc200, 0x0000147b, 0xfface200,
  421. 0x00001483, 0xffb40000, 0x0000148b, 0xffb42000,
  422. 0x00001494, 0xffb44000, 0x0000149c, 0xffb46100,
  423. 0x000014a4, 0xffb48100, 0x000014ac, 0xffb4a100,
  424. 0x000014b4, 0xffb4c200, 0x000014bd, 0xffb4e200,
  425. 0x000014c5, 0xffbd0000, 0x000014cd, 0xffbd2000,
  426. 0x000014d5, 0xffbd4000, 0x000014de, 0xffbd6100,
  427. 0x000014e6, 0xffbd8100, 0x000014ee, 0xffbda100,
  428. 0x000014f6, 0xffbdc200, 0x000014ff, 0xffbde200,
  429. 0x00001800, 0xffc50000, 0x00001808, 0xffc52000,
  430. 0x00001810, 0xffc54000, 0x00001818, 0xffc56100,
  431. 0x00001820, 0xffc58100, 0x00001829, 0xffc5a100,
  432. 0x00001831, 0xffc5c200, 0x00001839, 0xffc5e200,
  433. 0x00001841, 0xffcd0000, 0x0000184a, 0xffcd2000,
  434. 0x00001852, 0xffcd4000, 0x0000185a, 0xffcd6100,
  435. 0x00001862, 0xffcd8100, 0x0000186a, 0xffcda100,
  436. 0x00001873, 0xffcdc200, 0x0000187b, 0xffcde200,
  437. 0x00001883, 0xffd50000, 0x0000188b, 0xffd52000,
  438. 0x00001894, 0xffd54000, 0x0000189c, 0xffd56100,
  439. 0x000018a4, 0xffd58100, 0x000018ac, 0xffd5a100,
  440. 0x000018b4, 0xffd5c200, 0x000018bd, 0xffd5e200,
  441. 0x000018c5, 0xffde0000, 0x000018cd, 0xffde2000,
  442. 0x000018d5, 0xffde4000, 0x000018de, 0xffde6100,
  443. 0x000018e6, 0xffde8100, 0x000018ee, 0xffdea100,
  444. 0x000018f6, 0xffdec200, 0x000018ff, 0xffdee200,
  445. 0x00001c00, 0xffe60000, 0x00001c08, 0xffe62000,
  446. 0x00001c10, 0xffe64000, 0x00001c18, 0xffe66100,
  447. 0x00001c20, 0xffe68100, 0x00001c29, 0xffe6a100,
  448. 0x00001c31, 0xffe6c200, 0x00001c39, 0xffe6e200,
  449. 0x00001c41, 0xffee0000, 0x00001c4a, 0xffee2000,
  450. 0x00001c52, 0xffee4000, 0x00001c5a, 0xffee6100,
  451. 0x00001c62, 0xffee8100, 0x00001c6a, 0xffeea100,
  452. 0x00001c73, 0xffeec200, 0x00001c7b, 0xffeee200,
  453. 0x00001c83, 0xfff60000, 0x00001c8b, 0xfff62000,
  454. 0x00001c94, 0xfff64000, 0x00001c9c, 0xfff66100,
  455. 0x00001ca4, 0xfff68100, 0x00001cac, 0xfff6a100,
  456. 0x00001cb4, 0xfff6c200, 0x00001cbd, 0xfff6e200,
  457. 0x00001cc5, 0xffff0000, 0x00001ccd, 0xffff2000,
  458. 0x00001cd5, 0xffff4000, 0x00001cde, 0xffff6100,
  459. 0x00001ce6, 0xffff8100, 0x00001cee, 0xffffa100,
  460. 0x00001cf6, 0xffffc200, 0x00001cff, 0xffffe200
  461. };
  462. static void rtgui_blit_line_2_3(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  463. {
  464. rt_uint16_t *src;
  465. rt_uint32_t *dst;
  466. src = (rt_uint16_t *)src_ptr;
  467. dst = (rt_uint32_t *)dst_ptr;
  468. line = line / 2;
  469. while (line)
  470. {
  471. *dst++ = RGB565_RGBA8888_LUT[src[LO] * 2] + RGB565_RGBA8888_LUT[src[HI] * 2 + 1];
  472. line--;
  473. src ++;
  474. }
  475. }
  476. void rtgui_blit_line_direct(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  477. {
  478. rt_memcpy(dst_ptr, src_ptr, line);
  479. }
  480. /* convert 4bpp to 3bpp */
  481. static void rtgui_blit_line_4_3(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  482. {
  483. line = line / 4;
  484. while (line)
  485. {
  486. *dst_ptr++ = *src_ptr++;
  487. *dst_ptr++ = *src_ptr++;
  488. *dst_ptr++ = *src_ptr++;
  489. src_ptr ++;
  490. line --;
  491. }
  492. }
  493. static void rtgui_blit_line_1_4(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  494. {
  495. }
  496. static void rtgui_blit_line_2_4(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  497. {
  498. }
  499. /* convert 3bpp to 4bpp */
  500. static void rtgui_blit_line_3_4(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  501. {
  502. line = line / 4;
  503. while (line)
  504. {
  505. *dst_ptr++ = *src_ptr++;
  506. *dst_ptr++ = *src_ptr++;
  507. *dst_ptr++ = *src_ptr++;
  508. *dst_ptr++ = 0;
  509. line --;
  510. }
  511. }
  512. static const rtgui_blit_line_func _blit_table[5][5] =
  513. {
  514. /* 0_0, 1_0, 2_0, 3_0, 4_0 */
  515. {RT_NULL, RT_NULL, RT_NULL, RT_NULL, RT_NULL },
  516. /* 0_1, 1_1, 2_1, 3_1, 4_1 */
  517. {RT_NULL, rtgui_blit_line_direct, rtgui_blit_line_2_1, rtgui_blit_line_3_1, rtgui_blit_line_4_1 },
  518. /* 0_2, 1_2, 2_2, 3_2, 4_2 */
  519. {RT_NULL, rtgui_blit_line_1_2, rtgui_blit_line_direct, rtgui_blit_line_3_2, rtgui_blit_line_4_2 },
  520. /* 0_3, 1_3, 2_3, 3_3, 4_3 */
  521. {RT_NULL, rtgui_blit_line_1_3, rtgui_blit_line_2_3, rtgui_blit_line_direct, rtgui_blit_line_4_3 },
  522. /* 0_4, 1_4, 2_4, 3_4, 4_4 */
  523. {RT_NULL, rtgui_blit_line_1_4, rtgui_blit_line_2_4, rtgui_blit_line_3_4, rtgui_blit_line_direct },
  524. };
  525. rtgui_blit_line_func rtgui_blit_line_get(int dst_bpp, int src_bpp)
  526. {
  527. RT_ASSERT(dst_bpp > 0 && dst_bpp < 5);
  528. RT_ASSERT(src_bpp > 0 && src_bpp < 5);
  529. return _blit_table[dst_bpp][src_bpp];
  530. }
  531. static void rtgui_blit_line_3_2_inv(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  532. {
  533. rt_uint16_t *dst;
  534. dst = (rt_uint16_t *)dst_ptr;
  535. line = line / 3;
  536. while (line)
  537. {
  538. *dst = (((*src_ptr << 8) & 0x0000F800) |
  539. ((*(src_ptr + 1) << 3) & 0x000007E0) |
  540. ((*(src_ptr + 2) >> 3) & 0x0000001F));
  541. src_ptr += 3;
  542. dst ++;
  543. line --;
  544. }
  545. return;
  546. }
  547. void rtgui_blit_line_2_2_inv(rt_uint8_t *dst_ptr, rt_uint8_t *src_ptr, int line)
  548. {
  549. rt_uint16_t *dst, *src;
  550. dst = (rt_uint16_t *)dst_ptr;
  551. src = (rt_uint16_t *)src_ptr;
  552. line = line / 2;
  553. while (line)
  554. {
  555. *dst = ((*src << 11) & 0xF800) | (*src & 0x07E0) | ((*src >> 11) & 0x001F);
  556. src ++;
  557. dst ++;
  558. line --;
  559. }
  560. }
  561. static const rtgui_blit_line_func _blit_table_inv[5][5] =
  562. {
  563. /* 0_0, 1_0, 2_0, 3_0, 4_0 */
  564. {RT_NULL, RT_NULL, RT_NULL, RT_NULL, RT_NULL },
  565. /* 0_1, 1_1, 2_1, 3_1, 4_1 */
  566. {RT_NULL, rtgui_blit_line_direct, rtgui_blit_line_2_1, rtgui_blit_line_3_1, rtgui_blit_line_4_1 },
  567. /* 0_2, 1_2, 2_2, 3_2, 4_2 */
  568. {RT_NULL, rtgui_blit_line_1_2, rtgui_blit_line_2_2_inv, rtgui_blit_line_3_2_inv, rtgui_blit_line_4_2 },
  569. /* 0_3, 1_3, 2_3, 3_3, 4_3 */
  570. {RT_NULL, rtgui_blit_line_1_3, rtgui_blit_line_2_3, rtgui_blit_line_direct, rtgui_blit_line_4_3 },
  571. /* 0_4, 1_4, 2_4, 3_4, 4_4 */
  572. {RT_NULL, rtgui_blit_line_1_4, rtgui_blit_line_2_4, rtgui_blit_line_3_4, rtgui_blit_line_direct },
  573. };
  574. /* get blit function for BGR565 */
  575. rtgui_blit_line_func rtgui_blit_line_get_inv(int dst_bpp, int src_bpp)
  576. {
  577. RT_ASSERT(dst_bpp > 0 && dst_bpp < 5);
  578. RT_ASSERT(src_bpp > 0 && src_bpp < 5);
  579. return _blit_table_inv[dst_bpp][src_bpp];
  580. }
  581. /* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
  582. /* blend a single 16 bit pixel at 50% */
  583. #define BLEND16_50(d, s, mask) \
  584. ((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
  585. /* blend two 16 bit pixels at 50% */
  586. #define BLEND2x16_50(d, s, mask) \
  587. (((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
  588. + (s & d & (~(mask | mask << 16))))
  589. static void
  590. Blit16to16SurfaceAlpha128(struct rtgui_blit_info * info, rt_uint16_t mask)
  591. {
  592. int width = info->dst_w;
  593. int height = info->dst_h;
  594. rt_uint16_t *srcp = (rt_uint16_t *) info->src;
  595. int srcskip = info->src_skip >> 1;
  596. rt_uint16_t *dstp = (rt_uint16_t *) info->dst;
  597. int dstskip = info->dst_skip >> 1;
  598. while (height--) {
  599. if (((unsigned int) srcp ^ (unsigned int) dstp) & 2) {
  600. /*
  601. * Source and destination not aligned, pipeline it.
  602. * This is mostly a win for big blits but no loss for
  603. * small ones
  604. */
  605. rt_uint32_t prev_sw;
  606. int w = width;
  607. /* handle odd destination */
  608. if ((unsigned int) dstp & 2) {
  609. rt_uint16_t d = *dstp, s = *srcp;
  610. *dstp = BLEND16_50(d, s, mask);
  611. dstp++;
  612. srcp++;
  613. w--;
  614. }
  615. srcp++; /* srcp is now 32-bit aligned */
  616. /* bootstrap pipeline with first halfword */
  617. prev_sw = ((rt_uint32_t *) srcp)[-1];
  618. while (w > 1) {
  619. rt_uint32_t sw, dw, s;
  620. sw = *(rt_uint32_t *) srcp;
  621. dw = *(rt_uint32_t *) dstp;
  622. s = (prev_sw >> 16) + (sw << 16);
  623. prev_sw = sw;
  624. *(rt_uint32_t *) dstp = BLEND2x16_50(dw, s, mask);
  625. dstp += 2;
  626. srcp += 2;
  627. w -= 2;
  628. }
  629. /* final pixel if any */
  630. if (w) {
  631. rt_uint16_t d = *dstp, s;
  632. s = (rt_uint16_t) (prev_sw >> 16);
  633. *dstp = BLEND16_50(d, s, mask);
  634. srcp++;
  635. dstp++;
  636. }
  637. srcp += srcskip - 1;
  638. dstp += dstskip;
  639. } else {
  640. /* source and destination are aligned */
  641. int w = width;
  642. /* first odd pixel? */
  643. if ((unsigned int) srcp & 2) {
  644. rt_uint16_t d = *dstp, s = *srcp;
  645. *dstp = BLEND16_50(d, s, mask);
  646. srcp++;
  647. dstp++;
  648. w--;
  649. }
  650. /* srcp and dstp are now 32-bit aligned */
  651. while (w > 1) {
  652. rt_uint32_t sw = *(rt_uint32_t *) srcp;
  653. rt_uint32_t dw = *(rt_uint32_t *) dstp;
  654. *(rt_uint32_t *) dstp = BLEND2x16_50(dw, sw, mask);
  655. srcp += 2;
  656. dstp += 2;
  657. w -= 2;
  658. }
  659. /* last odd pixel? */
  660. if (w) {
  661. rt_uint16_t d = *dstp, s = *srcp;
  662. *dstp = BLEND16_50(d, s, mask);
  663. srcp++;
  664. dstp++;
  665. }
  666. srcp += srcskip;
  667. dstp += dstskip;
  668. }
  669. }
  670. }
  671. /* fast RGB565->RGB565 blending with pixel alpha */
  672. static void
  673. Blit565to565PixelAlpha(struct rtgui_blit_info * info)
  674. {
  675. unsigned alpha = info->a;
  676. if (alpha == 128)
  677. {
  678. Blit16to16SurfaceAlpha128(info, 0xf7de);
  679. return;
  680. }
  681. else if (alpha == 255)
  682. {
  683. int len = info->dst_w * 2;
  684. int height = info->dst_h;
  685. rt_uint8_t *srcp = info->src;
  686. rt_uint8_t *dstp = info->dst;
  687. while (height--)
  688. {
  689. rt_memcpy(dstp, srcp, len);
  690. dstp += info->dst_skip + len;
  691. srcp += info->src_skip + len;
  692. }
  693. return;
  694. }
  695. else
  696. {
  697. /* R and B only have 5 bits. So 5 bits of alpha is enough. */
  698. alpha >>= 3;
  699. }
  700. if (alpha == 0)
  701. return;
  702. {
  703. int width = info->dst_w / 2;
  704. int height = info->dst_h;
  705. rt_uint32_t *srcp = (rt_uint32_t *) info->src;
  706. rt_uint32_t *dstp = (rt_uint32_t *) info->dst;
  707. while (height--) {
  708. DUFFS_LOOP4(
  709. {
  710. rt_uint32_t s = *srcp++;
  711. rt_uint32_t d = *dstp;
  712. rt_uint32_t su = (s << 16) | (s >> 16);
  713. rt_uint32_t du = (d << 16) | (d >> 16);
  714. s &= 0x07e0f81f;
  715. d &= 0x07e0f81f;
  716. su &= 0x07e0f81f;
  717. du &= 0x07e0f81f;
  718. du = ((su - du) * alpha / 32 + du) & 0x07e0f81f;
  719. d = ((s - d) * alpha / 32 + d) & 0x07e0f81f;
  720. *dstp++ = (du << 16) | (du >> 16) | d;
  721. }, width);
  722. /* Deal with the last pixel. */
  723. if (info->dst_w % 2)
  724. {
  725. rt_uint32_t s = *(rt_uint16_t*)srcp;
  726. rt_uint32_t d = *(rt_uint16_t*)dstp;
  727. s = (s | s << 16) & 0x07e0f81f;
  728. d = (d | d << 16) & 0x07e0f81f;
  729. d += (s - d) * alpha / 32;
  730. d &= 0x07e0f81f;
  731. *(rt_uint16_t*)dstp = (rt_uint16_t)(d | d >> 16);
  732. srcp = (rt_uint32_t*)((unsigned int)srcp + 2);
  733. dstp = (rt_uint32_t*)((unsigned int)dstp + 2);
  734. }
  735. srcp = (rt_uint32_t*)((unsigned int)srcp + info->src_skip);
  736. dstp = (rt_uint32_t*)((unsigned int)dstp + info->dst_skip);
  737. }
  738. }
  739. }
  740. /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
  741. static void BlitRGBtoRGBSurfaceAlpha128(struct rtgui_blit_info *info)
  742. {
  743. int width = info->dst_w;
  744. int height = info->dst_h;
  745. rt_uint32_t *srcp = (rt_uint32_t *)info->src;
  746. int srcskip = info->src_skip >> 2;
  747. rt_uint32_t *dstp = (rt_uint32_t *)info->dst;
  748. int dstskip = info->dst_skip >> 2;
  749. while(height--) {
  750. DUFFS_LOOP4({
  751. rt_uint32_t s = *srcp++;
  752. rt_uint32_t d = *dstp;
  753. *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
  754. + (s & d & 0x00010101)) | 0xff000000;
  755. }, width);
  756. srcp += srcskip;
  757. dstp += dstskip;
  758. }
  759. }
  760. /* fast RGB888->(A)RGB888 blending with surface alpha */
  761. static void BlitRGBtoRGBSurfaceAlpha(struct rtgui_blit_info *info)
  762. {
  763. unsigned int alpha = info->a;
  764. if(alpha == 128) {
  765. BlitRGBtoRGBSurfaceAlpha128(info);
  766. } else {
  767. int width = info->dst_w;
  768. int height = info->dst_h;
  769. rt_uint32_t *srcp = (rt_uint32_t *)info->src;
  770. int srcskip = info->src_skip >> 2;
  771. rt_uint32_t *dstp = (rt_uint32_t *)info->dst;
  772. int dstskip = info->dst_skip >> 2;
  773. while(height--) {
  774. DUFFS_LOOP4({
  775. rt_uint32_t s;
  776. rt_uint32_t d;
  777. rt_uint32_t s1;
  778. rt_uint32_t d1;
  779. s = *srcp;
  780. d = *dstp;
  781. s1 = s & 0xff00ff;
  782. d1 = d & 0xff00ff;
  783. d1 = (d1 + ((s1 - d1) * alpha >> 8))
  784. & 0xff00ff;
  785. s &= 0xff00;
  786. d &= 0xff00;
  787. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  788. *dstp = d1 | d | 0xff000000;
  789. ++srcp;
  790. ++dstp;
  791. }, width);
  792. srcp += srcskip;
  793. dstp += dstskip;
  794. }
  795. }
  796. }
  797. /* fast ARGB8888->RGB565 blending with pixel alpha */
  798. static void BlitARGBto565PixelAlpha(struct rtgui_blit_info * info)
  799. {
  800. int width = info->dst_w;
  801. int height = info->dst_h;
  802. rt_uint32_t *srcp = (rt_uint32_t *) info->src;
  803. int srcskip = info->src_skip >> 2;
  804. rt_uint16_t *dstp = (rt_uint16_t *) info->dst;
  805. int dstskip = info->dst_skip >> 1;
  806. while (height--) {
  807. /* *INDENT-OFF* */
  808. DUFFS_LOOP4({
  809. rt_uint32_t s = *srcp;
  810. unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
  811. /* FIXME: Here we special-case opaque alpha since the
  812. compositioning used (>>8 instead of /255) doesn't handle
  813. it correctly. Also special-case alpha=0 for speed?
  814. Benchmark this! */
  815. if(alpha) {
  816. if(alpha == (255 >> 3)) {
  817. *dstp = (rt_uint16_t)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3 & 0x1f));
  818. } else {
  819. rt_uint32_t d = *dstp;
  820. /*
  821. * convert source and destination to G0RAB65565
  822. * and blend all components at the same time
  823. */
  824. s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
  825. + (s >> 3 & 0x1f);
  826. d = (d | d << 16) & 0x07e0f81f;
  827. d += (s - d) * alpha >> 5;
  828. d &= 0x07e0f81f;
  829. *dstp = (rt_uint16_t)(d | d >> 16);
  830. }
  831. }
  832. srcp++;
  833. dstp++;
  834. }, width);
  835. /* *INDENT-ON* */
  836. srcp += srcskip;
  837. dstp += dstskip;
  838. }
  839. }
  840. /* fast ARGB888->(A)RGB888 blending with pixel alpha */
  841. static void BlitRGBtoRGBPixelAlpha(struct rtgui_blit_info *info)
  842. {
  843. int width = info->dst_w;
  844. int height = info->dst_h;
  845. rt_uint32_t *srcp = (rt_uint32_t *)info->src;
  846. int srcskip = info->src_skip >> 2;
  847. rt_uint32_t *dstp = (rt_uint32_t *)info->dst;
  848. int dstskip = info->dst_skip >> 2;
  849. while(height--) {
  850. DUFFS_LOOP4({
  851. rt_uint32_t dalpha;
  852. rt_uint32_t d;
  853. rt_uint32_t s1;
  854. rt_uint32_t d1;
  855. rt_uint32_t s = *srcp;
  856. rt_uint32_t alpha = s >> 24;
  857. /* FIXME: Here we special-case opaque alpha since the
  858. compositioning used (>>8 instead of /255) doesn't handle
  859. it correctly. Also special-case alpha=0 for speed?
  860. Benchmark this! */
  861. if(alpha == 255) {
  862. *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
  863. } else {
  864. /*
  865. * take out the middle component (green), and process
  866. * the other two in parallel. One multiply less.
  867. */
  868. d = *dstp;
  869. dalpha = d & 0xff000000;
  870. s1 = s & 0xff00ff;
  871. d1 = d & 0xff00ff;
  872. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
  873. s &= 0xff00;
  874. d &= 0xff00;
  875. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  876. *dstp = d1 | d | dalpha;
  877. }
  878. ++srcp;
  879. ++dstp;
  880. }, width);
  881. srcp += srcskip;
  882. dstp += dstskip;
  883. }
  884. }
  885. static void BlitARGB8888toARGB8888PixelAlpha(struct rtgui_blit_info *info)
  886. {
  887. rt_uint32_t srcpixel;
  888. rt_uint32_t srcR, srcG, srcB, srcA;
  889. rt_uint32_t dstpixel;
  890. rt_uint32_t dstR, dstG, dstB, dstA;
  891. while (info->dst_h--) {
  892. rt_uint32_t *src = (rt_uint32_t *)info->src;
  893. rt_uint32_t *dst = (rt_uint32_t *)info->dst;
  894. int n = info->dst_w;
  895. while (n--) {
  896. srcpixel = *src;
  897. srcA = (rt_uint8_t)(srcpixel >> 24); srcR = (rt_uint8_t)(srcpixel >> 16); srcG = (rt_uint8_t)(srcpixel >> 8); srcB = (rt_uint8_t)srcpixel;
  898. dstpixel = *dst;
  899. dstA = (rt_uint8_t)(dstpixel >> 24); dstR = (rt_uint8_t)(dstpixel >> 16); dstG = (rt_uint8_t)(dstpixel >> 8); dstB = (rt_uint8_t)dstpixel;
  900. dstR = srcR + ((255 - srcA) * dstR) / 255;
  901. dstG = srcG + ((255 - srcA) * dstG) / 255;
  902. dstB = srcB + ((255 - srcA) * dstB) / 255;
  903. dstA = srcA + ((255 - srcA) * dstA) / 255;
  904. dstpixel = ((rt_uint32_t)dstA << 24) | ((rt_uint32_t)dstR << 16) | ((rt_uint32_t)dstG << 8) | dstB;
  905. *dst = dstpixel;
  906. ++src;
  907. ++dst;
  908. }
  909. info->src += info->src_pitch;
  910. info->dst += info->dst_pitch;
  911. }
  912. }
  913. /* Special optimized blit for RGB 5-6-5 --> 32-bit RGB surfaces */
  914. #define RGB565_32(dst, src, map) (map[src[LO]*2] + map[src[HI]*2+1])
  915. static void
  916. BlitRGB565to32(struct rtgui_blit_info * info, const rt_uint32_t* map)
  917. {
  918. int width, height;
  919. rt_uint8_t *src;
  920. rt_uint32_t *dst;
  921. int srcskip, dstskip;
  922. /* Set up some basic variables */
  923. width = info->dst_w;
  924. height = info->dst_h;
  925. src = (rt_uint8_t *) info->src;
  926. srcskip = info->src_skip;
  927. dst = (rt_uint32_t *) info->dst;
  928. dstskip = info->dst_skip / 4;
  929. while (height--) {
  930. /* *INDENT-OFF* */
  931. DUFFS_LOOP(
  932. {
  933. *dst++ = RGB565_32(dst, src, map);
  934. src += 2;
  935. },
  936. width);
  937. /* *INDENT-ON* */
  938. src += srcskip;
  939. dst += dstskip;
  940. }
  941. }
  942. static void
  943. BlitRGB565toARGB8888(struct rtgui_blit_info * info)
  944. {
  945. BlitRGB565to32(info, RGB565_ARGB8888_LUT);
  946. }
  947. void rtgui_blit(struct rtgui_blit_info * info)
  948. {
  949. if (info->src_h == 0 ||
  950. info->src_w == 0 ||
  951. info->dst_h == 0 ||
  952. info->dst_w == 0)
  953. return;
  954. /* We only use the dst_w in the low level drivers. So adjust the info right
  955. * here. Note the origin is always (0, 0). */
  956. if (info->src_w < info->dst_w)
  957. {
  958. info->dst_w = info->src_w;
  959. info->dst_skip = info->dst_pitch - info->dst_w *
  960. rtgui_color_get_bpp(info->dst_fmt);
  961. }
  962. else if (info->src_w > info->dst_w)
  963. {
  964. info->src_skip = info->src_pitch - info->dst_w *
  965. rtgui_color_get_bpp(info->src_fmt);
  966. }
  967. if (info->src_h < info->dst_h)
  968. info->dst_h = info->src_h;
  969. if (info->src_fmt == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  970. {
  971. if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  972. Blit565to565PixelAlpha(info);
  973. else if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
  974. BlitRGB565toARGB8888(info);
  975. }
  976. else if (info->src_fmt == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
  977. {
  978. if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  979. BlitARGBto565PixelAlpha(info);
  980. else if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_RGB888)
  981. BlitRGBtoRGBPixelAlpha(info);
  982. else if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
  983. BlitARGB8888toARGB8888PixelAlpha(info);
  984. }
  985. else if (info->src_fmt == RTGRAPHIC_PIXEL_FORMAT_RGB888)
  986. {
  987. if (info->dst_fmt == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
  988. BlitRGBtoRGBSurfaceAlpha(info);
  989. }
  990. }
  991. RTM_EXPORT(rtgui_blit);
  992. #if 0
  993. void rtgui_blit_client(rtgui_blit_info_src* src, struct rtgui_dc* client, struct rtgui_rect *dc_rect)
  994. {
  995. int index;
  996. struct rtgui_widget* widget;
  997. rtgui_region_t clip_region;
  998. rtgui_rect_t rect;
  999. /* get owner widget */
  1000. widget = RTGUI_CONTAINER_OF(client, struct rtgui_widget, dc_type);
  1001. /* get rect information */
  1002. if (dc_rect == NULL) rtgui_dc_get_rect(client, &rect);
  1003. else rect = *dc_rect;
  1004. /* to device */
  1005. rtgui_rect_moveto(&rect, widget->extent.x1, widget->extent.y1);
  1006. /* get the clipped region */
  1007. rtgui_region_intersect_rect(&clip_region, &widget->clip, &rect);
  1008. /* only 1 rect in extant */
  1009. if (clip_region.data == RT_NULL)
  1010. {
  1011. struct rtgui_blit_info info;
  1012. }
  1013. else
  1014. {
  1015. /* blit on each rect */
  1016. for (index = 0; index < clip_region.data.numRects; index ++)
  1017. {
  1018. }
  1019. }
  1020. rtgui_region_fini(&clip_region);
  1021. }
  1022. RTM_EXPORT(rtgui_blit_client);
  1023. #endif