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/SkMakeUnique.h"
13 #include "src/core/SkTSearch.h"
14 #include "src/core/SkTSort.h"
15 #include "src/utils/SkJSONWriter.h"
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 }
22 
23 // 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[])24 static int find_string(const SkTArray<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.count(),
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(SkTArray<SkString> * out,const char in[])48 static void eat_space_sep_strings(SkTArray<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.reset();
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         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
122         SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
123     }
124     fInitialized = true;
125     return true;
126 }
127 
has(const char ext[]) const128 bool GrGLExtensions::has(const char ext[]) const {
129     SkASSERT(fInitialized);
130     return find_string(fStrings, ext) >= 0;
131 }
132 
remove(const char ext[])133 bool GrGLExtensions::remove(const char ext[]) {
134     SkASSERT(fInitialized);
135     int idx = find_string(fStrings, ext);
136     if (idx < 0) {
137         return false;
138     }
139 
140     // This is not terribly effecient but we really only expect this function to be called at
141     // most a handful of times when our test programs start.
142     fStrings.removeShuffle(idx);
143     if (idx != fStrings.count()) {
144         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
145         SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), cmp);
146     }
147     return true;
148 }
149 
add(const char ext[])150 void GrGLExtensions::add(const char ext[]) {
151     int idx = find_string(fStrings, ext);
152     if (idx < 0) {
153         // This is not the most effecient approach since we end up looking at all of the
154         // extensions after the add
155         fStrings.emplace_back(ext);
156         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
157         SkTInsertionSort(&fStrings.front(), &fStrings.back(), cmp);
158     }
159 }
160 
161 #ifdef SK_ENABLE_DUMP_GPU
dumpJSON(SkJSONWriter * writer) const162 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
163     writer->beginArray();
164     for (int i = 0; i < fStrings.count(); ++i) {
165         writer->appendString(fStrings[i].c_str());
166     }
167     writer->endArray();
168 }
169 #else
dumpJSON(SkJSONWriter * writer) const170 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { }
171 #endif
172