c - SDL2 渲染在基于 Rockchip 的板上不超过 30 FPS
问题描述
我正在尝试在 Rock64 ARM 板上实现动画,该板具有带有 Mali GPU 的 Rockchip RK3328。我正在使用 SDL2,但我遇到了低帧率。所以我写了一些测试代码:
#include <SDL2/SDL.h>
#include <stdbool.h>
// generates a texture with the given color and the output's size
SDL_Texture *colorTexture(SDL_Renderer *renderer,
unsigned char r, unsigned char g, unsigned char b) {
int w, h, pitch;
SDL_GetRendererOutputSize(renderer, &w, &h);
SDL_Texture *ret = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, w, h);
void *pixels;
SDL_LockTexture(ret, NULL, &pixels, &pitch);
for (int y = 0; y < h; ++y) {
unsigned char *cur = pixels + y * pitch;
for (int x = 0; x < w; ++x) {
*cur++ = 255; *cur++ = b; *cur++ = g; *cur++ = r;
}
}
SDL_UnlockTexture(ret);
return ret;
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_OPENGL, &window, &renderer);
SDL_Texture *blue = colorTexture(renderer, 0, 0, 255);
SDL_Texture *red = colorTexture(renderer, 255, 0, 0);
int w, h;
SDL_GetWindowSize(window, &w, &h);
bool running = true;
SDL_Texture *first = blue, *second = red;
uint32_t start = SDL_GetTicks();
int frameCount = 0;
while (running) {
SDL_Event evt;
for (int x = 0; x < w; ++x) {
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_QUIT) {
running = false;
goto after_animation;
}
}
SDL_Rect firstRect = {.x = 0, .y = 0, .w = x, .h = h},
secondRect = {.x = x, .y = 0, .w = w - x, .h = h};
SDL_RenderCopy(renderer, first, &firstRect, &firstRect);
SDL_RenderCopy(renderer, second, &secondRect, &secondRect);
SDL_RenderPresent(renderer);
frameCount++;
uint32_t cur = SDL_GetTicks();
if (cur - start >= 1000) {
printf("%d FPS\n", frameCount);
frameCount = 0;
start = cur;
}
}
after_animation:;
SDL_Texture *tmp = first; first = second; second = tmp;
}
SDL_Quit();
}
根据这里的帖子,即使是 4k 输出(我的显示器是 4k 电视),该板也能够达到高于 60 FPS 的帧速率。但是,我的测试代码仅报告 30 FPS。它只渲染两个与屏幕大小完全相同的纹理,并水平滚动显示左侧的一个纹理和右侧的一个纹理。我相信我应该能够达到超过 30 FPS 的帧速率。如何加快渲染速度?
解决方案
推荐阅读
- javascript - 是否可以监视 HTML 元素中的事件侦听器以了解其中一个被删除的时间?
- office-ui-fabric - 在源代码中,是否有可能像主题构建器那样基于原色创建完整的主题调色板?
- html - 带有下拉菜单的 CSS 水平滚动菜单
- reactjs - 使用钩子对对象进行简单的 setState
- javascript - 有没有办法避免重复代码?
- python - 如何从 Python 列表中弹出多个随机元素?
- xero-api - 使用唯一 ID 识别发票中的产品 - Xero API
- java - 如何在三星 Galaxy S10 或 S10+ 中测试接近传感器?
- css - 锚标记中的 Svg 图标可单击,但在 Microsoft Edge 或 IE 11 中不起作用
- c - 从已知地址访问数组