Implement CFX_RenderDevice::GetFlipMatrix().

Consolidate matrix transforms from several places.

Change-Id: I2febcaed69afd1cf3eabf68f27ffe24301f07ca2
Reviewed-on: https://pdfium-review.googlesource.com/c/44631
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 0e8975d..3f7b492 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -2391,8 +2391,8 @@
       if (bitmap_alpha < 255) {
 #ifdef _SKIA_SUPPORT_
         std::unique_ptr<CFX_ImageRenderer> dummy;
-        CFX_Matrix m(pDIBitmap->GetWidth(), 0, 0, -pDIBitmap->GetHeight(), left,
-                     top + pDIBitmap->GetHeight());
+        CFX_Matrix m = CFX_RenderDevice::GetFlipMatrix(
+            pDIBitmap->GetWidth(), pDIBitmap->GetHeight(), left, top);
         m_pDevice->StartDIBits(pDIBitmap, bitmap_alpha, 0, m, 0, &dummy);
         return;
 #else
diff --git a/core/fpdfdoc/cpdf_defaultappearance.h b/core/fpdfdoc/cpdf_defaultappearance.h
index 73e35c7..c386269 100644
--- a/core/fpdfdoc/cpdf_defaultappearance.h
+++ b/core/fpdfdoc/cpdf_defaultappearance.h
@@ -10,7 +10,6 @@
 #include <utility>
 
 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
-#include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxge/cfx_color.h"
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp
index 90e80be..8fe1f6b 100644
--- a/core/fxge/cfx_renderdevice.cpp
+++ b/core/fxge/cfx_renderdevice.cpp
@@ -364,15 +364,9 @@
 
 FXTEXT_CHARPOS::FXTEXT_CHARPOS(const FXTEXT_CHARPOS&) = default;
 
-FXTEXT_CHARPOS::~FXTEXT_CHARPOS(){};
+FXTEXT_CHARPOS::~FXTEXT_CHARPOS() = default;
 
-CFX_RenderDevice::CFX_RenderDevice()
-    : m_pBitmap(nullptr),
-      m_Width(0),
-      m_Height(0),
-      m_bpp(0),
-      m_RenderCaps(0),
-      m_DeviceClass(0) {}
+CFX_RenderDevice::CFX_RenderDevice() = default;
 
 CFX_RenderDevice::~CFX_RenderDevice() {
   RestoreState(false);
@@ -381,6 +375,14 @@
 #endif
 }
 
+// static
+CFX_Matrix CFX_RenderDevice::GetFlipMatrix(float width,
+                                           float height,
+                                           float left,
+                                           float top) {
+  return CFX_Matrix(width, 0, 0, -height, left, top + height);
+}
+
 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
 void CFX_RenderDevice::Flush(bool release) {
   if (release)
diff --git a/core/fxge/cfx_renderdevice.h b/core/fxge/cfx_renderdevice.h
index 5eede11..4f802af 100644
--- a/core/fxge/cfx_renderdevice.h
+++ b/core/fxge/cfx_renderdevice.h
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include "core/fpdfdoc/cpdf_defaultappearance.h"
+#include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "core/fxge/fx_dib.h"
 
@@ -101,7 +102,11 @@
   CFX_RenderDevice();
   virtual ~CFX_RenderDevice();
 
-  // Take ownership of |pDriver|.
+  static CFX_Matrix GetFlipMatrix(float width,
+                                  float height,
+                                  float left,
+                                  float top);
+
   void SetDeviceDriver(std::unique_ptr<RenderDeviceDriverIface> pDriver);
   RenderDeviceDriverIface* GetDeviceDriver() const {
     return m_pDeviceDriver.get();
@@ -297,11 +302,11 @@
                          BlendMode blend_type);
 
   RetainPtr<CFX_DIBitmap> m_pBitmap;
-  int m_Width;
-  int m_Height;
-  int m_bpp;
-  int m_RenderCaps;
-  int m_DeviceClass;
+  int m_Width = 0;
+  int m_Height = 0;
+  int m_bpp = 0;
+  int m_RenderCaps = 0;
+  int m_DeviceClass = 0;
   FX_RECT m_ClipBox;
   std::unique_ptr<RenderDeviceDriverIface> m_pDeviceDriver;
 };
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index 2067243..1cda61f 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -2247,8 +2247,8 @@
     return true;
 
 #ifdef _SKIA_SUPPORT_
-  CFX_Matrix m(pBitmap->GetWidth(), 0, 0, -pBitmap->GetHeight(), left,
-               top + pBitmap->GetHeight());
+  CFX_Matrix m = CFX_RenderDevice::GetFlipMatrix(
+      pBitmap->GetWidth(), pBitmap->GetHeight(), left, top);
   std::unique_ptr<CFX_ImageRenderer> dummy;
   return StartDIBits(pBitmap, 0xFF, argb, m, 0, &dummy, blend_type);
 #endif  // _SKIA_SUPPORT_
@@ -2280,9 +2280,9 @@
   m_pCache->FlushForDraw();
   if (!m_pBitmap->GetBuffer())
     return true;
-  CFX_Matrix m(dest_width, 0, 0, -dest_height, dest_left,
-               dest_top + dest_height);
 
+  CFX_Matrix m = CFX_RenderDevice::GetFlipMatrix(dest_width, dest_height,
+                                                 dest_left, dest_top);
   m_pCanvas->save();
   SkRect skClipRect = SkRect::MakeLTRB(pClipRect->left, pClipRect->bottom,
                                        pClipRect->right, pClipRect->top);
@@ -2501,8 +2501,9 @@
     BlendMode blend_type) {
   if (!m_pBitmap || !m_pBitmap->GetBuffer())
     return true;
-  CFX_Matrix m(pBitmap->GetWidth(), 0, 0, -pBitmap->GetHeight(), dest_left,
-               dest_top + pBitmap->GetHeight());
+
+  CFX_Matrix m = CFX_RenderDevice::GetFlipMatrix(
+      pBitmap->GetWidth(), pBitmap->GetHeight(), dest_left, dest_top);
   return DrawBitsWithMask(pBitmap, pMask, bitmap_alpha, m, blend_type);
 }
 
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index 9e01bd2..9f5a7cd 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -346,9 +346,8 @@
                                int left,
                                int top) {
   StartRendering();
-  CFX_Matrix matrix((float)(pSource->GetWidth()), 0.0f, 0.0f,
-                    -(float)(pSource->GetHeight()), (float)(left),
-                    (float)(top + pSource->GetHeight()));
+  CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(
+      pSource->GetWidth(), pSource->GetHeight(), left, top);
   return DrawDIBits(pSource, color, matrix, 0);
 }
 
@@ -360,8 +359,8 @@
                                    int dest_height,
                                    uint32_t flags) {
   StartRendering();
-  CFX_Matrix matrix((float)(dest_width), 0.0f, 0.0f, (float)(-dest_height),
-                    (float)(dest_left), (float)(dest_top + dest_height));
+  CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(dest_width, dest_height,
+                                                      dest_left, dest_top);
   return DrawDIBits(pSource, color, matrix, flags);
 }