blob: 8561419883366ff7c493156bef48eb7629c3ac33 [file] [log] [blame]
[email protected]a37d7ff2014-01-17 21:31:001// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gl/gl_version_info.h"
6
sieverse3b17fd52015-01-29 04:00:537#include "base/strings/string_number_conversions.h"
cwallez3b2dfed2016-06-27 20:03:278#include "base/strings/string_split.h"
sieverse3b17fd52015-01-29 04:00:539#include "base/strings/string_tokenizer.h"
[email protected]a37d7ff2014-01-17 21:31:0010#include "base/strings/string_util.h"
11
kbrb1cca002015-04-11 02:55:2012namespace {
13
14bool DesktopCoreCommonCheck(
15 bool is_es, unsigned major_version, unsigned minor_version) {
16 return (!is_es &&
17 ((major_version == 3 && minor_version >= 2) ||
18 major_version > 3));
19}
20
21}
22
kylechar7a463842016-05-26 14:46:1223namespace gl {
[email protected]a37d7ff2014-01-17 21:31:0024
cwallez3b2dfed2016-06-27 20:03:2725GLVersionInfo::GLVersionInfo(const char* version_str,
26 const char* renderer_str,
kbrc9f0e10c2015-03-31 19:49:1227 const char* extensions_str)
cwallez3b2dfed2016-06-27 20:03:2728 : GLVersionInfo() {
29 std::set<std::string> extensions;
30 if (extensions_str) {
31 auto split = base::SplitString(extensions_str, " ", base::KEEP_WHITESPACE,
32 base::SPLIT_WANT_NONEMPTY);
33 extensions.insert(split.begin(), split.end());
34 }
35 Initialize(version_str, renderer_str, extensions);
kbrc9f0e10c2015-03-31 19:49:1236}
37
cwallez3b2dfed2016-06-27 20:03:2738GLVersionInfo::GLVersionInfo(const char* version_str,
39 const char* renderer_str,
kbrc9f0e10c2015-03-31 19:49:1240 const std::set<std::string>& extensions)
cwallez3b2dfed2016-06-27 20:03:2741 : GLVersionInfo() {
42 Initialize(version_str, renderer_str, extensions);
kbrc9f0e10c2015-03-31 19:49:1243}
44
cwallez3b2dfed2016-06-27 20:03:2745GLVersionInfo::GLVersionInfo()
[email protected]a37d7ff2014-01-17 21:31:0046 : is_es(false),
sieverse3b17fd52015-01-29 04:00:5347 is_angle(false),
junov0c614142016-07-21 18:56:0248 is_mesa(false),
sieverse3b17fd52015-01-29 04:00:5349 major_version(0),
50 minor_version(0),
zmo9192d282016-04-29 18:22:5851 is_es2(false),
kbrc9f0e10c2015-03-31 19:49:1252 is_es3(false),
cwallez3b2dfed2016-06-27 20:03:2753 is_desktop_core_profile(false),
54 is_es3_capable(false) {}
55
56void GLVersionInfo::Initialize(const char* version_str,
57 const char* renderer_str,
58 const std::set<std::string>& extensions) {
[email protected]a37d7ff2014-01-17 21:31:0059 if (version_str) {
kbrc9f0e10c2015-03-31 19:49:1260 ParseVersionString(version_str, &major_version, &minor_version,
zmo9192d282016-04-29 18:22:5861 &is_es, &is_es2, &is_es3);
[email protected]a37d7ff2014-01-17 21:31:0062 }
[email protected]17a961192014-02-14 15:20:5263 if (renderer_str) {
brettw95509312015-07-16 23:57:3364 is_angle = base::StartsWith(renderer_str, "ANGLE",
65 base::CompareCase::SENSITIVE);
junov0c614142016-07-21 18:56:0266 is_mesa = base::StartsWith(renderer_str, "Mesa",
67 base::CompareCase::SENSITIVE);
[email protected]17a961192014-02-14 15:20:5268 }
cwallez3b2dfed2016-06-27 20:03:2769 is_desktop_core_profile =
70 DesktopCoreCommonCheck(is_es, major_version, minor_version) &&
71 extensions.find("GL_ARB_compatibility") == extensions.end();
72 is_es3_capable = IsES3Capable(extensions);
73}
74
75bool GLVersionInfo::IsES3Capable(
76 const std::set<std::string>& extensions) const {
77 auto has_extension = [&extensions](std::string extension) -> bool {
78 return extensions.find(extension) != extensions.end();
79 };
80
81 // Version ES3 capable without extensions needed.
82 if (IsAtLeastGLES(3, 0) || IsAtLeastGL(4, 2)) {
83 return true;
84 }
85
zmo05f50f9b2016-10-29 00:40:2986 // Don't try supporting ES3 on ES2, or desktop before 3.3.
87 if (is_es || !IsAtLeastGL(3, 3)) {
cwallez3b2dfed2016-06-27 20:03:2788 return false;
89 }
90
zmo05f50f9b2016-10-29 00:40:2991 bool has_transform_feedback =
92 (IsAtLeastGL(4, 0) || has_extension("GL_ARB_transform_feedback2"));
kbr2bd1f052016-12-05 20:41:1993
94 // This code used to require the GL_ARB_gpu_shader5 extension in order to
95 // have support for dynamic indexing of sampler arrays, which was
96 // optionally supported in ESSL 1.00. However, since this is expressly
97 // forbidden in ESSL 3.00, and some desktop drivers (specifically
98 // Mesa/Gallium on AMD GPUs) don't support it, we no longer require it.
99
zmo05f50f9b2016-10-29 00:40:29100 // tex storage is available in core spec since GL 4.2.
zmo7ec10212016-10-26 23:27:40101 bool has_tex_storage = has_extension("GL_ARB_texture_storage");
zmo05f50f9b2016-10-29 00:40:29102
103 // TODO(cwallez) check for texture related extensions. See crbug.com/623577
104
kbr2bd1f052016-12-05 20:41:19105 return (has_transform_feedback && has_tex_storage);
[email protected]a37d7ff2014-01-17 21:31:00106}
107
kbrc9f0e10c2015-03-31 19:49:12108void GLVersionInfo::ParseVersionString(const char* version_str,
109 unsigned* major_version,
110 unsigned* minor_version,
111 bool* is_es,
zmo9192d282016-04-29 18:22:58112 bool* is_es2,
kbrc9f0e10c2015-03-31 19:49:12113 bool* is_es3) {
114 // Make sure the outputs are always initialized.
115 *major_version = 0;
116 *minor_version = 0;
117 *is_es = false;
zmo9192d282016-04-29 18:22:58118 *is_es2 = false;
kbrc9f0e10c2015-03-31 19:49:12119 *is_es3 = false;
zmo9a7b17e2015-05-06 00:22:33120 if (!version_str)
121 return;
brettw8e2106d2015-08-11 19:30:22122 std::string lstr(base::ToLowerASCII(version_str));
kbrc9f0e10c2015-03-31 19:49:12123 *is_es = (lstr.length() > 12) && (lstr.substr(0, 9) == "opengl es");
124 if (*is_es)
125 lstr = lstr.substr(10, 3);
xinghua.cao49993312016-06-22 07:21:50126 base::StringTokenizer tokenizer(lstr.begin(), lstr.end(), ". ");
kbrc9f0e10c2015-03-31 19:49:12127 unsigned major, minor;
128 if (tokenizer.GetNext() &&
129 base::StringToUint(tokenizer.token_piece(), &major)) {
130 *major_version = major;
131 if (tokenizer.GetNext() &&
132 base::StringToUint(tokenizer.token_piece(), &minor)) {
133 *minor_version = minor;
134 }
135 }
zmo9192d282016-04-29 18:22:58136 if (*is_es && *major_version == 2)
137 *is_es2 = true;
kbrc9f0e10c2015-03-31 19:49:12138 if (*is_es && *major_version == 3)
139 *is_es3 = true;
martina.kollarova9709e1c2016-03-07 09:28:11140 DCHECK(major_version != 0);
kbrc9f0e10c2015-03-31 19:49:12141}
142
kylechar7a463842016-05-26 14:46:12143} // namespace gl