绘图路径和硬件加速 [英] Drawing paths and hardware acceleration

查看:26
本文介绍了绘图路径和硬件加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我正在绘制一条相当大的路径,但遇到了一些性能问题.路径目前有 32,000 点长,但我的应用程序应该至少扩展到 128,000 点.我真的无法对路径的大小做任何事情,因为数据集就那么大,我需要能够一次显示整个路径并允许放大.

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 least 128,000 points. I can't really do anything about the size of the path, as the datasets are just that large and I need to be able to display the whole path at once and allow zooming in.

我使用的是运行 Android 4.2 的 Nexus 10,它默认为未明确禁用它的应用程序启用硬件加速.

I'm using a Nexus 10 running Android 4.2, which has hardware acceleration enabled by default for applications that don't explicitly disable it.

路径是用下面的代码创建的(我省略了一些设置和其他不相关的部分):

The path is created with the following code (I omitted some setup and other irrelevant parts):

dataPath.moveTo(0, offset - (float) data[leftLimit]/ scalingFactor);
        for (int i = leftLimit; i < rightLimit; ++i) {
            x = (i - leftLimit) * dx;
            y = offset - (float) data[i]/ scalingFactor;
            dataPath.lineTo(x, y);
        }

然后在onDraw()方法中绘制:

canvas.drawColor(Color.WHITE);
canvas.drawPath(dataPath, linePaint);

我测量了使用 adb shell dumpsys gfxinfo 在有和没有硬件加速的情况下绘制我的视图所需的时间,令我惊讶的是硬件加速要慢得多:

I measured the time it takes to draw my view using adb shell dumpsys gfxinfo with and without hardware acceleration, and to my suprise the hardware acceleration is much slower:

硬件加速:

没有硬件加速:

硬件加速版本每帧大约需要 200-300 毫秒,大部分时间花费在处理阶段.非加速版本大约需要 50 ms,其中 2/3 在 Draw 阶段,1/3 在 process 阶段.

The hardware accelerated version takes around 200-300 ms per frame, most spent in the Process stage. The non-accelerated version takes around 50 ms, with 2/3 in the Draw stage and 1/3 in the process stage.

显然,即使我没有硬件加速的更快版本仍然太慢,无法达到 60 fps,或者当我转移到更大的数据集时甚至几乎无法使用.

Obviously even my faster version without hardware acceleration is still too slow to achieve 60 fps, or to be even barely useable when I move to larger datasets.

将路径渲染到位图然后仅转换该位图以适应屏幕的想法在我的情况下也是有问题的.我需要支持在路径上进行非常远的放大,并且要在不使路径质量变得更差的情况下进行放大,我将不得不渲染路径的超大位图(并且可能会遇到内存限制和纹理大小限制).当放大很远时,我要么只创建路径的一部分的新图像,要么切换到直接渲染路径,如果性能仍然与我所拥有的相似,这可能会导致延迟大于帧率现在.

The idea to render the path to a bitmap and then only transform that bitmap to fit the screen is also problematic in my case. I need to support zooming in very far onto the path, and to enable zooming in without the path quality getting much worse I would have to render oversized bitmaps of the path (and would likely run into memory limits and the texture size limits). And when zooming in far I would have to either create newer images of only parts of the path, or switch to just rendering the path directly, which likely would lead to delays greater than the framerate if the performance is still similar to what I have right now.

我现在想知道的是

  • 绘制线条/路径只是 GPU 不擅长的事情,不应尝试硬件加速,还是我可能做错了什么导致性能不佳?
  • 我能做些什么来以可接受的性能绘制如此巨大的路径吗?

推荐答案

绘制线条/路径只是 GPU 不擅长的东西吗?不应该尝试硬件加速,或者我可能会做些什么错误导致性能不佳?

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?

路径总是使用 CPU 渲染.当应用程序是硬件加速时,这意味着渲染器将首先使用 CPU 将您的路径绘制成位图,然后将该位图作为纹理上传到 GPU,最后在屏幕上绘制纹理.

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.

线路完全是硬件加速的.在这种情况下,我建议您使用 Canvas.drawLines() 而不是使用 Path.

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?

您应该使用 Canvas.drawLines() 或自己将路径渲染到您管理的 Bitmap 中.

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

这篇关于绘图路径和硬件加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