SurfaceView 绘制性能 [英] SurfaceView draw performance

查看:36
本文介绍了SurfaceView 绘制性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android SurfaceView 编写测试应用程序.我想看看它是否是一种可行的 2D 游戏开发方法.目前表现不是太好.

I am writing a test application using Android SurfaceView. I want to see if it is a viable approach to 2D game development. At the moment performance is not too great.

似乎每 15 秒左右就会出现微小的抖动和减速.我只在屏幕上绘制了大约 30 个矩形.我希望它能够轻松做到这一点.

There seems to be tiny little jerks and slowdowns every 15 or so seconds. I am only drawing about 30 rectangles on screen. I would hope it would be able to do this with ease.

我决定分析性能.我每 10 帧记录一次平均绘制时间.我注意到的是,它有很好的抽奖时间,然后跳起来.我确定我的代码没有在游戏循环中分配内存,所以我认为这个问题不是 GC.

I decided to profile performance. I log the average draw time every 10 frames. What I have noticed is that it goes periods with good draw times, and then it jumps up. I am certain my code does not allocate memory in the game loop, so I think this problem is not the GC.

我注意到的一点是性能下降了大约 100 帧,写在一个相当模糊的日志语句之后.有谁知道 android 正在打印的内容可能会发生什么.

Something I have noticed is that the performance drops about 100 frames in, write after a rather ambiguous log statement. Does anyone have any idea what might be happening with what android is printing.

每次我启动应用程序时,在这个关于删除文件的日志语句之后,绘制时间都会下降......

Everytime I start app, the draw time drops after this log statement about removing file....

01-18 16:53:20.221 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 14 01-18 16:53:20.58919296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=18 01-18 16:53:20.919 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 13 01-18 16:53:21.25019296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=13 01-18 16:53:21.581 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 13 01-18 16:53:21.91119296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=13 01-18 16:53:22.241 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 13 01-18 16:53:22.57219296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=13 01-18 16:53:22.906 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 18 01-18 16:53:23.277 552-650/?D/TaskPersister﹕ removeObsoleteFile: 删除文件=5725_task.xml 01-1816:53:23.278 552-650/?D/TaskPersister:removeObsoleteFile:删除文件=5725_task_thumbnail.png 01-18 16:53:23.33019296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=32 01-18 16:53:23.680 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 18 01-18 16:53:24.03219296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=20 01-18 16:53:24.399 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 31 01-18 16:53:24.77719296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=26 01-18 16:53:25.148 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 23 01-18 16:53:25.47319296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=12 01-18 16:53:25.804 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 14 01-18 16:53:26.14219296-19370/com.example.scott.coloursquares I/game_screen﹕绘制时间=14 01-18 16:53:26.467 19296-19370/com.example.scott.coloursquaresI/game_screen﹕绘制时间 = 14

01-18 16:53:20.221 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:20.589 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:20.919 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.250 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.581 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.911 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.241 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.572 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.906 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:23.277 552-650/? D/TaskPersister﹕ removeObsoleteFile: deleting file=5725_task.xml 01-18 16:53:23.278 552-650/? D/TaskPersister﹕ removeObsoleteFile: deleting file=5725_task_thumbnail.png 01-18 16:53:23.330 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 32 01-18 16:53:23.680 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:24.032 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 20 01-18 16:53:24.399 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 31 01-18 16:53:24.777 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 26 01-18 16:53:25.148 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 23 01-18 16:53:25.473 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 12 01-18 16:53:25.804 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:26.142 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:26.467 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14

推荐答案

SurfaceView 有两个部分,Surface 和 View.视图集成到所有其他 UI 元素中,并且可以与标准组件分层.您可以创建一个自定义视图并使用 Canvas 在其上进行绘制.在所有最近的设备上,这将利用硬件加速,尽管 并非所有画布 API 已实现.

SurfaceView has two parts, the Surface and the View. The View integrates into all the other UI elements, and can be layered with standard components. You can create a custom View and draw on it with a Canvas. On all recent devices, that will take advantage of hardware acceleration, though not all of the Canvas API is implemented.

Surface 是一个完全独立的层,它在具有渲染的 View 元素的层的上方或下方进行合成.您可以在软件中、使用 Canvas 或使用 OpenGL ES 在 GPU 上绘制它.GLSurfaceView 是 SurfaceView 的包装器,负责处理 EGL 设置和一些线程问题,但没有什么特别之处.有关将 GLES 与普通 SurfaceView 和 TextureView 一起使用的示例,请参阅 Grafika,它有一个包含所有 EGL 的方便库东西处理好了.

The Surface is a completely independent layer that is composited above or below the layer that has the rendered View elements. You can draw on it in software, using Canvas, or with the GPU by using OpenGL ES. GLSurfaceView is a wrapper around SurfaceView that takes care of EGL setup and some threading issues, but there's nothing special about it. For examples of using GLES with plain SurfaceView and TextureView, see Grafika, which has a handy library with all the EGL stuff taken care of.

画布在 Surface 上的渲染不是硬件加速的.随着屏幕变大(逐像素),渲染变得越来越昂贵.Nexus 4 的 CPU+总线速度与像素数的比率相当不错.在 Nexus 4 上流畅的动画在 Nexus 10 上会有些卡顿.(尝试 Grafika多表面测试",选择弹跳",在日志中查看帧速率.)

Canvas rendering onto a Surface is not hardware accelerated. As screens get larger (pixel-wise), rendering becomes increasingly expensive. The Nexus 4 has a pretty good ratio of CPU+bus speed to pixel count. Animation that would be smooth on the Nexus 4 would choke a bit on the Nexus 10. (Try the Grafika "multi-surface test", select "bounce", watch the frame rate in the log.)

为了获得良好的性能,并为自己节省大量工作,研究开源图形或游戏引擎可能是值得的.或者从一个现有的小型项目开始,例如 Android Breakout.

To get good performance, and save yourself a lot of work, it might be worth your while to investigate an open-source graphics or game engine. Or start with a small existing project like Android Breakout.

这篇关于SurfaceView 绘制性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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