OLD | NEW |
(Empty) | |
| 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 |
| 10 #include "gl/GLContext.h" |
| 11 #include "AvailabilityMacros.h" |
| 12 |
| 13 #include <OpenGL/OpenGL.h> |
| 14 #include <dlfcn.h> |
| 15 |
| 16 namespace { |
| 17 class MacGLContext : public sk_gpu_test::GLContext { |
| 18 public: |
| 19 MacGLContext(); |
| 20 ~MacGLContext() override; |
| 21 |
| 22 private: |
| 23 void destroyGLContext(); |
| 24 |
| 25 void onPlatformMakeCurrent() const override; |
| 26 void onPlatformSwapBuffers() const override; |
| 27 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
| 28 |
| 29 CGLContextObj fContext; |
| 30 void* fGLLibrary; |
| 31 }; |
| 32 |
| 33 MacGLContext::MacGLContext() |
| 34 : fContext(nullptr) |
| 35 , fGLLibrary(RTLD_DEFAULT) { |
| 36 CGLPixelFormatAttribute attributes[] = { |
| 37 #if MAC_OS_X_VERSION_10_7 |
| 38 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core
, |
| 39 #endif |
| 40 kCGLPFADoubleBuffer, |
| 41 (CGLPixelFormatAttribute)0 |
| 42 }; |
| 43 CGLPixelFormatObj pixFormat; |
| 44 GLint npix; |
| 45 |
| 46 CGLChoosePixelFormat(attributes, &pixFormat, &npix); |
| 47 |
| 48 if (nullptr == pixFormat) { |
| 49 SkDebugf("CGLChoosePixelFormat failed."); |
| 50 return; |
| 51 } |
| 52 |
| 53 CGLCreateContext(pixFormat, nullptr, &fContext); |
| 54 CGLReleasePixelFormat(pixFormat); |
| 55 |
| 56 if (nullptr == fContext) { |
| 57 SkDebugf("CGLCreateContext failed."); |
| 58 return; |
| 59 } |
| 60 |
| 61 CGLSetCurrentContext(fContext); |
| 62 |
| 63 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateNativeInterface()); |
| 64 if (nullptr == gl.get()) { |
| 65 SkDebugf("Context could not create GL interface.\n"); |
| 66 this->destroyGLContext(); |
| 67 return; |
| 68 } |
| 69 if (!gl->validate()) { |
| 70 SkDebugf("Context could not validate GL interface.\n"); |
| 71 this->destroyGLContext(); |
| 72 return; |
| 73 } |
| 74 |
| 75 fGLLibrary = dlopen( |
| 76 "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.
dylib", |
| 77 RTLD_LAZY); |
| 78 |
| 79 this->init(gl.release()); |
| 80 } |
| 81 |
| 82 MacGLContext::~MacGLContext() { |
| 83 this->teardown(); |
| 84 this->destroyGLContext(); |
| 85 } |
| 86 |
| 87 void MacGLContext::destroyGLContext() { |
| 88 if (fContext) { |
| 89 CGLReleaseContext(fContext); |
| 90 fContext = nullptr; |
| 91 } |
| 92 if (RTLD_DEFAULT != fGLLibrary) { |
| 93 dlclose(fGLLibrary); |
| 94 } |
| 95 } |
| 96 |
| 97 void MacGLContext::onPlatformMakeCurrent() const { |
| 98 CGLSetCurrentContext(fContext); |
| 99 } |
| 100 |
| 101 void MacGLContext::onPlatformSwapBuffers() const { |
| 102 CGLFlushDrawable(fContext); |
| 103 } |
| 104 |
| 105 GrGLFuncPtr MacGLContext::onPlatformGetProcAddress(const char* procName) const { |
| 106 return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName)); |
| 107 } |
| 108 |
| 109 } // anonymous namespace |
| 110 |
| 111 namespace sk_gpu_test { |
| 112 GLContext* CreatePlatformGLContext(GrGLStandard forcedGpuAPI, GLContext* shareCo
ntext) { |
| 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 } // namespace sk_gpu_test |
OLD | NEW |