From 23ecf270d09ab09897172120da42f2829ea2bccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lenclud?= Date: Mon, 25 Mar 2024 10:52:15 +0100 Subject: [PATCH] Layer type settings option Allows users to control WebView hardware acceleration. Thus notably turning WebGL on and off. Also useful on platforms where hardware acceleration is not working reliably. For instance, WSA on Windows 11. Related to Slion/Fulguris#616. --- app/src/main/java/fulguris/enums/LayerType.kt | 26 ++++++++++++++ .../settings/preferences/UserPreferences.kt | 6 ++++ app/src/main/java/fulguris/view/WebPageTab.kt | 34 ++++++++++++++----- app/src/main/res/drawable/ic_eye.xml | 2 +- app/src/main/res/drawable/ic_layers.xml | 5 +++ app/src/main/res/drawable/ic_text_variant.xml | 2 +- app/src/main/res/values/arrays.xml | 6 ++++ app/src/main/res/values/donottranslate.xml | 2 +- app/src/main/res/values/strings.xml | 4 +++ app/src/main/res/xml/preference_display.xml | 11 ++++++ 10 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/fulguris/enums/LayerType.kt create mode 100644 app/src/main/res/drawable/ic_layers.xml diff --git a/app/src/main/java/fulguris/enums/LayerType.kt b/app/src/main/java/fulguris/enums/LayerType.kt new file mode 100644 index 000000000..1866afd91 --- /dev/null +++ b/app/src/main/java/fulguris/enums/LayerType.kt @@ -0,0 +1,26 @@ +package fulguris.enums + + +import android.view.View +import fulguris.settings.preferences.IntEnum +import android.view.WindowManager + +/** + * View layer type as an enum so that we can conveniently use it as a preference + */ +enum class LayerType(override val value: Int) : IntEnum { + /** + * The view is rendered normally and is not backed by an off-screen buffer. This is the default behavior. + * Though in my experience this uses hardware acceleration too. + */ + None(View.LAYER_TYPE_NONE), + /** + * The view is rendered in software into a bitmap. + */ + Software(View.LAYER_TYPE_SOFTWARE), + /** + * The view is rendered in hardware into a hardware texture if the application is hardware accelerated. + */ + Hardware(View.LAYER_TYPE_HARDWARE) +} + diff --git a/app/src/main/java/fulguris/settings/preferences/UserPreferences.kt b/app/src/main/java/fulguris/settings/preferences/UserPreferences.kt index a252fecac..53b28a8a8 100644 --- a/app/src/main/java/fulguris/settings/preferences/UserPreferences.kt +++ b/app/src/main/java/fulguris/settings/preferences/UserPreferences.kt @@ -21,6 +21,7 @@ import fulguris.browser.ProxyChoice import fulguris.browser.SuggestionNumChoice import fulguris.enums.LogLevel import fulguris.enums.HeaderInfo +import fulguris.enums.LayerType import fulguris.settings.preferences.delegates.booleanPreference import fulguris.settings.preferences.delegates.enumPreference import fulguris.settings.preferences.delegates.floatResPreference @@ -194,6 +195,11 @@ class UserPreferences @Inject constructor( */ var clearCookiesExitEnabled by preferences.booleanPreference(CLEAR_COOKIES_EXIT, false) + /** + * Determine if we use hardware acceleration and WebGL. + */ + var layerType by preferences.enumPreference(R.string.pref_key_layer_type, LayerType.Hardware) + /** * The index of the rendering mode that should be used by the browser. */ diff --git a/app/src/main/java/fulguris/view/WebPageTab.kt b/app/src/main/java/fulguris/view/WebPageTab.kt index d41d3ab33..91c620a91 100644 --- a/app/src/main/java/fulguris/view/WebPageTab.kt +++ b/app/src/main/java/fulguris/view/WebPageTab.kt @@ -60,6 +60,7 @@ import fulguris.constant.Uris import fulguris.constant.WINDOWS_DESKTOP_USER_AGENT_PREFIX import fulguris.di.HiltEntryPoint import fulguris.di.configPrefs +import fulguris.enums.LayerType import fulguris.extensions.canScrollVertically import fulguris.extensions.dp import fulguris.extensions.isDarkTheme @@ -249,7 +250,7 @@ class WebPageTab( val userPreferences: UserPreferences = hiltEntryPoint.userPreferences val dialogBuilder: LightningDialogBuilder = hiltEntryPoint.dialogBuilder - val proxyUtils: fulguris.utils.ProxyUtils = hiltEntryPoint.proxyUtils + val proxyUtils: ProxyUtils = hiltEntryPoint.proxyUtils val databaseScheduler: Scheduler = hiltEntryPoint.databaseScheduler() val mainScheduler: Scheduler = hiltEntryPoint.mainScheduler() val networkConnectivityModel: NetworkConnectivityModel = hiltEntryPoint.networkConnectivityModel @@ -257,6 +258,11 @@ class WebPageTab( private val networkDisposable: Disposable + /** + * Will decide to enable hardware acceleration and WebGL or not + */ + private var layerType = LayerType.Hardware + /** * This method determines whether the current tab is visible or not. * @@ -577,6 +583,7 @@ class WebPageTab( } settings.defaultTextEncodingName = userPreferences.textEncoding + layerType = userPreferences.layerType setColorMode(userPreferences.renderingMode) if (!isIncognito) { @@ -823,6 +830,14 @@ class WebPageTab( fun stopLoading() { webView?.stopLoading() } + + /** + * Layer type notably determines if we use hardware acceleration and WebGL + */ + private fun setLayerType() { + Timber.d("$ihs : setLayerType: $layerType") + webView?.setLayerType(layerType.value, paint) + } /** * This method forces the layer type to hardware, which @@ -830,7 +845,8 @@ class WebPageTab( * of the current [WebPageTab]. */ private fun setHardwareRendering() { - webView?.setLayerType(View.LAYER_TYPE_HARDWARE, paint) + //webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, paint) + webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, paint) } /** @@ -839,7 +855,7 @@ class WebPageTab( * the layers when necessary. */ private fun setNormalRendering() { - webView?.setLayerType(View.LAYER_TYPE_NONE, null) + webView?.setLayerType(View.LAYER_TYPE_NONE, paint) } /** @@ -849,7 +865,7 @@ class WebPageTab( * the view. */ fun setSoftwareRendering() { - webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, null) + webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, paint) } /** @@ -873,14 +889,14 @@ class WebPageTab( //setNormalRendering() // SL: enabled that and the performance gain is very noticeable on F(x)tec Pro1 // Notably on: https://www.bbc.com/worklife - setHardwareRendering() + setLayerType() } RenderingMode.INVERTED -> { val filterInvert = ColorMatrixColorFilter( negativeColorArray ) paint.colorFilter = filterInvert - setHardwareRendering() + setLayerType() invertPage = true } @@ -889,7 +905,7 @@ class WebPageTab( cm.setSaturation(0f) val filterGray = ColorMatrixColorFilter(cm) paint.colorFilter = filterGray - setHardwareRendering() + setLayerType() } RenderingMode.INVERTED_GRAYSCALE -> { val matrix = ColorMatrix() @@ -900,7 +916,7 @@ class WebPageTab( concat.setConcat(matrix, matrixGray) val filterInvertGray = ColorMatrixColorFilter(concat) paint.colorFilter = filterInvertGray - setHardwareRendering() + setLayerType() invertPage = true } @@ -908,7 +924,7 @@ class WebPageTab( RenderingMode.INCREASE_CONTRAST -> { val increaseHighContrast = ColorMatrixColorFilter(increaseContrastColorArray) paint.colorFilter = increaseHighContrast - setHardwareRendering() + setLayerType() } } diff --git a/app/src/main/res/drawable/ic_eye.xml b/app/src/main/res/drawable/ic_eye.xml index 2d0d3adf1..308ec38a9 100644 --- a/app/src/main/res/drawable/ic_eye.xml +++ b/app/src/main/res/drawable/ic_eye.xml @@ -5,6 +5,6 @@ android:viewportHeight="24" android:tint="?attr/colorControlNormal"> diff --git a/app/src/main/res/drawable/ic_layers.xml b/app/src/main/res/drawable/ic_layers.xml new file mode 100644 index 000000000..7d7954aff --- /dev/null +++ b/app/src/main/res/drawable/ic_layers.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_text_variant.xml b/app/src/main/res/drawable/ic_text_variant.xml index 7ce8c57c1..2d4c54680 100644 --- a/app/src/main/res/drawable/ic_text_variant.xml +++ b/app/src/main/res/drawable/ic_text_variant.xml @@ -5,6 +5,6 @@ android:viewportHeight="24" android:tint="?attr/colorControlNormal"> diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 768087a0b..c44b3f0ba 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -59,6 +59,12 @@ @string/action_ask + + @string/layer_type_none + @string/layer_type_software + @string/layer_type_hardware + + @string/label_url @string/label_short_url diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index d908d0050..195444567 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -161,7 +161,7 @@ pref_key_toolbar_label pref_key_add_configuration pref_key_configurations - + pref_key_layer_type pref_key_portrait diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c87c55cb7..6a5cbaa16 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -80,6 +80,9 @@ Yes No Ask + None + Software - WebGL off + Hardware - WebGL on Proceed Abort Text size @@ -459,6 +462,7 @@ Restore all tabs Restore tab + Layer type Domains Manage domain settings Resource domains diff --git a/app/src/main/res/xml/preference_display.xml b/app/src/main/res/xml/preference_display.xml index 7722147a3..bc372e873 100644 --- a/app/src/main/res/xml/preference_display.xml +++ b/app/src/main/res/xml/preference_display.xml @@ -193,6 +193,17 @@ + +