Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] fix one layer of "overdraw" by default #22126

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonathanpeppers
Copy link
Member

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.

image

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:

image

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

@jsuarezruiz jsuarezruiz added platform/android 🤖 legacy-area-perf Startup / Runtime performance labels Apr 30, 2024
Fixes: dotnet#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 dotnet#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
Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly all the golden tests, UITests comparing a snapshot with a reference image are failing, because:
image

The detail page, is renderered on top of the previous one.

See the differences:
image

@jonathanpeppers
Copy link
Member Author

If this changes behavior in even the UI Test app, I want to discuss it before we consider this one.

It might make sense to put this in .NET 9-only, or just put it in the project template.

Customers could use this workaround for now:

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

@Eilon Eilon added the t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) label May 10, 2024
@dainius-r
Copy link

@jonathanpeppers tried using workaround with android styles <item name="android:windowBackground">@null</item>. Changing to this, everything seems working normally except Shell modal pages won't show.

@jonathanpeppers
Copy link
Member Author

except Shell modal pages won't show.

@dainius-r is there an exception? (it should appear in adb logcat output) Or can you share a screenshot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-perf Startup / Runtime performance platform/android 🤖 t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maui in Android Overdraws even with blank project
4 participants