Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Draw Background with no GPU OverDraw like Whatsapp

In order to improve app performance, I encounter GPU Overdraw problem. According to Romain Guy's Article, here are the basic Colors:


  • No color means there is no overdraw. The pixel was painted only once. In this example, you can see that the background is intact.

  • Blue indicates an overdraw of 1x. The pixel was painted twice. Large blue areas are acceptable (if the entire window is blue, you can get rid of one layer.)

  • Green indicates an overdraw of 2x. The pixel was painted three times. Medium-sized green areas are acceptable but you should try to optimize them away.

  • Light red indicates an overdraw of 3x. The pixel was painted four times. Small light red areas are acceptable.

  • Dark red indicates an overdraw of 4x or more. The pixel was painted 5 times or more. This is wrong. Fix it.`


To Test that, I create a simple project with XML as follows

XML Code

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"

    android:layout_margin="50dp"
    android:background="#fff">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

</RelativeLayout>

and get result:
n00b Me

Then I tried Whatsapp's overdraw. To my suprise:
Awesome Whatsapp

So How Whatsapp is drawing background wallpaper with no overdraw (no blue tint) but in my simple xml, even coloring gives one overdraw??

PS: I have intentionally added background color to show that adding color gives overdraw but adding wallpaper doesn't

like image 350
Aexyn Avatar asked Apr 22 '15 12:04

Aexyn


People also ask

Does debug GPU overdraw increase performance?

The Debug GPU Overdraw tool uses color-coding to show the number of times your app draws each pixel on the screen. The higher this count, the more likely it is that overdraw affects your app's performance.

What is debug GPU overdraw in developer options?

Overdraw occurs when your app draws the same pixel more than once within the same frame. So this visualization shows where your app might be doing more rendering work than necessary, which can be a performance problem due to extra GPU effort to render pixels that won't be visible to the user.

What is HWUI?

HWUI. The HWUI library enables UI components to be accelerated using the GPU. HWUI was introduced in Honeycomb to make the user interface fast, responsive and smooth.


2 Answers

Every Activity has a default background Drawable coming from the Theme's android:windowBackground attribute.

WhatsApp is using a background image which can be repeated in both directions in order to fill up the whole screen. This way they don't need to hassle around with different images for different screen sizes - it's super responsive!

So let's say you have such a tiled image (e.g. drawable-nodpi/background_tile.png)

Just create a new xml Drawable (e.g. drawable/background.xml) which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background_tile"
    android:tileMode="repeat" />

Now simply set this Drawable as a background for your Activity's Theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@drawable/background</item>
</style>

You can also set the background Drawable of an Activity at runtime with getWindow().setBackgroundDrawable or getWindow().setBackgroundDrawableResource

You can also set the background Drawable to null, for example if the whole Activity shows a MapView or something similar.

Also note that the Zygote will use the Theme's background Drawable during the app startup phase if the Activity is your main launcher Activity.

like image 193
astuetz Avatar answered Oct 02 '22 15:10

astuetz


Applying Bill Gates's hint, it turns out setting background in styles.xml in app actually saves one overdraw. This code saves one overDraw

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowBackground">@color/highlighted_text_material_dark</item>
    </style>

</resources>

Now Adding background doesn't take any overDraw so Relative Layout is Still One overDraw

BackGround with no OverDraw

UPDATE The Above method Works, and is good if you want same color background for all of your app. But in case you want different colors and still wanna save OverDraw, replace above line with

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

One Step Further
Boom.. You are good to go..

TIP: If your app has black color as background.. go for it save one more overdraw ;)

like image 42
Aexyn Avatar answered Oct 02 '22 17:10

Aexyn