[GTK] Add API to handle the accelerated compositing policy
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 30 Jan 2017 13:50:37 +0000 (13:50 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 30 Jan 2017 13:50:37 +0000 (13:50 +0000)
https://bugs.webkit.org/show_bug.cgi?id=167509

Reviewed by Michael Catanzaro.

Source/WebKit2:

Now that we have brought back the on demand mode, we should allow applications to choose the policy, without
having to deal with environment variables. Settings also allows to set different policy depending on the web
view, so for example evolution could disable AC for the composer, but leave the on demand mode for the email
viewer. This patch adds a single new setting hardware-acceleration-policy to handle both
acceleratedCompositingEnabled and forceCompositingMode preferences using an enum with values
WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND, WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS and
WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER.

* UIProcess/API/gtk/WebKitSettings.cpp:
(webKitSettingsSetProperty): Add setter for hardware-acceleration-policy property.
(webKitSettingsGetProperty): Add getter for hardware-acceleration-policy property.
(webkit_settings_class_init): Add hardware-acceleration-policy property.
(webkit_settings_get_hardware_acceleration_policy): Return policy according to the preferences.
(webkit_settings_set_hardware_acceleration_policy): set preferences according to the given policy.
* UIProcess/API/gtk/WebKitSettings.h:
* UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Add new symbols.
* WebProcess/WebPage/DrawingAreaImpl.cpp:
(WebKit::DrawingAreaImpl::updatePreferences):

Tools:

Handle new setting in MiniBrowser. The settings dialog doesn't support enum settings so it needs to be handled
as a special case. Also add test cases to the get/set API.

* MiniBrowser/gtk/BrowserSettingsDialog.c:
(hardwareAccelerationPolicyToString):
(stringToHardwareAccelerationPolicy):
(cellRendererChanged):
(browserSettingsDialogConstructed):
* TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitSettings.cpp:
(testWebKitSettings):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211363 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/gtk/WebKitSettings.cpp
Source/WebKit2/UIProcess/API/gtk/WebKitSettings.h
Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt
Source/WebKit2/WebProcess/WebPage/DrawingAreaImpl.cpp
Tools/ChangeLog
Tools/MiniBrowser/gtk/BrowserSettingsDialog.c
Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitSettings.cpp

index c083c37..9e3def9 100644 (file)
@@ -1,3 +1,29 @@
+2017-01-30  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] Add API to handle the accelerated compositing policy
+        https://bugs.webkit.org/show_bug.cgi?id=167509
+
+        Reviewed by Michael Catanzaro.
+
+        Now that we have brought back the on demand mode, we should allow applications to choose the policy, without
+        having to deal with environment variables. Settings also allows to set different policy depending on the web
+        view, so for example evolution could disable AC for the composer, but leave the on demand mode for the email
+        viewer. This patch adds a single new setting hardware-acceleration-policy to handle both
+        acceleratedCompositingEnabled and forceCompositingMode preferences using an enum with values
+        WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND, WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS and
+        WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER.
+
+        * UIProcess/API/gtk/WebKitSettings.cpp:
+        (webKitSettingsSetProperty): Add setter for hardware-acceleration-policy property.
+        (webKitSettingsGetProperty): Add getter for hardware-acceleration-policy property.
+        (webkit_settings_class_init): Add hardware-acceleration-policy property.
+        (webkit_settings_get_hardware_acceleration_policy): Return policy according to the preferences.
+        (webkit_settings_set_hardware_acceleration_policy): set preferences according to the given policy.
+        * UIProcess/API/gtk/WebKitSettings.h:
+        * UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Add new symbols.
+        * WebProcess/WebPage/DrawingAreaImpl.cpp:
+        (WebKit::DrawingAreaImpl::updatePreferences):
+
 2017-01-29  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         [Threaded Compositor] Crash on WebCore::GLContext::version()
