Skip to content

Slow blitting with combined pixel and surface alpha #1618

Open

Description

@mcpalmer1980

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

Activity

  1. deleted a comment from FakeOG4life on Apr 11, 2020
  2. nthykier commented on May 9, 2020

    @nthykier
    Contributor

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

  3. mcpalmer1980 commented on May 10, 2020

    @mcpalmer1980
    ContributorAuthor

    Sure here's the file
    flyboy

  4. nthykier commented on May 10, 2020

    @nthykier
    Contributor

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

  5. added
    reproducibleconfirmed that the issue can be replicated and there are instructions to reproduce the issue
    on May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformanceRelated to the speed or resource usage of the projectSDL2Surfacepygame.SurfacehardA hard challenge to solvereproducibleconfirmed that the issue can be replicated and there are instructions to reproduce the issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions