Cherry-pick: [Android WebView] Change tile size to 384

Cherry-pick of Chromium crrev.com/r232457

BUG: 11446261

Original description:

512x512 tiles uses exactly 1MB in memory. However the allocator
on certain devices allocates 1.25MB instead (due to performance
heuristics), so 20% of allocated tile memory is wasted in this
case.

Determined experimentally that 384 tiles do not have any overhead, so
update tile size to 384.

Also reduce the number of resources per webview from 200 to 150 to
overcome potential file handle limit problems, and add a command line
switch to control this value.

Change-Id: I76f333d9ffac4295150155f9d029de44a54e9cd0
diff --git a/android_webview/browser/in_process_view_renderer.cc b/android_webview/browser/in_process_view_renderer.cc
index cb670c1..a96459e 100644
--- a/android_webview/browser/in_process_view_renderer.cc
+++ b/android_webview/browser/in_process_view_renderer.cc
@@ -147,7 +147,7 @@
 
 // Used to calculate memory and resource allocation. Determined experimentally.
 size_t g_memory_multiplier = 10;
-const size_t kMaxNumTilesToFillDisplay = 20;
+size_t g_num_gralloc_limit = 150;
 const size_t kBytesPerPixel = 4;
 const size_t kMemoryAllocationStep = 5 * 1024 * 1024;
 
@@ -290,23 +290,29 @@
   if (cl->HasSwitch(switches::kTileMemoryMultiplier)) {
     std::string string_value =
         cl->GetSwitchValueASCII(switches::kTileMemoryMultiplier);
-    int int_value;
+    int int_value = 0;
     if (base::StringToInt(string_value, &int_value) &&
         int_value >= 2 && int_value <= 50) {
       g_memory_multiplier = int_value;
     }
   }
 
-  if (cl->HasSwitch(switches::kDefaultTileWidth) ||
-      cl->HasSwitch(switches::kDefaultTileHeight)) {
-    return;
+  if (cl->HasSwitch(switches::kNumGrallocBuffersPerWebview)) {
+    std::string string_value =
+        cl->GetSwitchValueASCII(switches::kNumGrallocBuffersPerWebview);
+    int int_value = 0;
+    if (base::StringToInt(string_value, &int_value) &&
+        int_value >= 50 && int_value <= 500) {
+      g_num_gralloc_limit = int_value;
+    }
   }
 
-  const int default_tile_size = 512;
-  std::stringstream size;
-  size << default_tile_size;
-  cl->AppendSwitchASCII(switches::kDefaultTileWidth, size.str());
-  cl->AppendSwitchASCII(switches::kDefaultTileHeight, size.str());
+  const char kDefaultTileSize[] = "384";
+  if (!cl->HasSwitch(switches::kDefaultTileWidth))
+    cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize);
+
+  if (!cl->HasSwitch(switches::kDefaultTileHeight))
+    cl->AppendSwitchASCII(switches::kDefaultTileHeight, kDefaultTileSize);
 }
 
 bool InProcessViewRenderer::RequestProcessGL() {
@@ -478,7 +484,7 @@
   // Round up to a multiple of kMemoryAllocationStep.
   policy.bytes_limit =
       (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
-  policy.num_resources_limit = kMaxNumTilesToFillDisplay * g_memory_multiplier;
+  policy.num_resources_limit = g_num_gralloc_limit;
   SetMemoryPolicy(policy);
 
   DCHECK(gl_surface_);
diff --git a/android_webview/common/aw_switches.cc b/android_webview/common/aw_switches.cc
index b2663fa..216b4e3 100644
--- a/android_webview/common/aw_switches.cc
+++ b/android_webview/common/aw_switches.cc
@@ -12,4 +12,6 @@
 
 const char kTileMemoryMultiplier[] = "tile-memory-multiplier";
 
+const char kNumGrallocBuffersPerWebview[] = "num-gralloc-buffers-per-webview";
+
 }  // namespace switches
diff --git a/android_webview/common/aw_switches.h b/android_webview/common/aw_switches.h
index e855bb7..c718c05 100644
--- a/android_webview/common/aw_switches.h
+++ b/android_webview/common/aw_switches.h
@@ -17,6 +17,9 @@
 // displays that a single layer will have enough memory for.
 extern const char kTileMemoryMultiplier[];
 
+// Maximum number of gralloc allocations per webview.
+extern const char kNumGrallocBuffersPerWebview[];
+
 }  // namespace switches
 
 #endif  // ANDROID_WEBVIEW_COMMON_AW_SWITCHES_H_