index 161bc39..9b420ba 100644 (file)
@@ -32,6 +32,7 @@
 #include "WebKitSettings.h"
 
 #include "ExperimentalFeatures.h"
+#include "WebKitEnumTypes.h"
 #include "WebKitPrivate.h"
 #include "WebKitSettingsPrivate.h"
 #include "WebPageProxy.h"
@@ -145,7 +146,8 @@ enum {
     PROP_ENABLE_SPATIAL_NAVIGATION,
     PROP_ENABLE_MEDIASOURCE,
     PROP_ALLOW_FILE_ACCESS_FROM_FILE_URLS,
-    PROP_ALLOW_UNIVERSAL_ACCESS_FROM_FILE_URLS
+    PROP_ALLOW_UNIVERSAL_ACCESS_FROM_FILE_URLS,
+    PROP_HARDWARE_ACCELERATION_POLICY,
 };
 
 static void webKitSettingsConstructed(GObject* object)
@@ -323,6 +325,9 @@ static void webKitSettingsSetProperty(GObject* object, guint propId, const GValu
     case PROP_ALLOW_UNIVERSAL_ACCESS_FROM_FILE_URLS:
         webkit_settings_set_allow_universal_access_from_file_urls(settings, g_value_get_boolean(value));
         break;
+    case PROP_HARDWARE_ACCELERATION_POLICY:
+        webkit_settings_set_hardware_acceleration_policy(settings, static_cast<WebKitHardwareAccelerationPolicy>(g_value_get_enum(value)));
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
         break;
@@ -486,6 +491,9 @@ static void webKitSettingsGetProperty(GObject* object, guint propId, GValue* val
     case PROP_ALLOW_UNIVERSAL_ACCESS_FROM_FILE_URLS:
         g_value_set_boolean(value, webkit_settings_get_allow_universal_access_from_file_urls(settings));
         break;
+    case PROP_HARDWARE_ACCELERATION_POLICY:
+        g_value_set_enum(value, webkit_settings_get_hardware_acceleration_policy(settings));
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
         break;
@@ -1279,6 +1287,28 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
             _("Whether or not universal access is allowed from the context of file scheme URLs"),
             FALSE,
             readWriteConstructParamFlags));
+
+    /**
+     * WebKitSettings:hardware-acceleration-policy:
+     *
+     * The #WebKitHardwareAccelerationPolicy to decide how to enable and disable
+     * hardware acceleration. The default value %WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND
+     * enables the hardware acceleration when the web contents request it, disabling it again
+     * when no longer needed. It's possible to enfore hardware acceleration to be always enabled
+     * by using %WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS. And it's also posible to disable it
+     * completely using %WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER. Note that disabling hardware
+     * acceleration might cause some websites to not render correctly or consume more CPU.
+     *
+     * Since: 2.16
+     */
+    g_object_class_install_property(gObjectClass,
+        PROP_HARDWARE_ACCELERATION_POLICY,
+        g_param_spec_enum("hardware-acceleration-policy",
+            _("Hardware Acceleration Policy"),
+            _("The policy to decide how to enable and disable hardware acceleration"),
+            WEBKIT_TYPE_HARDWARE_ACCELERATION_POLICY,
+            WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND,
+            readWriteConstructParamFlags));
 }
 
 WebPreferences* webkitSettingsGetPreferences(WebKitSettings* settings)
@@ -3142,3 +3172,83 @@ void webkit_settings_set_allow_universal_access_from_file_urls(WebKitSettings* s
     priv->preferences->setAllowUniversalAccessFromFileURLs(allowed);
     g_object_notify(G_OBJECT(settings), "allow-universal-access-from-file-urls");
 }
