首页 > 解决方案 > 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 的帧速率。如何加快渲染速度?

标签: cperformancesdl-2frame-rate

解决方案


推荐阅读