2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "gl/GrGLExtensions.h"
9 #include "gl/GrGLDefines.h"
10 #include "gl/GrGLUtil.h"
12 #include "SkTSearch.h"
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;
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()) {
26 SkString extensionStr(ext);
27 int idx = SkTSearch<SkString, extension_compare>(&strings.front(),
34 GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(SkNEW(SkTArray<SkString>)) {
38 GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) {
39 *fStrings = *that.fStrings;
40 fInitialized = that.fInitialized;
44 bool GrGLExtensions::init(GrGLStandard standard,
45 GrGLGetStringProc getString,
46 GrGLGetStringiProc getStringi,
47 GrGLGetIntegervProc getIntegerv) {
51 if (NULL == getString) {
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) {
60 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
61 bool indexed = version >= GR_GL_VER(3, 0);
64 if (NULL == getStringi || NULL == getIntegerv) {
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);
75 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
76 if (NULL == extensions) {
80 // skip over multiple spaces between extensions
81 while (' ' == *extensions) {
84 // quit once we reach the end of the string.
85 if ('\0' == *extensions) {
88 // we found an extension
89 size_t length = strcspn(extensions, " ");
90 fStrings->push_back().set(extensions, length);
94 if (!fStrings->empty()) {
95 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
96 SkTQSort(&fStrings->front(), &fStrings->back(), cmp);
102 bool GrGLExtensions::has(const char ext[]) const {
103 SkASSERT(fInitialized);
104 return find_string(*fStrings, ext) >= 0;
107 bool GrGLExtensions::remove(const char ext[]) {
108 SkASSERT(fInitialized);
109 int idx = find_string(*fStrings, ext);
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);
123 void GrGLExtensions::print(const char* sep) const {
127 int cnt = fStrings->count();
128 for (int i = 0; i < cnt; ++i) {
129 GrPrintf("%s%s", (*fStrings)[i].c_str(), (i < cnt - 1) ? sep : "");