Cherry-pick: [Android WebView] Turn on accelerated canvas based on View.isHardwareAccelerated

Clean cherry-pick of crrev.com/r245648

Original description:

In onAttachedToWindow, check that if the view is hardware accelerated,
then enable hardware accelerated 2d canvas. Similarly disable canvas on
detach.

Note that accelerated 2d canvas is still off by default, but this patch
adds a command line switch to turn it on.

BUG: 13328348

Change-Id: I7ceb7b4ef6cefca6b37f7911357fbcd9e2dbef6b
diff --git a/android_webview/common/aw_switches.cc b/android_webview/common/aw_switches.cc
index 216b4e3..2c78aa4 100644
--- a/android_webview/common/aw_switches.cc
+++ b/android_webview/common/aw_switches.cc
@@ -14,4 +14,6 @@
 
 const char kNumGrallocBuffersPerWebview[] = "num-gralloc-buffers-per-webview";
 
+const char kEnableAccelerated2dCanvas[] = "enable-accelerated-2d-canvas";
+
 }  // namespace switches
diff --git a/android_webview/common/aw_switches.h b/android_webview/common/aw_switches.h
index c718c05..7b31bfd 100644
--- a/android_webview/common/aw_switches.h
+++ b/android_webview/common/aw_switches.h
@@ -20,6 +20,10 @@
 // Maximum number of gralloc allocations per webview.
 extern const char kNumGrallocBuffersPerWebview[];
 
+// Explicitly enable accelerated 2d canvas.
+// TODO(boliu): Remove this switch once on by default.
+extern const char kEnableAccelerated2dCanvas[];
+
 }  // namespace switches
 
 #endif  // ANDROID_WEBVIEW_COMMON_AW_SWITCHES_H_
diff --git a/android_webview/java/src/org/chromium/android_webview/AwContents.java b/android_webview/java/src/org/chromium/android_webview/AwContents.java
index c11aeac..7e7ab13 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwContents.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwContents.java
@@ -1551,6 +1551,8 @@
         mContentViewCore.onAttachedToWindow();
         nativeOnAttachedToWindow(mNativeAwContents, mContainerView.getWidth(),
                 mContainerView.getHeight());
+        mSettings.setEnableSupportedHardwareAcceleratedFeatures(
+            mContainerView.isHardwareAccelerated());
 
         if (mComponentCallbacks != null) return;
         mComponentCallbacks = new AwComponentCallbacks();
@@ -1569,6 +1571,8 @@
 
         mContentViewCore.onDetachedFromWindow();
 
+        mSettings.setEnableSupportedHardwareAcceleratedFeatures(false);
+
         if (mComponentCallbacks != null) {
             mContainerView.getContext().unregisterComponentCallbacks(mComponentCallbacks);
             mComponentCallbacks = null;
diff --git a/android_webview/java/src/org/chromium/android_webview/AwSettings.java b/android_webview/java/src/org/chromium/android_webview/AwSettings.java
index fb4216e..0b68a36 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwSettings.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwSettings.java
@@ -83,6 +83,7 @@
     private String mDefaultVideoPosterURL;
     private float mInitialPageScalePercent = 0;
     private boolean mSpatialNavigationEnabled;  // Default depends on device features.
+    private boolean mEnableSupportedHardwareAcceleratedFeatures = false;
 
     private final boolean mSupportLegacyQuirks;
 
@@ -400,6 +401,20 @@
         return mSpatialNavigationEnabled;
     }
 
+    void setEnableSupportedHardwareAcceleratedFeatures(boolean enable) {
+        synchronized (mAwSettingsLock) {
+            if (mEnableSupportedHardwareAcceleratedFeatures != enable) {
+                mEnableSupportedHardwareAcceleratedFeatures = enable;
+                mEventHandler.updateWebkitPreferencesLocked();
+            }
+        }
+    }
+
+    @CalledByNative
+    private boolean getEnableSupportedHardwareAcceleratedFeaturesLocked() {
+        return mEnableSupportedHardwareAcceleratedFeatures;
+    }
+
     /**
      * See {@link android.webkit.WebSettings#setNeedInitialFocus}.
      */
