Drop support for disabling the SkiaState cache

Drops alternate code paths for Skia rendering without using the
SkiaState cache. These code paths are never used in production, and in
many cases, the cache is required for correct behavior anyway.

Given these differences, disabling the cache is not useful for
debugging, either, and the alternate code paths should be cleaned up.

Bug: pdfium:1845
Change-Id: I74f9ba8d2a4e0d1cd84c5a1bcce0d8b4dd27a7ac
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/102410
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index ef51a8f..f6330e5 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -78,7 +78,6 @@
 #if SHOW_SKIA_PATH
 #define SHOW_SKIA_PATH_SHORTHAND 0  // set to 1 for abbreviated path contents
 #endif
-#define DRAW_SKIA_CLIP 0  // set to 1 to draw a green rectangle around the clip
 
 #if SHOW_SKIA_PATH
 void DebugShowSkiaPaint(const SkPaint& paint) {
@@ -150,34 +149,6 @@
 #endif  // SHOW_SKIA_PATH
 }
 
-#if DRAW_SKIA_CLIP
-
-SkPaint DebugClipPaint() {
-  SkPaint paint;
-  paint.setAntiAlias(true);
-  paint.setColor(SK_ColorGREEN);
-  paint.setStyle(SkPaint::kStroke_Style);
-  return paint;
-}
-
-void DebugDrawSkiaClipRect(SkCanvas* canvas, const SkRect& rect) {
-  SkPaint paint = DebugClipPaint();
-  canvas->drawRect(rect, paint);
-}
-
-void DebugDrawSkiaClipPath(SkCanvas* canvas, const SkPath& path) {
-  SkPaint paint = DebugClipPaint();
-  canvas->drawPath(path, paint);
-}
-
-#else  // DRAW_SKIA_CLIP
-
-void DebugDrawSkiaClipRect(SkCanvas* canvas, const SkRect& rect) {}
-
-void DebugDrawSkiaClipPath(SkCanvas* canvas, const SkPath& path) {}
-
-#endif  // DRAW_SKIA_CLIP
-
 bool IsRGBColorGrayScale(uint32_t color) {
   return FXARGB_R(color) == FXARGB_G(color) &&
          FXARGB_R(color) == FXARGB_B(color);
@@ -740,15 +711,13 @@
   // mark all cached state as uninitialized
   explicit SkiaState(CFX_SkiaDeviceDriver* pDriver) : m_pDriver(pDriver) {}
 
-  bool DrawPath(const CFX_Path& path,
+  void DrawPath(const CFX_Path& path,
                 const CFX_Matrix* pMatrix,
                 const CFX_GraphStateData* pDrawState,
                 uint32_t fill_color,
                 uint32_t stroke_color,
                 const CFX_FillRenderOptions& fill_options,
                 BlendMode blend_type) {
-    if (m_debugDisable)
-      return false;
     int drawIndex = std::min(m_drawIndex, m_commands.size());
     if (Accumulator::kText == m_type || drawIndex != m_commandIndex ||
         (Accumulator::kPath == m_type &&
@@ -780,7 +749,6 @@
     if (MatrixOffset(pMatrix, &delta))
       skPath.offset(delta.fX, delta.fY);
     m_skPath.addPath(skPath);
-    return true;
   }
 
   void FlushPath() {
@@ -873,8 +841,6 @@
                 float font_size,
                 uint32_t color,
                 const CFX_TextRenderOptions& options) {
-    if (m_debugDisable)
-      return false;
     float scaleX = 1;
     bool oneAtATime = false;
     bool hasRSX = HasRSX(nChars, pCharPos, &scaleX, &oneAtATime);
@@ -1007,11 +973,9 @@
 
   bool IsEmpty() const { return m_commands.empty(); }
 
-  bool SetClipFill(const CFX_Path& path,
+  void SetClipFill(const CFX_Path& path,
                    const CFX_Matrix* pMatrix,
                    const CFX_FillRenderOptions& fill_options) {
-    if (m_debugDisable)
-      return false;
     SkPath skClipPath;
     if (path.GetPoints().size() == 5 || path.GetPoints().size() == 4) {
       absl::optional<CFX_FloatRect> maybe_rectf = path.GetRect(pMatrix);
@@ -1033,16 +997,16 @@
       SkMatrix skMatrix = ToSkMatrix(*pMatrix);
       skClipPath.transform(skMatrix);
     }
-    return SetClip(skClipPath);
+    SetClip(skClipPath);
   }
 
-  bool SetClip(const SkPath& skClipPath) {
+  void SetClip(const SkPath& skClipPath) {
     // if a pending draw depends on clip state that is cached, flush it and draw
     if (m_commandIndex < m_commands.size()) {
       if (m_commands[m_commandIndex] == Clip::kPath &&
           m_clips[m_commandIndex] == skClipPath) {
         ++m_commandIndex;
-        return true;
+        return;
       }
       Flush();
     }
@@ -1061,14 +1025,11 @@
       m_clips.push_back(skClipPath);
     }
     ++m_commandIndex;
-    return true;
   }
 
-  bool SetClipStroke(const CFX_Path& path,
+  void SetClipStroke(const CFX_Path& path,
                      const CFX_Matrix* pMatrix,
                      const CFX_GraphStateData* pGraphState) {
-    if (m_debugDisable)
-      return false;
     SkPath skPath = BuildPath(path);
     SkMatrix skMatrix = ToSkMatrix(*pMatrix);
     SkPaint skPaint;
@@ -1076,7 +1037,7 @@
     SkPath dst_path;
     skPaint.getFillPath(skPath, &dst_path);
     dst_path.transform(skMatrix);
-    return SetClip(dst_path);
+    SetClip(dst_path);
   }
 
   bool MatrixOffset(const CFX_Matrix* pMatrix, SkPoint* delta) {
@@ -1101,15 +1062,12 @@
     return true;
   }
 
-  // returns true if caller should apply command to skia canvas
-  bool ClipSave() {
-    if (m_debugDisable)
-      return false;
+  void ClipSave() {
     int count = m_commands.size();
     if (m_commandIndex < count) {
       if (Clip::kSave == m_commands[m_commandIndex]) {
         ++m_commandIndex;
-        return true;
+        return;
       }
       Flush();
       AdjustClip(m_commandIndex);
@@ -1121,20 +1079,15 @@
       m_clips.push_back(m_skEmptyPath);
     }
     ++m_commandIndex;
-    return true;
   }
 
-  bool ClipRestore() {
-    if (m_debugDisable)
-      return false;
-
+  void ClipRestore() {
     for (int i = m_commandIndex - 1; i > 0; --i) {
       if (m_commands[i] == Clip::kSave) {
         m_commandIndex = i;
         break;
       }
     }
-    return true;
   }
 
   bool DrawChanged(const CFX_Matrix* pMatrix,
@@ -1217,8 +1170,6 @@
   }
 
   void Flush() {
-    if (m_debugDisable)
-      return;
     if (Accumulator::kPath == m_type || Accumulator::kText == m_type) {
       AdjustClip(std::min(m_drawIndex, m_commands.size()));
       Accumulator::kPath == m_type ? FlushPath() : FlushText();
@@ -1226,8 +1177,6 @@
   }
 
   void FlushForDraw() {
-    if (m_debugDisable)
-      return;
     Flush();                     // draw any pending text or path
     AdjustClip(m_commandIndex);  // set up clip stack with any pending state
   }
@@ -1299,7 +1248,6 @@
   bool m_fillPath = false;
   bool m_groupKnockout = false;
   bool m_isSubstFontBold = false;
-  bool m_debugDisable = false;  // turn off cache for debugging
 };
 
 // convert a stroking path to scanlines
@@ -1604,17 +1552,15 @@
 }
 
 void CFX_SkiaDeviceDriver::SaveState() {
-  if (!m_pCache->ClipSave())
-    m_pCanvas->save();
+  m_pCache->ClipSave();
 }
 
 void CFX_SkiaDeviceDriver::RestoreState(bool bKeepSaved) {
   if (m_pCache->IsEmpty())
     return;
-  if (!m_pCache->ClipRestore())
-    m_pCanvas->restore();
-  if (bKeepSaved && !m_pCache->ClipSave())
-    m_pCanvas->save();
+  m_pCache->ClipRestore();
+  if (bKeepSaved)
+    m_pCache->ClipSave();
 }
 
 bool CFX_SkiaDeviceDriver::SetClip_PathFill(
@@ -1624,7 +1570,7 @@
   m_FillOptions = fill_options;
   CFX_Matrix identity;
   const CFX_Matrix* deviceMatrix = pObject2Device ? pObject2Device : &identity;
-  bool cached = m_pCache->SetClipFill(path, deviceMatrix, fill_options);
+  m_pCache->SetClipFill(path, deviceMatrix, fill_options);
   if (path.GetPoints().size() == 5 || path.GetPoints().size() == 4) {
     absl::optional<CFX_FloatRect> maybe_rectf = path.GetRect(deviceMatrix);
     if (maybe_rectf.has_value()) {
@@ -1632,14 +1578,6 @@
       rectf.Intersect(CFX_FloatRect(0, 0,
                                     (float)GetDeviceCaps(FXDC_PIXEL_WIDTH),
                                     (float)GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
-      // note that PDF's y-axis goes up; Skia's y-axis goes down
-      if (!cached) {
-        SkRect skClipRect =
-            SkRect::MakeLTRB(rectf.left, rectf.bottom, rectf.right, rectf.top);
-        DebugDrawSkiaClipRect(m_pCanvas, skClipRect);
-        m_pCanvas->clipRect(skClipRect, SkClipOp::kIntersect, true);
-      }
-
       DebugShowCanvasClip(this, m_pCanvas);
       return true;
     }
@@ -1649,10 +1587,6 @@
   SkMatrix skMatrix = ToSkMatrix(*deviceMatrix);
   skClipPath.transform(skMatrix);
   DebugShowSkiaPath(skClipPath);
-  if (!cached) {
-    DebugDrawSkiaClipPath(m_pCanvas, skClipPath);
-    m_pCanvas->clipPath(skClipPath, SkClipOp::kIntersect, true);
-  }
   DebugShowCanvasClip(this, m_pCanvas);
   return true;
 }
@@ -1662,7 +1596,7 @@
     const CFX_Matrix* pObject2Device,      // required transformation
     const CFX_GraphStateData* pGraphState  // graphic state, for pen attributes
 ) {
-  bool cached = m_pCache->SetClipStroke(path, pObject2Device, pGraphState);
+  m_pCache->SetClipStroke(path, pObject2Device, pGraphState);
 
   // build path data
   SkPath skPath = BuildPath(path);
@@ -1672,10 +1606,6 @@
   SkPath dst_path;
   skPaint.getFillPath(skPath, &dst_path);
   dst_path.transform(skMatrix);
-  if (!cached) {
-    DebugDrawSkiaClipPath(m_pCanvas, dst_path);
-    m_pCanvas->clipPath(dst_path, SkClipOp::kIntersect, true);
-  }
   DebugShowCanvasClip(this, m_pCanvas);
   return true;
 }
@@ -1689,61 +1619,8 @@
     const CFX_FillRenderOptions& fill_options,
     BlendMode blend_type) {
   m_FillOptions = fill_options;
-  if (m_pCache->DrawPath(path, pObject2Device, pGraphState, fill_color,
-                         stroke_color, fill_options, blend_type)) {
-    return true;
-  }
-  SkMatrix skMatrix;
-  if (pObject2Device)
-    skMatrix = ToSkMatrix(*pObject2Device);
-  else
-    skMatrix.setIdentity();
-  SkPaint skPaint;
-  skPaint.setAntiAlias(!fill_options.aliased_path);
-  if (fill_options.full_cover)
-    skPaint.setBlendMode(SkBlendMode::kPlus);
-  int stroke_alpha = FXARGB_A(stroke_color);
-  bool is_paint_stroke = pGraphState && stroke_alpha;
-  if (is_paint_stroke)
-    PaintStroke(&skPaint, pGraphState, skMatrix);
-  SkPath skPath = BuildPath(path);
-  SkAutoCanvasRestore scoped_save_restore(m_pCanvas, /*doSave=*/true);
-  m_pCanvas->concat(skMatrix);
-  bool do_stroke = true;
-  if (fill_options.fill_type != CFX_FillRenderOptions::FillType::kNoFill &&
-      fill_color) {
-    skPath.setFillType(GetAlternateOrWindingFillType(fill_options));
-    SkPath strokePath;
-    const SkPath* fillPath = &skPath;
-    if (is_paint_stroke) {
-      if (m_bGroupKnockout) {
-        skPaint.getFillPath(skPath, &strokePath);
-        if (stroke_color == fill_color &&
-            Op(skPath, strokePath, SkPathOp::kUnion_SkPathOp, &strokePath)) {
-          fillPath = &strokePath;
-          do_stroke = false;
-        } else if (Op(skPath, strokePath, SkPathOp::kDifference_SkPathOp,
-                      &strokePath)) {
-          fillPath = &strokePath;
-        }
-      }
-    }
-    skPaint.setStyle(SkPaint::kFill_Style);
-    skPaint.setColor(fill_color);
-    DebugShowSkiaDrawPath(this, m_pCanvas, skPaint, *fillPath);
-    m_pCanvas->drawPath(*fillPath, skPaint);
-  }
-  if (is_paint_stroke && do_stroke) {
-    skPaint.setStyle(SkPaint::kStroke_Style);
-    skPaint.setColor(stroke_color);
-    if (!skPath.isLastContourClosed() && IsPathAPoint(skPath)) {
-      DCHECK_GE(skPath.countPoints(), 1);
-      m_pCanvas->drawPoint(skPath.getPoint(0), skPaint);
-    } else {
-      DebugShowSkiaDrawPath(this, m_pCanvas, skPaint, skPath);
-      m_pCanvas->drawPath(skPath, skPaint);
-    }
-  }
+  m_pCache->DrawPath(path, pObject2Device, pGraphState, fill_color,
+                     stroke_color, fill_options, blend_type);
   return true;
 }