blob: 5ccb3393d40f45b27e2890f75b4cbcd03073dad8 [file] [log] [blame]
bsalomon@google.com1744f972013-02-26 21:46:32 +00001/*
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
bsalomon@google.comff436612013-02-27 19:07:32 +000012#include "SkTSearch.h"
13#include "SkTSort.h"
14
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000015namespace { // This cannot be static because it is used as a template parameter.
bsalomon@google.com20f7f172013-05-17 19:05:03 +000016inline bool extension_compare(const SkString& a, const SkString& b) {
17 return strcmp(a.c_str(), b.c_str()) < 0;
bsalomon@google.comff436612013-02-27 19:07:32 +000018}
19}
20
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000021// 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(SkNEW(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
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000044bool GrGLExtensions::init(GrGLStandard standard,
bsalomon@google.com1744f972013-02-26 21:46:32 +000045 GrGLGetStringProc getString,
46 GrGLGetStringiProc getStringi,
47 GrGLGetIntegervProc getIntegerv) {
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000048 fInitialized = false;
49 fStrings->reset();
50
bsalomon@google.com1744f972013-02-26 21:46:32 +000051 if (NULL == getString) {
52 return false;
53 }
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000054
55 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
56 const GrGLubyte* verString = getString(GR_GL_VERSION);
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000057 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
58 if (GR_GL_INVALID_VER == version) {
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000059 return false;
bsalomon@google.com1744f972013-02-26 21:46:32 +000060 }
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000061
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000062 bool indexed = version >= GR_GL_VER(3, 0);
63
bsalomon@google.com1744f972013-02-26 21:46:32 +000064 if (indexed) {
65 if (NULL == getStringi || NULL == getIntegerv) {
66 return false;
67 }
68 GrGLint extensionCnt = 0;
69 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000070 fStrings->push_back_n(extensionCnt);
bsalomon@google.com1744f972013-02-26 21:46:32 +000071 for (int i = 0; i < extensionCnt; ++i) {
72 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000073 (*fStrings)[i] = ext;
bsalomon@google.com1744f972013-02-26 21:46:32 +000074 }
75 } else {
76 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
skia.committer@gmail.comeed625d2013-03-09 07:01:15 +000077 if (NULL == extensions) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000078 return false;
79 }
bsalomon@google.com1744f972013-02-26 21:46:32 +000080 while (true) {
bsalomon@google.coma2240722013-03-08 16:06:28 +000081 // skip over multiple spaces between extensions
82 while (' ' == *extensions) {
83 ++extensions;
84 }
85 // quit once we reach the end of the string.
86 if ('\0' == *extensions) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000087 break;
88 }
bsalomon@google.coma2240722013-03-08 16:06:28 +000089 // we found an extension
90 size_t length = strcspn(extensions, " ");
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000091 fStrings->push_back().set(extensions, length);
bsalomon@google.coma2240722013-03-08 16:06:28 +000092 extensions += length;
bsalomon@google.com1744f972013-02-26 21:46:32 +000093 }
bsalomon@google.com1744f972013-02-26 21:46:32 +000094 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000095 if (!fStrings->empty()) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +000096 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000097 SkTQSort(&fStrings->front(), &fStrings->back(), cmp);
bsalomon@google.coma1d27cd2013-03-08 19:01:01 +000098 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000099 fInitialized = true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000100 return true;
101}
102
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000103bool GrGLExtensions::has(const char ext[]) const {
104 SkASSERT(fInitialized);
105 return find_string(*fStrings, ext) >= 0;
106}
107
108bool GrGLExtensions::remove(const char ext[]) {
109 SkASSERT(fInitialized);
110 int idx = find_string(*fStrings, ext);
111 if (idx >= 0) {
112 // This is not terribly effecient but we really only expect this function to be called at
113 // most a handful of times when our test programs start.
114 SkAutoTDelete< SkTArray<SkString> > oldStrings(fStrings.detach());
115 fStrings.reset(SkNEW(SkTArray<SkString>(oldStrings->count() - 1)));
116 fStrings->push_back_n(idx, &oldStrings->front());
117 fStrings->push_back_n(oldStrings->count() - idx - 1, &(*oldStrings)[idx] + 1);
118 return true;
119 } else {
bsalomon@google.com42db2e42013-06-11 19:22:44 +0000120 return false;
121 }
bsalomon@google.com1744f972013-02-26 21:46:32 +0000122}
bsalomon@google.com00142c42013-05-02 19:42:54 +0000123
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000124void GrGLExtensions::add(const char ext[]) {
125 int idx = find_string(*fStrings, ext);
126 if (idx < 0) {
127 // This is not the most effecient approach since we end up doing a full sort of the
128 // extensions after the add
129 fStrings->push_back().set(ext);
130 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
131 SkTQSort(&fStrings->front(), &fStrings->back(), cmp);
132 }
133}
134
bsalomon@google.com00142c42013-05-02 19:42:54 +0000135void GrGLExtensions::print(const char* sep) const {
136 if (NULL == sep) {
137 sep = " ";
138 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000139 int cnt = fStrings->count();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000140 for (int i = 0; i < cnt; ++i) {
tfarina38406c82014-10-31 07:11:12 -0700141 SkDebugf("%s%s", (*fStrings)[i].c_str(), (i < cnt - 1) ? sep : "");
bsalomon@google.com00142c42013-05-02 19:42:54 +0000142 }
143}