1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2010 Linaro Limited
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * Contributor(s):
29  *      Alexandros Frantzis <alexandros.frantzis@linaro.org>
30  *      Heiko Lewin <heiko.lewin@gmx.de>
31  */
32 
33 #include "cairoint.h"
34 #include "cairo-gl-private.h"
35 
36 #include <errno.h>
37 
38 int
_cairo_gl_get_version(void)39 _cairo_gl_get_version (void)
40 {
41     int major, minor;
42     const char *version = (const char *) glGetString (GL_VERSION);
43     const char *dot = version == NULL ? NULL : strchr (version, '.');
44     const char *major_start = dot;
45 
46     /* Sanity check */
47     if (dot == NULL || dot == version || *(dot + 1) == '\0') {
48 	major = 0;
49 	minor = 0;
50     } else {
51 	/* Find the start of the major version in the string */
52 	while (major_start > version && *major_start != ' ')
53 	    --major_start;
54 	major = strtol (major_start, NULL, 10);
55 	minor = strtol (dot + 1, NULL, 10);
56     }
57 
58     return CAIRO_GL_VERSION_ENCODE (major, minor);
59 }
60 
61 
62 cairo_gl_flavor_t
_cairo_gl_degrade_flavor_by_build_features(cairo_gl_flavor_t flavor)63 _cairo_gl_degrade_flavor_by_build_features (cairo_gl_flavor_t flavor) {
64     switch(flavor) {
65     case CAIRO_GL_FLAVOR_DESKTOP:
66 #if CAIRO_HAS_GL_SURFACE
67 	return CAIRO_GL_FLAVOR_DESKTOP;
68 #else
69 	return CAIRO_GL_FLAVOR_NONE;
70 #endif
71 
72     case CAIRO_GL_FLAVOR_ES3:
73 #if CAIRO_HAS_GLESV3_SURFACE
74 	return CAIRO_GL_FLAVOR_ES3;
75 #else
76 	/* intentional fall through: degrade to GLESv2 if GLESv3-surfaces are not available */
77 #endif
78 
79     case CAIRO_GL_FLAVOR_ES2:
80 #if CAIRO_HAS_GLESV2_SURFACE
81 	return CAIRO_GL_FLAVOR_ES2;
82 #else
83 	/* intentional fall through: no OpenGL in first place or no surfaces for it's version */
84 #endif
85 
86     default:
87 	return CAIRO_GL_FLAVOR_NONE;
88     }
89 }
90 
91 cairo_gl_flavor_t
_cairo_gl_get_flavor(void)92 _cairo_gl_get_flavor (void)
93 {
94     const char *version = (const char *) glGetString (GL_VERSION);
95     cairo_gl_flavor_t flavor;
96 
97     if (version == NULL) {
98 	flavor = CAIRO_GL_FLAVOR_NONE;
99     } else if (strstr (version, "OpenGL ES 3") != NULL) {
100 	flavor = CAIRO_GL_FLAVOR_ES3;
101     } else if (strstr (version, "OpenGL ES 2") != NULL) {
102 	flavor = CAIRO_GL_FLAVOR_ES2;
103     } else {
104 	flavor = CAIRO_GL_FLAVOR_DESKTOP;
105     }
106 
107     return _cairo_gl_degrade_flavor_by_build_features(flavor);
108 }
109 
110 unsigned long
_cairo_gl_get_vbo_size(void)111 _cairo_gl_get_vbo_size (void)
112 {
113     unsigned long vbo_size;
114 
115     const char *env = getenv ("CAIRO_GL_VBO_SIZE");
116     if (env == NULL) {
117 	vbo_size = CAIRO_GL_VBO_SIZE_DEFAULT;
118     } else {
119 	errno = 0;
120 	vbo_size = strtol (env, NULL, 10);
121 	assert (errno == 0);
122 	assert (vbo_size > 0);
123     }
124 
125     return vbo_size;
126 }
127 
128 cairo_bool_t
_cairo_gl_has_extension(const char * ext)129 _cairo_gl_has_extension (const char *ext)
130 {
131     const char *extensions = (const char *) glGetString (GL_EXTENSIONS);
132     size_t len = strlen (ext);
133     const char *ext_ptr = extensions;
134 
135     if (unlikely (ext_ptr == NULL))
136 	return 0;
137 
138     while ((ext_ptr = strstr (ext_ptr, ext)) != NULL) {
139 	if (ext_ptr[len] == ' ' || ext_ptr[len] == '\0')
140 	    break;
141 	ext_ptr += len;
142     }
143 
144     return (ext_ptr != NULL);
145 }
146