+
+/**
+ * webkit_settings_get_hardware_acceleration_policy:
+ * @settings: a #WebKitSettings
+ *
+ * Get the #WebKitSettings:hardware-acceleration-policy property.
+ *
+ * Return: a #WebKitHardwareAccelerationPolicy
+ *
+ * Since: 2.16
+ */
+WebKitHardwareAccelerationPolicy webkit_settings_get_hardware_acceleration_policy(WebKitSettings* settings)
+{
+    g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND);
+
+    WebKitSettingsPrivate* priv = settings->priv;
+    if (!priv->preferences->acceleratedCompositingEnabled())
+        return WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER;
+
+    if (priv->preferences->forceCompositingMode())
+        return WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS;
+
+    return WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND;
+}
+
+/**
+ * webkit_settings_set_hardware_acceleration_policy:
+ * @settings: a #WebKitSettings
+ * @policy: a #WebKitHardwareAccelerationPolicy
+ *
+ * Set the #WebKitSettings:hardware-acceleration-policy property.
+ *
+ * Since: 2.16
+ */
+void webkit_settings_set_hardware_acceleration_policy(WebKitSettings* settings, WebKitHardwareAccelerationPolicy policy)
+{
+    g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
+
+    WebKitSettingsPrivate* priv = settings->priv;
+    bool changed = false;
+    switch (policy) {
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS:
+        if (!priv->preferences->acceleratedCompositingEnabled()) {
+            priv->preferences->setAcceleratedCompositingEnabled(true);
+            changed = true;
+        }
+        if (!priv->preferences->forceCompositingMode()) {
+            priv->preferences->setForceCompositingMode(true);
+            changed = true;
+        }
+        break;
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER:
+        if (priv->preferences->acceleratedCompositingEnabled()) {
+            priv->preferences->setAcceleratedCompositingEnabled(false);
+            changed = true;
+        }
+
+        if (priv->preferences->forceCompositingMode()) {
+            priv->preferences->setForceCompositingMode(false);
+            changed = true;
+        }
+        break;
+
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND:
+        if (!priv->preferences->acceleratedCompositingEnabled()) {
+            priv->preferences->setAcceleratedCompositingEnabled(true);
+            changed = true;
+        }
+
+        if (priv->preferences->forceCompositingMode()) {
+            priv->preferences->setForceCompositingMode(false);
+            changed = true;
+        }
+
+        break;
+    }
+
+    if (changed)
+        g_object_notify(G_OBJECT(settings), "hardware-acceleration-policy");
+}
index 3f4c38b..7f63577 100644 (file)
@@ -47,6 +47,22 @@ G_BEGIN_DECLS
 #define WEBKIT_IS_SETTINGS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),  WEBKIT_TYPE_SETTINGS))
 #define WEBKIT_SETTINGS_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj),  WEBKIT_TYPE_SETTINGS, WebKitSettingsClass))
 
+/**
+ * WebKitHardwareAccelerationPolicy:
+ * @WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND: Hardware acceleration is enabled/disabled as request by web contents.
+ * @WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS: Hardware acceleration is always enabled, even for websites not requesting it.
+ * @WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER: Hardware acceleration is always disabled, even for websites requesting it.
+ *
+ * Enum values used for determining the hardware acceleration policy.
+ *
+ * Since: 2.16
+ */
+typedef enum {
+    WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND,
+    WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS,
+    WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER
+} WebKitHardwareAccelerationPolicy;
+
 typedef struct _WebKitSettings WebKitSettings;
 typedef struct _WebKitSettingsClass WebKitSettingsClass;
 typedef struct _WebKitSettingsPrivate WebKitSettingsPrivate;
@@ -428,6 +444,13 @@ WEBKIT_API void
 webkit_settings_set_allow_universal_access_from_file_urls      (WebKitSettings *settings,
                                                                 gboolean        allowed);
 
+WEBKIT_API WebKitHardwareAccelerationPolicy
+webkit_settings_get_hardware_acceleration_policy               (WebKitSettings *settings);
+
+WEBKIT_API void
+webkit_settings_set_hardware_acceleration_policy               (WebKitSettings *settings,
+                                                                WebKitHardwareAccelerationPolicy policy);
+
 G_END_DECLS
 
 #endif /* WebKitSettings_h */
