1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/gpu/gl/GrGLExtensions.h"
9 #include "src/gpu/ganesh/gl/GrGLDefines.h"
10 #include "src/gpu/ganesh/gl/GrGLUtil.h"
11
12 #include "src/base/SkTSearch.h"
13 #include "src/base/SkTSort.h"
14
15 using namespace skia_private;
16
17 namespace { // This cannot be static because it is used as a template parameter.
extension_compare(const SkString & a,const SkString & b)18 inline bool extension_compare(const SkString& a, const SkString& b) {
19 return strcmp(a.c_str(), b.c_str()) < 0;
20 }
21 } // namespace
22
23 // finds the index of ext in strings or a negative result if ext is not found.
find_string(const TArray<SkString> & strings,const char ext[])24 static int find_string(const TArray<SkString>& strings, const char ext[]) {
25 if (strings.empty()) {
26 return -1;
27 }
28 SkString extensionStr(ext);
29 int idx = SkTSearch<SkString, extension_compare>(&strings.front(),
30 strings.size(),
31 extensionStr,
32 sizeof(SkString));
33 return idx;
34 }
35
GrGLExtensions(const GrGLExtensions & that)36 GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) {
37 *this = that;
38 }
39
operator =(const GrGLExtensions & that)40 GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) {
41 if (this != &that) {
42 fStrings = that.fStrings;
43 fInitialized = that.fInitialized;
44 }
45 return *this;
46 }
47
eat_space_sep_strings(TArray<SkString> * out,const char in[])48 static void eat_space_sep_strings(TArray<SkString>* out, const char in[]) {
49 if (!in) {
50 return;
51 }
52 while (true) {
53 // skip over multiple spaces between extensions
54 while (' ' == *in) {
55 ++in;
56 }
57 // quit once we reach the end of the string.
58 if ('\0' == *in) {
59 break;
60 }
61 // we found an extension
62 size_t length = strcspn(in, " ");
63 out->push_back().set(in, length);
64 in += length;
65 }
66 }
67
init(GrGLStandard standard,GrGLFunction<GrGLGetStringFn> getString,GrGLFunction<GrGLGetStringiFn> getStringi,GrGLFunction<GrGLGetIntegervFn> getIntegerv,GrGLFunction<GrEGLQueryStringFn> queryString,GrEGLDisplay eglDisplay)68 bool GrGLExtensions::init(GrGLStandard standard,
69 GrGLFunction<GrGLGetStringFn> getString,
70 GrGLFunction<GrGLGetStringiFn> getStringi,
71 GrGLFunction<GrGLGetIntegervFn> getIntegerv,
72 GrGLFunction<GrEGLQueryStringFn> queryString,
73 GrEGLDisplay eglDisplay) {
74 fInitialized = false;
75 fStrings.clear();
76
77 if (!getString) {
78 return false;
79 }
80
81 const GrGLubyte* verString = getString(GR_GL_VERSION);
82 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
83 if (GR_GL_INVALID_VER == version) {
84 return false;
85 }
86
87 bool indexed = false;
88 if (GR_IS_GR_GL(standard) || GR_IS_GR_GL_ES(standard)) {
89 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
90 indexed = version >= GR_GL_VER(3, 0);
91 } else if (GR_IS_GR_WEBGL(standard)) {
92 // WebGL (1.0 or 2.0) doesn't natively support glGetStringi, but enscripten adds it in
93 // https://github.com/emscripten-core/emscripten/issues/3472
94 indexed = version >= GR_GL_VER(2, 0);
95 }
96
97 if (indexed) {
98 if (!getStringi || !getIntegerv) {
99 return false;
100 }
101 GrGLint extensionCnt = 0;
102 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
103 fStrings.push_back_n(extensionCnt);
104 for (int i = 0; i < extensionCnt; ++i) {
105 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
106 fStrings[i] = ext;
107 }
108 } else {
109 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
110 if (!extensions) {
111 return false;
112 }
113 eat_space_sep_strings(&fStrings, extensions);
114 }
115 if (queryString) {
116 const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS);
117
118 eat_space_sep_strings(&fStrings, extensions);
119 }
120 if (!fStrings.empty()) {
121 SkTQSort(fStrings.begin(), fStrings.end(), extension_compare);
122 }
123 fInitialized = true;
124 return true;
125 }
126
has(const char ext[]) const127 bool GrGLExtensions::has(const char ext[]) const {
128 SkASSERT(fInitialized);
129 return find_string(fStrings, ext) >= 0;
130 }
131
remove(const char ext[])132 bool GrGLExtensions::remove(const char ext[]) {
133 SkASSERT(fInitialized);
134 int idx = find_string(fStrings, ext);
135 if (idx < 0) {
136 return false;
137 }
138
139 // This is not terribly effecient but we really only expect this function to be called at
140 // most a handful of times when our test programs start.
141 fStrings.removeShuffle(idx);
142 if (idx != fStrings.size()) {
143 SkTInsertionSort(fStrings.begin() + idx, fStrings.size() - idx, extension_compare);
144 }
145 return true;
146 }
147
add(const char ext[])148 void GrGLExtensions::add(const char ext[]) {
149 int idx = find_string(fStrings, ext);
150 if (idx < 0) {
151 // This is not the most effecient approach since we end up looking at all of the
152 // extensions after the add
153 fStrings.emplace_back(ext);
154 SkTInsertionSort(fStrings.begin(), fStrings.size(), extension_compare);
155 }
156 }
157
158 #ifdef SK_ENABLE_DUMP_GPU
159 #include "src/utils/SkJSONWriter.h"
160
dumpJSON(SkJSONWriter * writer) const161 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
162 writer->beginArray();
163 for (int i = 0; i < fStrings.size(); ++i) {
164 writer->appendString(fStrings[i]);
165 }
166 writer->endArray();
167 }
168 #else
dumpJSON(SkJSONWriter * writer) const169 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { }
170 #endif
171