Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextureView hardware acceleration with lockCanvas()

I'm trying to use the hardware acceleration for Android with my canvas. I used to have a SurfaceView which I did lockCanvas() on to get a canvas which I later draw on, but I changed to TextureView since I couldn't get SurfaceView to use hardware acceleration. I'm currently trying to get this canvas to use hardware acceleration.

Canvas canvas = this.lockCanvas();
System.out.println(this.isHardwareAccelerated() + ", " + canvas.isHardwareAccelerated());

Gives me the output: true, false (this is a TextureView)

Does anyone know why the canvas is not hardware accelerated, and how to make it so?

Edit: As far as I have found, it seems that I have to use OpenGL. However, I would still like to know if there are any announced plans to make hardware acceleration possible for such a canvas.

like image 203
Alle Avatar asked Apr 01 '12 16:04

Alle


People also ask

Should I turn on hardware acceleration on android?

If your application uses only standard views and Drawable s, turning it on globally should not cause any adverse drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your custom views or drawing calls.

How do I enable GPU acceleration on android?

Go back to Settings, scroll down and you should be able to see a new option called Developer options. Tap on it. Scroll down to the Hardware accelerated rendering and enable the toggle next to Force GPU rendering.

What is the use of hardware acceleration in android?

Hardware acceleration is the process to use device hardware to speedup drawing operations of android application. In other word, android use hardware acceleration to speed up 2D rendering or fast up the image and video rendering. It is by default enabled if your Target API level is >=14.


2 Answers

TextureView works only when the application is hardware accelerated, but the Canvas it returns from lockCanvas() is currently never hardware accelerated. This means that you will draw inside the TextureView's Canvas in software, but TextureView itself will be drawn using the GPU. Currently, the only way to get a hardware accelerated Canvas is to use the onDraw(Canvas) method of a View.

like image 70
Romain Guy Avatar answered Sep 22 '22 16:09

Romain Guy


Check the Hardware Acceleration article.

Basically if you want your TextureView to be hardware accelerated you must make sure that hardware acceleration is enabled at some level in the context of your TextureView, i.e one of the following:

  • At application level: <application android:hardwareAccelerated="true" ...>
  • At activity level: <activity android:hardwareAccelerated="true" />
  • At window level:

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

Note that hardware acceleration is actually mandatory to use a TextureView:

TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing.

like image 39
sdabet Avatar answered Sep 21 '22 16:09

sdabet