From 9d32fbbb1b1fe141143def11dc324b07ec6c89e3 Mon Sep 17 00:00:00 2001
From: "chrono.wu" <chrono.wu@kaiostech.com>
Date: Thu, 11 Apr 2019 16:18:43 +0800
Subject: [PATCH] bug 58204 - Limit skia batch size to avoid triggering LMK for
 killing APP. r=Vincent,Eastern

---
 gfx/skia/skia/src/gpu/GrDrawTarget.cpp               | 12 ++++++++++--
 gfx/skia/skia/src/gpu/GrOvalRenderer.cpp             | 10 ++++++++++
 .../skia/src/gpu/batches/GrAAConvexPathRenderer.cpp  |  2 ++
 .../gpu/batches/GrAADistanceFieldPathRenderer.cpp    |  2 ++
 .../src/gpu/batches/GrAAHairLinePathRenderer.cpp     |  2 ++
 .../batches/GrAALinearizingConvexPathRenderer.cpp    |  2 ++
 .../skia/src/gpu/batches/GrAAStrokeRectBatch.cpp     |  2 ++
 gfx/skia/skia/src/gpu/batches/GrAtlasTextBatch.h     |  2 ++
 gfx/skia/skia/src/gpu/batches/GrBatch.h              |  2 ++
 .../skia/src/gpu/batches/GrDefaultPathRenderer.cpp   |  2 ++
 gfx/skia/skia/src/gpu/batches/GrDrawAtlasBatch.h     |  2 ++
 gfx/skia/skia/src/gpu/batches/GrDrawVerticesBatch.h  |  2 ++
 gfx/skia/skia/src/gpu/batches/GrNinePatch.cpp        |  2 ++
 .../skia/src/gpu/batches/GrNonAAStrokeRectBatch.cpp  |  2 ++
 gfx/skia/skia/src/gpu/batches/GrTInstanceBatch.h     |  2 ++
 gfx/skia/skia/src/gpu/effects/GrDashingEffect.cpp    |  2 ++
 gfx/thebes/gfxPrefs.h                                |  1 +
 17 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/gfx/skia/skia/src/gpu/GrDrawTarget.cpp b/gfx/skia/skia/src/gpu/GrDrawTarget.cpp
index e2e188eb12cf..1528a890c777 100644
--- a/gfx/skia/skia/src/gpu/GrDrawTarget.cpp
+++ b/gfx/skia/skia/src/gpu/GrDrawTarget.cpp
@@ -31,6 +31,8 @@
 #include "batches/GrRectBatchFactory.h"
 #include "batches/GrStencilPathBatch.h"
 
+#include "gfxPrefs.h"
+
 ////////////////////////////////////////////////////////////////////////////////
 
 // Experimentally we have found that most batching occurs within the first 10 comparisons.
