Skip to content

Commit

Permalink
Layer type settings option
Browse files Browse the repository at this point in the history
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 #616.
  • Loading branch information
Slion committed Mar 25, 2024
1 parent 6d50f94 commit 23ecf27
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 12 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/fulguris/enums/LayerType.kt
Original file line number Diff line number Diff line change
@@ -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)
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
34 changes: 25 additions & 9 deletions app/src/main/java/fulguris/view/WebPageTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,14 +250,19 @@ 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
val defaultDomainSettings = DomainPreferences(activity)

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.
*
Expand Down Expand Up @@ -577,6 +583,7 @@ class WebPageTab(
}

settings.defaultTextEncodingName = userPreferences.textEncoding
layerType = userPreferences.layerType
setColorMode(userPreferences.renderingMode)

if (!isIncognito) {
Expand Down Expand Up @@ -823,14 +830,23 @@ 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
* enables hardware rendering on the WebView instance
* 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)
}

/**
Expand All @@ -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)
}

/**
Expand All @@ -849,7 +865,7 @@ class WebPageTab(
* the view.
*/
fun setSoftwareRendering() {
webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, paint)
}

/**
Expand All @@ -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
}
Expand All @@ -889,7 +905,7 @@ class WebPageTab(
cm.setSaturation(0f)
val filterGray = ColorMatrixColorFilter(cm)
paint.colorFilter = filterGray
setHardwareRendering()
setLayerType()
}
RenderingMode.INVERTED_GRAYSCALE -> {
val matrix = ColorMatrix()
Expand All @@ -900,15 +916,15 @@ class WebPageTab(
concat.setConcat(matrix, matrixGray)
val filterInvertGray = ColorMatrixColorFilter(concat)
paint.colorFilter = filterInvertGray
setHardwareRendering()
setLayerType()

invertPage = true
}

RenderingMode.INCREASE_CONTRAST -> {
val increaseHighContrast = ColorMatrixColorFilter(increaseContrastColorArray)
paint.colorFilter = increaseHighContrast
setHardwareRendering()
setLayerType()
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_eye.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/black"
android:fillColor="@android:color/white"
android:pathData="M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9M12,4.5C17,4.5 21.27,7.61 23,12C21.27,16.39 17,19.5 12,19.5C7,19.5 2.73,16.39 1,12C2.73,7.61 7,4.5 12,4.5M3.18,12C4.83,15.36 8.24,17.5 12,17.5C15.76,17.5 19.17,15.36 20.82,12C19.17,8.64 15.76,6.5 12,6.5C8.24,6.5 4.83,8.64 3.18,12Z" />
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_layers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="?android:colorControlNormal" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M11.99,18.54l-7.37,-5.73L3,14.07l9,7 9,-7 -1.63,-1.27 -7.38,5.74zM12,16l7.36,-5.73L21,9l-9,-7 -9,7 1.63,1.27L12,16z"/>

</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_text_variant.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/black"
android:fillColor="@android:color/white"
android:pathData="M11 3C10.18 3 9.44 3.5 9.14 4.27L3.64 18.27C3.12 19.58 4.09 21 5.5 21H7.75C8.59 21 9.33 20.5 9.62 19.7L10.26 18H13.74L14.38 19.7C14.67 20.5 15.42 21 16.25 21H18.5C19.91 21 20.88 19.58 20.36 18.27L14.86 4.27C14.56 3.5 13.82 3 13 3M11 5H13L18.5 19H16.25L15.12 16H8.87L7.75 19H5.5M12 7.67L9.62 14H14.37Z" />
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<item>@string/action_ask</item>
</string-array>

<string-array name="LayerTypes">
<item>@string/layer_type_none</item>
<item>@string/layer_type_software</item>
<item>@string/layer_type_hardware</item>
</string-array>

<string-array name="HeaderInfo">
<item>@string/label_url</item>
<item>@string/label_short_url</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
<string name="pref_key_toolbar_label">pref_key_toolbar_label</string>
<string name="pref_key_add_configuration">pref_key_add_configuration</string>
<string name="pref_key_configurations">pref_key_configurations</string>

<string name="pref_key_layer_type">pref_key_layer_type</string>


<string name="pref_key_portrait">pref_key_portrait</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<string name="action_yes">Yes</string>
<string name="action_no">No</string>
<string name="action_ask">Ask</string>
<string name="layer_type_none">None</string>
<string name="layer_type_software">Software - WebGL off</string>
<string name="layer_type_hardware">Hardware - WebGL on</string>
<string name="action_proceed">Proceed</string>
<string name="action_abort">Abort</string>
<string name="title_text_size">Text size</string>
Expand Down Expand Up @@ -459,6 +462,7 @@
<string name="action_restore_all_tabs">Restore all tabs</string>
<string name="action_restore_tab">Restore tab</string>

<string name="settings_title_layer_type">Layer type</string>
<string name="settings_title_domains">Domains</string>
<string name="settings_summary_domains">Manage domain settings</string>
<string name="settings_title_resource_domains">Resource domains</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/preference_display.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@

<PreferenceCategory android:title="@string/pref_category_webpage">

<fulguris.preference.EnumListPreference
a:key="@string/pref_key_layer_type"
a:title="@string/settings_title_layer_type"
a:useSimpleSummaryProvider="true"
a:defaultValue="Hardware"
a:entries="@array/LayerTypes"
a:icon="@drawable/ic_layers"
a:singleLineTitle="false"
a:enumClassName="fulguris.enums.LayerType"
/>

<Preference
android:key="@string/pref_key_rendering_mode"
android:title="@string/rendering_mode"
Expand Down

0 comments on commit 23ecf27

Please sign in to comment.