Fix Skia rendering when using `reverse-byte-order` option.

When using `--reverse-byte-order` rendering option, the R and B
channels in the backing SkCanvas are already flipped during the
construction of Skia rendering device. We no longer needed to flip the
byte order in Upsample() calls.

Bug: chromium:1396264, pdfium:1955
Change-Id: I272a70a8e588c772044877058cf9739154b86220
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/103110
Commit-Queue: Nigi <nigi@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index 7f8034d..f3ed873 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -600,8 +600,7 @@
               SkBitmap* skBitmap,
               int* widthPtr,
               int* heightPtr,
-              bool forceAlpha,
-              bool bRgbByteOrder) {
+              bool forceAlpha) {
   void* buffer = pSource->GetBuffer().data();
   if (!buffer)
     return false;
@@ -629,7 +628,7 @@
           dst32_storage = Fill32BppDestStorageWith1BppSource(pSource);
           buffer = dst32_storage.data();
           rowBytes = width * sizeof(uint32_t);
-          colorType = Get32BitSkColorType(bRgbByteOrder);
+          colorType = kBGRA_8888_SkColorType;
           break;
         }
 
@@ -662,7 +661,7 @@
         dst32_storage = Fill32BppDestStorageWithPalette(pSource, src_palette);
         buffer = dst32_storage.data();
         rowBytes = width * sizeof(uint32_t);
-        colorType = Get32BitSkColorType(bRgbByteOrder);
+        colorType = kBGRA_8888_SkColorType;
       }
       break;
     case 24: {
@@ -679,12 +678,12 @@
       }
       buffer = dst32_storage.data();
       rowBytes = width * sizeof(uint32_t);
-      colorType = Get32BitSkColorType(bRgbByteOrder);
+      colorType = kBGRA_8888_SkColorType;
       alphaType = kOpaque_SkAlphaType;
       break;
     }
     case 32:
-      colorType = Get32BitSkColorType(bRgbByteOrder);
+      colorType = kBGRA_8888_SkColorType;
       pSource->DebugVerifyBufferIsPreMultiplied(buffer);
       break;
     default:
@@ -2088,11 +2087,11 @@
   int maskWidth;
   int maskHeight;
   if (!Upsample(pSource, src8_storage, src32_storage, &skBitmap, &srcWidth,
-                &srcHeight, /*forceAlpha=*/false, m_bRgbByteOrder)) {
+                &srcHeight, /*forceAlpha=*/false)) {
     return false;
   }
   if (!Upsample(pMask, mask8_storage, mask32_storage, &skMask, &maskWidth,
-                &maskHeight, /*forceAlpha=*/true, m_bRgbByteOrder)) {
+                &maskHeight, /*forceAlpha=*/true)) {
     return false;
   }
   {
@@ -2170,7 +2169,7 @@
   int width;
   int height;
   if (!Upsample(pSource, dst8_storage, dst32_storage, &skBitmap, &width,
-                &height, /*forceAlpha=*/false, m_bRgbByteOrder)) {
+                &height, /*forceAlpha=*/false)) {
     return false;
   }
   {
diff --git a/fpdfsdk/fpdf_view_embeddertest.cpp b/fpdfsdk/fpdf_view_embeddertest.cpp
index 4462154..0f33f6e 100644
--- a/fpdfsdk/fpdf_view_embeddertest.cpp
+++ b/fpdfsdk/fpdf_view_embeddertest.cpp
@@ -1448,6 +1448,21 @@
   UnloadPage(page);
 }
 
