开发者

Improving Performance of CALayer Filters

开发者 https://www.devze.com 2023-02-05 16:35 出处:网络
I\'m working on a Cocoa fullscreen application.I am using 1 NSView that has 1 CALayer that has multiple sublayers.Right now for testing - I am using any keystrokes to add dots (20 x 20 ) to the screen

I'm working on a Cocoa fullscreen application. I am using 1 NSView that has 1 CALayer that has multiple sublayers. Right now for testing - I am using any keystrokes to add dots (20 x 20 ) to the screen. This is just for testing of drawing the dots. My issue is that I am using a filter on my dot layers - specifically I am using CIDiscBlur - and once I reach about 30 dots - the drawing of the dots significantly slows down. There can be a 1 - 1.5 second delay between the key press and the appearance of the dot. I have noticed that if I remove setting the CIDisBlur filter on the layers - that there is 开发者_JAVA技巧no slow down.

Are there any best practices or tips I should be using when drawing this many sublayers? Any help would be great.

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIDiscBlur"];
    [blurFilter setDefaults];
    [blurFilter setValue:(id)[NSNumber numberWithFloat:15.0] forKey:@"inputRadius"];

    dotFilters = [[NSArray arrayWithObjects:(id)blurFilter, nil] retain];

    CGColorRef purpleColor = CGColorCreateGenericRGB(0.604, 0.247, 0.463, 1.0);

    CALayer *dot = [[CALayer layer] retain];
    dot.backgroundColor = purpleColor;
    dot.cornerRadius = 15.0f;
    dot.filters = dotFilters;

    NSRect screenRect = [[self.window screen] frame];

    //  10 point border around the screen

    CGFloat width = screenRect.size.width - 20;
    CGFloat height = screenRect.size.height - 20;

    #define ARC4RANDOM_MAX      0x100000000
    width = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * width + 10;
    height = ((CGFloat)arc4random() / ARC4RANDOM_MAX) * height + 10;

    dot.frame = CGRectMake(width, height, 20,20);//30, 30);


    [dot addSublayer:dotsLayer];

I also tried using masksToBounds = YES to see if that helped - but no luck.


You can probably get a performance gain by not using corner radius to make your round layers. While it's a nice little shortcut to just make a round layer in a static context, when you're animating, it will degrade performance significantly. You'd be better off specifying a circular path to a CAShapeLayer or dropping down to Core Graphics and just drawing a circle in the drawInContext call. To test if I'm right, just comment out your call to set the corner radius and apply your filter. See if that speeds things up. If not, then I'm not sure what's up. It may mean you'll have to find a different way to get your effect without a filter. If you'll always have the same look for your dots, you'll can probably "cheat" by using an image.

Best regards.

0

精彩评论

暂无评论...
验证码 换一张
取 消