Add SkNativeGLContext implementation for iOS.

R=caryclark@google.com
Review URL: https://codereview.appspot.com/6589055

git-svn-id: http://skia.googlecode.com/svn/trunk@5767 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/gpu.gyp b/gyp/gpu.gyp
index 94988ad..98ab257 100644
--- a/gyp/gpu.gyp
+++ b/gyp/gpu.gyp
@@ -6,7 +6,7 @@
         ],
       }],
       ['skia_os != "mac"', {
-        'sources/': [ ['exclude', '_mac.(h|cpp)$'],
+        'sources/': [ ['exclude', '_mac.(h|cpp|m|mm)$'],
         ],
       }],
       ['skia_os != "linux"', {
@@ -14,7 +14,7 @@
         ],
       }],
       ['skia_os != "ios"', {
-        'sources/': [ ['exclude', '_iOS.(h|cpp)$'],
+        'sources/': [ ['exclude', '_iOS.(h|cpp|m|mm)$'],
         ],
       }],
       ['skia_os != "android"', {
diff --git a/gyp/gpu.gypi b/gyp/gpu.gypi
index 840abf5..9afddc4 100644
--- a/gyp/gpu.gypi
+++ b/gyp/gpu.gypi
@@ -237,6 +237,7 @@
       '<(skia_src_path)/gpu/gl/win/SkNativeGLContext_win.cpp',
       '<(skia_src_path)/gpu/gl/unix/SkNativeGLContext_unix.cpp',
       '<(skia_src_path)/gpu/gl/android/SkNativeGLContext_android.cpp',
+      '<(skia_src_path)/gpu/gl/iOS/SkNativeGLContext_iOS.mm',
     ],
     'skgr_angle_gl_sources': [
       '<(skia_include_path)/gpu/gl/SkANGLEGLContext.h',
diff --git a/include/gpu/gl/SkNativeGLContext.h b/include/gpu/gl/SkNativeGLContext.h
index 36461ba..52118da 100644
--- a/include/gpu/gl/SkNativeGLContext.h
+++ b/include/gpu/gl/SkNativeGLContext.h
@@ -12,7 +12,6 @@
 
 #if defined(SK_BUILD_FOR_MAC)
     #include <AGL/agl.h>
-
 #elif defined(SK_BUILD_FOR_ANDROID)
     #include <GLES2/gl2.h>
     #include <EGL/egl.h>
@@ -51,6 +50,8 @@
         EGLContext fOldEGLContext;
         EGLDisplay fOldDisplay;
         EGLSurface fOldSurface;
+    #elif defined(SK_BUILD_FOR_IOS)
+        void* fEAGLContext;
     #endif
     };
 
@@ -75,6 +76,8 @@
     EGLContext fContext;
     EGLDisplay fDisplay;
     EGLSurface fSurface;
+#elif defined(SK_BUILD_FOR_IOS)
+    void* fEAGLContext;
 #endif
 };
 
diff --git a/src/gpu/gl/iOS/SkNativeGLContext_iOS.mm b/src/gpu/gl/iOS/SkNativeGLContext_iOS.mm
new file mode 100644
index 0000000..46bd03a
--- /dev/null
+++ b/src/gpu/gl/iOS/SkNativeGLContext_iOS.mm
@@ -0,0 +1,62 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gl/SkNativeGLContext.h"
+#import <OpenGLES/EAGL.h>
+
+#define EAGLCTX ((EAGLContext*)(fEAGLContext))
+
+SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
+    fEAGLContext = [EAGLContext currentContext];
+    if (EAGLCTX) {
+        [EAGLCTX retain];
+    }
+}
+
+SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
+    if (EAGLCTX) {
+        [EAGLContext setCurrentContext:EAGLCTX];
+        [EAGLCTX release];
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+SkNativeGLContext::SkNativeGLContext()
+    : fEAGLContext(NULL) {
+}
+
+SkNativeGLContext::~SkNativeGLContext() {
+    this->destroyGLContext();
+}
+
+void SkNativeGLContext::destroyGLContext() {
+    if ([EAGLContext currentContext] == EAGLCTX) {
+        [EAGLContext setCurrentContext:nil];
+    }
+    [EAGLCTX release];
+}
+
+const GrGLInterface* SkNativeGLContext::createGLContext() {
+    fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
+    [EAGLContext setCurrentContext:EAGLCTX];
+    
+    const GrGLInterface* interface = GrGLCreateNativeInterface();
+    if (!interface) {
+        SkDebugf("Failed to create gl interface");
+        this->destroyGLContext();
+        return NULL;
+    }
+    return interface;
+}
+
+void SkNativeGLContext::makeCurrent() const {
+    if (![EAGLContext setCurrentContext:EAGLCTX]) {
+        SkDebugf("Could not set the context.\n");
+    }
+}