Open In App

Hardware Acceleration in Android 13

Last Updated : 07 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The practice of using a device’s hardware to speed up an Android application’s drawing operations is known as hardware acceleration. To put it another way, Android uses hardware acceleration to accelerate 2D rendering or the rendering of images and videos. In this article, we will be knowing how to implement Hardware Acceleration in your Android Application so that your app can take benefit from it.

Activating Hardware Acceleration in Apps targeting Android 13

Turning it on worldwide shouldn’t have any negative effects on the drawing if your application just makes use of conventional views and Drawables. Turning on hardware acceleration, however, can have an impact on certain of your custom views or drawing calls since not all 2D drawing operations are supported. Problems frequently appear as undetectable components, errors, or improperly drawn pixels. Android addresses this by allowing you to enable or disable hardware acceleration at various levels.

Components that can be used while Accelerating Hardware

Not all the components which we have in our Android Application can be used towards HA, there are certain aspects that have been laid down, and only those can be accelerated.

They include:

  1. Activity
  2. Application
  3. Window
  4. View

Also note that if you need to enable hardware acceleration, you need to work with the other components as well, in order to get things working, because one thing may depend on the other while you design your application.

Understanding Application Level in Applications

To enable hardware acceleration for your entire application, add the following attribute to the application> element in your Android manifest file:

<application android:hardwareAccelerated="true" ...> 

GeekTip: This may be enabled by default in some applications, and may even be required by some. applications which use extensive CPU processing to have this enabled.

Understanding Hardware Acceleration in Activities

You can also adjust hardware acceleration for specific tasks if your program doesn’t function properly when hardware acceleration is enabled globally. The android:hardwareAccelerated attribute for the activity> element can be used to activate or disable hardware acceleration at the activity level. The application in the example below has hardware acceleration enabled, but one activity has it turned off:

XML




<!-- You can use this flag to set them explicitally, 
     and then work at getting more hardware juice -->
<application android:hardwareAccelerated="true"> </application>


Understanding Hardware Acceleration in Windows

The following code will enable hardware acceleration for a specific window if you require even finer control:

Java




getGFGWindow().setFlags(
    // Use these flags in your main activity to get the
    // hardware acceleration working in that activity Geeks
    // for Geeks
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);


Understanding Hardware Acceleration at View Level

With the code below, you may turn off hardware acceleration for a specific view during runtime:

Java




// Use this method to set the layer type and then work
// normally Geeks for Geeks
gfgView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


GeekTip: A non-hardware accelerated Canvas can still be used to draw a view that is attached to a hardware-accelerated window. For example, this occurs when a view is drawn into a bitmap for caching purposes.

Verifying if the View is already Accelerated

When it comes to things like custom views, it can be helpful for an application to know whether it is now hardware accelerated. This is especially helpful if your application performs a lot of custom drawing and the new rendering pipeline does not adequately support all activities. To determine whether an application is hardware accelerated, there are two different methods:

  • If the Canvas is hardware accelerated, the Canvas.isHardwareAccelerated() function returns true.
  • If a view is affixed to a hardware-accelerated window, the View.isHardwareAccelerated() function returns true.

Drawing Models in Android

The Android framework uses a new drawing model that makes use of display lists to render your application on the screen when hardware acceleration is enabled. It helps to understand how Android draws views without hardware acceleration in order to completely comprehend display lists and how they could impact your application. The software-based and hardware-accelerated drawing models are described in the sections that follow. Views are drawn using the following two processes in the software drawing model:

  1. Reject the hierarchy
  2. Create a hierarchy.

You can do this by adding the below code to your android application, and then working with other things:

Java




gfgView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
ObjectAnimator gfgViewAnimatorLatch
    = ObjectAnimator.ofFloat(view, "rotationY", 180);
gfgViewAnimatorLatch.addListener(
    new AnimatorListenerAdapter() {
        @Override
        // Use this method when you 
          // want to perform something
        // when the animation ends.
        public void onAnimationEnd(Animator animation)
        {
            gfgView.setLayerType(View.LAYER_TYPE_NONE,
                                 null);
        }
    });
animator.start();


When Hardware Acceleration Invalidates

Either the developer or the device service can invalidate the HA, whenever required, or when the CPU usage is at an all-time high, there could be two conditions under which this can happen:

  1. The drawing model can conceal application flaws, which brings up the second problem. A view whose content you altered might be refreshed even though invalidate() was not called on it because the Android system redraws views when they intersect the dirty region. When this occurs, getting the right behavior depends on another view being invalidated.
  2. First off, each time a draw pass is made, a significant amount of code must be executed. For instance, if your application calls invalidate() on a button that is positioned above another view and that view hasn’t changed, the Android system will still refresh the view.

GeekTip: Draw only a few layers on top of one another. Remove any views that are entirely hidden beneath other opaque views. Consider combining multiple layers into one if you need to draw them layered on top of each other.

Conclusion

Performance can be drastically improved by switching to hardware-accelerated 2D graphics, but your application should still be built to take full advantage of the GPU. Hope this article helps you understand how to add Hardware Acceleration in your Android App, and that you use this to create better user experiences for your users.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads