| Flutter Impeller |

description_gles.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "impeller/renderer/backend/gles/description_gles.h"
6
7 #include <algorithm>
8 #include <cctype>
9 #include <iomanip>
10 #include <sstream>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 #include "impeller/base/strings.h"
16 #include "impeller/base/validation.h"
17 #include "impeller/renderer/backend/gles/proc_table_gles.h"
18
19 namespace impeller {
20
21 static std::string GetGLString(const ProcTableGLES& gl, GLenum name) {
22 auto str = gl.GetString(name);
23 if (str == nullptr) {
24 return "";
25 }
26 return reinterpret_cast<const char*>(str);
27 }
28
29 static std::string GetGLStringi(const ProcTableGLES& gl,
30 GLenum name,
31 int index) {
32 auto str = gl.GetStringi(name, index);
33 if (str == nullptr) {
34 return "";
35 }
36 return reinterpret_cast<const char*>(str);
37 }
38
39 static bool DetermineIfES(const std::string& version) {
40 return HasPrefix(version, "OpenGL ES");
41 }
42
43 static bool DetermineIfANGLE(const std::string& version) {
44 return version.find("ANGLE") != std::string::npos;
45 }
46
47 static std::optional<Version> DetermineVersion(std::string version) {
48 // Format for OpenGL "OpenGL<space>ES<space><version
49 // number><space><vendor-specific information>".
50 //
51 // Format for OpenGL SL "OpenGL<space>ES<space>GLSL<space>ES<space><version
52 // number><space><vendor-specific information>"
53 //
54 // The prefixes appear to be absent on Desktop GL.
55
56 version = StripPrefix(version, "OpenGL ES ");
57 version = StripPrefix(version, "GLSL ES ");
58
59 if (version.empty()) {
60 return std::nullopt;
61 }
62
63 std::stringstream stream;
64 for (size_t i = 0; i < version.size(); i++) {
65 const auto character = version[i];
66 if (std::isdigit(character) || character == '.') {
67 stream << character;
68 } else {
69 break;
70 }
71 }
72 std::istringstream istream;
73 istream.str(stream.str());
74 std::vector<size_t> version_components;
75 for (std::string version_component;
76 std::getline(istream, version_component, '.');) {
77 version_components.push_back(std::stoul(version_component));
78 }
79 return Version::FromVector(version_components);
80 }
81
82 DescriptionGLES::DescriptionGLES(const ProcTableGLES& gl)
83 : vendor_(GetGLString(gl, GL_VENDOR)),
84 renderer_(GetGLString(gl, GL_RENDERER)),
85 gl_version_string_(GetGLString(gl, GL_VERSION)),
86 sl_version_string_(GetGLString(gl, GL_SHADING_LANGUAGE_VERSION)) {
87 is_es_ = DetermineIfES(gl_version_string_);
88 is_angle_ = DetermineIfANGLE(gl_version_string_);
89
90 auto gl_version = DetermineVersion(gl_version_string_);
91 if (!gl_version.has_value()) {
92 VALIDATION_LOG << "Could not determine GL version.";
93 return;
94 }
95 gl_version_ = gl_version.value();
96
97 // GL_NUM_EXTENSIONS is only available in OpenGL 3+ and OpenGL ES 3+
98 if (gl_version_.IsAtLeast(Version(3, 0, 0))) {
99 int extension_count = 0;
100 gl.GetIntegerv(GL_NUM_EXTENSIONS, &extension_count);
101 for (auto i = 0; i < extension_count; i++) {
102 extensions_.insert(GetGLStringi(gl, GL_EXTENSIONS, i));
103 }
104 } else {
105 const auto extensions = GetGLString(gl, GL_EXTENSIONS);
106 std::stringstream extensions_stream(extensions);
107 std::string extension;
108 while (std::getline(extensions_stream, extension, ' ')) {
109 extensions_.insert(extension);
110 }
111 }
112
113 auto sl_version = DetermineVersion(sl_version_string_);
114 if (!sl_version.has_value()) {
115 VALIDATION_LOG << "Could not determine SL version.";
116 return;
117 }
118 sl_version_ = sl_version.value();
119
120 is_valid_ = true;
121 }
122
123 DescriptionGLES::~DescriptionGLES() = default;
124
125 bool DescriptionGLES::IsValid() const {
126 return is_valid_;
127 }
128
129 std::string DescriptionGLES::GetString() const {
130 if (!IsValid()) {
131 return "Unknown Renderer.";
132 }
133
134 std::vector<std::pair<std::string, std::string>> items;
135
136 items.emplace_back(std::make_pair("Vendor", vendor_));
137 items.emplace_back(std::make_pair("Renderer", renderer_));
138 items.emplace_back(std::make_pair("GL Version", gl_version_string_));
139 items.emplace_back(
140 std::make_pair("Shading Language Version", sl_version_string_));
141 items.emplace_back(
142 std::make_pair("Extensions", std::to_string(extensions_.size())));
143
144 size_t max_width = 0u;
145 for (const auto& item : items) {
146 max_width = std::max(max_width, item.first.size());
147 }
148
149 std::stringstream stream;
150 stream << "OpenGL Renderer:" << std::endl;
151 for (const auto& item : items) {
152 stream << std::setw(max_width + 1) << item.first << ": " << item.second
153 << std::endl;
154 }
155
156 const auto pad = std::string(max_width + 3, ' ');
157 for (const auto& extension : extensions_) {
158 stream << pad << extension << std::endl;
159 }
160
161 return stream.str();
162 }
163
164 Version DescriptionGLES::GetGlVersion() const {
165 return gl_version_;
166 }
167
168 bool DescriptionGLES::IsES() const {
169 return is_es_;
170 }
171
172 bool DescriptionGLES::IsANGLE() const {
173 return is_angle_;
174 }
175
176 bool DescriptionGLES::HasExtension(const std::string& ext) const {
177 return extensions_.find(ext) != extensions_.end();
178 }
179
180 bool DescriptionGLES::HasDebugExtension() const {
181 // Angle just logs calls instead of forwarding debug information to the
182 // backend. This just overwhelms the logs and is of limited use. Disable on
183 // Angle.
184 return HasExtension("GL_KHR_debug") && !IsANGLE();
185 }
186
187 } // namespace impeller
impeller::DescriptionGLES::IsValid
bool IsValid() const
Definition: description_gles.cc:125
impeller::DescriptionGLES::IsES
bool IsES() const
Definition: description_gles.cc:168
impeller::DescriptionGLES::GetGlVersion
Version GetGlVersion() const
Definition: description_gles.cc:164
impeller::DescriptionGLES::IsANGLE
bool IsANGLE() const
Definition: description_gles.cc:172
impeller::DescriptionGLES::DescriptionGLES
DescriptionGLES(const ProcTableGLES &gl)
Definition: description_gles.cc:82
impeller::DescriptionGLES::~DescriptionGLES
~DescriptionGLES()
impeller::DescriptionGLES::GetString
std::string GetString() const
Definition: description_gles.cc:129
impeller::DescriptionGLES::HasExtension
bool HasExtension(const std::string &ext) const
Definition: description_gles.cc:176
impeller::DescriptionGLES::HasDebugExtension
bool HasDebugExtension() const
Returns whether GLES includes the debug extension.
Definition: description_gles.cc:180
impeller::ProcTableGLES
Definition: proc_table_gles.h:281
description_gles.h
impeller
Definition: allocation.cc:12
impeller::GetGLString
static std::string GetGLString(const ProcTableGLES &gl, GLenum name)
Definition: description_gles.cc:21
impeller::DetermineIfES
static bool DetermineIfES(const std::string &version)
Definition: description_gles.cc:39
impeller::DetermineIfANGLE
static bool DetermineIfANGLE(const std::string &version)
Definition: description_gles.cc:43
impeller::HasPrefix
bool HasPrefix(const std::string &string, const std::string &prefix)
Definition: strings.cc:9
impeller::DetermineVersion
static std::optional< Version > DetermineVersion(std::string version)
Definition: description_gles.cc:47
impeller::StripPrefix
std::string StripPrefix(const std::string &string, const std::string &to_strip)
Definition: strings.cc:21
impeller::GetGLStringi
static std::string GetGLStringi(const ProcTableGLES &gl, GLenum name, int index)
Definition: description_gles.cc:29
proc_table_gles.h
strings.h
impeller::Version
Definition: version.h:16
impeller::Version::IsAtLeast
constexpr bool IsAtLeast(const Version &other) const
Definition: version.h:31
impeller::Version::FromVector
static std::optional< Version > FromVector(const std::vector< size_t > &version)
Definition: version.cc:11
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91