简体   繁体   English

为什么chromecast在动画帧之间需要很长时间

[英]Why does chromecast take so long between animation frames

I have a game I'm developing, that runs quite smoothly on chrome on my Mac, but very slowly on my chromecast. 我有一个正在开发的游戏,在我的Mac上使用chrome运行得相当顺利,但在我的chromecast上却非常慢。 I've optimized the JS quite a bit already. 我已经对JS进行了相当的优化。

I assumed it was just the low powered hardware in the chromecast, combined with the slowish JS. 我认为它只是chromecast中的低功耗硬件,再加上缓慢的JS。

But investigating, using the performance object in JS it seems that the delay between the animation frames being called is much longer than my code takes. 但是调查一下,使用JS中的性能对象,似乎被调用的动画帧之间的延迟比我的代码要长得多。

 Court.prototype.update = function () { if (!window.court.paused) { if (window.debug) { console.log('time since last update ' + (performance.now() - window.start) + ' ms'); } window.start = performance.now(); window.court.draw(); // my drawing routing var end = performance.now(); if (window.debug) { console.log('to exit court.draw() took ' + (end - window.start) + ' ms'); } // reschedule next animation update window.requestAnimationFrame(window.court.update); } }; 

When I run that code and follow the console output from the chromecast I get this: 当我运行该代码并按照chromecast的控制台输出时,我得到:

time since last update 89.75099999998201 ms 自上次更新以来的时间89.75099999998201 ms

court.draw() took 0.984999999665888 ms court.draw()花了0.984999999665888毫秒

time since last update 89.35899999960384 ms 自上次更新以来的时间89.35899999960384 ms

court.draw() took 29.77499999997235 ms court.draw()花了29.77499999997235毫秒

time since last update 106.37199999973382 ms 自上次更新以来的时间106.37199999973382 ms

court.draw() took 1.5410000000920263 ms court.draw()花了1.5410000000920263 ms

time since last update 93.46499999992375 ms 自上次更新以来的时间93.46499999992375 ms

court.draw() took 0.3149999997767736 ms court.draw()花了0.3149999997767736毫秒

time since last update 91.99499999977706 ms 自上次更新以来的时间91.99499999977706 ms

court.draw() took 0.31399999988934724 ms court.draw()花了0.31399999988934724毫秒

time since last update 126.3730000000578 ms 自上次更新以来的时间126.3730000000578 ms

court.draw() took 9.191000000100757 ms court.draw()花了9.191000000100757 ms

time since last update 104.55799999999726 ms 自上次更新以来的时间104.55799999999726 ms

court.draw() took 0.3160000001080334 ms court.draw()花了0.3160000001080334毫秒

time since last update 99.06599999976606 ms 自上次更新以来的时间99.06599999976606 ms

court.draw() took 0.3130000000091968 ms court.draw()耗时0.3130000000091968 ms

time since last update 94.06499999977677 ms 自上次更新以来的时间94.06499999977677 ms

court.draw() took 0.3140000003404566 ms court.draw()花了0.3140000003404566 ms

time since last update 88.65700000023935 ms 自上次更新以来的时间88.65700000023935 ms

So, my drawing routine takes between 1 and 30ms, but the Animation Frame is only called about every 100ms, to give a maximum refresh rate of 10fps. 因此,我的绘图例程需要1到30毫秒,但动画帧大约每100毫秒调用一次,最大刷新率为10fps。

Is there any way to get chromecast to reduce the refresh rate? 有没有办法让chromecast降低刷新率?

We have been able to get 30FPS on Chromecast using requestAnimationFrame. 我们已经能够使用requestAnimationFrame在Chromecast上获得30FPS。 It is very important to optimize your code for the device. 优化设备代码非常重要。 Re-use objects aggressively, do not allocate objects or add new properties in the game loop. 积极地重用对象,不在游戏循环中分配对象或添加新属性。

I would recommend starting with just a basic requestAnimationFrame handler without any code to get the baseline performance. 我建议从一个基本的requestAnimationFrame处理程序开始,没有任何代码来获得基线性能。 Then start adding your animation logic and use the Chrome dev tools to measure and find the bottlenecks. 然后开始添加动画逻辑并使用Chrome开发工具来衡量和找到瓶颈。

I'm pretty close to giving up now, concluding that Chromecast is slow, full-stop. 我现在非常接近放弃,认为Chromecast很慢,全程停止。

I've removed all my drawing code and have a routine that calculates a couple of thing only, and a couple of calls to methods on JS classes that do very little. 我已经删除了所有的绘图代码,并且只有一个例程来计算一些东西,并且对JS类上的方法进行了几次调用。

Timing it and the time between frames I get a repeating cycle of three frames, with a time between frames of 10ms, 10ms and 30ms - averaging 50ms - or approx 60fps which is OK. 定时和帧之间的时间我得到三帧的重复周期,帧间隔10ms,10ms和30ms - 平均50ms - 或大约60fps,这是可以的。

court.draw() took 5.6939999999485735 ms Time since last frame 10.18 ms court.draw()取自5.6939999999485735 ms自上一帧起的时间为10.18 ms

court.draw() took 5.745999999817286 ms Time since last frame 10.55 ms court.draw()取自5.745999999817286 ms自上一帧以来的时间10.55毫秒

court.draw() took 5.739999999605061 ms Time since last frame 29.22 ms court.draw()花了5.739999999605061 ms自上一帧29.22 ms以来的时间

Just clearing and drawing a very small rectangle representing a ball each frame bumps the time up to 25ms in my code, and 70ms between frames....making it unbearably slow... 只需清除并绘制一个代表球的非常小的矩形,每帧在我的代码中将时间提高到25ms,在帧之间提高70ms ......使它变得难以忍受......

With no tools to get any further insight into why it's so slow, I feel like I'm just wasting my time now.. 没有任何工具可以进一步了解为什么它如此缓慢,我觉得我现在只是在浪费时间..

I have instrumented to code to measure main calls, and also to enable/disable drawing calls (calls to context.ClearRect() and context.FillRect() for the canvas) 我已经检测了代码来测量主调用,还启用/禁用绘图调用(对canvas的调用context.ClearRect()和context.FillRect())

Conclusions: 结论:

When not drawing: 不绘图时:

My code takes about 15-16ms (need to improve that a bit!) Refresh Period between frames is about 20ms so there is 4-5ms of overhead per frame even when not drawing... 我的代码需要大约15-16ms(需要稍微提高一点!)帧之间的刷新周期大约是20ms,因此即使不绘制,每帧也有4-5ms的开销...

When drawing: 绘图时:

My code takes about 28ms --> 12ms extra for calls to drawing methods Refresh Period between frames is about 65ms so there is 37ms overhead per frame when drawing --> 12ms of it in "foreground" in my own code ---> 25ms of it in "background" between calls to requestAnimationFrame() 我的代码需要大约28ms - > 12ms用于调用绘图方法刷新帧之间的间隔大约是65ms所以绘制时每帧有37ms的开销 - >在我自己的代码中的“前景”中> 12ms ---> 25ms在requestAnimationFrame()调用之间的“背景”中

So even if I get my own code down to 10ms (say) I doubt it will reduce the over time to 16ms required for 60fps, as (presumably) the background work is to fill the pixels on the display and there's no way I can reduce that more....I only remove/draw things when strictly necessary already. 因此,即使我将自己的代码缩短到10毫秒(比方说),我怀疑它会将持续时间减少到60fps所需的16ms,因为(可能)背景工作是填充显示器上​​的像素,我无法减少更多....我只在必要时才删除/绘制东西。

I'm drawing 1, 2, or 3 tiny boxes in the canvas, in monochrome....not sure if there are some additional canvas drawing tricks I can try? 我在画布上画了1,2或3个小盒子,单色......不确定是否有一些额外的画布绘制技巧我可以试试?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM