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