Core Animation slow down
Core Animation slow down
- Subject: Core Animation slow down
- From: <email@hidden>
- Date: Mon, 23 Mar 2009 07:59:28 -0600
<pre>Hi all. I had some previous questions about Core Animation, this is
not related to that at all (so don't take the previously mentioned code and
methods into consideration). </pre><pre>I have a transparent window, in
which I create a view, and a root layer, and from then on just create and
manage sublayers directly (code for that below). </pre><pre>My problem
is when I first run the app, it's very smooth (looking like 60fps). But
when I leave it running for a while (sometimes after a couple of minutes,
sometimes after half an hour) it slows down, really really down (e.g.
roughly 1fps).</pre><pre> </pre><pre>My setup is very simple: I am
setting up a timer: [NSTimer
scheduledTimerWithTimeInterval:5 target:self selector:@selector(animateFired:) userInfo:nil repeats:YES]; </pre><pre>animateFired
does nothing other than applies simple random implicit animation to a
couple of layers (rotation, position, scale etc).</pre><pre>My CALayers are
are drawn via delegates which load (in the delegate init) and display PDF
content (in the drawLayer - code below).</pre><pre>I can see from the
console that the drawLayer is only being called once (during app init) as
expected.</pre><pre>I have no other code running and have no leaks (nothing
is allocated after init methods, the only code that runs is the timer
callback which simply sets layer properties). </pre><pre> </pre><pre>I
cannot understand why this slow down is, and why sometimes it's after a
couple minutes, and sometimes comes in a lot later. </pre><pre>Also
this does not happen on my dev machine, but 3 other machines I've tested on
(which all have similar but slightly lower spec).</pre><pre>I am about to
install xcode on the other machines as well, but was wondering if I am
missing anything (e.g. about window/view creation, other deep system issues
I need to care about. )?</pre><pre> </pre><pre>window & view
creation:</pre><p style="font: normal normal normal 10px/normal Monaco;
margin: 0px">- (<span style="color:
#aa0d91">void</span>)applicationDidFinishLaunching:(<span style="color:
#5c2699">NSNotification</span>*)aNotification {</p><p style="font: normal
normal normal 10px/normal Monaco; color: #c41a16; margin: 0px"><span
style="color: #000000"><span class="Apple-tab-span" style="white-space:
pre"> </span></span><span style="color: #2e0d6e">NSLog</span><span
style="color:
#000000">(</span>@"applicationDidFinishLaunching"<span
style="color: #000000">);</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span>[[</span><span style="color:
#3f6e74">TransparentWindow</span><span style="color: #000000">
</span>alloc<span style="color: #000000">] </span><span style="color:
#26474b">initWithContentRect</span><span style="color:
#000000">:[[</span><span style="color: #5c2699">NSScreen</span><span
style="color: #000000"> </span>mainScreen<span style="color: #000000">]
</span>visibleFrame<span style="color:
#000000">]];</span></p><pre>} </pre><p style="font: normal normal
normal 10px/normal Monaco; margin: 0px">-(<span style="color:
#aa0d91">id</span>) initWithContentRect:(<span style="color:
#5c2699">NSRect</span>)windowRect {</p><p style="font: normal normal normal
10px/normal Monaco; color: #c41a16; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span></span><span style="color: #2e0d6e">NSLog</span><span
style="color:
#000000">(</span>@"TransparentWindow::initWithContentRect"<span
style="color: #000000">);</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span></span><span style="color: #aa0d91">if</span><span
style="color: #000000">(</span><span style="color:
#aa0d91">self</span><span style="color: #000000"> = [</span><span
style="color: #aa0d91">super</span><span style="color: #000000">
</span>initWithContentRect<span style="color: #000000">:windowRect
</span>styleMask<span style="color:
#000000">:</span>NSBorderlessWindowMask<span class="Apple-style-span"
style="color: #000000"> </span>backing<span style="color:
#000000">:</span>NSBackingStoreBuffered<span style="color: #000000">
</span>defer<span style="color: #000000">:</span><span style="color:
#aa0d91">NO</span><span style="color: #000000">]) {</span></p><p
style="font: normal normal normal 10px/normal Monaco; color: #2e0d6e;
margin: 0px"><span style="color: #000000"><span class="Apple-tab-span"
style="white-space: pre"> </span>[</span><span style="color:
#aa0d91">self</span><span style="color: #000000"> </span>setOpaque<span
style="color: #000000">:</span><span style="color: #aa0d91">NO</span><span
style="color: #000000">];</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span>[</span><span style="color: #aa0d91">self</span><span
style="color: #000000"> </span>setBackgroundColor<span style="color:
#000000">:[</span><span style="color: #5c2699">NSColor</span><span
style="color: #000000"> </span>clearColor<span style="color:
#000000">]]; </span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span>[</span><span style="color: #aa0d91">self</span><span
style="color: #000000"> </span>setMovableByWindowBackground<span
style="color: #000000">:</span><span style="color: #aa0d91">NO</span><span
style="color: #000000">]; </span></p><p style="font: normal normal
normal 10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span>[</span><span style="color: #aa0d91">self</span><span
style="color: #000000"> </span>makeKeyAndOrderFront<span style="color:
#000000">:</span><span style="color: #aa0d91">nil</span><span style="color:
#000000">];</span></p><p style="font: normal normal normal 10px/normal
Monaco; color: #2e0d6e; margin: 0px"><span style="color: #000000"><span
class="Apple-tab-span" style="white-space: pre"> </span>[</span><span
style="color: #aa0d91">self</span><span style="color: #000000">
</span>setAcceptsMouseMovedEvents<span style="color: #000000">:</span><span
style="color: #aa0d91">YES</span><span style="color:
#000000">];</span></p><p style="font: normal normal normal 10px/normal
Monaco; min-height: 14px; margin: 0px"><span class="Apple-tab-span"
style="white-space: pre"> </span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span>[</span><span style="color: #aa0d91">self</span><span
style="color: #000000"> </span>setContentView<span style="color:
#000000">:[[[</span><span style="color: #5c2699">NSView</span><span
style="color: #000000"> </span>alloc<span style="color: #000000">]
</span>initWithFrame<span style="color: #000000">:</span>NSMakeRect<span
style="color: #000000">(</span><span style="color: #1c00cf">0</span><span
style="color: #000000">, </span><span style="color: #1c00cf">0</span><span
style="color: #000000">, windowRect.</span><span style="color:
#5c2699">size</span><span style="color: #000000">.</span><span
style="color: #5c2699">width</span><span style="color: #000000">,
windowRect.</span><span style="color: #5c2699">size</span><span
style="color: #000000">.</span><span style="color:
#5c2699">height</span><span style="color: #000000">)]
</span>autorelease<span style="color: #000000">]];</span></p><p
style="font: normal normal normal 10px/normal Monaco; margin: 0px"><span
class="Apple-tab-span" style="white-space: pre"> </span>}</p><p
style="font: normal normal normal 10px/normal Monaco; color: #c41a16;
margin: 0px"><span style="color: #000000"><span class="Apple-tab-span"
style="white-space: pre"> </span></span><span style="color:
#2e0d6e">NSLog</span><span style="color:
#000000">(</span>@"/TransparentWindow::initWithContentRect"<span
style="color: #000000">);</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #aa0d91; margin: 0px"><span style="color:
#000000"><span class="Apple-tab-span" style="white-space:
pre"> </span></span>return<span style="color: #000000"> </span>self<span
style="color: #000000">;</span></p><pre><span class="Apple-style-span"
style="font-family: Monaco; font-size: 10px; white-space: normal">}<span
class="Apple-tab-span" style="white-space: pre"> </span></span>
</pre><pre>Creating root layer:</pre><pre><p style="font: normal normal
normal 10px/normal Monaco; margin: 0px"><span style="color:
#5c2699">NSSize</span> windowSize<span class="Apple-tab-span"
style="white-space: pre"> </span>= [[<span style="color:
#5c2699">NSApplication</span> <span style="color:
#2e0d6e">sharedApplication</span>] <span style="color:
#2e0d6e">keyWindow</span>].frame.<span style="color:
#5c2699">size</span>;</p><p style="font: normal normal normal 10px/normal
Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#3f6e74">rootView</span><span style="color: #000000"><span
class="Apple-tab-span" style="white-space: pre"> </span>= [[[</span><span
style="color: #5c2699">NSApplication</span><span style="color: #000000">
</span>sharedApplication<span style="color: #000000">]
</span>keyWindow<span style="color: #000000">] </span>contentView<span
style="color: #000000">];</span></p><p style="font: normal normal normal
10px/normal Monaco; margin: 0px"><span style="color:
#3f6e74">rootView</span>.wantsLayer<span class="Apple-tab-span"
style="white-space: pre"> </span>= <span style="color:
#aa0d91">YES</span>;</p><p style="font: normal normal normal 10px/normal
Monaco; min-height: 14px; margin: 0px"> </p><p style="font: normal
normal normal 10px/normal Monaco; min-height: 14px; margin: 0px"><span
style="color: #5c2699">CATransform3D</span> sublayerTransform = <span
style="color: #2e0d6e">CATransform3DMakeTranslation</span>(windowSize.<span
style="color: #5c2699">width</span>/<span style="color: #1c00cf">2</span>,
windowSize.<span style="color: #5c2699">height</span>/<span style="color:
#1c00cf">2</span>, <span style="color: #1c00cf">0</span>); // offset
(0, 0) to center of layer</p><p style="font: normal normal normal
10px/normal Monaco; color: #007400; margin: 0px"><span style="color:
#000000">sublayerTransform.</span><span style="color:
#5c2699">m34</span><span style="color: #000000"> = -</span><span
style="color: #1c00cf">.0003</span><span style="color: #000000">;<span
class="Apple-tab-span" style="white-space: pre"> </span></span>//
perspective. bigger is more</p><p style="font: normal normal normal
10px/normal Monaco; margin: 0px"><span style="color:
#3f6e74">rootView</span>.layer.sublayerTransform<span
class="Apple-tab-span" style="white-space: pre"> </span>=
sublayerTransform;</p><p style="font: normal normal normal 10px/normal
Monaco; min-height: 14px; margin: 0px"><span class="Apple-style-span"
style="font-family: Verdana; font-size: 12px"> </span></p><span
class="Apple-style-span" style="white-space: normal"><pre
style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size:
12px">CALayer Delegate drawRect: </pre><p style="font: normal normal
normal 10px/normal Monaco; margin: 0px">-(<span style="color:
#aa0d91">void</span>)drawLayer:(<span style="color:
#5c2699">CALayer</span> *)layer inContext:(<span style="color:
#5c2699">CGContextRef</span>)context {</p><p style="font: normal normal
normal 10px/normal Monaco; color: #c41a16; margin: 0px"><span style="color:
#2e0d6e"><span class="Apple-style-span" style="color: #000000">
</span>NSLog</span><span style="color:
#000000">(</span>@"PDFLayerDelegate::drawLayer %@"<span
style="color: #000000">, </span><span style="color:
#3f6e74">filename</span><span style="color: #000000">);</span></p><p
style="font: normal normal normal 10px/normal Monaco; min-height: 14px;
margin: 0px"><span class="Apple-style-span" style="color: #2e0d6e"><span
style="color: #5c2699"><span class="Apple-style-span" style="color:
#000000"> </span>CGAffineTransform</span><span
style="color: #000000"> m<span class="Apple-tab-span"
style="white-space:
pre"> </span>= </span>CGPDFPageGetDrawingTransform<span
style="color: #000000">(</span><span style="color:
#3f6e74">page</span><span style="color:
#000000">, </span>kCGPDFArtBox<span style="color:
#000000">, </span>CGContextGetClipBoundingBox<span style="color:
#000000">(context), </span><span style="color: #1c00cf">0</span><span
style="color: #000000">, </span><span style="color:
#aa0d91">NO</span><span style="color: #000000">);</span></span></p><p
style="font: normal normal normal 10px/normal Monaco; color: #2e0d6e;
margin: 0px"><span style="color: #000000">
</span>CGContextSaveGState<span style="color:
#000000">(context);</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"> </span>CGContextConcatCTM<span style="color:
#000000">(context, m);</span></p><p style="font: normal normal normal
10px/normal Monaco; color: #2e0d6e; margin: 0px"><span style="color:
#000000"> </span>CGContextClipToRect<span style="color:
#000000">(context, </span>CGPDFPageGetBoxRect<span style="color:
#000000">(</span><span style="color: #3f6e74">page</span><span
style="color: #000000">, </span>kCGPDFArtBox<span style="color:
#000000">));</span><span style="color: #007400">// 4</span></p><p
style="font: normal normal normal 10px/normal Monaco; color: #2e0d6e;
margin: 0px"><span style="color: #000000">
</span>CGContextDrawPDFPage<span style="color:
#000000">(context, </span><span style="color:
#3f6e74">page</span><span style="color: #000000">);</span></p><p
style="font: normal normal normal 10px/normal Monaco; color: #2e0d6e;
margin: 0px"><span style="color: #000000">
</span>CGContextRestoreGState<span style="color:
#000000">(context);</span></p><p style="font: normal normal normal
10px/normal Monaco; margin: 0px">}</p></span><p style="font: normal normal
normal 10px/normal Monaco; min-height: 14px; margin: 0px"><span
class="Apple-style-span" style="font-family: Verdana; font-size:
12px"> </span> </p> </pre>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden