gfx/skia/trunk/src/gpu/gl/GrGLUtil.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/GrGLUtil.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,302 @@
     1.4 +/*
     1.5 + * Copyright 2011 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +
    1.12 +#include "GrGLUtil.h"
    1.13 +#include "SkMatrix.h"
    1.14 +#include <stdio.h>
    1.15 +
    1.16 +void GrGLClearErr(const GrGLInterface* gl) {
    1.17 +    while (GR_GL_NO_ERROR != gl->fFunctions.fGetError()) {}
    1.18 +}
    1.19 +
    1.20 +namespace {
    1.21 +const char *get_error_string(uint32_t err) {
    1.22 +    switch (err) {
    1.23 +    case GR_GL_NO_ERROR:
    1.24 +        return "";
    1.25 +    case GR_GL_INVALID_ENUM:
    1.26 +        return "Invalid Enum";
    1.27 +    case GR_GL_INVALID_VALUE:
    1.28 +        return "Invalid Value";
    1.29 +    case GR_GL_INVALID_OPERATION:
    1.30 +        return "Invalid Operation";
    1.31 +    case GR_GL_OUT_OF_MEMORY:
    1.32 +        return "Out of Memory";
    1.33 +    case GR_GL_CONTEXT_LOST:
    1.34 +        return "Context Lost";
    1.35 +    }
    1.36 +    return "Unknown";
    1.37 +}
    1.38 +}
    1.39 +
    1.40 +void GrGLCheckErr(const GrGLInterface* gl,
    1.41 +                  const char* location,
    1.42 +                  const char* call) {
    1.43 +    uint32_t err = GR_GL_GET_ERROR(gl);
    1.44 +    if (GR_GL_NO_ERROR != err) {
    1.45 +        GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err));
    1.46 +        if (NULL != location) {
    1.47 +            GrPrintf(" at\n\t%s", location);
    1.48 +        }
    1.49 +        if (NULL != call) {
    1.50 +            GrPrintf("\n\t\t%s", call);
    1.51 +        }
    1.52 +        GrPrintf("\n");
    1.53 +    }
    1.54 +}
    1.55 +
    1.56 +namespace {
    1.57 +// Mesa uses a non-standard version string of format: 1.4 Mesa <mesa_major>.<mesa_minor>.
    1.58 +// The mapping of from mesa version to GL version came from here: http://www.mesa3d.org/intro.html
    1.59 +bool get_gl_version_for_mesa(int mesaMajorVersion, int* major, int* minor) {
    1.60 +    switch (mesaMajorVersion) {
    1.61 +        case 2:
    1.62 +        case 3:
    1.63 +        case 4:
    1.64 +        case 5:
    1.65 +        case 6:
    1.66 +            *major = 1;
    1.67 +            *minor = mesaMajorVersion - 1;
    1.68 +            return true;
    1.69 +        case 7:
    1.70 +            *major = 2;
    1.71 +            *minor = 1;
    1.72 +            return true;
    1.73 +        case 8:
    1.74 +            *major = 3;
    1.75 +            *minor = 0;
    1.76 +            return true;
    1.77 +        case 9:
    1.78 +            *major = 3;
    1.79 +            *minor = 1;
    1.80 +            return true;
    1.81 +        case 10:
    1.82 +            *major = 3;
    1.83 +            *minor = 3;
    1.84 +            return true;
    1.85 +        default:
    1.86 +            return false;
    1.87 +    }
    1.88 +}
    1.89 +}
    1.90 +
    1.91 +///////////////////////////////////////////////////////////////////////////////
    1.92 +
    1.93 +#if GR_GL_LOG_CALLS
    1.94 +    bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
    1.95 +#endif
    1.96 +
    1.97 +#if GR_GL_CHECK_ERROR
    1.98 +    bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
    1.99 +#endif
   1.100 +
   1.101 +///////////////////////////////////////////////////////////////////////////////
   1.102 +
   1.103 +GrGLStandard GrGLGetStandardInUseFromString(const char* versionString) {
   1.104 +    if (NULL == versionString) {
   1.105 +        SkDEBUGFAIL("NULL GL version string.");
   1.106 +        return kNone_GrGLStandard;
   1.107 +    }
   1.108 +
   1.109 +    int major, minor;
   1.110 +
   1.111 +    // check for desktop
   1.112 +    int n = sscanf(versionString, "%d.%d", &major, &minor);
   1.113 +    if (2 == n) {
   1.114 +        return kGL_GrGLStandard;
   1.115 +    }
   1.116 +
   1.117 +    // check for ES 1
   1.118 +    char profile[2];
   1.119 +    n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor);
   1.120 +    if (4 == n) {
   1.121 +        // we no longer support ES1.
   1.122 +        return kNone_GrGLStandard;
   1.123 +    }
   1.124 +
   1.125 +    // check for ES2
   1.126 +    n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
   1.127 +    if (2 == n) {
   1.128 +        return kGLES_GrGLStandard;
   1.129 +    }
   1.130 +    return kNone_GrGLStandard;
   1.131 +}
   1.132 +
   1.133 +bool GrGLIsMesaFromVersionString(const char* versionString) {
   1.134 +    int major, minor, mesaMajor, mesaMinor;
   1.135 +    int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
   1.136 +    return 4 == n;
   1.137 +}
   1.138 +
   1.139 +bool GrGLIsChromiumFromRendererString(const char* rendererString) {
   1.140 +    return 0 == strcmp(rendererString, "Chromium");
   1.141 +}
   1.142 +
   1.143 +GrGLVersion GrGLGetVersionFromString(const char* versionString) {
   1.144 +    if (NULL == versionString) {
   1.145 +        SkDEBUGFAIL("NULL GL version string.");
   1.146 +        return 0;
   1.147 +    }
   1.148 +
   1.149 +    int major, minor;
   1.150 +
   1.151 +    // check for mesa
   1.152 +    int mesaMajor, mesaMinor;
   1.153 +    int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
   1.154 +    if (4 == n) {
   1.155 +        if (get_gl_version_for_mesa(mesaMajor, &major, &minor)) {
   1.156 +            return GR_GL_VER(major, minor);
   1.157 +        } else {
   1.158 +            return 0;
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    n = sscanf(versionString, "%d.%d", &major, &minor);
   1.163 +    if (2 == n) {
   1.164 +        return GR_GL_VER(major, minor);
   1.165 +    }
   1.166 +
   1.167 +    char profile[2];
   1.168 +    n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
   1.169 +               &major, &minor);
   1.170 +    if (4 == n) {
   1.171 +        return GR_GL_VER(major, minor);
   1.172 +    }
   1.173 +
   1.174 +    n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
   1.175 +    if (2 == n) {
   1.176 +        return GR_GL_VER(major, minor);
   1.177 +    }
   1.178 +
   1.179 +    return 0;
   1.180 +}
   1.181 +
   1.182 +GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
   1.183 +    if (NULL == versionString) {
   1.184 +        SkDEBUGFAIL("NULL GLSL version string.");
   1.185 +        return 0;
   1.186 +    }
   1.187 +
   1.188 +    int major, minor;
   1.189 +
   1.190 +    int n = sscanf(versionString, "%d.%d", &major, &minor);
   1.191 +    if (2 == n) {
   1.192 +        return GR_GLSL_VER(major, minor);
   1.193 +    }
   1.194 +
   1.195 +    n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
   1.196 +    if (2 == n) {
   1.197 +        return GR_GLSL_VER(major, minor);
   1.198 +    }
   1.199 +
   1.200 +#ifdef SK_BUILD_FOR_ANDROID
   1.201 +    // android hack until the gpu vender updates their drivers
   1.202 +    n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
   1.203 +    if (2 == n) {
   1.204 +        return GR_GLSL_VER(major, minor);
   1.205 +    }
   1.206 +#endif
   1.207 +
   1.208 +    return 0;
   1.209 +}
   1.210 +
   1.211 +GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
   1.212 +    if (NULL != vendorString) {
   1.213 +        if (0 == strcmp(vendorString, "ARM")) {
   1.214 +            return kARM_GrGLVendor;
   1.215 +        }
   1.216 +        if (0 == strcmp(vendorString, "Imagination Technologies")) {
   1.217 +            return kImagination_GrGLVendor;
   1.218 +        }
   1.219 +        if (0 == strncmp(vendorString, "Intel ", 6) || 0 == strcmp(vendorString, "Intel")) {
   1.220 +            return kIntel_GrGLVendor;
   1.221 +        }
   1.222 +        if (0 == strcmp(vendorString, "Qualcomm")) {
   1.223 +            return kQualcomm_GrGLVendor;
   1.224 +        }
   1.225 +    }
   1.226 +    return kOther_GrGLVendor;
   1.227 +}
   1.228 +
   1.229 +GrGLRenderer GrGLGetRendererFromString(const char* rendererString) {
   1.230 +    if (NULL != rendererString) {
   1.231 +        if (0 == strcmp(rendererString, "NVIDIA Tegra 3")) {
   1.232 +            return kTegra3_GrGLRenderer;
   1.233 +        } else if (0 == strcmp(rendererString, "NVIDIA Tegra")) {
   1.234 +            return kTegra2_GrGLRenderer;
   1.235 +        }
   1.236 +    }
   1.237 +    return kOther_GrGLRenderer;
   1.238 +}
   1.239 +
   1.240 +GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
   1.241 +    const GrGLubyte* v;
   1.242 +    GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
   1.243 +    return GrGLGetVersionFromString((const char*) v);
   1.244 +}
   1.245 +
   1.246 +GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
   1.247 +    const GrGLubyte* v;
   1.248 +    GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
   1.249 +    return GrGLGetGLSLVersionFromString((const char*) v);
   1.250 +}
   1.251 +
   1.252 +GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
   1.253 +    const GrGLubyte* v;
   1.254 +    GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
   1.255 +    return GrGLGetVendorFromString((const char*) v);
   1.256 +}
   1.257 +
   1.258 +GrGLRenderer GrGLGetRenderer(const GrGLInterface* gl) {
   1.259 +    const GrGLubyte* v;
   1.260 +    GR_GL_CALL_RET(gl, v, GetString(GR_GL_RENDERER));
   1.261 +    return GrGLGetRendererFromString((const char*) v);
   1.262 +}
   1.263 +
   1.264 +template<> void GrGLGetMatrix<3>(GrGLfloat* dest, const SkMatrix& src) {
   1.265 +    // Col 0
   1.266 +    dest[0] = SkScalarToFloat(src[SkMatrix::kMScaleX]);
   1.267 +    dest[1] = SkScalarToFloat(src[SkMatrix::kMSkewY]);
   1.268 +    dest[2] = SkScalarToFloat(src[SkMatrix::kMPersp0]);
   1.269 +
   1.270 +    // Col 1
   1.271 +    dest[3] = SkScalarToFloat(src[SkMatrix::kMSkewX]);
   1.272 +    dest[4] = SkScalarToFloat(src[SkMatrix::kMScaleY]);
   1.273 +    dest[5] = SkScalarToFloat(src[SkMatrix::kMPersp1]);
   1.274 +
   1.275 +    // Col 2
   1.276 +    dest[6] = SkScalarToFloat(src[SkMatrix::kMTransX]);
   1.277 +    dest[7] = SkScalarToFloat(src[SkMatrix::kMTransY]);
   1.278 +    dest[8] = SkScalarToFloat(src[SkMatrix::kMPersp2]);
   1.279 +}
   1.280 +
   1.281 +template<> void GrGLGetMatrix<4>(GrGLfloat* dest, const SkMatrix& src) {
   1.282 +    // Col 0
   1.283 +    dest[0]  = SkScalarToFloat(src[SkMatrix::kMScaleX]);
   1.284 +    dest[1]  = SkScalarToFloat(src[SkMatrix::kMSkewY]);
   1.285 +    dest[2]  = 0;
   1.286 +    dest[3]  = SkScalarToFloat(src[SkMatrix::kMPersp0]);
   1.287 +
   1.288 +    // Col 1
   1.289 +    dest[4]  = SkScalarToFloat(src[SkMatrix::kMSkewX]);
   1.290 +    dest[5]  = SkScalarToFloat(src[SkMatrix::kMScaleY]);
   1.291 +    dest[6]  = 0;
   1.292 +    dest[7]  = SkScalarToFloat(src[SkMatrix::kMPersp1]);
   1.293 +
   1.294 +    // Col 2
   1.295 +    dest[8]  = 0;
   1.296 +    dest[9]  = 0;
   1.297 +    dest[10] = 1;
   1.298 +    dest[11] = 0;
   1.299 +
   1.300 +    // Col 3
   1.301 +    dest[12] = SkScalarToFloat(src[SkMatrix::kMTransX]);
   1.302 +    dest[13] = SkScalarToFloat(src[SkMatrix::kMTransY]);
   1.303 +    dest[14] = 0;
   1.304 +    dest[15] = SkScalarToFloat(src[SkMatrix::kMPersp2]);
   1.305 +}

mercurial