Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow blitting with combined pixel and surface alpha #1618

Open
mcpalmer1980 opened this issue Apr 4, 2020 · 3 comments
Open

Slow blitting with combined pixel and surface alpha #1618

mcpalmer1980 opened this issue Apr 4, 2020 · 3 comments
Labels
hard A hard challenge to solve Performance Related to the speed or resource usage of the project reproducible confirmed that the issue can be replicated and there are instructions to reproduce the issue SDL2 Surface pygame.Surface

Comments

@mcpalmer1980
Copy link
Contributor

mcpalmer1980 commented Apr 4, 2020

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

@pygame pygame deleted a comment from FakeOG4life Apr 11, 2020
@MyreMylar MyreMylar added Performance Related to the speed or resource usage of the project Surface pygame.Surface labels May 9, 2020
@nthykier
Copy link
Contributor

nthykier commented May 9, 2020

@mcpalmer1980 Can you share the image flyboy.png as well? It will help us debug the issue more reliably.

@mcpalmer1980
Copy link
Contributor Author

Sure here's the file
flyboy

@nthykier
Copy link
Contributor

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

@nthykier nthykier added the reproducible confirmed that the issue can be replicated and there are instructions to reproduce the issue label May 10, 2020
@MyreMylar MyreMylar added hard A hard challenge to solve SDL2 labels May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hard A hard challenge to solve Performance Related to the speed or resource usage of the project reproducible confirmed that the issue can be replicated and there are instructions to reproduce the issue SDL2 Surface pygame.Surface
Projects
None yet
Development

No branches or pull requests

3 participants