3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "gl/SkGLContext.h"
9 #include "AvailabilityMacros.h"
11 #include <OpenGL/OpenGL.h>
14 class MacGLContext : public SkGLContext {
17 ~MacGLContext() override;
18 void makeCurrent() const override;
19 void swapBuffers() const override;
22 void destroyGLContext();
24 CGLContextObj fContext;
27 MacGLContext::MacGLContext()
29 CGLPixelFormatAttribute attributes[] = {
30 #if MAC_OS_X_VERSION_10_7
31 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
34 (CGLPixelFormatAttribute)0
36 CGLPixelFormatObj pixFormat;
39 CGLChoosePixelFormat(attributes, &pixFormat, &npix);
41 if (NULL == pixFormat) {
42 SkDebugf("CGLChoosePixelFormat failed.");
46 CGLCreateContext(pixFormat, NULL, &fContext);
47 CGLReleasePixelFormat(pixFormat);
49 if (NULL == fContext) {
50 SkDebugf("CGLCreateContext failed.");
54 CGLSetCurrentContext(fContext);
56 fGL.reset(GrGLCreateNativeInterface());
57 if (NULL == fGL.get()) {
58 SkDebugf("Context could not create GL interface.\n");
59 this->destroyGLContext();
62 if (!fGL->validate()) {
63 SkDebugf("Context could not validate GL interface.\n");
64 this->destroyGLContext();
69 MacGLContext::~MacGLContext() {
70 this->destroyGLContext();
73 void MacGLContext::destroyGLContext() {
76 CGLReleaseContext(fContext);
81 void MacGLContext::makeCurrent() const {
82 CGLSetCurrentContext(fContext);
85 void MacGLContext::swapBuffers() const {
86 CGLFlushDrawable(fContext);
89 } // anonymous namespace
91 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) {
92 if (kGLES_GrGLStandard == forcedGpuAPI) {
95 MacGLContext* ctx = SkNEW(MacGLContext);
96 if (!ctx->isValid()) {