Cherry pick: Android WebView: fix missing an invalidate

Cherry pick from Chrmoium
https://crrev.com/673cf0160d7b5e520841a65f4425f8be1a48fbb1

BUG: 17623119

Original description:
When webview translates into the screen from offscreen by calling
WebView#SetTranslateX/Y, onDrawHardware is called with an empty global
visible rect.
When the webview is still offscreen, Android frameworks won't call
DrawGL on WebView, which means the "parent draw constraints" stored in
hardware renderer won't contain the draw constraints when the webview is
offscreen.
When webview gets an ondraw with an empty global visible rect, we need
to make sure the parent draw constraints of the corresponding DrawGL is
always posted back to child compositor and triggers an invalidate.

BUG=417479

Review URL: https://codereview.chromium.org/603633003

Cr-Commit-Position: refs/heads/master@{#296589}

Conflicts:
	android_webview/browser/browser_view_renderer.cc
	android_webview/browser/shared_renderer_state.cc
	android_webview/browser/shared_renderer_state.h

Change-Id: Iea9e6d5f0dd021d19cdcf6f5a98d92fd735dac26
diff --git a/android_webview/browser/browser_view_renderer.cc b/android_webview/browser/browser_view_renderer.cc
index 734fa23..4ebd5e9 100644
--- a/android_webview/browser/browser_view_renderer.cc
+++ b/android_webview/browser/browser_view_renderer.cc
@@ -240,8 +240,10 @@
   if (!compositor_)
     return false;
 
-  if (last_on_draw_global_visible_rect_.IsEmpty())
+  if (last_on_draw_global_visible_rect_.IsEmpty()) {
+    shared_renderer_state_->SetForceInvalidateOnNextDrawGL(true);
     return client_->RequestDrawGL(java_canvas, false);
+  }
 
   if (!hardware_enabled_) {
     hardware_enabled_ = compositor_->InitializeHwDraw();
@@ -303,9 +305,12 @@
 void BrowserViewRenderer::UpdateParentDrawConstraints() {
   // Post an invalidate if the parent draw constraints are stale and there is
   // no pending invalidate.
-  if (!parent_draw_constraints_.Equals(
-          shared_renderer_state_->ParentDrawConstraints()))
+  if (shared_renderer_state_->NeedsForceInvalidateOnNextDrawGL() ||
+      !parent_draw_constraints_.Equals(
+        shared_renderer_state_->ParentDrawConstraints())) {
+    shared_renderer_state_->SetForceInvalidateOnNextDrawGL(false);
     EnsureContinuousInvalidation(true);
+  }
 }
 
 void BrowserViewRenderer::ReturnUnusedResource(scoped_ptr<DrawGLInput> input) {
diff --git a/android_webview/browser/hardware_renderer.cc b/android_webview/browser/hardware_renderer.cc
index 43d38e2..fa42b73 100644
--- a/android_webview/browser/hardware_renderer.cc
+++ b/android_webview/browser/hardware_renderer.cc
@@ -198,11 +198,10 @@
   // compositor might not have the tiles rasterized as the animation goes on.
   ParentCompositorDrawConstraints draw_constraints(
       draw_info->is_layer, transform, gfx::Rect(viewport_));
-  if (!draw_constraints_.Equals(draw_constraints)) {
-    draw_constraints_ = draw_constraints;
-    shared_renderer_state_->PostExternalDrawConstraintsToChildCompositor(
-        draw_constraints);
-  }
+
+  draw_constraints_ = draw_constraints;
+  shared_renderer_state_->PostExternalDrawConstraintsToChildCompositor(
+      draw_constraints);
 
   if (!delegated_layer_.get())
     return;
diff --git a/android_webview/browser/shared_renderer_state.cc b/android_webview/browser/shared_renderer_state.cc
index 508fdc4..1dd6462 100644
--- a/android_webview/browser/shared_renderer_state.cc
+++ b/android_webview/browser/shared_renderer_state.cc
@@ -81,6 +81,7 @@
       weak_factory_on_ui_thread_(this),
       ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()),
       inside_hardware_release_(false),
+      needs_force_invalidate_on_next_draw_gl_(false),
       share_context_(NULL) {
   DCHECK(ui_loop_->BelongsToCurrentThread());
   DCHECK(client_on_ui_);
@@ -146,21 +147,27 @@
   return draw_gl_input_.Pass();
 }
 
-void SharedRendererState::UpdateDrawConstraints(
+bool SharedRendererState::UpdateDrawConstraints(
     const ParentCompositorDrawConstraints& parent_draw_constraints) {
   base::AutoLock lock(lock_);
-  parent_draw_constraints_ = parent_draw_constraints;
+  if (needs_force_invalidate_on_next_draw_gl_ ||
+      !parent_draw_constraints_.Equals(parent_draw_constraints)) {
+    parent_draw_constraints_ = parent_draw_constraints;
+    return true;
+  }
+
+  return false;
 }
 
 void SharedRendererState::PostExternalDrawConstraintsToChildCompositor(
     const ParentCompositorDrawConstraints& parent_draw_constraints) {
-  UpdateDrawConstraints(parent_draw_constraints);
-
-  // No need to hold the lock_ during the post task.
-  ui_loop_->PostTask(
-      FROM_HERE,
-      base::Bind(&SharedRendererState::UpdateParentDrawConstraintsOnUIThread,
-                 ui_thread_weak_ptr_));
+  if (UpdateDrawConstraints(parent_draw_constraints)) {
+    // No need to hold the lock_ during the post task.
+    ui_loop_->PostTask(
+        FROM_HERE,
+        base::Bind(&SharedRendererState::UpdateParentDrawConstraintsOnUIThread,
+                   ui_thread_weak_ptr_));
+  }
 }
 
 const ParentCompositorDrawConstraints
@@ -169,6 +176,18 @@
   return parent_draw_constraints_;
 }
 
+void SharedRendererState::SetForceInvalidateOnNextDrawGL(
+    bool needs_force_invalidate_on_next_draw_gl) {
+  base::AutoLock lock(lock_);
+  needs_force_invalidate_on_next_draw_gl_ =
+      needs_force_invalidate_on_next_draw_gl;
+}
+
+bool SharedRendererState::NeedsForceInvalidateOnNextDrawGL() const {
+  base::AutoLock lock(lock_);
+  return needs_force_invalidate_on_next_draw_gl_;
+}
+
 void SharedRendererState::SetInsideHardwareRelease(bool inside) {
   base::AutoLock lock(lock_);
   inside_hardware_release_ = inside;
diff --git a/android_webview/browser/shared_renderer_state.h b/android_webview/browser/shared_renderer_state.h
index 2245f73..79b10b6 100644
--- a/android_webview/browser/shared_renderer_state.h
+++ b/android_webview/browser/shared_renderer_state.h
@@ -57,7 +57,8 @@
   scoped_ptr<DrawGLInput> PassDrawGLInput();
 
   bool IsInsideHardwareRelease() const;
-  void UpdateDrawConstraints(
+  // Returns true if the draw constraints are updated.
+  bool UpdateDrawConstraints(
       const ParentCompositorDrawConstraints& parent_draw_constraints);
   void PostExternalDrawConstraintsToChildCompositor(
       const ParentCompositorDrawConstraints& parent_draw_constraints);
@@ -67,6 +68,10 @@
   void SetSharedContext(gpu::GLInProcessContext* context);
   gpu::GLInProcessContext* GetSharedContext() const;
 
+  void SetForceInvalidateOnNextDrawGL(
+      bool needs_force_invalidate_on_next_draw_gl);
+  bool NeedsForceInvalidateOnNextDrawGL() const;
+
   void InsertReturnedResources(const cc::ReturnedResourceArray& resources);
   void SwapReturnedResources(cc::ReturnedResourceArray* resources);
   bool ReturnedResourcesEmpty() const;
@@ -90,6 +95,7 @@
   mutable base::Lock lock_;
   scoped_ptr<DrawGLInput> draw_gl_input_;
   bool inside_hardware_release_;
+  bool needs_force_invalidate_on_next_draw_gl_;
   ParentCompositorDrawConstraints parent_draw_constraints_;
   gpu::GLInProcessContext* share_context_;
   cc::ReturnedResourceArray returned_resources_;