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