iPhone Core Graphics too slow?

Started by
3 comments, last by Dynx 14 years ago
Hi all, Without testing or anything whatsoever, I developed my entire framework around core graphics on iPhone. I set up a timer for 60 Hz and every frame I reset the view and draw the following images: 320x480 (4x - background) Here is my draw function that is called every time timer is fired:

- (void)drawRect:(CGRect)rect 
{
	if( !context ) 
		context = UIGraphicsGetCurrentContext();
	
	//Draw background
	CGContextDrawImage(context, CGRectMake(0,0,320, 480), background1Image);
 	CGContextDrawImage(context, CGRectMake(0,0,320, 480), background2Image);
	CGContextDrawImage(context, CGRectMake(0,0,320, 480), background3Image);
	CGContextDrawImage(context, CGRectMake(0,0,320, 480), background4Image);
}

I removed all interaction code, everything and the game runs around 20 Hz. If I draw colored rectangles instead of images from PNGs, it runs around 55Hz. Is this expected (as in you can't make a 2D game with textures using CG on iPhone) or am I doing something terribly wrong?
Advertisement
That is not completely unexpected given how you are drawing.

CGContextDrawImage is inherently slow. It must reprocess and scale your image.

It doesn't store those processed results, so it must do a bunch of image processing every time you call it.


Consider using an approach that only processes the images once. Some alternatives may be loading them as textures so the graphics card does the work, or storing them in the final format and using a draw function that does not perform any additional processing.
Even if you did this complete in OpenGL it would struggle a bit. The iPhone's main problem is fillrate, so actually drawing to the pixels is slow. You are drawing the entire screen 4 times each frame - thats ALOT of pixels. Any reason you can't combine the layers? or split them into seperate smaller images so that each pixel is only drawn once or twice?

The 2nd slowest thing on the iphone is alphablending ... and if your drawing 4 images on top of each other I assume they're blended (this is why the flag colour quads are so much faster). So all in all you've done the two worst things you can do to an iphone - lots of overdraw, and lots of blending.

There really isn't much you can do but redesign your game to make it use fewer layers with less blending.
Yeah. the whole CoreGraphics stuff isn't really meant to be inside a draw loop. If you want to do a draw loop style game use OpenGL ES instead. The Core Graphics library is only meant to occasionally update the frame and you only ever re-render when something has changed. Basically App instead of Game. Games will almost always be OpenGL ES.

-me
Thanks for all the replies.

Working with UIImageView's solved the problem. So as you guys said, placing an image into a UIView makes the GPU draw it once and cache the result.

I have my frames back again!

This topic is closed to new replies.

Advertisement