gfx/skia/trunk/src/gpu/gl/android/SkNativeGLContext_android.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/android/SkNativeGLContext_android.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 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 +#include "gl/SkNativeGLContext.h"
    1.12 +
    1.13 +SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
    1.14 +    fOldEGLContext = eglGetCurrentContext();
    1.15 +    fOldDisplay = eglGetCurrentDisplay();
    1.16 +    fOldSurface = eglGetCurrentSurface(EGL_DRAW);
    1.17 +
    1.18 +}
    1.19 +
    1.20 +SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
    1.21 +    if (NULL != fOldDisplay) {
    1.22 +        eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
    1.23 +    }
    1.24 +}
    1.25 +
    1.26 +///////////////////////////////////////////////////////////////////////////////
    1.27 +
    1.28 +SkNativeGLContext::SkNativeGLContext()
    1.29 +    : fContext(EGL_NO_CONTEXT)
    1.30 +    , fDisplay(EGL_NO_DISPLAY)
    1.31 +    , fSurface(EGL_NO_SURFACE) {
    1.32 +}
    1.33 +
    1.34 +SkNativeGLContext::~SkNativeGLContext() {
    1.35 +    this->destroyGLContext();
    1.36 +}
    1.37 +
    1.38 +void SkNativeGLContext::destroyGLContext() {
    1.39 +    if (fDisplay) {
    1.40 +        eglMakeCurrent(fDisplay, 0, 0, 0);
    1.41 +
    1.42 +        if (fContext) {
    1.43 +            eglDestroyContext(fDisplay, fContext);
    1.44 +            fContext = EGL_NO_CONTEXT;
    1.45 +        }
    1.46 +
    1.47 +        if (fSurface) {
    1.48 +            eglDestroySurface(fDisplay, fSurface);
    1.49 +            fSurface = EGL_NO_SURFACE;
    1.50 +        }
    1.51 +
    1.52 +        //TODO should we close the display?
    1.53 +        fDisplay = EGL_NO_DISPLAY;
    1.54 +    }
    1.55 +}
    1.56 +
    1.57 +const GrGLInterface* SkNativeGLContext::createGLContext() {
    1.58 +    static const EGLint kEGLContextAttribsForOpenGL[] = {
    1.59 +        EGL_NONE
    1.60 +    };
    1.61 +
    1.62 +    static const EGLint kEGLContextAttribsForOpenGLES[] = {
    1.63 +        EGL_CONTEXT_CLIENT_VERSION, 2,
    1.64 +        EGL_NONE
    1.65 +    };
    1.66 +
    1.67 +    static const struct {
    1.68 +        const EGLint* fContextAttribs;
    1.69 +        EGLenum fAPI;
    1.70 +        EGLint  fRenderableTypeBit;
    1.71 +        GrGLStandard fStandard;
    1.72 +    } kAPIs[] = {
    1.73 +        {   // OpenGL
    1.74 +            kEGLContextAttribsForOpenGL,
    1.75 +            EGL_OPENGL_API,
    1.76 +            EGL_OPENGL_BIT,
    1.77 +            kGL_GrGLStandard
    1.78 +        },
    1.79 +        {   // OpenGL ES. This seems to work for both ES2 and 3 (when available).
    1.80 +            kEGLContextAttribsForOpenGLES,
    1.81 +            EGL_OPENGL_ES_API,
    1.82 +            EGL_OPENGL_ES2_BIT,
    1.83 +            kGLES_GrGLStandard
    1.84 +        },
    1.85 +    };
    1.86 +
    1.87 +    const GrGLInterface* interface = NULL;
    1.88 +
    1.89 +    for (size_t api = 0; NULL == interface && api < SK_ARRAY_COUNT(kAPIs); ++api) {
    1.90 +        fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    1.91 +
    1.92 +        EGLint majorVersion;
    1.93 +        EGLint minorVersion;
    1.94 +        eglInitialize(fDisplay, &majorVersion, &minorVersion);
    1.95 +
    1.96 +#if 0
    1.97 +        SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
    1.98 +        SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
    1.99 +        SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
   1.100 +        SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
   1.101 +#endif
   1.102 +
   1.103 +        if (!eglBindAPI(kAPIs[api].fAPI)) {
   1.104 +            continue;
   1.105 +        }
   1.106 +
   1.107 +        EGLint numConfigs;
   1.108 +        const EGLint configAttribs[] = {
   1.109 +            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
   1.110 +            EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
   1.111 +            EGL_RED_SIZE, 8,
   1.112 +            EGL_GREEN_SIZE, 8,
   1.113 +            EGL_BLUE_SIZE, 8,
   1.114 +            EGL_ALPHA_SIZE, 8,
   1.115 +            EGL_NONE
   1.116 +        };
   1.117 +
   1.118 +        EGLConfig surfaceConfig;
   1.119 +        if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
   1.120 +            SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
   1.121 +            continue;
   1.122 +        }
   1.123 +
   1.124 +        fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs);
   1.125 +        if (EGL_NO_CONTEXT == fContext) {
   1.126 +            SkDebugf("eglCreateContext failed.  EGL Error: 0x%08x\n", eglGetError());
   1.127 +            continue;
   1.128 +        }
   1.129 +
   1.130 +        static const EGLint kSurfaceAttribs[] = {
   1.131 +            EGL_WIDTH, 1,
   1.132 +            EGL_HEIGHT, 1,
   1.133 +            EGL_NONE
   1.134 +        };
   1.135 +
   1.136 +        fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
   1.137 +        if (EGL_NO_SURFACE == fSurface) {
   1.138 +            SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
   1.139 +            this->destroyGLContext();
   1.140 +            continue;
   1.141 +        }
   1.142 +
   1.143 +        if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
   1.144 +            SkDebugf("eglMakeCurrent failed.  EGL Error: 0x%08x\n", eglGetError());
   1.145 +            this->destroyGLContext();
   1.146 +            continue;
   1.147 +        }
   1.148 +
   1.149 +        interface = GrGLCreateNativeInterface();
   1.150 +        if (NULL == interface) {
   1.151 +            SkDebugf("Failed to create gl interface.\n");
   1.152 +            this->destroyGLContext();
   1.153 +            continue;
   1.154 +        }
   1.155 +
   1.156 +        if (!interface->validate()) {
   1.157 +            interface->unref();
   1.158 +            interface = NULL;
   1.159 +            this->destroyGLContext();
   1.160 +        }
   1.161 +    }
   1.162 +
   1.163 +    return interface;
   1.164 +}
   1.165 +
   1.166 +void SkNativeGLContext::makeCurrent() const {
   1.167 +    if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
   1.168 +        SkDebugf("Could not set the context.\n");
   1.169 +    }
   1.170 +}
   1.171 +
   1.172 +void SkNativeGLContext::swapBuffers() const {
   1.173 +    if (!eglSwapBuffers(fDisplay, fSurface)) {
   1.174 +        SkDebugf("Could not complete eglSwapBuffers.\n");
   1.175 +    }
   1.176 +}

mercurial