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 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 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 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 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 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 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 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 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Swizzle.h"
#include "Logging.h"
#include "Orientation.h"
#include "Tools.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/UniquePtr.h"
#ifdef USE_SSE2
# include "mozilla/SSE.h"
#endif
#ifdef USE_NEON
# include "mozilla/arm.h"
#endif
#include <new>
namespace mozilla {
namespace gfx {
/**
* Convenience macros for dispatching to various format combinations.
*/
// Hash the formats to a relatively dense value to optimize jump table
// generation. The first 6 formats in SurfaceFormat are the 32-bit BGRA variants
// and are the most common formats dispatched here. Room is reserved in the
// lowish bits for up to these 6 destination formats. If a destination format is
// >= 6, the 6th bit is set to avoid collisions.
#define FORMAT_KEY(aSrcFormat, aDstFormat) \
(int(aSrcFormat) * 6 + int(aDstFormat) + (int(int(aDstFormat) >= 6) << 6))
#define FORMAT_CASE_EXPR(aSrcFormat, aDstFormat, ...) \
case FORMAT_KEY(aSrcFormat, aDstFormat): \
__VA_ARGS__; \
return true;
#define FORMAT_CASE(aSrcFormat, aDstFormat, ...) \
FORMAT_CASE_EXPR(aSrcFormat, aDstFormat, FORMAT_CASE_CALL(__VA_ARGS__))
#define FORMAT_CASE_ROW(aSrcFormat, aDstFormat, ...) \
case FORMAT_KEY(aSrcFormat, aDstFormat): \
return &__VA_ARGS__;
/**
* Constexpr functions for analyzing format attributes in templates.
*/
// Whether B comes before R in pixel memory layout.
static constexpr bool IsBGRFormat(SurfaceFormat aFormat) {
return aFormat == SurfaceFormat::B8G8R8A8 ||
#if MOZ_LITTLE_ENDIAN()
aFormat == SurfaceFormat::R5G6B5_UINT16 ||
#endif
aFormat == SurfaceFormat::B8G8R8X8 || aFormat == SurfaceFormat::B8G8R8;
}
// Whether the order of B and R need to be swapped to map from src to dst.
static constexpr bool ShouldSwapRB(SurfaceFormat aSrcFormat,
SurfaceFormat aDstFormat) {
return IsBGRFormat(aSrcFormat) != IsBGRFormat(aDstFormat);
}
// The starting byte of the RGB components in pixel memory.
static constexpr uint32_t RGBByteIndex(SurfaceFormat aFormat) {
return aFormat == SurfaceFormat::A8R8G8B8 ||
aFormat == SurfaceFormat::X8R8G8B8
? 1
: 0;
}
// The byte of the alpha component, which just comes after RGB.
static constexpr uint32_t AlphaByteIndex(SurfaceFormat aFormat) {
return (RGBByteIndex(aFormat) + 3) % 4;
}
// The endian-dependent bit shift to access RGB of a UINT32 pixel.
static constexpr uint32_t RGBBitShift(SurfaceFormat aFormat) {
#if MOZ_LITTLE_ENDIAN()
return 8 * RGBByteIndex(aFormat);
#else
return 8 - 8 * RGBByteIndex(aFormat);
#endif
}
// The endian-dependent bit shift to access alpha of a UINT32 pixel.
static constexpr uint32_t AlphaBitShift(SurfaceFormat aFormat) {
return (RGBBitShift(aFormat) + 24) % 32;
}
// Whether the pixel format should ignore the value of the alpha channel and
// treat it as opaque.
static constexpr bool IgnoreAlpha(SurfaceFormat aFormat) {
return aFormat == SurfaceFormat::B8G8R8X8 ||
aFormat == SurfaceFormat::R8G8B8X8 ||
aFormat == SurfaceFormat::X8R8G8B8;
}
// Whether to force alpha to opaque to map from src to dst.
static constexpr bool ShouldForceOpaque(SurfaceFormat aSrcFormat,
SurfaceFormat aDstFormat) {
return IgnoreAlpha(aSrcFormat) != IgnoreAlpha(aDstFormat);
}
#ifdef USE_SSE2
/**
* SSE2 optimizations
*/
template <bool aSwapRB, bool aOpaqueAlpha>
void Premultiply_SSE2(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define PREMULTIPLY_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Premultiply_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void PremultiplyRow_SSE2(const uint8_t*, uint8_t*, int32_t);
# define PREMULTIPLY_ROW_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
PremultiplyRow_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void Unpremultiply_SSE2(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define UNPREMULTIPLY_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Unpremultiply_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void UnpremultiplyRow_SSE2(const uint8_t*, uint8_t*, int32_t);
# define UNPREMULTIPLY_ROW_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
UnpremultiplyRow_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void Swizzle_SSE2(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define SWIZZLE_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Swizzle_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void SwizzleRow_SSE2(const uint8_t*, uint8_t*, int32_t);
# define SWIZZLE_ROW_SSE2(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
SwizzleRow_SSE2<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void UnpackRowRGB24_SSSE3(const uint8_t*, uint8_t*, int32_t);
# define UNPACK_ROW_RGB_SSSE3(aDstFormat) \
FORMAT_CASE_ROW( \
SurfaceFormat::R8G8B8, aDstFormat, \
UnpackRowRGB24_SSSE3<ShouldSwapRB(SurfaceFormat::R8G8B8, aDstFormat)>)
template <bool aSwapRB>
void UnpackRowRGB24_AVX2(const uint8_t*, uint8_t*, int32_t);
# define UNPACK_ROW_RGB_AVX2(aDstFormat) \
FORMAT_CASE_ROW( \
SurfaceFormat::R8G8B8, aDstFormat, \
UnpackRowRGB24_AVX2<ShouldSwapRB(SurfaceFormat::R8G8B8, aDstFormat)>)
#endif
#ifdef USE_NEON
/**
* ARM NEON optimizations
*/
template <bool aSwapRB, bool aOpaqueAlpha>
void Premultiply_NEON(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define PREMULTIPLY_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Premultiply_NEON<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void PremultiplyRow_NEON(const uint8_t*, uint8_t*, int32_t);
# define PREMULTIPLY_ROW_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
PremultiplyRow_NEON<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void Unpremultiply_NEON(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define UNPREMULTIPLY_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Unpremultiply_NEON<ShouldSwapRB(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void UnpremultiplyRow_NEON(const uint8_t*, uint8_t*, int32_t);
# define UNPREMULTIPLY_ROW_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
UnpremultiplyRow_NEON<ShouldSwapRB(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void Swizzle_NEON(const uint8_t*, int32_t, uint8_t*, int32_t, IntSize);
# define SWIZZLE_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
Swizzle_NEON<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB, bool aOpaqueAlpha>
void SwizzleRow_NEON(const uint8_t*, uint8_t*, int32_t);
# define SWIZZLE_ROW_NEON(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
SwizzleRow_NEON<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat)>)
template <bool aSwapRB>
void UnpackRowRGB24_NEON(const uint8_t*, uint8_t*, int32_t);
# define UNPACK_ROW_RGB_NEON(aDstFormat) \
FORMAT_CASE_ROW( \
SurfaceFormat::R8G8B8, aDstFormat, \
UnpackRowRGB24_NEON<ShouldSwapRB(SurfaceFormat::R8G8B8, aDstFormat)>)
#endif
/**
* Premultiplying
*/
// Fallback premultiply implementation that uses splayed pixel math to reduce
// the multiplications used. That is, the R and B components are isolated from
// the G and A components, which then can be multiplied as if they were two
// 2-component vectors. Otherwise, an approximation if divide-by-255 is used
// which is faster than an actual division. These optimizations are also used
// for the SSE2 and NEON implementations.
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void PremultiplyChunkFallback(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
// Load and process 1 entire pixel at a time.
uint32_t color = *reinterpret_cast<const uint32_t*>(aSrc);
uint32_t a = aSrcAShift ? color >> aSrcAShift : color & 0xFF;
// Isolate the R and B components.
uint32_t rb = (color >> aSrcRGBShift) & 0x00FF00FF;
// Swap the order of R and B if necessary.
if (aSwapRB) {
rb = (rb >> 16) | (rb << 16);
}
// Approximate the multiply by alpha and divide by 255 which is
// essentially:
// c = c*a + 255; c = (c + (c >> 8)) >> 8;
// However, we omit the final >> 8 to fold it with the final shift into
// place depending on desired output format.
rb = rb * a + 0x00FF00FF;
rb = (rb + ((rb >> 8) & 0x00FF00FF)) & 0xFF00FF00;
// Use same approximation as above, but G is shifted 8 bits left.
// Alpha is left out and handled separately.
uint32_t g = color & (0xFF00 << aSrcRGBShift);
g = g * a + (0xFF00 << aSrcRGBShift);
g = (g + (g >> 8)) & (0xFF0000 << aSrcRGBShift);
// The above math leaves RGB shifted left by 8 bits.
// Shift them right if required for the output format.
// then combine them back together to produce output pixel.
// Add the alpha back on if the output format is not opaque.
*reinterpret_cast<uint32_t*>(aDst) =
(rb >> (8 - aDstRGBShift)) | (g >> (8 + aSrcRGBShift - aDstRGBShift)) |
(aOpaqueAlpha ? 0xFF << aDstAShift : a << aDstAShift);
aSrc += 4;
aDst += 4;
} while (aSrc < end);
}
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void PremultiplyRowFallback(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
PremultiplyChunkFallback<aSwapRB, aOpaqueAlpha, aSrcRGBShift, aSrcAShift,
aDstRGBShift, aDstAShift>(aSrc, aDst, aLength);
}
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void PremultiplyFallback(const uint8_t* aSrc, int32_t aSrcGap,
uint8_t* aDst, int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
PremultiplyChunkFallback<aSwapRB, aOpaqueAlpha, aSrcRGBShift, aSrcAShift,
aDstRGBShift, aDstAShift>(aSrc, aDst, aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define PREMULTIPLY_FALLBACK_CASE(aSrcFormat, aDstFormat) \
FORMAT_CASE( \
aSrcFormat, aDstFormat, \
PremultiplyFallback<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat), \
RGBBitShift(aSrcFormat), AlphaBitShift(aSrcFormat), \
RGBBitShift(aDstFormat), AlphaBitShift(aDstFormat)>)
#define PREMULTIPLY_FALLBACK(aSrcFormat) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8A8) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8X8) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8A8) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8X8) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::A8R8G8B8) \
PREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::X8R8G8B8)
#define PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW(aSrcFormat, aDstFormat, \
PremultiplyRowFallback< \
ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat), \
RGBBitShift(aSrcFormat), AlphaBitShift(aSrcFormat), \
RGBBitShift(aDstFormat), AlphaBitShift(aDstFormat)>)
#define PREMULTIPLY_ROW_FALLBACK(aSrcFormat) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8A8) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8X8) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8A8) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8X8) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::A8R8G8B8) \
PREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::X8R8G8B8)
// If rows are tightly packed, and the size of the total area will fit within
// the precision range of a single row, then process all the data as if it was
// a single row.
static inline IntSize CollapseSize(const IntSize& aSize, int32_t aSrcStride,
int32_t aDstStride) {
if (aSrcStride == aDstStride && (aSrcStride & 3) == 0 &&
aSrcStride / 4 == aSize.width) {
CheckedInt32 area = CheckedInt32(aSize.width) * CheckedInt32(aSize.height);
if (area.isValid()) {
return IntSize(area.value(), 1);
}
}
return aSize;
}
static inline int32_t GetStrideGap(int32_t aWidth, SurfaceFormat aFormat,
int32_t aStride) {
CheckedInt32 used = CheckedInt32(aWidth) * BytesPerPixel(aFormat);
if (!used.isValid() || used.value() < 0) {
return -1;
}
return aStride - used.value();
}
bool PremultiplyData(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst,
int32_t aDstStride, SurfaceFormat aDstFormat,
const IntSize& aSize) {
if (aSize.IsEmpty()) {
return true;
}
IntSize size = CollapseSize(aSize, aSrcStride, aDstStride);
// Find gap from end of row to the start of the next row.
int32_t srcGap = GetStrideGap(aSize.width, aSrcFormat, aSrcStride);
int32_t dstGap = GetStrideGap(aSize.width, aDstFormat, aDstStride);
MOZ_ASSERT(srcGap >= 0 && dstGap >= 0);
if (srcGap < 0 || dstGap < 0) {
return false;
}
#define FORMAT_CASE_CALL(...) __VA_ARGS__(aSrc, srcGap, aDst, dstGap, size)
#ifdef USE_SSE2
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
PREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
PREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_FALLBACK(SurfaceFormat::B8G8R8A8)
PREMULTIPLY_FALLBACK(SurfaceFormat::R8G8B8A8)
PREMULTIPLY_FALLBACK(SurfaceFormat::A8R8G8B8)
default:
break;
}
#undef FORMAT_CASE_CALL
MOZ_ASSERT(false, "Unsupported premultiply formats");
return false;
}
SwizzleRowFn PremultiplyRow(SurfaceFormat aSrcFormat,
SurfaceFormat aDstFormat) {
#ifdef USE_SSE2
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
PREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
PREMULTIPLY_ROW_FALLBACK(SurfaceFormat::B8G8R8A8)
PREMULTIPLY_ROW_FALLBACK(SurfaceFormat::R8G8B8A8)
PREMULTIPLY_ROW_FALLBACK(SurfaceFormat::A8R8G8B8)
default:
break;
}
MOZ_ASSERT_UNREACHABLE("Unsupported premultiply formats");
return nullptr;
}
/**
* Unpremultiplying
*/
// Generate a table of 8.16 fixed-point reciprocals representing 1/alpha.
#define UNPREMULQ(x) (0xFF00FFU / (x))
#define UNPREMULQ_2(x) UNPREMULQ(x), UNPREMULQ((x) + 1)
#define UNPREMULQ_4(x) UNPREMULQ_2(x), UNPREMULQ_2((x) + 2)
#define UNPREMULQ_8(x) UNPREMULQ_4(x), UNPREMULQ_4((x) + 4)
#define UNPREMULQ_16(x) UNPREMULQ_8(x), UNPREMULQ_8((x) + 8)
#define UNPREMULQ_32(x) UNPREMULQ_16(x), UNPREMULQ_16((x) + 16)
static const uint32_t sUnpremultiplyTable[256] = {0,
UNPREMULQ(1),
UNPREMULQ_2(2),
UNPREMULQ_4(4),
UNPREMULQ_8(8),
UNPREMULQ_16(16),
UNPREMULQ_32(32),
UNPREMULQ_32(64),
UNPREMULQ_32(96),
UNPREMULQ_32(128),
UNPREMULQ_32(160),
UNPREMULQ_32(192),
UNPREMULQ_32(224)};
// Fallback unpremultiply implementation that uses 8.16 fixed-point reciprocal
// math to eliminate any division by the alpha component. This optimization is
// used for the SSE2 and NEON implementations, with some adaptations. This
// implementation also accesses color components using individual byte accesses
// as this profiles faster than accessing the pixel as a uint32_t and
// shifting/masking to access components.
template <bool aSwapRB, uint32_t aSrcRGBIndex, uint32_t aSrcAIndex,
uint32_t aDstRGBIndex, uint32_t aDstAIndex>
static void UnpremultiplyChunkFallback(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
uint8_t r = aSrc[aSrcRGBIndex + (aSwapRB ? 2 : 0)];
uint8_t g = aSrc[aSrcRGBIndex + 1];
uint8_t b = aSrc[aSrcRGBIndex + (aSwapRB ? 0 : 2)];
uint8_t a = aSrc[aSrcAIndex];
// Access the 8.16 reciprocal from the table based on alpha. Multiply by
// the reciprocal and shift off the fraction bits to approximate the
// division by alpha.
uint32_t q = sUnpremultiplyTable[a];
aDst[aDstRGBIndex + 0] = (r * q) >> 16;
aDst[aDstRGBIndex + 1] = (g * q) >> 16;
aDst[aDstRGBIndex + 2] = (b * q) >> 16;
aDst[aDstAIndex] = a;
aSrc += 4;
aDst += 4;
} while (aSrc < end);
}
template <bool aSwapRB, uint32_t aSrcRGBIndex, uint32_t aSrcAIndex,
uint32_t aDstRGBIndex, uint32_t aDstAIndex>
static void UnpremultiplyRowFallback(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
UnpremultiplyChunkFallback<aSwapRB, aSrcRGBIndex, aSrcAIndex, aDstRGBIndex,
aDstAIndex>(aSrc, aDst, aLength);
}
template <bool aSwapRB, uint32_t aSrcRGBIndex, uint32_t aSrcAIndex,
uint32_t aDstRGBIndex, uint32_t aDstAIndex>
static void UnpremultiplyFallback(const uint8_t* aSrc, int32_t aSrcGap,
uint8_t* aDst, int32_t aDstGap,
IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
UnpremultiplyChunkFallback<aSwapRB, aSrcRGBIndex, aSrcAIndex, aDstRGBIndex,
aDstAIndex>(aSrc, aDst, aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define UNPREMULTIPLY_FALLBACK_CASE(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
UnpremultiplyFallback< \
ShouldSwapRB(aSrcFormat, aDstFormat), \
RGBByteIndex(aSrcFormat), AlphaByteIndex(aSrcFormat), \
RGBByteIndex(aDstFormat), AlphaByteIndex(aDstFormat)>)
#define UNPREMULTIPLY_FALLBACK(aSrcFormat) \
UNPREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8A8) \
UNPREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8A8) \
UNPREMULTIPLY_FALLBACK_CASE(aSrcFormat, SurfaceFormat::A8R8G8B8)
#define UNPREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW(aSrcFormat, aDstFormat, \
UnpremultiplyRowFallback< \
ShouldSwapRB(aSrcFormat, aDstFormat), \
RGBByteIndex(aSrcFormat), AlphaByteIndex(aSrcFormat), \
RGBByteIndex(aDstFormat), AlphaByteIndex(aDstFormat)>)
#define UNPREMULTIPLY_ROW_FALLBACK(aSrcFormat) \
UNPREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::B8G8R8A8) \
UNPREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::R8G8B8A8) \
UNPREMULTIPLY_ROW_FALLBACK_CASE(aSrcFormat, SurfaceFormat::A8R8G8B8)
bool UnpremultiplyData(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst,
int32_t aDstStride, SurfaceFormat aDstFormat,
const IntSize& aSize) {
if (aSize.IsEmpty()) {
return true;
}
IntSize size = CollapseSize(aSize, aSrcStride, aDstStride);
// Find gap from end of row to the start of the next row.
int32_t srcGap = GetStrideGap(aSize.width, aSrcFormat, aSrcStride);
int32_t dstGap = GetStrideGap(aSize.width, aDstFormat, aDstStride);
MOZ_ASSERT(srcGap >= 0 && dstGap >= 0);
if (srcGap < 0 || dstGap < 0) {
return false;
}
#define FORMAT_CASE_CALL(...) __VA_ARGS__(aSrc, srcGap, aDst, dstGap, size)
#ifdef USE_SSE2
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_FALLBACK(SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_FALLBACK(SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_FALLBACK(SurfaceFormat::A8R8G8B8)
default:
break;
}
#undef FORMAT_CASE_CALL
MOZ_ASSERT(false, "Unsupported unpremultiply formats");
return false;
}
SwizzleRowFn UnpremultiplyRow(SurfaceFormat aSrcFormat,
SurfaceFormat aDstFormat) {
#ifdef USE_SSE2
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPREMULTIPLY_ROW_FALLBACK(SurfaceFormat::B8G8R8A8)
UNPREMULTIPLY_ROW_FALLBACK(SurfaceFormat::R8G8B8A8)
UNPREMULTIPLY_ROW_FALLBACK(SurfaceFormat::A8R8G8B8)
default:
break;
}
MOZ_ASSERT_UNREACHABLE("Unsupported premultiply formats");
return nullptr;
}
/**
* Swizzling
*/
// Fallback swizzle implementation that uses shifting and masking to reorder
// pixels.
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void SwizzleChunkFallback(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
uint32_t rgba = *reinterpret_cast<const uint32_t*>(aSrc);
if (aSwapRB) {
// Handle R and B swaps by exchanging words and masking.
uint32_t rb =
((rgba << 16) | (rgba >> 16)) & (0x00FF00FF << aSrcRGBShift);
uint32_t ga = rgba & ((0xFF << aSrcAShift) | (0xFF00 << aSrcRGBShift));
rgba = rb | ga;
}
// If src and dst shifts differ, rotate left or right to move RGB into
// place, i.e. ARGB -> RGBA or ARGB -> RGBA.
if (aDstRGBShift > aSrcRGBShift) {
rgba = (rgba << 8) | (aOpaqueAlpha ? 0x000000FF : rgba >> 24);
} else if (aSrcRGBShift > aDstRGBShift) {
rgba = (rgba >> 8) | (aOpaqueAlpha ? 0xFF000000 : rgba << 24);
} else if (aOpaqueAlpha) {
rgba |= 0xFF << aDstAShift;
}
*reinterpret_cast<uint32_t*>(aDst) = rgba;
aSrc += 4;
aDst += 4;
} while (aSrc < end);
}
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void SwizzleRowFallback(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
SwizzleChunkFallback<aSwapRB, aOpaqueAlpha, aSrcRGBShift, aSrcAShift,
aDstRGBShift, aDstAShift>(aSrc, aDst, aLength);
}
template <bool aSwapRB, bool aOpaqueAlpha, uint32_t aSrcRGBShift,
uint32_t aSrcAShift, uint32_t aDstRGBShift, uint32_t aDstAShift>
static void SwizzleFallback(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
SwizzleChunkFallback<aSwapRB, aOpaqueAlpha, aSrcRGBShift, aSrcAShift,
aDstRGBShift, aDstAShift>(aSrc, aDst, aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define SWIZZLE_FALLBACK(aSrcFormat, aDstFormat) \
FORMAT_CASE( \
aSrcFormat, aDstFormat, \
SwizzleFallback<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat), \
RGBBitShift(aSrcFormat), AlphaBitShift(aSrcFormat), \
RGBBitShift(aDstFormat), AlphaBitShift(aDstFormat)>)
#define SWIZZLE_ROW_FALLBACK(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
SwizzleRowFallback<ShouldSwapRB(aSrcFormat, aDstFormat), \
ShouldForceOpaque(aSrcFormat, aDstFormat), \
RGBBitShift(aSrcFormat), AlphaBitShift(aSrcFormat), \
RGBBitShift(aDstFormat), AlphaBitShift(aDstFormat)>)
// Fast-path for matching formats.
template <int32_t aBytesPerPixel>
static void SwizzleRowCopy(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
if (aSrc != aDst) {
memcpy(aDst, aSrc, aLength * aBytesPerPixel);
}
}
// Fast-path for matching formats.
static void SwizzleCopy(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize, int32_t aBPP) {
if (aSrc != aDst) {
int32_t rowLength = aBPP * aSize.width;
for (int32_t height = aSize.height; height > 0; height--) {
memcpy(aDst, aSrc, rowLength);
aSrc += rowLength + aSrcGap;
aDst += rowLength + aDstGap;
}
}
}
// Fast-path for conversions that swap all bytes.
template <bool aOpaqueAlpha, uint32_t aSrcAShift, uint32_t aDstAShift>
static void SwizzleChunkSwap(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
// Use an endian swap to move the bytes, i.e. BGRA -> ARGB.
uint32_t rgba = *reinterpret_cast<const uint32_t*>(aSrc);
#if MOZ_LITTLE_ENDIAN()
rgba = NativeEndian::swapToBigEndian(rgba);
#else
rgba = NativeEndian::swapToLittleEndian(rgba);
#endif
if (aOpaqueAlpha) {
rgba |= 0xFF << aDstAShift;
}
*reinterpret_cast<uint32_t*>(aDst) = rgba;
aSrc += 4;
aDst += 4;
} while (aSrc < end);
}
template <bool aOpaqueAlpha, uint32_t aSrcAShift, uint32_t aDstAShift>
static void SwizzleRowSwap(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
SwizzleChunkSwap<aOpaqueAlpha, aSrcAShift, aDstAShift>(aSrc, aDst, aLength);
}
template <bool aOpaqueAlpha, uint32_t aSrcAShift, uint32_t aDstAShift>
static void SwizzleSwap(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
SwizzleChunkSwap<aOpaqueAlpha, aSrcAShift, aDstAShift>(aSrc, aDst,
aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define SWIZZLE_SWAP(aSrcFormat, aDstFormat) \
FORMAT_CASE( \
aSrcFormat, aDstFormat, \
SwizzleSwap<ShouldForceOpaque(aSrcFormat, aDstFormat), \
AlphaBitShift(aSrcFormat), AlphaBitShift(aDstFormat)>)
#define SWIZZLE_ROW_SWAP(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
SwizzleRowSwap<ShouldForceOpaque(aSrcFormat, aDstFormat), \
AlphaBitShift(aSrcFormat), AlphaBitShift(aDstFormat)>)
static void SwizzleChunkSwapRGB24(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 3 * aLength;
do {
uint8_t r = aSrc[0];
uint8_t g = aSrc[1];
uint8_t b = aSrc[2];
aDst[0] = b;
aDst[1] = g;
aDst[2] = r;
aSrc += 3;
aDst += 3;
} while (aSrc < end);
}
static void SwizzleRowSwapRGB24(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
SwizzleChunkSwapRGB24(aSrc, aDst, aLength);
}
static void SwizzleSwapRGB24(const uint8_t* aSrc, int32_t aSrcGap,
uint8_t* aDst, int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
SwizzleChunkSwapRGB24(aSrc, aDst, aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define SWIZZLE_SWAP_RGB24(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, SwizzleSwapRGB24)
#define SWIZZLE_ROW_SWAP_RGB24(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW(aSrcFormat, aDstFormat, SwizzleRowSwapRGB24)
// Fast-path for conversions that force alpha to opaque.
template <uint32_t aDstAShift>
static void SwizzleChunkOpaqueUpdate(uint8_t*& aBuffer, int32_t aLength) {
const uint8_t* end = aBuffer + 4 * aLength;
do {
uint32_t rgba = *reinterpret_cast<const uint32_t*>(aBuffer);
// Just add on the alpha bits to the source.
rgba |= 0xFF << aDstAShift;
*reinterpret_cast<uint32_t*>(aBuffer) = rgba;
aBuffer += 4;
} while (aBuffer < end);
}
template <uint32_t aDstAShift>
static void SwizzleChunkOpaqueCopy(const uint8_t*& aSrc, uint8_t* aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
uint32_t rgba = *reinterpret_cast<const uint32_t*>(aSrc);
// Just add on the alpha bits to the source.
rgba |= 0xFF << aDstAShift;
*reinterpret_cast<uint32_t*>(aDst) = rgba;
aSrc += 4;
aDst += 4;
} while (aSrc < end);
}
template <uint32_t aDstAShift>
static void SwizzleRowOpaque(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
if (aSrc == aDst) {
SwizzleChunkOpaqueUpdate<aDstAShift>(aDst, aLength);
} else {
SwizzleChunkOpaqueCopy<aDstAShift>(aSrc, aDst, aLength);
}
}
template <uint32_t aDstAShift>
static void SwizzleOpaque(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
if (aSrc == aDst) {
// Modifying in-place, so just write out the alpha.
for (int32_t height = aSize.height; height > 0; height--) {
SwizzleChunkOpaqueUpdate<aDstAShift>(aDst, aSize.width);
aDst += aDstGap;
}
} else {
for (int32_t height = aSize.height; height > 0; height--) {
SwizzleChunkOpaqueCopy<aDstAShift>(aSrc, aDst, aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
}
#define SWIZZLE_OPAQUE(aSrcFormat, aDstFormat) \
FORMAT_CASE(aSrcFormat, aDstFormat, SwizzleOpaque<AlphaBitShift(aDstFormat)>)
#define SWIZZLE_ROW_OPAQUE(aSrcFormat, aDstFormat) \
FORMAT_CASE_ROW(aSrcFormat, aDstFormat, \
SwizzleRowOpaque<AlphaBitShift(aDstFormat)>)
// Packing of 32-bit formats to RGB565.
template <bool aSwapRB, uint32_t aSrcRGBShift, uint32_t aSrcRGBIndex>
static void PackToRGB565(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
const uint8_t* end = aSrc + 4 * aSize.width;
do {
uint32_t rgba = *reinterpret_cast<const uint32_t*>(aSrc);
// Isolate the R, G, and B components and shift to final endian-dependent
// locations.
uint16_t rgb565;
if (aSwapRB) {
rgb565 = ((rgba & (0xF8 << aSrcRGBShift)) << (8 - aSrcRGBShift)) |
((rgba & (0xFC00 << aSrcRGBShift)) >> (5 + aSrcRGBShift)) |
((rgba & (0xF80000 << aSrcRGBShift)) >> (19 + aSrcRGBShift));
} else {
rgb565 = ((rgba & (0xF8 << aSrcRGBShift)) >> (3 + aSrcRGBShift)) |
((rgba & (0xFC00 << aSrcRGBShift)) >> (5 + aSrcRGBShift)) |
((rgba & (0xF80000 << aSrcRGBShift)) >> (8 + aSrcRGBShift));
}
*reinterpret_cast<uint16_t*>(aDst) = rgb565;
aSrc += 4;
aDst += 2;
} while (aSrc < end);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
// Packing of 32-bit formats to 24-bit formats.
template <bool aSwapRB, uint32_t aSrcRGBShift, uint32_t aSrcRGBIndex>
static void PackChunkToRGB24(const uint8_t*& aSrc, uint8_t*& aDst,
int32_t aLength) {
const uint8_t* end = aSrc + 4 * aLength;
do {
uint8_t r = aSrc[aSrcRGBIndex + (aSwapRB ? 2 : 0)];
uint8_t g = aSrc[aSrcRGBIndex + 1];
uint8_t b = aSrc[aSrcRGBIndex + (aSwapRB ? 0 : 2)];
aDst[0] = r;
aDst[1] = g;
aDst[2] = b;
aSrc += 4;
aDst += 3;
} while (aSrc < end);
}
template <bool aSwapRB, uint32_t aSrcRGBShift, uint32_t aSrcRGBIndex>
static void PackRowToRGB24(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
PackChunkToRGB24<aSwapRB, aSrcRGBShift, aSrcRGBIndex>(aSrc, aDst, aLength);
}
template <bool aSwapRB, uint32_t aSrcRGBShift, uint32_t aSrcRGBIndex>
static void PackToRGB24(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
PackChunkToRGB24<aSwapRB, aSrcRGBShift, aSrcRGBIndex>(aSrc, aDst,
aSize.width);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define PACK_RGB_CASE(aSrcFormat, aDstFormat, aPackFunc) \
FORMAT_CASE(aSrcFormat, aDstFormat, \
aPackFunc<ShouldSwapRB(aSrcFormat, aDstFormat), \
RGBBitShift(aSrcFormat), RGBByteIndex(aSrcFormat)>)
#define PACK_RGB(aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::B8G8R8A8, aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::B8G8R8X8, aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::R8G8B8A8, aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::R8G8B8X8, aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::A8R8G8B8, aDstFormat, aPackFunc) \
PACK_RGB_CASE(SurfaceFormat::X8R8G8B8, aDstFormat, aPackFunc)
#define PACK_ROW_RGB_CASE(aSrcFormat, aDstFormat, aPackFunc) \
FORMAT_CASE_ROW( \
aSrcFormat, aDstFormat, \
aPackFunc<ShouldSwapRB(aSrcFormat, aDstFormat), RGBBitShift(aSrcFormat), \
RGBByteIndex(aSrcFormat)>)
#define PACK_ROW_RGB(aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::B8G8R8A8, aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::B8G8R8X8, aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::R8G8B8A8, aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::R8G8B8X8, aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::A8R8G8B8, aDstFormat, aPackFunc) \
PACK_ROW_RGB_CASE(SurfaceFormat::X8R8G8B8, aDstFormat, aPackFunc)
// Packing of 32-bit formats to A8.
template <uint32_t aSrcAIndex>
static void PackToA8(const uint8_t* aSrc, int32_t aSrcGap, uint8_t* aDst,
int32_t aDstGap, IntSize aSize) {
for (int32_t height = aSize.height; height > 0; height--) {
const uint8_t* end = aSrc + 4 * aSize.width;
do {
*aDst++ = aSrc[aSrcAIndex];
aSrc += 4;
} while (aSrc < end);
aSrc += aSrcGap;
aDst += aDstGap;
}
}
#define PACK_ALPHA_CASE(aSrcFormat, aDstFormat, aPackFunc) \
FORMAT_CASE(aSrcFormat, aDstFormat, aPackFunc<AlphaByteIndex(aSrcFormat)>)
#define PACK_ALPHA(aDstFormat, aPackFunc) \
PACK_ALPHA_CASE(SurfaceFormat::B8G8R8A8, aDstFormat, aPackFunc) \
PACK_ALPHA_CASE(SurfaceFormat::R8G8B8A8, aDstFormat, aPackFunc) \
PACK_ALPHA_CASE(SurfaceFormat::A8R8G8B8, aDstFormat, aPackFunc)
template <bool aSwapRB>
void UnpackRowRGB24(const uint8_t* aSrc, uint8_t* aDst, int32_t aLength) {
// Because we are expanding, we can only process the data back to front in
// case we are performing this in place.
const uint8_t* src = aSrc + 3 * (aLength - 1);
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst + 4 * aLength);
while (src >= aSrc) {
uint8_t r = src[aSwapRB ? 2 : 0];
uint8_t g = src[1];
uint8_t b = src[aSwapRB ? 0 : 2];
#if MOZ_LITTLE_ENDIAN()
*--dst = 0xFF000000 | (b << 16) | (g << 8) | r;
#else
*--dst = 0x000000FF | (b << 8) | (g << 16) | (r << 24);
#endif
src -= 3;
}
}
// Force instantiation of swizzle variants here.
template void UnpackRowRGB24<false>(const uint8_t*, uint8_t*, int32_t);
template void UnpackRowRGB24<true>(const uint8_t*, uint8_t*, int32_t);
#define UNPACK_ROW_RGB(aDstFormat) \
FORMAT_CASE_ROW( \
SurfaceFormat::R8G8B8, aDstFormat, \
UnpackRowRGB24<ShouldSwapRB(SurfaceFormat::R8G8B8, aDstFormat)>)
static void UnpackRowRGB24_To_ARGB(const uint8_t* aSrc, uint8_t* aDst,
int32_t aLength) {
// Because we are expanding, we can only process the data back to front in
// case we are performing this in place.
const uint8_t* src = aSrc + 3 * (aLength - 1);
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst + 4 * aLength);
while (src >= aSrc) {
uint8_t r = src[0];
uint8_t g = src[1];
uint8_t b = src[2];
#if MOZ_LITTLE_ENDIAN()
*--dst = 0x000000FF | (r << 8) | (g << 16) | (b << 24);
#else
*--dst = 0xFF000000 | (r << 24) | (g << 16) | b;
#endif
src -= 3;
}
}
#define UNPACK_ROW_RGB_TO_ARGB(aDstFormat) \
FORMAT_CASE_ROW(SurfaceFormat::R8G8B8, aDstFormat, UnpackRowRGB24_To_ARGB)
bool SwizzleData(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst, int32_t aDstStride,
SurfaceFormat aDstFormat, const IntSize& aSize) {
if (aSize.IsEmpty()) {
return true;
}
IntSize size = CollapseSize(aSize, aSrcStride, aDstStride);
// Find gap from end of row to the start of the next row.
int32_t srcGap = GetStrideGap(aSize.width, aSrcFormat, aSrcStride);
int32_t dstGap = GetStrideGap(aSize.width, aDstFormat, aDstStride);
MOZ_ASSERT(srcGap >= 0 && dstGap >= 0);
if (srcGap < 0 || dstGap < 0) {
return false;
}
#define FORMAT_CASE_CALL(...) __VA_ARGS__(aSrc, srcGap, aDst, dstGap, size)
#ifdef USE_SSE2
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
SWIZZLE_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_SSE2(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_SSE2(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_SSE2(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_SSE2(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
SWIZZLE_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_NEON(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_NEON(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_NEON(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_NEON(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
SWIZZLE_FALLBACK(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_FALLBACK(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_FALLBACK(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_FALLBACK(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::A8R8G8B8)
SWIZZLE_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::X8R8G8B8)
SWIZZLE_FALLBACK(SurfaceFormat::A8R8G8B8, SurfaceFormat::R8G8B8A8)
SWIZZLE_FALLBACK(SurfaceFormat::X8R8G8B8, SurfaceFormat::R8G8B8X8)
SWIZZLE_FALLBACK(SurfaceFormat::A8R8G8B8, SurfaceFormat::R8G8B8X8)
SWIZZLE_FALLBACK(SurfaceFormat::X8R8G8B8, SurfaceFormat::R8G8B8A8)
SWIZZLE_SWAP(SurfaceFormat::B8G8R8A8, SurfaceFormat::A8R8G8B8)
SWIZZLE_SWAP(SurfaceFormat::B8G8R8A8, SurfaceFormat::X8R8G8B8)
SWIZZLE_SWAP(SurfaceFormat::B8G8R8X8, SurfaceFormat::X8R8G8B8)
SWIZZLE_SWAP(SurfaceFormat::B8G8R8X8, SurfaceFormat::A8R8G8B8)
SWIZZLE_SWAP(SurfaceFormat::A8R8G8B8, SurfaceFormat::B8G8R8A8)
SWIZZLE_SWAP(SurfaceFormat::A8R8G8B8, SurfaceFormat::B8G8R8X8)
SWIZZLE_SWAP(SurfaceFormat::X8R8G8B8, SurfaceFormat::B8G8R8X8)
SWIZZLE_SWAP(SurfaceFormat::X8R8G8B8, SurfaceFormat::B8G8R8A8)
SWIZZLE_SWAP_RGB24(SurfaceFormat::R8G8B8, SurfaceFormat::B8G8R8)
SWIZZLE_SWAP_RGB24(SurfaceFormat::B8G8R8, SurfaceFormat::R8G8B8)
SWIZZLE_OPAQUE(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_OPAQUE(SurfaceFormat::B8G8R8X8, SurfaceFormat::B8G8R8A8)
SWIZZLE_OPAQUE(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_OPAQUE(SurfaceFormat::R8G8B8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_OPAQUE(SurfaceFormat::A8R8G8B8, SurfaceFormat::X8R8G8B8)
SWIZZLE_OPAQUE(SurfaceFormat::X8R8G8B8, SurfaceFormat::A8R8G8B8)
PACK_RGB(SurfaceFormat::R5G6B5_UINT16, PackToRGB565)
PACK_RGB(SurfaceFormat::B8G8R8, PackToRGB24)
PACK_RGB(SurfaceFormat::R8G8B8, PackToRGB24)
PACK_ALPHA(SurfaceFormat::A8, PackToA8)
default:
break;
}
if (aSrcFormat == aDstFormat) {
// If the formats match, just do a generic copy.
SwizzleCopy(aSrc, srcGap, aDst, dstGap, size, BytesPerPixel(aSrcFormat));
return true;
}
#undef FORMAT_CASE_CALL
MOZ_ASSERT(false, "Unsupported swizzle formats");
return false;
}
static bool SwizzleYFlipDataInternal(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst,
int32_t aDstStride,
SurfaceFormat aDstFormat,
const IntSize& aSize,
SwizzleRowFn aSwizzleFn) {
if (!aSwizzleFn) {
return false;
}
// Guarantee our width and height are both greater than zero.
if (aSize.IsEmpty()) {
return true;
}
// Unlike SwizzleData/PremultiplyData, we don't use the stride gaps directly,
// but we can use it to verify that the stride is valid for our width and
// format.
int32_t srcGap = GetStrideGap(aSize.width, aSrcFormat, aSrcStride);
int32_t dstGap = GetStrideGap(aSize.width, aDstFormat, aDstStride);
MOZ_ASSERT(srcGap >= 0 && dstGap >= 0);
if (srcGap < 0 || dstGap < 0) {
return false;
}
// Swapping/swizzling to a new buffer is trivial.
if (aSrc != aDst) {
const uint8_t* src = aSrc;
const uint8_t* srcEnd = aSrc + aSize.height * aSrcStride;
uint8_t* dst = aDst + (aSize.height - 1) * aDstStride;
while (src < srcEnd) {
aSwizzleFn(src, dst, aSize.width);
src += aSrcStride;
dst -= aDstStride;
}
return true;
}
if (aSrcStride != aDstStride) {
return false;
}
// If we are swizzling in place, then we need a temporary row buffer.
UniquePtr<uint8_t[]> rowBuffer(new (std::nothrow) uint8_t[aDstStride]);
if (!rowBuffer) {
return false;
}
// Swizzle and swap the top and bottom rows until we meet in the middle.
int32_t middleRow = aSize.height / 2;
uint8_t* top = aDst;
uint8_t* bottom = aDst + (aSize.height - 1) * aDstStride;
for (int32_t row = 0; row < middleRow; ++row) {
memcpy(rowBuffer.get(), bottom, aDstStride);
aSwizzleFn(top, bottom, aSize.width);
aSwizzleFn(rowBuffer.get(), top, aSize.width);
top += aDstStride;
bottom -= aDstStride;
}
// If there is an odd numbered row, we haven't swizzled it yet.
if (aSize.height % 2 == 1) {
top = aDst + middleRow * aDstStride;
aSwizzleFn(top, top, aSize.width);
}
return true;
}
bool SwizzleYFlipData(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst,
int32_t aDstStride, SurfaceFormat aDstFormat,
const IntSize& aSize) {
return SwizzleYFlipDataInternal(aSrc, aSrcStride, aSrcFormat, aDst,
aDstStride, aDstFormat, aSize,
SwizzleRow(aSrcFormat, aDstFormat));
}
bool PremultiplyYFlipData(const uint8_t* aSrc, int32_t aSrcStride,
SurfaceFormat aSrcFormat, uint8_t* aDst,
int32_t aDstStride, SurfaceFormat aDstFormat,
const IntSize& aSize) {
return SwizzleYFlipDataInternal(aSrc, aSrcStride, aSrcFormat, aDst,
aDstStride, aDstFormat, aSize,
PremultiplyRow(aSrcFormat, aDstFormat));
}
SwizzleRowFn SwizzleRow(SurfaceFormat aSrcFormat, SurfaceFormat aDstFormat) {
#ifdef USE_SSE2
if (mozilla::supports_avx2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPACK_ROW_RGB_AVX2(SurfaceFormat::R8G8B8X8)
UNPACK_ROW_RGB_AVX2(SurfaceFormat::R8G8B8A8)
UNPACK_ROW_RGB_AVX2(SurfaceFormat::B8G8R8X8)
UNPACK_ROW_RGB_AVX2(SurfaceFormat::B8G8R8A8)
default:
break;
}
if (mozilla::supports_ssse3()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPACK_ROW_RGB_SSSE3(SurfaceFormat::R8G8B8X8)
UNPACK_ROW_RGB_SSSE3(SurfaceFormat::R8G8B8A8)
UNPACK_ROW_RGB_SSSE3(SurfaceFormat::B8G8R8X8)
UNPACK_ROW_RGB_SSSE3(SurfaceFormat::B8G8R8A8)
default:
break;
}
if (mozilla::supports_sse2()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
SWIZZLE_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_SSE2(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_SSE2(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_SSE2(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_SSE2(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_SSE2(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_SSE2(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
#ifdef USE_NEON
if (mozilla::supports_neon()) switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
UNPACK_ROW_RGB_NEON(SurfaceFormat::R8G8B8X8)
UNPACK_ROW_RGB_NEON(SurfaceFormat::R8G8B8A8)
UNPACK_ROW_RGB_NEON(SurfaceFormat::B8G8R8X8)
UNPACK_ROW_RGB_NEON(SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_NEON(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_NEON(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_NEON(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_NEON(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_NEON(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_NEON(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
default:
break;
}
#endif
switch (FORMAT_KEY(aSrcFormat, aDstFormat)) {
SWIZZLE_ROW_FALLBACK(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::B8G8R8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::B8G8R8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8A8, SurfaceFormat::A8R8G8B8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::R8G8B8X8, SurfaceFormat::X8R8G8B8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::A8R8G8B8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::X8R8G8B8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::A8R8G8B8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_FALLBACK(SurfaceFormat::X8R8G8B8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::B8G8R8A8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::B8G8R8X8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::R8G8B8A8, SurfaceFormat::R8G8B8X8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::R8G8B8X8, SurfaceFormat::R8G8B8A8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::A8R8G8B8, SurfaceFormat::X8R8G8B8)
SWIZZLE_ROW_OPAQUE(SurfaceFormat::X8R8G8B8, SurfaceFormat::A8R8G8B8)
SWIZZLE_ROW_SWAP(SurfaceFormat::B8G8R8A8, SurfaceFormat::A8R8G8B8)
SWIZZLE_ROW_SWAP(SurfaceFormat::B8G8R8A8, SurfaceFormat::X8R8G8B8)
SWIZZLE_ROW_SWAP(SurfaceFormat::B8G8R8X8, SurfaceFormat::X8R8G8B8)
SWIZZLE_ROW_SWAP(SurfaceFormat::B8G8R8X8, SurfaceFormat::A8R8G8B8)
SWIZZLE_ROW_SWAP(SurfaceFormat::A8R8G8B8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_SWAP(SurfaceFormat::A8R8G8B8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_SWAP(SurfaceFormat::X8R8G8B8, SurfaceFormat::B8G8R8X8)
SWIZZLE_ROW_SWAP(SurfaceFormat::X8R8G8B8, SurfaceFormat::B8G8R8A8)
SWIZZLE_ROW_SWAP_RGB24(SurfaceFormat::R8G8B8, SurfaceFormat::B8G8R8)
SWIZZLE_ROW_SWAP_RGB24(SurfaceFormat::B8G8R8, SurfaceFormat::R8G8B8)
UNPACK_ROW_RGB(SurfaceFormat::R8G8B8X8)
UNPACK_ROW_RGB(SurfaceFormat::R8G8B8A8)
UNPACK_ROW_RGB(SurfaceFormat::B8G8R8X8)
UNPACK_ROW_RGB(SurfaceFormat::B8G8R8A8)
UNPACK_ROW_RGB_TO_ARGB(SurfaceFormat::A8R8G8B8)
UNPACK_ROW_RGB_TO_ARGB(SurfaceFormat::X8R8G8B8)
PACK_ROW_RGB(SurfaceFormat::R8G8B8, PackRowToRGB24)
PACK_ROW_RGB(SurfaceFormat::B8G8R8, PackRowToRGB24)
default:
break;
}
if (aSrcFormat == aDstFormat) {
switch (BytesPerPixel(aSrcFormat)) {
case 4:
return &SwizzleRowCopy<4>;
case 3:
return &SwizzleRowCopy<3>;
default:
break;
}
}
MOZ_ASSERT_UNREACHABLE("Unsupported swizzle formats");
return nullptr;
}
static IntRect ReorientRowRotate0FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Reverse order of pixels in the row.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.width;
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst + aSrcRow * aDstStride) +
aDstSize.width - 1;
do {
*dst-- = *src++;
} while (src < end);
return IntRect(0, aSrcRow, aDstSize.width, 1);
}
static IntRect ReorientRowRotate90FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into left to right columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst) + aSrcRow;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst += stride;
} while (src < end);
return IntRect(aSrcRow, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate180FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into bottom to top rows.
uint8_t* dst = aDst + (aDstSize.height - aSrcRow - 1) * aDstStride;
memcpy(dst, aSrc, aDstSize.width * sizeof(uint32_t));
return IntRect(0, aDstSize.height - aSrcRow - 1, aDstSize.width, 1);
}
static IntRect ReorientRowRotate270FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into right to left
// columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst + (aDstSize.height - 1) * aDstStride) +
aDstSize.width - aSrcRow - 1;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst -= stride;
} while (src < end);
return IntRect(aDstSize.width - aSrcRow - 1, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate0Fallback(const uint8_t* aSrc, int32_t aSrcRow,
uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels into the destination.
uint8_t* dst = aDst + aSrcRow * aDstStride;
memcpy(dst, aSrc, aDstSize.width * sizeof(uint32_t));
return IntRect(0, aSrcRow, aDstSize.width, 1);
}
static IntRect ReorientRowRotate90Fallback(const uint8_t* aSrc, int32_t aSrcRow,
uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into right to left columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst) + aDstSize.width - aSrcRow - 1;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst += stride;
} while (src < end);
return IntRect(aDstSize.width - aSrcRow - 1, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate180Fallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into bottom to top
// rows.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.width;
uint32_t* dst = reinterpret_cast<uint32_t*>(
aDst + (aDstSize.height - aSrcRow - 1) * aDstStride) +
aDstSize.width - 1;
do {
*dst-- = *src++;
} while (src < end);
return IntRect(0, aDstSize.height - aSrcRow - 1, aDstSize.width, 1);
}
static IntRect ReorientRowRotate270Fallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into left to right
// column.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst + (aDstSize.height - 1) * aDstStride) +
aSrcRow;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst -= stride;
} while (src < end);
return IntRect(aSrcRow, 0, 1, aDstSize.height);
}
ReorientRowFn ReorientRow(const struct image::Orientation& aOrientation) {
switch (aOrientation.flip) {
case image::Flip::Unflipped:
switch (aOrientation.rotation) {
case image::Angle::D0:
return &ReorientRowRotate0Fallback;
case image::Angle::D90:
return &ReorientRowRotate90Fallback;
case image::Angle::D180:
return &ReorientRowRotate180Fallback;
case image::Angle::D270:
return &ReorientRowRotate270Fallback;
default:
break;
}
break;
case image::Flip::Horizontal:
switch (aOrientation.rotation) {
case image::Angle::D0:
return &ReorientRowRotate0FlipFallback;
case image::Angle::D90:
if (aOrientation.flipFirst) {
return &ReorientRowRotate270FlipFallback;
} else {
return &ReorientRowRotate90FlipFallback;
}
case image::Angle::D180:
return &ReorientRowRotate180FlipFallback;
case image::Angle::D270:
if (aOrientation.flipFirst) {
return &ReorientRowRotate90FlipFallback;
} else {
return &ReorientRowRotate270FlipFallback;
}
default:
break;
}
break;
default:
break;
}
MOZ_ASSERT_UNREACHABLE("Unhandled orientation!");
return nullptr;
}
} // namespace gfx
} // namespace mozilla
|