blob: e51fa04ce839bc3e95e0abe11c3fd5063a6bf313 [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
halcanary385fe4d2015-08-26 13:07:48 -070034GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(new SkTArray<SkString>) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000035 *this = that;
36}
37
38GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) {
39 *fStrings = *that.fStrings;
40 fInitialized = that.fInitialized;
41 return *this;
42}
43
bsalomonb1a32ad2015-11-16 06:48:44 -080044static 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
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000064bool GrGLExtensions::init(GrGLStandard standard,
bsalomon@google.com1744f972013-02-26 21:46:32 +000065 GrGLGetStringProc getString,
66 GrGLGetStringiProc getStringi,
bsalomonb1a32ad2015-11-16 06:48:44 -080067 GrGLGetIntegervProc getIntegerv,
68 GrEGLQueryStringProc queryString,
69 GrEGLDisplay eglDisplay) {
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000070 fInitialized = false;
71 fStrings->reset();
72
bsalomonb1a32ad2015-11-16 06:48:44 -080073 if (!getString) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000074 return false;
75 }
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000076
77 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
78 const GrGLubyte* verString = getString(GR_GL_VERSION);
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000079 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
80 if (GR_GL_INVALID_VER == version) {
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000081 return false;
bsalomon@google.com1744f972013-02-26 21:46:32 +000082 }
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000083
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000084 bool indexed = version >= GR_GL_VER(3, 0);
85
bsalomon@google.com1744f972013-02-26 21:46:32 +000086 if (indexed) {
bsalomonb1a32ad2015-11-16 06:48:44 -080087 if (!getStringi || !getIntegerv) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000088 return false;
89 }
90 GrGLint extensionCnt = 0;
91 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000092 fStrings->push_back_n(extensionCnt);
bsalomon@google.com1744f972013-02-26 21:46:32 +000093 for (int i = 0; i < extensionCnt; ++i) {
94 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000095 (*fStrings)[i] = ext;
bsalomon@google.com1744f972013-02-26 21:46:32 +000096 }
97 } else {
98 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
bsalomonb1a32ad2015-11-16 06:48:44 -080099 if (!extensions) {
bsalomon@google.com1744f972013-02-26 21:46:32 +0000100 return false;
101 }
bsalomonb1a32ad2015-11-16 06:48:44 -0800102 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);
bsalomon@google.com1744f972013-02-26 21:46:32 +0000108 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000109 if (!fStrings->empty()) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000110 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000111 SkTQSort(&fStrings->front(), &fStrings->back(), cmp);
bsalomon@google.coma1d27cd2013-03-08 19:01:01 +0000112 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000113 fInitialized = true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000114 return true;
115}
116
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000117bool 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());
halcanary385fe4d2015-08-26 13:07:48 -0700129 fStrings.reset(new SkTArray<SkString>(oldStrings->count() - 1));
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000130 fStrings->push_back_n(idx, &oldStrings->front());
131 fStrings->push_back_n(oldStrings->count() - idx - 1, &(*oldStrings)[idx] + 1);
132 return true;
133 } else {
bsalomon@google.com42db2e42013-06-11 19:22:44 +0000134 return false;
135 }
bsalomon@google.com1744f972013-02-26 21:46:32 +0000136}
bsalomon@google.com00142c42013-05-02 19:42:54 +0000137
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000138void 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
bsalomon@google.com00142c42013-05-02 19:42:54 +0000149void GrGLExtensions::print(const char* sep) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700150 if (nullptr == sep) {
bsalomon@google.com00142c42013-05-02 19:42:54 +0000151 sep = " ";
152 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000153 int cnt = fStrings->count();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000154 for (int i = 0; i < cnt; ++i) {
tfarina38406c82014-10-31 07:11:12 -0700155 SkDebugf("%s%s", (*fStrings)[i].c_str(), (i < cnt - 1) ? sep : "");
bsalomon@google.com00142c42013-05-02 19:42:54 +0000156 }
157}