Skip to content

Commit

Permalink
[android] fix one layer of "overdraw" by default
Browse files Browse the repository at this point in the history
Fixes: #18245
Context: https://developer.android.com/studio/debug/dev-options#hardware
Context: https://developer.android.com/topic/performance/rendering/inspect-gpu-rendering#debug_overdraw
Context: https://stackoverflow.com/q/6499004

There is `Debug GPU Overdraw` developer option in Android that can be
enabled to see overdraw in the app.

* Blue: overdrawn by 1
* Green: overdrawn by 2
* Pink: overdrawn by 3
* Red: overdrawn by 4+

The .NET MAUI project template shows `Blue` by default, in a project
with nothing on the screen. A workaround is noted on #18245, but the
*best* workaround would be in `Platforms\Android\MainActivity.cs`:

    protected override void OnCreate(Bundle? savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Clear the Window's background
        Window?.SetBackgroundDrawable(null);
    }

With this change in place, the `Blue` overdraw is gone in the .NET
MAUI project template.

The result is .NET MAUI apps on Android should inherently use less GPU
(the size of the screen), which could be noticeable on low-end devices.
It could be particularly noticeable in apps that are moving into the
`Red` range based on the developer's XAML & layout choices.

We can make this same change in .NET MAUI's default `Maui.MainTheme`
Android theme:

    <item name="android:windowBackground">@null</item>

The default project template has a XAML style that sets the background:

    <Style TargetType="Page" ApplyToDerivedTypes="True">
        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource OffBlack}}" />
    </Style>

So the only case this change would be noticed (wrong color), would be if:

* Developer is using a `Page` with no `Background` set, and no
  `Background` set on the top-most layout.

* Developer removed all XAML styles or is migrating a Xamarin.Forms app

In this case, a developer could do one of:

* Apply a `Background` to the `Page`

* Extend the `Maui.MainTheme` style, but with a `Background` set
  • Loading branch information
jonathanpeppers committed Apr 29, 2024
1 parent e33d176 commit ff06b60
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/Core/src/Platform/Android/Resources/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<item name="checkboxStyle">@style/MauiCheckBox</item>
<item name="android:textAllCaps">false</item>
<item name="alertDialogTheme">@style/MauiAlertDialogTheme</item>
<item name="android:windowBackground">@null</item>
</style>
<style name="Maui.MainTheme.NoActionBar" parent="Maui.MainTheme">
<item name="windowActionBar">false</item>
Expand Down

0 comments on commit ff06b60

Please sign in to comment.