1 2/* 3 * Copyright 2012 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#include "gl/SkGLContext.h" 10#import <OpenGLES/EAGL.h> 11 12#define EAGLCTX ((EAGLContext*)(fEAGLContext)) 13 14namespace { 15 16class IOSGLContext : public SkGLContext { 17public: 18 IOSGLContext(); 19 ~IOSGLContext() override; 20 void makeCurrent() const override; 21 void swapBuffers() const override; 22 23private: 24 void destroyGLContext(); 25 26 void* fEAGLContext; 27}; 28 29IOSGLContext::IOSGLContext() 30 : fEAGLContext(NULL) { 31 32 fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 33 [EAGLContext setCurrentContext:EAGLCTX]; 34 35 fGL.reset(GrGLCreateNativeInterface()); 36 if (NULL == fGL.get()) { 37 SkDebugf("Failed to create gl interface"); 38 this->destroyGLContext(); 39 return; 40 } 41 if (!fGL->validate()) { 42 SkDebugf("Failed to validate gl interface"); 43 this->destroyGLContext(); 44 return; 45 } 46} 47 48IOSGLContext::~IOSGLContext() { 49 this->destroyGLContext(); 50} 51 52void IOSGLContext::destroyGLContext() { 53 fGL.reset(NULL); 54 if (fEAGLContext) { 55 if ([EAGLContext currentContext] == EAGLCTX) { 56 [EAGLContext setCurrentContext:nil]; 57 } 58 [EAGLCTX release]; 59 fEAGLContext = NULL; 60 } 61} 62 63 64void IOSGLContext::makeCurrent() const { 65 if (![EAGLContext setCurrentContext:EAGLCTX]) { 66 SkDebugf("Could not set the context.\n"); 67 } 68} 69 70void IOSGLContext::swapBuffers() const { } 71 72} // anonymous namespace 73 74SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { 75 if (kGL_GrGLStandard == forcedGpuAPI) { 76 return NULL; 77 } 78 IOSGLContext* ctx = SkNEW(IOSGLContext); 79 if (!ctx->isValid()) { 80 SkDELETE(ctx); 81 return NULL; 82 } 83 return ctx; 84} 85 86