blob: 5dfc03ca4f28c44eeaee03befc6e41bac28a5c92 [file] [log] [blame]
twiz@google.com59a190b2011-03-14 21:23:01 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
twiz@google.com59a190b2011-03-14 21:23:01 +000018#include "GrTypes.h"
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000019#include "GrGLInterface.h"
20#include "GrGLDefines.h"
twiz@google.com59a190b2011-03-14 21:23:01 +000021
22#include <stdio.h>
23
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000024GrGLInterface* gGLInterface = NULL;
twiz@google.com59a190b2011-03-14 21:23:01 +000025
26void gl_version_from_string(int* major, int* minor,
27 const char* versionString) {
28 if (NULL == versionString) {
29 GrAssert(0);
30 *major = 0;
31 *minor = 0;
32 return;
33 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000034
twiz@google.com59a190b2011-03-14 21:23:01 +000035 int n = sscanf(versionString, "%d.%d", major, minor);
twiz@google.com0f31ca72011-03-18 17:38:11 +000036 if (2 == n) {
37 return;
twiz@google.com59a190b2011-03-14 21:23:01 +000038 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000039
twiz@google.com59a190b2011-03-14 21:23:01 +000040 char profile[2];
twiz@google.com0f31ca72011-03-18 17:38:11 +000041 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
42 major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000043 bool ok = 4 == n;
44 if (!ok) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000045 n = sscanf(versionString, "OpenGL ES %d.%d", major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000046 ok = 2 == n;
47 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000048
twiz@google.com59a190b2011-03-14 21:23:01 +000049 if (!ok) {
50 GrAssert(0);
51 *major = 0;
52 *minor = 0;
53 return;
54 }
twiz@google.com59a190b2011-03-14 21:23:01 +000055}
56
57bool has_gl_extension_from_string(const char* ext,
58 const char* extensionString) {
59 int extLength = strlen(ext);
60
61 while (true) {
62 int n = strcspn(extensionString, " ");
63 if (n == extLength && 0 == strncmp(ext, extensionString, n)) {
64 return true;
65 }
66 if (0 == extensionString[n]) {
67 return false;
68 }
69 extensionString += n+1;
70 }
71
72 return false;
73}
74
twiz@google.com59a190b2011-03-14 21:23:01 +000075
bsalomon@google.com91826102011-03-21 19:51:57 +000076GR_API void GrGLSetGLInterface(GrGLInterface* gl_interface) {
twiz@google.com59a190b2011-03-14 21:23:01 +000077 gGLInterface = gl_interface;
78}
79
bsalomon@google.com91826102011-03-21 19:51:57 +000080GR_API GrGLInterface* GrGLGetGLInterface() {
twiz@google.com59a190b2011-03-14 21:23:01 +000081 return gGLInterface;
82}
83
twiz@google.com59a190b2011-03-14 21:23:01 +000084bool has_gl_extension(const char* ext) {
85 const char* glstr = reinterpret_cast<const char*>(
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000086 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
twiz@google.com59a190b2011-03-14 21:23:01 +000087
88 return has_gl_extension_from_string(ext, glstr);
89}
90
91void gl_version(int* major, int* minor) {
92 const char* v = reinterpret_cast<const char*>(
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000093 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +000094 gl_version_from_string(major, minor, v);
95}