index 43fa5b8..6cba454 100644 (file)
@@ -358,6 +358,7 @@ webkit_back_forward_list_item_get_type
 <SECTION>
 <FILE>WebKitSettings</FILE>
 WebKitSettings
+WebKitHardwareAccelerationPolicy
 webkit_settings_new
 webkit_settings_new_with_settings
 webkit_settings_get_auto_load_images
@@ -461,6 +462,8 @@ webkit_settings_get_allow_file_access_from_file_urls
 webkit_settings_set_allow_file_access_from_file_urls
 webkit_settings_get_allow_universal_access_from_file_urls
 webkit_settings_set_allow_universal_access_from_file_urls
+webkit_settings_get_hardware_acceleration_policy
+webkit_settings_set_hardware_acceleration_policy
 
 <SUBSECTION Standard>
 WebKitSettingsClass
index 0e51511..99917dd 100644 (file)
@@ -177,8 +177,8 @@ void DrawingAreaImpl::updatePreferences(const WebPreferencesStore& store)
 #if USE(COORDINATED_GRAPHICS_THREADED)
     // Fixed position elements need to be composited and create stacking contexts
     // in order to be scrolled by the ScrollingCoordinator.
-    settings.setAcceleratedCompositingForFixedPositionEnabled(true);
-    settings.setFixedPositionCreatesStackingContext(true);
+    settings.setAcceleratedCompositingForFixedPositionEnabled(settings.acceleratedCompositingEnabled());
+    settings.setFixedPositionCreatesStackingContext(settings.acceleratedCompositingEnabled());
 #endif
 
     m_alwaysUseCompositing = settings.acceleratedCompositingEnabled() && settings.forceCompositingMode();
index 4c933df..1ca7a2f 100644 (file)
@@ -1,3 +1,21 @@
+2017-01-30  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] Add API to handle the accelerated compositing policy
+        https://bugs.webkit.org/show_bug.cgi?id=167509
+
+        Reviewed by Michael Catanzaro.
+
+        Handle new setting in MiniBrowser. The settings dialog doesn't support enum settings so it needs to be handled
+        as a special case. Also add test cases to the get/set API.
+
+        * MiniBrowser/gtk/BrowserSettingsDialog.c:
+        (hardwareAccelerationPolicyToString):
+        (stringToHardwareAccelerationPolicy):
+        (cellRendererChanged):
+        (browserSettingsDialogConstructed):
+        * TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitSettings.cpp:
+        (testWebKitSettings):
+
 2017-01-29  Andy Estes  <aestes@apple.com>
 
         [QuickLook] Add a WebPreference to enable saving QuickLook documents in WebKitLegacy
