Repository navigation
Slow blitting with combined pixel and surface alpha #1618
Copy link
Copy link
Open
Labels
PerformanceRelated to the speed or resource usage of the projectRelated to the speed or resource usage of the projectSDL2Surfacepygame.Surfacepygame.SurfacehardA hard challenge to solveA hard challenge to solvereproducibleconfirmed that the issue can be replicated and there are instructions to reproduce the issueconfirmed that the issue can be replicated and there are instructions to reproduce the issue
Description
I recently discovered that surfaces can use both pixel and surface alpha in pygame2dev6. Curious about performance, I wrote a quick test. My results were somewhat surprising. When combined, the two alphas blit four times slower than either one alone.
Blubberquark suggested I submit an issue, presumably to remind him to take a look at some point. Feel free to close this at any time. This combination wasn't supported in previous releases, and future users hoping for fancy effects with pygame2 will likely use the renderer instead of software surfaces.
Test 0: 1000 blits of image size 1333, 1600
tested Blit no alpha: 68.406ms
tested Blit surface alpha: 464.457ms
tested Blit pixel alpha: 381.999ms
tested Blit pixel and surface alpha: 1925.622ms
Test 1: 1000 blits of image size 1333, 1600
tested Blit no alpha: 63.964ms
tested Blit surface alpha: 451.907ms
tested Blit pixel alpha: 380.308ms
tested Blit pixel and surface alpha: 1929.704ms
Test 2: 1000 blits of image size 1333, 1600
tested Blit no alpha: 64.182ms
tested Blit surface alpha: 457.898ms
tested Blit pixel alpha: 375.786ms
tested Blit pixel and surface alpha: 1927.906ms
Here's the code I used
import time
import pygame
filename = 'flyboy.png'
pygame.display.init()
screen = pygame.display.set_mode((720, 480))
ino_alpha = pygame.image.load(filename).convert()
isurf_alpha = pygame.image.load(filename).convert()
isurf_alpha.set_alpha(100)
ipixel_alpha = pygame.image.load(filename).convert_alpha()
idual_alpha = pygame.image.load(filename).convert_alpha()
idual_alpha.set_alpha(100)
def test(methods, tries = 1000):
for method in methods:
start = time.time()
name = method(tries)
results = (time.time() - start) * 1000
print('tested {}: {}ms'.format(name, round(results, 3)))
def no_alpha(tries):
for t in range(tries):
screen.blit(ino_alpha, (10,10))
return 'Blit no alpha'
def surf_alpha(tries):
for t in range(tries):
screen.blit(isurf_alpha, (10,10))
return 'Blit surface alpha'
def pixel_alpha(tries):
for t in range(tries):
screen.blit(ipixel_alpha, (10,10))
return 'Blit pixel alpha'
def dual_alpha(tries):
for t in range(tries):
screen.blit(idual_alpha, (10,10))
return 'Blit pixel and surface alpha'
for i in range(3):
tries = 1000
w, h = ino_alpha.get_size()
print(f'\nTest {i}: {tries} blits of image size {w}, {h}')
test((no_alpha, surf_alpha, pixel_alpha, dual_alpha), tries)
Related Docs: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
PerformanceRelated to the speed or resource usage of the projectRelated to the speed or resource usage of the projectSDL2Surfacepygame.Surfacepygame.SurfacehardA hard challenge to solveA hard challenge to solvereproducibleconfirmed that the issue can be replicated and there are instructions to reproduce the issueconfirmed that the issue can be replicated and there are instructions to reproduce the issue
Activity
nthykier commentedon May 9, 2020
@mcpalmer1980 Can you share the image
flyboy.pngas well? It will help us debug the issue more reliably.mcpalmer1980 commentedon May 10, 2020
Sure here's the file

nthykier commentedon May 10, 2020
AFAICT, the slowness happens primarily in
SDL_BlitSurface. I am not sure how much we can do about that.