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