index 1e4dcd8..511b3db 100644 (file)
@@ -55,6 +55,34 @@ struct _BrowserSettingsDialogClass {
 
 G_DEFINE_TYPE(BrowserSettingsDialog, browser_settings_dialog, GTK_TYPE_DIALOG)
 
+static const char *hardwareAccelerationPolicyToString(WebKitHardwareAccelerationPolicy policy)
+{
+    switch (policy) {
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS:
+        return "always";
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER:
+        return "never";
+    case WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND:
+        return "ondemand";
+    }
+
+    g_assert_not_reached();
+    return "ondemand";
+}
+
+static int stringToHardwareAccelerationPolicy(const char *policy)
+{
+    if (!g_ascii_strcasecmp(policy, "always"))
+        return WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS;
+    if (!g_ascii_strcasecmp(policy, "never"))
+        return WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER;
+    if (!g_ascii_strcasecmp(policy, "ondemand"))
+        return WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND;
+
+    g_warning("Invalid value %s for hardware-acceleration-policy setting valid values are always, never and ondemand", policy);
+    return -1;
+}
+
 static void cellRendererChanged(GtkCellRenderer *renderer, const char *path, const GValue *value, BrowserSettingsDialog *dialog)
 {
     GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(dialog->settingsList));
@@ -62,12 +90,21 @@ static void cellRendererChanged(GtkCellRenderer *renderer, const char *path, con
     GtkTreeIter iter;
     gtk_tree_model_get_iter(model, &iter, treePath);
 
+    gboolean updateTreeStore = TRUE;
     char *name;
     gtk_tree_model_get(model, &iter, SETTINGS_LIST_COLUMN_NAME, &name, -1);
-    g_object_set_property(G_OBJECT(dialog->settings), name, value);
+    if (!g_strcmp0(name, "hardware-acceleration-policy")) {
+        int policy = stringToHardwareAccelerationPolicy(g_value_get_string(value));
+        if (policy != -1)
+            webkit_settings_set_hardware_acceleration_policy(dialog->settings, policy);
+        else
+            updateTreeStore = FALSE;
+    } else
+        g_object_set_property(G_OBJECT(dialog->settings), name, value);
     g_free(name);
 
-    gtk_list_store_set(GTK_LIST_STORE(model), &iter, SETTINGS_LIST_COLUMN_VALUE, value, -1);
+    if (updateTreeStore)
+        gtk_list_store_set(GTK_LIST_STORE(model), &iter, SETTINGS_LIST_COLUMN_VALUE, value, -1);
     gtk_tree_path_free(treePath);
 }
 
@@ -134,10 +171,19 @@ static void browserSettingsDialogConstructed(GObject *object)
         GParamSpec *property = properties[i];
         const char *name = g_param_spec_get_name(property);
         const char *nick = g_param_spec_get_nick(property);
+        char *blurb = g_markup_escape_text(g_param_spec_get_blurb(property), -1);
 
         GValue value = { 0, { { 0 } } };
-        g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(property));
-        g_object_get_property(G_OBJECT(settings), name, &value);
+        if (!g_strcmp0(name, "hardware-acceleration-policy")) {
+            g_value_init(&value, G_TYPE_STRING);
+            g_value_set_string(&value, hardwareAccelerationPolicyToString(webkit_settings_get_hardware_acceleration_policy(settings)));
+            char *extendedBlutb = g_strdup_printf("%s (always, never or ondemand)", blurb);
+            g_free(blurb);
+            blurb = extendedBlutb;
+        } else {
+            g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(property));
+            g_object_get_property(G_OBJECT(settings), name, &value);
+        }
 
         GtkAdjustment *adjustment = NULL;
         if (G_PARAM_SPEC_VALUE_TYPE(property) == G_TYPE_UINT) {
@@ -146,7 +192,6 @@ static void browserSettingsDialogConstructed(GObject *object)
                                             uIntProperty->maximum, 1, 1, 1);
         }
 
-        char *blurb = g_markup_escape_text(g_param_spec_get_blurb(property), -1);
         GtkTreeIter iter;
         gtk_list_store_append(model, &iter);
         gtk_list_store_set(model, &iter,
index c4646ef..db60c2f 100644 (file)
@@ -285,6 +285,15 @@ static void testWebKitSettings(Test*, gconstpointer)
     webkit_settings_set_allow_universal_access_from_file_urls(settings, TRUE);
     g_assert(webkit_settings_get_allow_universal_access_from_file_urls(settings));
 
+    // Ondemand is the default hardware acceleration policy.
+    g_assert_cmpuint(webkit_settings_get_hardware_acceleration_policy(settings), ==, WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND);
+    webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER);
+    g_assert_cmpuint(webkit_settings_get_hardware_acceleration_policy(settings), ==, WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER);
+    webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS);
+    g_assert_cmpuint(webkit_settings_get_hardware_acceleration_policy(settings), ==, WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS);
+    webkit_settings_set_hardware_acceleration_policy(settings, WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND);
+    g_assert_cmpuint(webkit_settings_get_hardware_acceleration_policy(settings), ==, WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND);
+
     g_object_unref(G_OBJECT(settings));
 }