gfx/skia/trunk/src/gpu/gl/angle/SkANGLEGLContext.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/gl/angle/SkANGLEGLContext.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2012 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +#include "gl/SkANGLEGLContext.h"
    1.13 +
    1.14 +SkANGLEGLContext::AutoContextRestore::AutoContextRestore() {
    1.15 +    fOldEGLContext = eglGetCurrentContext();
    1.16 +    fOldDisplay = eglGetCurrentDisplay();
    1.17 +    fOldSurface = eglGetCurrentSurface(EGL_DRAW);
    1.18 +
    1.19 +}
    1.20 +
    1.21 +SkANGLEGLContext::AutoContextRestore::~AutoContextRestore() {
    1.22 +    if (NULL != fOldDisplay) {
    1.23 +        eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
    1.24 +    }
    1.25 +}
    1.26 +
    1.27 +///////////////////////////////////////////////////////////////////////////////
    1.28 +
    1.29 +SkANGLEGLContext::SkANGLEGLContext()
    1.30 +    : fContext(EGL_NO_CONTEXT)
    1.31 +    , fDisplay(EGL_NO_DISPLAY)
    1.32 +    , fSurface(EGL_NO_SURFACE) {
    1.33 +}
    1.34 +
    1.35 +SkANGLEGLContext::~SkANGLEGLContext() {
    1.36 +    this->destroyGLContext();
    1.37 +}
    1.38 +
    1.39 +void SkANGLEGLContext::destroyGLContext() {
    1.40 +    if (fDisplay) {
    1.41 +        eglMakeCurrent(fDisplay, 0, 0, 0);
    1.42 +
    1.43 +        if (fContext) {
    1.44 +            eglDestroyContext(fDisplay, fContext);
    1.45 +            fContext = EGL_NO_CONTEXT;
    1.46 +        }
    1.47 +
    1.48 +        if (fSurface) {
    1.49 +            eglDestroySurface(fDisplay, fSurface);
    1.50 +            fSurface = EGL_NO_SURFACE;
    1.51 +        }
    1.52 +
    1.53 +        //TODO should we close the display?
    1.54 +        fDisplay = EGL_NO_DISPLAY;
    1.55 +    }
    1.56 +}
    1.57 +
    1.58 +const GrGLInterface* SkANGLEGLContext::createGLContext() {
    1.59 +
    1.60 +    fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    1.61 +
    1.62 +    EGLint majorVersion;
    1.63 +    EGLint minorVersion;
    1.64 +    eglInitialize(fDisplay, &majorVersion, &minorVersion);
    1.65 +
    1.66 +    EGLint numConfigs;
    1.67 +    static const EGLint configAttribs[] = {
    1.68 +        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
    1.69 +        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
    1.70 +        EGL_RED_SIZE, 8,
    1.71 +        EGL_GREEN_SIZE, 8,
    1.72 +        EGL_BLUE_SIZE, 8,
    1.73 +        EGL_ALPHA_SIZE, 8,
    1.74 +        EGL_NONE
    1.75 +    };
    1.76 +
    1.77 +    EGLConfig surfaceConfig;
    1.78 +    eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
    1.79 +
    1.80 +    static const EGLint contextAttribs[] = {
    1.81 +        EGL_CONTEXT_CLIENT_VERSION, 2,
    1.82 +        EGL_NONE
    1.83 +    };
    1.84 +    fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
    1.85 +
    1.86 +
    1.87 +    static const EGLint surfaceAttribs[] = {
    1.88 +            EGL_WIDTH, 1,
    1.89 +            EGL_HEIGHT, 1,
    1.90 +            EGL_NONE
    1.91 +        };
    1.92 +    fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
    1.93 +
    1.94 +    eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
    1.95 +
    1.96 +    const GrGLInterface* interface = GrGLCreateANGLEInterface();
    1.97 +    if (NULL == interface) {
    1.98 +        SkDebugf("Could not create ANGLE GL interface!\n");
    1.99 +        this->destroyGLContext();
   1.100 +        return NULL;
   1.101 +    }
   1.102 +
   1.103 +    return interface;
   1.104 +}
   1.105 +
   1.106 +void SkANGLEGLContext::makeCurrent() const {
   1.107 +    if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
   1.108 +        SkDebugf("Could not set the context.\n");
   1.109 +    }
   1.110 +}
   1.111 +
   1.112 +void SkANGLEGLContext::swapBuffers() const {
   1.113 +    if (!eglSwapBuffers(fDisplay, fSurface)) {
   1.114 +        SkDebugf("Could not complete eglSwapBuffers.\n");
   1.115 +    }
   1.116 +}

mercurial