Drawing paths and hardware acceleration

前端 未结 3 805
孤城傲影
孤城傲影 2020-12-07 11:12

I\'m drawing a rather large path in my view and I\'m running into some performance problems. The path is currently 32,000 points long, but my application should scale to at

相关标签:
3条回答
  • 2020-12-07 11:54

    Is drawing lines/paths just something the GPU is bad at and that one should not try to hardware accelerate, or am I likely doing something wrong that causes the bad performance?

    Paths are always rendered using the CPU. When the app is hardware accelerated this means the renderer will first draw your path using the CPU into a bitmap, then upload that bitmap as a texture to the GPU and finally draw the texture on screen.

    Lines are entirely hardware accelerated. Instead of using a Path I recommend you use Canvas.drawLines() in this case.

    Is there anything I can do to draw such huge paths with acceptable performance?

    You should either use Canvas.drawLines() or render the path yourself into a Bitmap that you manage.

    0 讨论(0)
  • 2020-12-07 12:04

    Drawing a path will involve more than just "telling" the GPU to draw lines. A path has many characteristics, for example how ends of lines are treated or that a line is dotted, something that is not GPU accelerated. There is probably then another hickup when you encounter that many lines that makes the combination of CPU and GPU acceleration go slower. The suggested solution above to move to canvas.drawLines instead of canvs.drawPath and try to not use a fancy Paint method might help.

    If that does not help you can try to use a GLSurfaceView and render lines directly into that instead. That will involve some OpenGL ES knowledge, but should not be incredibly hard to do and might be a lot more effective.

    0 讨论(0)
  • 2020-12-07 12:08

    It seems like you might be running into general bottleneck in the GPU. Have a look at the following links:

    How to make route drawing more efficient

    Android canvas path real time performance

    Switching from canvas to OpenGL

    Especially the first one, which suggest that you make a bitmap and draw that instead. It looks like you can get some better performance if you change to openGL. There might be some magic way of getting the existing method to work, but I am not aware of that at this moment.

    Why you are seeing the behavior you do is probably because you send all the data to the GPU between each draw. You should cache it on the GPU and use translation in stead. Not recreating the data and sending it all to the GPU. You are probably I/O bound, meaning the transmission rate between the CPU and GPU is what is limiting your performance. I can not be 100% sure of this based on the data you have provided, but that is my best guess. Try different caching techniques, mainly the png-cache from link #1.

    0 讨论(0)
提交回复
热议问题