How to detect if the system supports hardware acceleration?

Asked

Viewed 1,057 times

6

Currently I do this so that one of mine activity that require acceleration work:

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>

Otherwise the application hangs and closes (crash) and in the log get the following error:

04-23 05:27:36.570: E/Chromium(902): [ERROR:gles2_cmd_decoder. cc(3389)] Gles2decoderimpl::Resizeoffscreenframebuffer failed to allocate Storage for Offscreen target Depth buffer.

However I would like to detect if there is support for hardware acceleration and activate it, this can be by manifest as much as by java. It is possible?

1 answer

5


Hardware acceleration is available with Android 3.0 (API level 11).
From Android 4.0(API level 14) it is enabled by default.

Hardware acceleration can be controlled at the following levels:

  • Application
    In the Androidmanifest.xml, indicating true or false to the attribute android:hardwareAccelerated, in section <application/>:

    <application android:hardwareAccelerated="true" ...>
    
  • Activity
    In the Androidmanifest.xml, indicating true or false to the attribute android:hardwareAccelerated, in section <activity/>:

    <application android:hardwareAccelerated="true">
        <activity ... />
        <activity android:hardwareAccelerated="false" />
    </application>
    
  • Window
    Using the method setFlags() Window to enable hardware acceleration:

    getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    

    Hardware acceleration cannot be disabled at this level.

  • View
    Using the method setLayerType() of View to disable hardware acceleration:

    myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    Hardware acceleration cannot be enabled at this level.

There are two ways to know if an application is hardware-accelerated:

Please note that before using the method you must be assured that the View is associated with a Window, what is not the case for example in onCreate().

A place where you have this guarantee is the onWindowFocusChanged() of Activity.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        boolean isAccelerated = view.isHardwareAccelerated();
    }

}

For more detailed information see the topic Hardware Acceleration in the documentation.

  • Not, getWindow() is an Activity method, it returns the Window of that Activity.

  • You say "Both ..." but then do not materialize, I suppose you meant "both ..., even though I have... ...., return false". I don’t know if these methods are correctly emulated. You may have to test on physical devices. Note that it is something I never used, the answer was based on documentation.

  • Please note that before using the method the view has to be associated with a Window. See the edit of the answer.

  • I tested the example I posted, using the onWindowFocusChanged(), and it worked.

  • I don’t know where or how you intend to use the methods but I do know that the Canvas.isHardwareAccelerated() within the method draw()of a View also works.

  • 1

    Ramaral I’m beginning to believe that the problem is not acceleration, I get this error GLES2DecoderImpl::ResizeOffscreenFrameBuffer failed to allocate storage for offscreen target depth buffer, this when the acceleration is configured in the manifest like this <activity android:hardwareAccelerated="false" /> and in case only occurs if I use a webView.loadUrl. I will mark this answer as correct, as it seems that both problems are unrelated. Please see this other question http://answall.com/q/163926/3635

  • Just a correction I wrote this <activity android:hardwareAccelerated="false" />, but I actually wanted to say this <activity android:hardwareAccelerated="true" />, would not have felt the false in most of the tests I did. Thanks again. A note, the question about webView I changed.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.