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