Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View.isHardwareAccelerated() is always false

I've got a View that is hardware accelerated on many devices. Within that View I need to be able to check whether hardware acceleration is used. According to Android SDK documentation, this is done using View.isHardwareAccelerated(). The interesting thing is that when testing on devices that support hardware acceleration, this method always returns false, even though the Views themselves actually are being hardware accelerated. (I've conclusively verified this) It gets more confusing:

If I make a call to View.getLayerType() I see that it is always set to View.LAYER_TYPE_NONE. If I make call to View.setLayerType(View.LAYER_TYPE_SOFTWARE) I can effectively disable hardware acceleration. I can re-enable it by calling either View.setLayerType(View.LAYER_TYPE_HARDWARE) OR View.setLayerType(View.LAYER_TYPE_NONE). Why would View.LAYER_TYPE_NONE enable hardware acceleration???

Moving on, for devices that do support hw acceleration it seems like I can just check status by checking whether or not View.LAYER_TYPE_SOFTWARE is set, however I have my suspicions that this method wont work on devices that do not support hw acceleration. I'd like to know whether I am misusing View.isHardwareAccelerated() or if it's simply broken.

EDIT: I think I'm a little closer to understanding the problem. This question was also helpful:

Detect Hardware Acceleration at Runtime: Android

My working theory is that the View doesnt yet know whether it's hardware accelerated or not when it's constructor is called, which is where I am doing my check. If this is the case, I'm wondering what I can override in my class that extends View that will allow me to successfully check hardware acceleration. I suspect that View.onSizeChanged(...) would work, but I'd like to avoid using something that can potentially be called numerous times.

EDIT #2: Looks like that was it. I moved my check into View.onSizeChanged() and it now reports correctly. I'm still wondering if theres a better place within my View that I can move this check though.

like image 485
Nick Avatar asked Aug 25 '12 06:08

Nick


2 Answers

I've been pondering this for a while myself. Good to see you figured out what the issue was. The documentation does correctly describe this but it's not terribly clear. "Returns: True if the view is attached to a window and the window is hardware accelerated"

like image 90
Bostwickenator Avatar answered Oct 13 '22 01:10

Bostwickenator


As Diane Hackborn mentions in this post: https://groups.google.com/forum/#!topic/android-developers/vAH0HAZg0uU

You should check isHardwareAccelerated() after the view was attached to the window - e.g. in onAttachedToWindow()!

like image 31
mAx Avatar answered Oct 13 '22 00:10

mAx