pycairo becomes slower with each call to show_text

51 Views Asked by At

I am rendering to a cairo.ImageSurface using pycairo, version 1.23.0. I display the bitmap using pygame. Most drawing operations as would be expected take a constant amount of time + noise. However cairo.Context.show_text() keeps increasing in time with each call. In particular on frame numbers which seem to somewhat correlate with powers of two, the time taken is excessive. Thus I suspect that something weird with memory management/internal data structures is going on. I have included a basic working example below.

Am I doing something wrong? I know that it's the "toy" font API, but including pango or similar would be overkill for the application.

Plot of draw time

import pygame
import cairo
import numpy as np
from time import perf_counter


pygame.init()
screen = pygame.display.set_mode((1280, 720), pygame.SCALED, vsync=1)

while True:
    img_surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1280, 720)
    ctx = cairo.Context(img_surf)
    ctx.set_source_rgb(.7, .7, .7)
    ctx.paint()

    start = perf_counter()
    ctx.set_source_rgb(.0, .0, .0)
    ctx.set_font_size(30)
    ctx.move_to(20, 50)
    ctx.show_text("Not very fast")
    stop = perf_counter()

    print(f"{stop - start:.07f}")

    img_surf.flush()

    result = np.copy(np.asarray(img_surf.get_data()))
    result = result.reshape((720, 1280, 4))[..., :3][..., ::-1]

    surface = pygame.surfarray.make_surface(np.transpose(result, (1, 0, 2)))
    screen.blit(surface, (0, 0))
    img_surf.finish()

    pygame.display.flip()

    list(pygame.event.get())
0

There are 0 best solutions below