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 "gl/GrGLExtensions.h"
9 #include "gl/GrGLDefines.h"
10 #include "gl/GrGLUtil.h"
11 
12 #include "SkJSONWriter.h"
13 #include "SkMakeUnique.h"
14 #include "SkTSearch.h"
15 #include "SkTSort.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     // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
82     const GrGLubyte* verString = getString(GR_GL_VERSION);
83     GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
84     if (GR_GL_INVALID_VER == version) {
85         return false;
86     }
87 
88     bool indexed = version >= GR_GL_VER(3, 0);
89 
90     if (indexed) {
91         if (!getStringi || !getIntegerv) {
92             return false;
93         }
94         GrGLint extensionCnt = 0;
95         getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
96         fStrings.push_back_n(extensionCnt);
97         for (int i = 0; i < extensionCnt; ++i) {
98             const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
99             fStrings[i] = ext;
100         }
101     } else {
102         const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
103         if (!extensions) {
104             return false;
105         }
106         eat_space_sep_strings(&fStrings, extensions);
107     }
108     if (queryString) {
109         const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS);
110 
111         eat_space_sep_strings(&fStrings, extensions);
112     }
113     if (!fStrings.empty()) {
114         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
115         SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
116     }
117     fInitialized = true;
118     return true;
119 }
120 
has(const char ext[]) const121 bool GrGLExtensions::has(const char ext[]) const {
122     SkASSERT(fInitialized);
123     return find_string(fStrings, ext) >= 0;
124 }
125 
remove(const char ext[])126 bool GrGLExtensions::remove(const char ext[]) {
127     SkASSERT(fInitialized);
128     int idx = find_string(fStrings, ext);
129     if (idx < 0) {
130         return false;
131     }
132 
133     // This is not terribly effecient but we really only expect this function to be called at
134     // most a handful of times when our test programs start.
135     fStrings.removeShuffle(idx);
136     if (idx != fStrings.count()) {
137         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
138         SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), cmp);
139     }
140     return true;
141 }
142 
add(const char ext[])143 void GrGLExtensions::add(const char ext[]) {
144     int idx = find_string(fStrings, ext);
145     if (idx < 0) {
146         // This is not the most effecient approach since we end up looking at all of the
147         // extensions after the add
148         fStrings.emplace_back(ext);
149         SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
150         SkTInsertionSort(&fStrings.front(), &fStrings.back(), cmp);
151     }
152 }
153 
154 #ifdef SK_ENABLE_DUMP_GPU
dumpJSON(SkJSONWriter * writer) const155 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
156     writer->beginArray();
157     for (int i = 0; i < fStrings.count(); ++i) {
158         writer->appendString(fStrings[i].c_str());
159     }
160     writer->endArray();
161 }
162 #else
dumpJSON(SkJSONWriter * writer) const163 void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { }
164 #endif
165