16

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.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Alle
  • 324
  • 4
  • 19
  • You should enable hardware acceleration for your activity: http://developer.android.com/guide/topics/manifest/activity-element.html#hwaccel . – Taras Shevchuk Nov 06 '12 at 08:03
  • You can check it in http://developer.android.com/guide/topics/graphics/hardware-accel.html . I don't know if this is the answer. Because it says that check Canvas.isHardwareAccelerated can be used in Canvas.onDraw. – e7fendy Nov 28 '12 at 02:33

2 Answers2

11

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.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
6

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.

sdabet
  • 18,360
  • 11
  • 89
  • 158