180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/SkNativeGLContext.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkNativeGLContext::AutoContextRestore::AutoContextRestore() {
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fOldAGLContext = aglGetCurrentContext();
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetCurrentContext(fOldAGLContext);
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru///////////////////////////////////////////////////////////////////////////////
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkNativeGLContext::SkNativeGLContext()
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    : fContext(NULL) {
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkNativeGLContext::~SkNativeGLContext() {
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->destroyGLContext();
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkNativeGLContext::destroyGLContext() {
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (fContext) {
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        aglDestroyContext(fContext);
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst GrGLInterface* SkNativeGLContext::createGLContext() {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GLint major, minor;
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // AGLContext ctx;
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglGetVersion(&major, &minor);
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    //SkDebugf("---- agl version %d %d\n", major, minor);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const GLint pixelAttrs[] = {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_RGBA,
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_ACCELERATED,
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        AGL_NONE
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    AGLPixelFormat format = aglChoosePixelFormat(NULL, 0, pixelAttrs);
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == format) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkDebugf("Format could not be found.\n");
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->destroyGLContext();
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fContext = aglCreateContext(format, NULL);
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == fContext) {
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkDebugf("Context could not be created.\n");
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->destroyGLContext();
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglDestroyPixelFormat(format);
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetCurrentContext(fContext);
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const GrGLInterface* interface = GrGLCreateNativeInterface();
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == interface) {
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkDebugf("Context could not create GL interface.\n");
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->destroyGLContext();
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return interface;
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkNativeGLContext::makeCurrent() const {
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    aglSetCurrentContext(fContext);
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
75