Cherry-pick: CC: Cap the size of the SOON_BIN.

Cherry-pick of Chromium crrev.com/r227711.

Conflicts:
	cc/resources/tile_manager.cc

BUG: 11446261

Original Description:

ALLOW_PREPAINT_ONLY is used to reduce memory usage, but
currently the SOON_BIN can still grow to an unbounded size
because we will schedule rasters for the entire SOON_BIN,
even though our paint rate is what truly determines if we
see checkerboard.

Minus raster tasks spikes seen in http://crbug.com/303397,
this keeps memory usage well below the limit when using
ALLOW_PREPAINT_ONLY.

Change-Id: I0c7677a45f9afb11177ec4dbb99ef7ca4a56b5c7
diff --git a/cc/resources/tile_manager.cc b/cc/resources/tile_manager.cc
index e6850bc..526f90a 100644
--- a/cc/resources/tile_manager.cc
+++ b/cc/resources/tile_manager.cc
@@ -67,9 +67,15 @@
                                           TreePriority tree_priority,
                                           bool is_ready_to_draw,
                                           bool is_active) {
-  // The amount of time for which we want to have prepainting coverage.
+  // The amount of time/pixels for which we want to have prepainting coverage.
+  // Note: All very arbitrary constants: metric-based tuning is welcome!
   const float kPrepaintingWindowTimeSeconds = 1.0f;
   const float kBackflingGuardDistancePixels = 314.0f;
+  // Note: The max distances here assume that SOON_BIN will never help overcome
+  // raster being too slow (only caching in advance will do that), so we just
+  // need enough padding to handle some latency and per-tile variability.
+  const float kMaxPrepaintingDistancePixelsHighRes = 2000.0f;
+  const float kMaxPrepaintingDistancePixelsLowRes = 4000.0f;
 
   // Don't let low res tiles be in the now bin unless we're in a mode where
   // we're prioritizing checkerboard prevention.
@@ -86,8 +92,16 @@
   if (prio.resolution == NON_IDEAL_RESOLUTION)
     return is_active ? EVENTUALLY_AND_ACTIVE_BIN : EVENTUALLY_BIN;
 
+  float max_prepainting_distance_pixels =
+      (prio.resolution == HIGH_RESOLUTION)
+          ? kMaxPrepaintingDistancePixelsHighRes
+          : kMaxPrepaintingDistancePixelsLowRes;
+
+  // Soon bin if we are within backfling-guard, or under both the time window
+  // and the max distance window.
   if (prio.distance_to_visible_in_pixels < kBackflingGuardDistancePixels ||
-      prio.time_to_visible_in_seconds < kPrepaintingWindowTimeSeconds)
+      (prio.time_to_visible_in_seconds < kPrepaintingWindowTimeSeconds &&
+       prio.distance_to_visible_in_pixels <= max_prepainting_distance_pixels))
     return SOON_BIN;
 
   return is_active ? EVENTUALLY_AND_ACTIVE_BIN : EVENTUALLY_BIN;