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.
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.
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.
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.
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.
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:
<application android:hardwareAccelerated="true" ...>
<activity android:hardwareAccelerated="true" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With