diff --git a/android_webview/lib/main/aw_main_delegate.cc b/android_webview/lib/main/aw_main_delegate.cc
index 5b79eb6..a172b86 100644
--- a/android_webview/lib/main/aw_main_delegate.cc
+++ b/android_webview/lib/main/aw_main_delegate.cc
@@ -8,6 +8,7 @@
 #include "android_webview/browser/gpu_memory_buffer_factory_impl.h"
 #include "android_webview/browser/in_process_view_renderer.h"
 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
+#include "android_webview/common/aw_switches.h"
 #include "android_webview/lib/aw_browser_dependency_factory_impl.h"
 #include "android_webview/native/aw_geolocation_permission_context.h"
 #include "android_webview/native/aw_quota_manager_bridge_impl.h"
@@ -66,8 +67,10 @@
   cl->AppendSwitch(switches::kDisableExperimentalWebGL);
   cl->AppendSwitch(switches::kDisableSharedWorkers);
 
-  // Ganesh backed 2D-Canvas is not yet working and causes crashes.
-  cl->AppendSwitch(switches::kDisableAccelerated2dCanvas);
+  // Ganesh backed 2D-Canvas integration is being implemented but not ready to
+  // be turned on by default yet.
+  if (!cl->HasSwitch(switches::kEnableAccelerated2dCanvas))
+    cl->AppendSwitch(switches::kDisableAccelerated2dCanvas);
 
   // File system API not supported (requires some new API; internal bug 6930981)
   cl->AppendSwitch(switches::kDisableFileSystem);
diff --git a/android_webview/native/aw_settings.cc b/android_webview/native/aw_settings.cc
index 5477dc7..6577a81 100644
--- a/android_webview/native/aw_settings.cc
+++ b/android_webview/native/aw_settings.cc
@@ -8,12 +8,14 @@
 #include "android_webview/native/aw_contents.h"
 #include "base/android/jni_android.h"
 #include "base/android/jni_string.h"
+#include "base/command_line.h"
 #include "base/supports_user_data.h"
 #include "content/public/browser/navigation_controller.h"
 #include "content/public/browser/navigation_entry.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/common/content_client.h"
+#include "content/public/common/content_switches.h"
 #include "jni/AwSettings_jni.h"
 #include "webkit/common/user_agent/user_agent.h"
 #include "webkit/common/webpreferences.h"
@@ -45,6 +47,9 @@
 AwSettings::AwSettings(JNIEnv* env, jobject obj, jlong web_contents)
     : WebContentsObserver(
           reinterpret_cast<content::WebContents*>(web_contents)),
+      accelerated_2d_canvas_disabled_by_switch_(
+          CommandLine::ForCurrentProcess()->HasSwitch(
+              switches::kDisableAccelerated2dCanvas)),
       aw_settings_(env, obj) {
   reinterpret_cast<content::WebContents*>(web_contents)->
       SetUserData(kAwSettingsUserDataKey, new AwSettingsUserData(this));
@@ -327,6 +332,11 @@
       Java_AwSettings_getPasswordEchoEnabledLocked(env, obj);
   web_prefs->spatial_navigation_enabled =
       Java_AwSettings_getSpatialNavigationLocked(env, obj);
+
+  web_prefs->accelerated_2d_canvas_enabled =
+      !accelerated_2d_canvas_disabled_by_switch_ &&
+      Java_AwSettings_getEnableSupportedHardwareAcceleratedFeaturesLocked(
+          env, obj);
 }
 
 static jlong Init(JNIEnv* env,
diff --git a/android_webview/native/aw_settings.h b/android_webview/native/aw_settings.h
index ba48877..632862e 100644
--- a/android_webview/native/aw_settings.h
+++ b/android_webview/native/aw_settings.h
@@ -51,6 +51,8 @@
   virtual void WebContentsDestroyed(
       content::WebContents* web_contents) OVERRIDE;
 
+  bool accelerated_2d_canvas_disabled_by_switch_;
+
   JavaObjectWeakGlobalRef aw_settings_;
 };