+// TODO(crbug.com/pdfium/1955): Remove this test once pixel tests can pass with
+// `reverse-byte-order` option.
+TEST_F(FPDFViewEmbedderTest, RenderBlueAndRedImagesWithReverByteOrderFlag) {
+  // When rendering with `FPDF_REVERSE_BYTE_ORDER` flag, the blue and red
+  // channels should be reversed.
+  ASSERT_TRUE(OpenDocument("bug_1396264.pdf"));
+  ScopedFPDFPage page(FPDF_LoadPage(document(), 0));
+  ASSERT_TRUE(page);
+
+  TestRenderPageBitmapWithFlags(page.get(), 0,
+                                "81e7f4498090977c848a21b5c6510d3a");
+  TestRenderPageBitmapWithFlags(page.get(), FPDF_REVERSE_BYTE_ORDER,
+                                "505ba6d1c7f4044c11c91873452a8bde");
+}
+
 TEST_F(FPDFViewEmbedderTest, RenderJpxLzwImageWithFlags) {
   static const char kNormalChecksum[] = "4bcd56cae1ca2622403e8af07242e71a";
   static const char kGrayscaleChecksum[] = "fe45ad56efe868ba82285fa5ffedc0cb";
diff --git a/testing/resources/bug_1396264.in b/testing/resources/bug_1396264.in
new file mode 100644
index 0000000..db1fc5a
--- /dev/null
+++ b/testing/resources/bug_1396264.in
@@ -0,0 +1,86 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Contents 4 0 R
+  /MediaBox [0 0 100 200]
+  /Resources <<
+    /XObject <<
+      % Both images are BGRA.
+      /ImBlue 5 0 R
+      /ImRed 6 0 R
+    >>
+  >>
+>>
+endobj
+{{object 4 0}} <<
+  {{streamlen}}
+>>
+stream
+q
+100 0 0 100 0 0 cm
+/ImBlue Do
+Q
+q
+100 0 0 100 0 100 cm
+/ImRed Do
+Q
+endstream
+endobj
+{{object 5 0}} <<
+  /Type /XObject
+  /Subtype /Image
+  /BitsPerComponent 8
+  /ColorSpace /DeviceRGB
+  /Filter [/ASCIIHexDecode /JPXDecode]
+  /Height 64
+  /Width 64
+  {{streamlen}}
+>>
+stream
+0000000c6a5020200d0a870a00000014667479706a703220000000006a7032200000004f6a7032
+68000000166968647200000040000000400004070700000000000f636f6c720100000000001000
+000022636465660004000000000001000100000002000200000003000300010000000000c06a70
+3263ff4fff51003200000000004000000040000000000000000000000040000000400000000000
+0000000004070101070101070101070101ff52000c00000001010504040001ff5c001340404848
+50484850484850484850484850ff640025000143726561746564206279204f70656e4a50454720
+76657273696f6e20322e332e30ff90000a0000000000360001ff93cfb41008908a6fdf801801ca
+bf00cfb40c01cabf0000000000000000000000000000000000000000ffd9
+endstream
+endobj
+{{object 6 0}} <<
+  /Type /XObject
+  /Subtype /Image
+  /BitsPerComponent 8
+  /ColorSpace /DeviceRGB
+  /Filter [/ASCIIHexDecode /JPXDecode]
+  /Height 64
+  /Width 64
+  {{streamlen}}
+>>
+stream
+0000000c6a5020200d0a870a00000014667479706a703220000000006a7032200000004f6a7032
+68000000166968647200000040000000400004070700000000000f636f6c720100000000001000
+000022636465660004000000000001000100000002000200000003000300010000000000c06a70
+3263ff4fff51003200000000004000000040000000000000000000000040000000400000000000
+0000000004070101070101070101070101ff52000c00000001010504040001ff5c001340404848
+50484850484850484850484850ff640025000143726561746564206279204f70656e4a50454720
+76657273696f6e20322e332e30ff90000a0000000000360001ff93cfb41008908a6f00df801801
+cabfcfb40c01cabf0000000000000000000000000000000000000000ffd9
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/bug_1396264.pdf b/testing/resources/bug_1396264.pdf
new file mode 100644
index 0000000..9c2bddc
--- /dev/null
+++ b/testing/resources/bug_1396264.pdf
@@ -0,0 +1,99 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+  /Type /Pages
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+3 0 obj <<
+  /Type /Page
+  /Parent 2 0 R
+  /Contents 4 0 R
+  /MediaBox [0 0 100 200]
+  /Resources <<
+    /XObject <<
+      % Both images are BGRA.
+      /ImBlue 5 0 R
+      /ImRed 6 0 R
+    >>
+  >>
+>>
+endobj
+4 0 obj <<
+  /Length 69
+>>
+stream
+q
+100 0 0 100 0 0 cm
+/ImBlue Do
+Q
+q
+100 0 0 100 0 100 cm
+/ImRed Do
+Q
+endstream
+endobj
+5 0 obj <<
+  /Type /XObject
+  /Subtype /Image
+  /BitsPerComponent 8
+  /ColorSpace /DeviceRGB
+  /Filter [/ASCIIHexDecode /JPXDecode]
+  /Height 64
+  /Width 64
+  /Length 614
+>>
+stream
+0000000c6a5020200d0a870a00000014667479706a703220000000006a7032200000004f6a7032
+68000000166968647200000040000000400004070700000000000f636f6c720100000000001000
+000022636465660004000000000001000100000002000200000003000300010000000000c06a70
+3263ff4fff51003200000000004000000040000000000000000000000040000000400000000000
+0000000004070101070101070101070101ff52000c00000001010504040001ff5c001340404848
+50484850484850484850484850ff640025000143726561746564206279204f70656e4a50454720
+76657273696f6e20322e332e30ff90000a0000000000360001ff93cfb41008908a6fdf801801ca
+bf00cfb40c01cabf0000000000000000000000000000000000000000ffd9
+endstream
+endobj
+6 0 obj <<
+  /Type /XObject
+  /Subtype /Image
+  /BitsPerComponent 8
+  /ColorSpace /DeviceRGB
+  /Filter [/ASCIIHexDecode /JPXDecode]
+  /Height 64
+  /Width 64
+  /Length 614
+>>
+stream
+0000000c6a5020200d0a870a00000014667479706a703220000000006a7032200000004f6a7032
+68000000166968647200000040000000400004070700000000000f636f6c720100000000001000
+000022636465660004000000000001000100000002000200000003000300010000000000c06a70
+3263ff4fff51003200000000004000000040000000000000000000000040000000400000000000
+0000000004070101070101070101070101ff52000c00000001010504040001ff5c001340404848
+50484850484850484850484850ff640025000143726561746564206279204f70656e4a50454720
+76657273696f6e20322e332e30ff90000a0000000000360001ff93cfb41008908a6f00df801801
+cabfcfb40c01cabf0000000000000000000000000000000000000000ffd9
+endstream
+endobj
+xref
+0 7
+0000000000 65535 f 
+0000000015 00000 n 
+0000000068 00000 n 
+0000000131 00000 n 
+0000000339 00000 n 
+0000000459 00000 n 
+0000001271 00000 n 
+trailer <<
+  /Root 1 0 R
+  /Size 7
+>>
+startxref
+2083
+%%EOF