15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2011 Google Inc.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Use of this source code is governed by a BSD-style license that can be
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * found in the LICENSE file.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "gl/SkNativeGLContext.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "AvailabilityMacros.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fOldCGLContext = CGLGetCurrentContext();
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CGLSetCurrentContext(fOldCGLContext);
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SkNativeGLContext::SkNativeGLContext()
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : fContext(NULL) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SkNativeGLContext::~SkNativeGLContext() {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    this->destroyGLContext();
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SkNativeGLContext::destroyGLContext() {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (NULL != fContext) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CGLReleaseContext(fContext);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
34
35const GrGLInterface* SkNativeGLContext::createGLContext() {
36    SkASSERT(NULL == fContext);
37
38    CGLPixelFormatAttribute attributes[] = {
39#if MAC_OS_X_VERSION_10_7
40        kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
41#endif
42        kCGLPFADoubleBuffer,
43        (CGLPixelFormatAttribute)0
44    };
45    CGLPixelFormatObj pixFormat;
46    GLint npix;
47
48    CGLChoosePixelFormat(attributes, &pixFormat, &npix);
49
50    if (NULL == pixFormat) {
51        SkDebugf("CGLChoosePixelFormat failed.");
52        return NULL;
53    }
54
55    CGLCreateContext(pixFormat, NULL, &fContext);
56    CGLReleasePixelFormat(pixFormat);
57
58    if (NULL == fContext) {
59        SkDebugf("CGLCreateContext failed.");
60        return NULL;
61    }
62
63    CGLSetCurrentContext(fContext);
64
65    const GrGLInterface* interface = GrGLCreateNativeInterface();
66    if (NULL == interface) {
67        SkDebugf("Context could not create GL interface.\n");
68        this->destroyGLContext();
69        return NULL;
70    }
71
72    return interface;
73}
74
75void SkNativeGLContext::makeCurrent() const {
76    CGLSetCurrentContext(fContext);
77}
78
79void SkNativeGLContext::swapBuffers() const {
80    CGLFlushDrawable(fContext);
81}
82