Fix GL attach/detach in Mac SampleApp
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 3 Apr 2012 18:06:20 +0000 (18:06 +0000)
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 3 Apr 2012 18:06:20 +0000 (18:06 +0000)
Review URL: http://codereview.appspot.com/5984043/

git-svn-id: http://skia.googlecode.com/svn/trunk@3589 2bbb7eff-a529-9590-31e7-b0007b416f81

src/views/mac/SkNSView.mm

index a6aa59c..30488ab 100644 (file)
@@ -225,6 +225,7 @@ static SkKey raw2key(UInt32 raw)
 ///////////////////////////////////////////////////////////////////////////////
 #include <OpenGL/OpenGL.h>
 
+namespace { 
 CGLContextObj createGLContext() {
     GLint major, minor;
     CGLGetVersion(&major, &minor);
@@ -254,6 +255,7 @@ CGLContextObj createGLContext() {
     CGLSetCurrentContext(ctx);
     return ctx;
 }
+}
 
 - (void)viewDidMoveToWindow {
     [super viewDidMoveToWindow];
@@ -267,10 +269,13 @@ CGLContextObj createGLContext() {
 }
 - (bool)attach:(SkOSWindow::SkBackEndTypes)attachType {
     if (nil == fGLContext) {
-        fGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:createGLContext()];
+        CGLContextObj ctx = createGLContext();
+        fGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:ctx];
+        CGLReleaseContext(ctx);
         if (NULL == fGLContext) {
             return false;
         }
+        [fGLContext setView:self];
     }
     
     [fGLContext makeCurrentContext];
@@ -283,10 +288,13 @@ CGLContextObj createGLContext() {
 }
 
 - (void)detach {
-    [fGLContext clearDrawable];
+    [fGLContext release];
+    fGLContext = nil;
 }
 
 - (void)present {
-    [fGLContext flushBuffer];
+    if (nil != fGLContext) {
+        [fGLContext flushBuffer];
+    }
 }
 @end