Home | History | Annotate | Download | only in mac
      1 
      2 /*
      3  * Copyright 2011 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 #include "SkTypes.h"
      9 #if defined(SK_BUILD_FOR_MAC)
     10 
     11 #include "gl/SkGLContext.h"
     12 #include "AvailabilityMacros.h"
     13 
     14 #include <OpenGL/OpenGL.h>
     15 #include <dlfcn.h>
     16 
     17 namespace {
     18 class MacGLContext : public SkGLContext {
     19 public:
     20     MacGLContext();
     21     ~MacGLContext() override;
     22 
     23 private:
     24     void destroyGLContext();
     25 
     26     void onPlatformMakeCurrent() const override;
     27     void onPlatformSwapBuffers() const override;
     28     GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
     29 
     30     CGLContextObj fContext;
     31     void* fGLLibrary;
     32 };
     33 
     34 MacGLContext::MacGLContext()
     35     : fContext(nullptr)
     36     , fGLLibrary(RTLD_DEFAULT) {
     37     CGLPixelFormatAttribute attributes[] = {
     38 #if MAC_OS_X_VERSION_10_7
     39         kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
     40 #endif
     41         kCGLPFADoubleBuffer,
     42         (CGLPixelFormatAttribute)0
     43     };
     44     CGLPixelFormatObj pixFormat;
     45     GLint npix;
     46 
     47     CGLChoosePixelFormat(attributes, &pixFormat, &npix);
     48 
     49     if (nullptr == pixFormat) {
     50         SkDebugf("CGLChoosePixelFormat failed.");
     51         return;
     52     }
     53 
     54     CGLCreateContext(pixFormat, nullptr, &fContext);
     55     CGLReleasePixelFormat(pixFormat);
     56 
     57     if (nullptr == fContext) {
     58         SkDebugf("CGLCreateContext failed.");
     59         return;
     60     }
     61 
     62     CGLSetCurrentContext(fContext);
     63 
     64     SkAutoTUnref<const GrGLInterface> gl(GrGLCreateNativeInterface());
     65     if (nullptr == gl.get()) {
     66         SkDebugf("Context could not create GL interface.\n");
     67         this->destroyGLContext();
     68         return;
     69     }
     70     if (!gl->validate()) {
     71         SkDebugf("Context could not validate GL interface.\n");
     72         this->destroyGLContext();
     73         return;
     74     }
     75 
     76     fGLLibrary = dlopen(
     77         "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
     78         RTLD_LAZY);
     79 
     80     this->init(gl.detach());
     81 }
     82 
     83 MacGLContext::~MacGLContext() {
     84     this->teardown();
     85     this->destroyGLContext();
     86 }
     87 
     88 void MacGLContext::destroyGLContext() {
     89     if (fContext) {
     90         CGLReleaseContext(fContext);
     91         fContext = nullptr;
     92     }
     93     if (RTLD_DEFAULT != fGLLibrary) {
     94         dlclose(fGLLibrary);
     95     }
     96 }
     97 
     98 void MacGLContext::onPlatformMakeCurrent() const {
     99     CGLSetCurrentContext(fContext);
    100 }
    101 
    102 void MacGLContext::onPlatformSwapBuffers() const {
    103     CGLFlushDrawable(fContext);
    104 }
    105 
    106 GrGLFuncPtr MacGLContext::onPlatformGetProcAddress(const char* procName) const {
    107     return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName));
    108 }
    109 
    110 } // anonymous namespace
    111 
    112 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI, SkGLContext* shareContext) {
    113     SkASSERT(!shareContext);
    114     if (shareContext) {
    115         return nullptr;
    116     }
    117 
    118     if (kGLES_GrGLStandard == forcedGpuAPI) {
    119         return nullptr;
    120     }
    121     MacGLContext* ctx = new MacGLContext;
    122     if (!ctx->isValid()) {
    123         delete ctx;
    124         return nullptr;
    125     }
    126     return ctx;
    127 }
    128 
    129 #endif//defined(SK_BUILD_FOR_MAC)
    130