Can cairo use SDL_Texture as a render target?

冷暖自知 提交于 2019-12-03 12:37:00

问题


Rendering to an SDL_Surface is possible with Cairo, but my application uses SDL_Renderer and SDL_Texture to take advantage of 2D accelerated rendering.

I am currently creating an SDL_Surface and copying it to a texture with SDL_CreateTextureFromSurface(), but this process is cumbersome and possibly slow (although it's not a bottleneck.) Is there a direct way to draw to a SDL_Texture?


回答1:


I've figured it out. Streaming SDL_Textures can expose the raw pixels in the ARGB8888 format, which is a format Cairo surfaces can also handle. The Cairo API is low level enough to only require the pixel buffer and pitch.

SDL_Texture *texture = SDL_CreateTexture(renderer,
    SDL_PIXELFORMAT_ARGB8888,
    SDL_TEXTUREACCESS_STREAMING,
    width, height);

void *pixels;
int pitch;
SDL_LockTexture(texture, NULL, &pixels, &pitch);
cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data(
    pixels,
    CAIRO_FORMAT_ARGB32,
    width, height, pitch);

paint(cairo_create(cairo_surface));

SDL_UnlockTexture(texture);

How convenient.



来源:https://stackoverflow.com/questions/24316393/can-cairo-use-sdl-texture-as-a-render-target

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!