Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better performance using images with "opaque" property? Why?

i just found something on that site: iphoneexamples.com. Looking to "Display images" i found something new to me.

myImage.opaque = YES; // explicitly opaque for performance

Could someone explain it to me, please? And for which kind (or usecase) of images does it work? When not? Would be great to know. Thanks for your time...

like image 953
geforce Avatar asked Mar 27 '11 19:03

geforce


4 Answers

The iPhone GPU is a tile-based renderer. If an overlaying layer is completely opaque over an entire tile, the GPU can ignore setting up and processing any graphics commands related to the layer underneath for that particular tile, in addition to not having to do compositing of the pixels in that tile.

If your image doesn't cover a complete tile, the GPU will still have to potentially process multiple layers. The size of a tile is implementation dependent, but tiny graphics images are far less likely to cover a tile. Huge images that cover multiple tiles will show the greatest advantage from being opaque.

like image 197
hotpaw2 Avatar answered Nov 11 '22 08:11

hotpaw2


From the View Programming Guide for iOS:

Declare Views as Opaque Whenever Possible

UIKit uses the opaque property of each view to determine whether the view can optimize compositing operations. Setting the value of this property to YES for a custom view tells UIKit that it does not need to render any content behind your view. Less rendering can lead to increased performance for your drawing code and is generally encouraged. Of course, if you set the opaque property to YES, your view must fills its bounds rectangle completely with fully opaque content.

hotpaw2 points out the behind-the-scenes reason for this, which can be found in the OpenGL ES Programming Guide for iOS:

Another advantage of deferred rendering is that it allows the GPU to perform hidden surface removal before fragments are processed. Pixels that are not visible are discarded without sampling textures or performing fragment processing, significantly reducing the calculations that the GPU must perform to render the tile. To gain the most benefit from this feature, draw as much of the frame with opaque content as possible and minimize use of blending, alpha testing, and the discard instruction in GLSL shaders. Because the hardware performs hidden surface removal, it is not necessary for your application to sort primitives from front to back.

like image 40
Brad Larson Avatar answered Nov 11 '22 08:11

Brad Larson


You get better performance when a view or layer is opaque than when it's not. If it's not opaque, the graphics system has to composite that layer with the layers below to produce the final image. If it is opaque, then it's just a matter of copying the pixels to the frame buffer.

like image 3
Caleb Avatar answered Nov 11 '22 07:11

Caleb


A little tricky issue to be aware of is that UIImageView will reset the opaque property to FALSE any time the image property is changed to a new UIImage. You could explicitly check in your code and set the opaque property after any change to the image property, or you could extend UIImageView and provide you own implementation of setImage that sets opaque after calling the super setImage method. I only found out about this by accident.

like image 2
MoDJ Avatar answered Nov 11 '22 06:11

MoDJ