@@ -443,7 +445,7 @@ void GrDrawTarget::recordBatch(GrBatch* batch) {
     GrBATCH_INFO("\tOutcome:\n");    
     int maxCandidates = SkTMin(fMaxBatchLookback, fBatches.count());
     if (maxCandidates) {
-        int i = 0;
+        int i = 0, totalBatchCount = 0;
         while (true) {
             GrBatch* candidate = fBatches.fromBack(i);
             // We cannot continue to search backwards if the render target changes
@@ -452,7 +454,13 @@ void GrDrawTarget::recordBatch(GrBatch* batch) {
                     candidate->name(), candidate->uniqueID());
                 break;
             }
-            if (candidate->combineIfPossible(batch, *this->caps())) {
+            totalBatchCount += candidate->geoDataCount();
+            if (totalBatchCount >= gfxPrefs::CanvasSkiaGLMaxBatchCount()) {
+                /* Bug-58204 to avoid mess amount of batches to trigger LMK kill APP, */
+                /* we limit the batch size to be default 1024, it can also be controlled by CanvasSkiaGLMaxBatchCount. */
+                SkDebugf("Batch count for candidate %p is full, need to drop it", candidate);
+                return;
+            } else if (candidate->combineIfPossible(batch, *this->caps())) {
                 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
                     candidate->uniqueID());
                 return;
diff --git a/gfx/skia/skia/src/gpu/GrOvalRenderer.cpp b/gfx/skia/skia/src/gpu/GrOvalRenderer.cpp
index 6cb203c05a75..a813bb7fa8e7 100644
--- a/gfx/skia/skia/src/gpu/GrOvalRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/GrOvalRenderer.cpp
@@ -680,6 +680,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
@@ -900,6 +902,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
@@ -1172,6 +1176,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
 
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
@@ -1530,6 +1536,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
@@ -1710,6 +1718,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrAAConvexPathRenderer.cpp b/gfx/skia/skia/src/gpu/batches/GrAAConvexPathRenderer.cpp
index 9fde34c186e0..d81c1db397a9 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAAConvexPathRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrAAConvexPathRenderer.cpp
@@ -764,6 +764,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrAADistanceFieldPathRenderer.cpp b/gfx/skia/skia/src/gpu/batches/GrAADistanceFieldPathRenderer.cpp
index e94cd591f3e3..11927333d6bb 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAADistanceFieldPathRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrAADistanceFieldPathRenderer.cpp
@@ -161,6 +161,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrAAHairLinePathRenderer.cpp b/gfx/skia/skia/src/gpu/batches/GrAAHairLinePathRenderer.cpp
index 6b7b30890003..d8d86662d5ec 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAAHairLinePathRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrAAHairLinePathRenderer.cpp
@@ -695,6 +695,8 @@ public:
         overrides->fUsePLSDstRead = true;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrAALinearizingConvexPathRenderer.cpp b/gfx/skia/skia/src/gpu/batches/GrAALinearizingConvexPathRenderer.cpp
index c830509a2a5a..ff4e4f3399b2 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAALinearizingConvexPathRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrAALinearizingConvexPathRenderer.cpp
@@ -143,6 +143,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrAAStrokeRectBatch.cpp b/gfx/skia/skia/src/gpu/batches/GrAAStrokeRectBatch.cpp
index 21c55f6a5457..700634ecba71 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAAStrokeRectBatch.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrAAStrokeRectBatch.cpp
@@ -71,6 +71,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
     bool canAppend(const SkMatrix& viewMatrix, bool miterStroke) {
diff --git a/gfx/skia/skia/src/gpu/batches/GrAtlasTextBatch.h b/gfx/skia/skia/src/gpu/batches/GrAtlasTextBatch.h
index 8f20313378c4..5e43ed2c5300 100644
--- a/gfx/skia/skia/src/gpu/batches/GrAtlasTextBatch.h
+++ b/gfx/skia/skia/src/gpu/batches/GrAtlasTextBatch.h
@@ -99,6 +99,8 @@ public:
 
     SkString dumpInfo() const override;
 
+    int geoDataCount() const override { return fGeoCount; }
+
 protected:
     void computePipelineOptimizations(GrInitInvariantOutput* color, 
                                       GrInitInvariantOutput* coverage,
diff --git a/gfx/skia/skia/src/gpu/batches/GrBatch.h b/gfx/skia/skia/src/gpu/batches/GrBatch.h
index a66d6ae308d6..0157b01ab659 100644
--- a/gfx/skia/skia/src/gpu/batches/GrBatch.h
+++ b/gfx/skia/skia/src/gpu/batches/GrBatch.h
@@ -117,6 +117,8 @@ public:
     /** Can remove this when multi-draw-buffer lands */
     virtual GrRenderTarget* renderTarget() const = 0;
 
+    virtual int geoDataCount() const { return 0; }
+
 protected:
     // NOTE, compute some bounds, even if extremely conservative.  Do *NOT* setLargest on the bounds
     // rect because we outset it for dst copy textures
diff --git a/gfx/skia/skia/src/gpu/batches/GrDefaultPathRenderer.cpp b/gfx/skia/skia/src/gpu/batches/GrDefaultPathRenderer.cpp
index c39a5306455a..7885fdd58f16 100644
--- a/gfx/skia/skia/src/gpu/batches/GrDefaultPathRenderer.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrDefaultPathRenderer.cpp
@@ -237,6 +237,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
         // Handle any color overrides
diff --git a/gfx/skia/skia/src/gpu/batches/GrDrawAtlasBatch.h b/gfx/skia/skia/src/gpu/batches/GrDrawAtlasBatch.h
index 4e89523dcbb4..1b74eeba12a3 100644
--- a/gfx/skia/skia/src/gpu/batches/GrDrawAtlasBatch.h
+++ b/gfx/skia/skia/src/gpu/batches/GrDrawAtlasBatch.h
@@ -42,6 +42,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
 private:
diff --git a/gfx/skia/skia/src/gpu/batches/GrDrawVerticesBatch.h b/gfx/skia/skia/src/gpu/batches/GrDrawVerticesBatch.h
index 9b2faf3d3dba..6a30782fa460 100644
--- a/gfx/skia/skia/src/gpu/batches/GrDrawVerticesBatch.h
+++ b/gfx/skia/skia/src/gpu/batches/GrDrawVerticesBatch.h
@@ -46,6 +46,8 @@ public:
                                       GrInitInvariantOutput* coverage,
                                       GrBatchToXPOverrides* overrides) const override;
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
 private:
diff --git a/gfx/skia/skia/src/gpu/batches/GrNinePatch.cpp b/gfx/skia/skia/src/gpu/batches/GrNinePatch.cpp
index 59040cbbe939..69a63bfeab27 100644
--- a/gfx/skia/skia/src/gpu/batches/GrNinePatch.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrNinePatch.cpp
@@ -82,6 +82,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
 private:
diff --git a/gfx/skia/skia/src/gpu/batches/GrNonAAStrokeRectBatch.cpp b/gfx/skia/skia/src/gpu/batches/GrNonAAStrokeRectBatch.cpp
index d036b90c27ce..9482de51df52 100644
--- a/gfx/skia/skia/src/gpu/batches/GrNonAAStrokeRectBatch.cpp
+++ b/gfx/skia/skia/src/gpu/batches/GrNonAAStrokeRectBatch.cpp
@@ -92,6 +92,8 @@ public:
         this->setupBounds(&fBounds, geo, snapToPixelCenters);
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     void setupBounds(SkRect* bounds, const Geometry& geo, bool snapToPixelCenters) {
         *bounds = geo.fRect;
diff --git a/gfx/skia/skia/src/gpu/batches/GrTInstanceBatch.h b/gfx/skia/skia/src/gpu/batches/GrTInstanceBatch.h
index bf371689bd30..c3788a5efd36 100644
--- a/gfx/skia/skia/src/gpu/batches/GrTInstanceBatch.h
+++ b/gfx/skia/skia/src/gpu/batches/GrTInstanceBatch.h
@@ -86,6 +86,8 @@ public:
         Impl::UpdateBoundsAfterAppend(geo, &fBounds);
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
 private:
     GrTInstanceBatch() : INHERITED(ClassID()) {}
 
diff --git a/gfx/skia/skia/src/gpu/effects/GrDashingEffect.cpp b/gfx/skia/skia/src/gpu/effects/GrDashingEffect.cpp
index 1516efd2b212..b22827fabc47 100644
--- a/gfx/skia/skia/src/gpu/effects/GrDashingEffect.cpp
+++ b/gfx/skia/skia/src/gpu/effects/GrDashingEffect.cpp
@@ -278,6 +278,8 @@ public:
         overrides->fUsePLSDstRead = false;
     }
 
+    int geoDataCount() const override { return fGeoData.count(); }
+
     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
 
 private:
diff --git a/gfx/thebes/gfxPrefs.h b/gfx/thebes/gfxPrefs.h
index c58b4d2c839d..bca19bb6bce2 100644
--- a/gfx/thebes/gfxPrefs.h
+++ b/gfx/thebes/gfxPrefs.h
@@ -253,6 +253,7 @@ private:
   DECL_GFX_PREF(Once, "gfx.canvas.skiagl.cache-items",         CanvasSkiaGLCacheItems, int32_t, 256);
   DECL_GFX_PREF(Once, "gfx.canvas.skiagl.cache-size",          CanvasSkiaGLCacheSize, int32_t, 96);
   DECL_GFX_PREF(Once, "gfx.canvas.skiagl.dynamic-cache",       CanvasSkiaGLDynamicCache, bool, false);
+  DECL_GFX_PREF(Live, "gfx.canvas.skiagl.max-batch-count",     CanvasSkiaGLMaxBatchCount, int32_t, 1024);
 
   DECL_GFX_PREF(Live, "gfx.color_management.enablev4",         CMSEnableV4, bool, false);
   DECL_GFX_PREF(Live, "gfx.color_management.mode",             CMSMode, int32_t,-1);
-- 
GitLab

