• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
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 package com.badlogic.gdx.graphics.glutils;
18 
19 import com.badlogic.gdx.Application;
20 import com.badlogic.gdx.Gdx;
21 
22 import java.text.NumberFormat;
23 import java.text.ParseException;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 
27 public class GLVersion {
28 
29 	private int majorVersion;
30 	private int minorVersion;
31 	private int releaseVersion;
32 
33 	private final String vendorString;
34 	private final String rendererString;
35 
36 	private final Type type;
37 
38 	private final String TAG = "GLVersion";
39 
GLVersion(Application.ApplicationType appType, String versionString, String vendorString, String rendererString)40 	public GLVersion (Application.ApplicationType appType, String versionString, String vendorString, String rendererString) {
41 		if (appType == Application.ApplicationType.Android) this.type = Type.GLES;
42 		else if (appType == Application.ApplicationType.iOS) this.type = Type.GLES;
43 		else if (appType == Application.ApplicationType.Desktop) this.type = Type.OpenGL;
44 		else if (appType == Application.ApplicationType.Applet) this.type = Type.OpenGL;
45 		else if (appType == Application.ApplicationType.WebGL) this.type = Type.WebGL;
46 		else this.type = Type.NONE;
47 
48 		if (type == Type.GLES) {
49 			//OpenGL<space>ES<space><version number><space><vendor-specific information>.
50 			extractVersion("OpenGL ES (\\d(\\.\\d){0,2})", versionString);
51 		} else if (type == Type.WebGL) {
52 			//WebGL<space><version number><space><vendor-specific information>
53 			extractVersion("WebGL (\\d(\\.\\d){0,2})", versionString);
54 		} else if (type == Type.OpenGL) {
55 			//<version number><space><vendor-specific information>
56 			extractVersion("(\\d(\\.\\d){0,2})", versionString);
57 		} else {
58 			majorVersion = -1;
59 			minorVersion = -1;
60 			releaseVersion = -1;
61 			vendorString = "";
62 			rendererString = "";
63 		}
64 
65 		this.vendorString = vendorString;
66 		this.rendererString = rendererString;
67 	}
68 
extractVersion(String patternString, String versionString)69 	private void extractVersion (String patternString, String versionString) {
70 		Pattern pattern = Pattern.compile(patternString);
71 		Matcher matcher = pattern.matcher(versionString);
72 		boolean found = matcher.find();
73 		if (found) {
74 			String result = matcher.group(1);
75 			String[] resultSplit = result.split("\\.");
76 			majorVersion = parseInt(resultSplit[0], 2);
77 			minorVersion = resultSplit.length < 2 ? 0 : parseInt(resultSplit[1], 0);
78 			releaseVersion = resultSplit.length < 3 ? 0 : parseInt(resultSplit[2], 0);
79 		} else {
80 			Gdx.app.log(TAG, "Invalid version string: " + versionString);
81 			majorVersion = 2;
82 			minorVersion = 0;
83 			releaseVersion = 0;
84 		}
85 	}
86 
87 	/** Forgiving parsing of gl major, minor and release versions as some manufacturers don't adhere to spec **/
88 	private int parseInt (String v, int defaultValue) {
89 		try {
90 			return Integer.parseInt(v);
91 		} catch (NumberFormatException nfe) {
92 			Gdx.app.error("LibGDX GL", "Error parsing number: " + v +", assuming: " + defaultValue);
93 			return defaultValue;
94 		}
95 	}
96 
97 	/** @return what {@link Type} of GL implementation this application has access to, e.g. {@link Type#OpenGL} or {@link Type#GLES}*/
98 	public Type getType () {
99 		return type;
100 	}
101 
102 	/** @return the major version of current GL connection. -1 if running headless */
103 	public int getMajorVersion () {
104 		return majorVersion;
105 	}
106 
107 	/** @return the minor version of the current GL connection. -1 if running headless */
108 	public int getMinorVersion () {
109 		return minorVersion;
110 	}
111 
112 	/** @return the release version of the current GL connection. -1 if running headless */
113 	public int getReleaseVersion () {
114 		return releaseVersion;
115 	}
116 
117 	/** @return the vendor string associated with the current GL connection */
118 	public String getVendorString () {
119 		return vendorString;
120 	}
121 
122 	/** @return the name of the renderer associated with the current GL connection.
123 	 * This name is typically specific to a particular configuration of a hardware platform. */
124 	public String getRendererString () {
125 		return rendererString;
126 	}
127 
128 	/**
129 	 * Checks to see if the current GL connection version is higher, or equal to the provided test versions.
130 	 *
131 	 * @param testMajorVersion the major version to test against
132 	 * @param testMinorVersion the minor version to test against
133 	 * @return true if the current version is higher or equal to the test version
134 	 */
135 	public boolean isVersionEqualToOrHigher (int testMajorVersion, int testMinorVersion) {
136 		return majorVersion > testMajorVersion || (majorVersion == testMajorVersion && minorVersion >= testMinorVersion);
137 	}
138 
139 	/** @return a string with the current GL connection data */
getDebugVersionString()140 	public String getDebugVersionString () {
141 		return "Type: " + type + "\n" +
142 				"Version: " + majorVersion + ":" + minorVersion + ":" + releaseVersion + "\n" +
143 				"Vendor: " + vendorString + "\n" +
144 				"Renderer: " + rendererString;
145 	}
146 
147 	public enum Type {
148 		OpenGL,
149 		GLES,
150 		WebGL,
151 		NONE
152 	}
153 }
154