|
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 "SkTSearch.h" |
|
13 #include "SkTSort.h" |
|
14 |
|
15 namespace { // This cannot be static because it is used as a template parameter. |
|
16 inline bool extension_compare(const SkString& a, const SkString& b) { |
|
17 return strcmp(a.c_str(), b.c_str()) < 0; |
|
18 } |
|
19 } |
|
20 |
|
21 // finds the index of ext in strings or a negative result if ext is not found. |
|
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 |
|
34 GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(SkNEW(SkTArray<SkString>)) { |
|
35 *this = that; |
|
36 } |
|
37 |
|
38 GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) { |
|
39 *fStrings = *that.fStrings; |
|
40 fInitialized = that.fInitialized; |
|
41 return *this; |
|
42 } |
|
43 |
|
44 bool GrGLExtensions::init(GrGLStandard standard, |
|
45 GrGLGetStringProc getString, |
|
46 GrGLGetStringiProc getStringi, |
|
47 GrGLGetIntegervProc getIntegerv) { |
|
48 fInitialized = false; |
|
49 fStrings->reset(); |
|
50 |
|
51 if (NULL == getString) { |
|
52 return false; |
|
53 } |
|
54 |
|
55 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES. |
|
56 const GrGLubyte* verString = getString(GR_GL_VERSION); |
|
57 if (NULL == verString) { |
|
58 return false; |
|
59 } |
|
60 GrGLVersion version = GrGLGetVersionFromString((const char*) verString); |
|
61 bool indexed = version >= GR_GL_VER(3, 0); |
|
62 |
|
63 if (indexed) { |
|
64 if (NULL == getStringi || NULL == getIntegerv) { |
|
65 return false; |
|
66 } |
|
67 GrGLint extensionCnt = 0; |
|
68 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt); |
|
69 fStrings->push_back_n(extensionCnt); |
|
70 for (int i = 0; i < extensionCnt; ++i) { |
|
71 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i); |
|
72 (*fStrings)[i] = ext; |
|
73 } |
|
74 } else { |
|
75 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS); |
|
76 if (NULL == extensions) { |
|
77 return false; |
|
78 } |
|
79 while (true) { |
|
80 // skip over multiple spaces between extensions |
|
81 while (' ' == *extensions) { |
|
82 ++extensions; |
|
83 } |
|
84 // quit once we reach the end of the string. |
|
85 if ('\0' == *extensions) { |
|
86 break; |
|
87 } |
|
88 // we found an extension |
|
89 size_t length = strcspn(extensions, " "); |
|
90 fStrings->push_back().set(extensions, length); |
|
91 extensions += length; |
|
92 } |
|
93 } |
|
94 if (!fStrings->empty()) { |
|
95 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
|
96 SkTQSort(&fStrings->front(), &fStrings->back(), cmp); |
|
97 } |
|
98 fInitialized = true; |
|
99 return true; |
|
100 } |
|
101 |
|
102 bool GrGLExtensions::has(const char ext[]) const { |
|
103 SkASSERT(fInitialized); |
|
104 return find_string(*fStrings, ext) >= 0; |
|
105 } |
|
106 |
|
107 bool GrGLExtensions::remove(const char ext[]) { |
|
108 SkASSERT(fInitialized); |
|
109 int idx = find_string(*fStrings, ext); |
|
110 if (idx >= 0) { |
|
111 // This is not terribly effecient but we really only expect this function to be called at |
|
112 // most a handful of times when our test programs start. |
|
113 SkAutoTDelete< SkTArray<SkString> > oldStrings(fStrings.detach()); |
|
114 fStrings.reset(SkNEW(SkTArray<SkString>(oldStrings->count() - 1))); |
|
115 fStrings->push_back_n(idx, &oldStrings->front()); |
|
116 fStrings->push_back_n(oldStrings->count() - idx - 1, &(*oldStrings)[idx] + 1); |
|
117 return true; |
|
118 } else { |
|
119 return false; |
|
120 } |
|
121 } |
|
122 |
|
123 void GrGLExtensions::add(const char ext[]) { |
|
124 int idx = find_string(*fStrings, ext); |
|
125 if (idx < 0) { |
|
126 // This is not the most effecient approach since we end up doing a full sort of the |
|
127 // extensions after the add |
|
128 fStrings->push_back().set(ext); |
|
129 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
|
130 SkTQSort(&fStrings->front(), &fStrings->back(), cmp); |
|
131 } |
|
132 } |
|
133 |
|
134 void GrGLExtensions::print(const char* sep) const { |
|
135 if (NULL == sep) { |
|
136 sep = " "; |
|
137 } |
|
138 int cnt = fStrings->count(); |
|
139 for (int i = 0; i < cnt; ++i) { |
|
140 GrPrintf("%s%s", (*fStrings)[i].c_str(), (i < cnt - 1) ? sep : ""); |
|
141 } |
|
142 } |