blob: e5a989f95b21dfa3a670f455ea0e20a6dbbd9885 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/gl/GrGLExtensions.h"
9#include "src/gpu/gl/GrGLDefines.h"
10#include "src/gpu/gl/GrGLUtil.h"
bsalomon@google.com1744f972013-02-26 21:46:32 +000011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkMakeUnique.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040013#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkTSort.h"
15#include "src/utils/SkJSONWriter.h"
bsalomon@google.comff436612013-02-27 19:07:32 +000016
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000017namespace { // This cannot be static because it is used as a template parameter.
bsalomon@google.com20f7f172013-05-17 19:05:03 +000018inline bool extension_compare(const SkString& a, const SkString& b) {
19 return strcmp(a.c_str(), b.c_str()) < 0;
bsalomon@google.comff436612013-02-27 19:07:32 +000020}
21}
22
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000023// finds the index of ext in strings or a negative result if ext is not found.
24static int find_string(const SkTArray<SkString>& strings, const char ext[]) {
25 if (strings.empty()) {
26 return -1;
27 }
28 SkString extensionStr(ext);
29 int idx = SkTSearch<SkString, extension_compare>(&strings.front(),
30 strings.count(),
31 extensionStr,
32 sizeof(SkString));
33 return idx;
34}
35
Hal Canary10450132018-03-21 12:49:56 -040036GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000037 *this = that;
38}
39
40GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) {
Hal Canary10450132018-03-21 12:49:56 -040041 if (this != &that) {
42 fStrings = that.fStrings;
43 fInitialized = that.fInitialized;
44 }
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000045 return *this;
46}
47
bsalomonb1a32ad2015-11-16 06:48:44 -080048static void eat_space_sep_strings(SkTArray<SkString>* out, const char in[]) {
49 if (!in) {
50 return;
51 }
52 while (true) {
53 // skip over multiple spaces between extensions
54 while (' ' == *in) {
55 ++in;
56 }
57 // quit once we reach the end of the string.
58 if ('\0' == *in) {
59 break;
60 }
61 // we found an extension
62 size_t length = strcspn(in, " ");
63 out->push_back().set(in, length);
64 in += length;
65 }
66}
67
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000068bool GrGLExtensions::init(GrGLStandard standard,
Brian Salomon2424ec42018-08-24 10:26:42 -040069 GrGLFunction<GrGLGetStringFn> getString,
70 GrGLFunction<GrGLGetStringiFn> getStringi,
71 GrGLFunction<GrGLGetIntegervFn> getIntegerv,
72 GrGLFunction<GrEGLQueryStringFn> queryString,
bsalomonb1a32ad2015-11-16 06:48:44 -080073 GrEGLDisplay eglDisplay) {
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000074 fInitialized = false;
Hal Canary10450132018-03-21 12:49:56 -040075 fStrings.reset();
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000076
bsalomonb1a32ad2015-11-16 06:48:44 -080077 if (!getString) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000078 return false;
79 }
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000080
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000081 const GrGLubyte* verString = getString(GR_GL_VERSION);
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000082 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
83 if (GR_GL_INVALID_VER == version) {
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000084 return false;
bsalomon@google.com1744f972013-02-26 21:46:32 +000085 }
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000086
Kevin Lubick39026282019-03-28 12:46:40 -040087 bool indexed = false;
88 if (GR_IS_GR_GL(standard) || GR_IS_GR_GL_ES(standard)) {
89 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
90 indexed = version >= GR_GL_VER(3, 0);
91 } else if (GR_IS_GR_WEBGL(standard)) {
92 // WebGL (1.0 or 2.0) doesn't natively support glGetStringi, but enscripten adds it in
93 // https://github.com/emscripten-core/emscripten/issues/3472
94 indexed = version >= GR_GL_VER(2, 0);
95 }
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000096
bsalomon@google.com1744f972013-02-26 21:46:32 +000097 if (indexed) {
bsalomonb1a32ad2015-11-16 06:48:44 -080098 if (!getStringi || !getIntegerv) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000099 return false;
100 }
101 GrGLint extensionCnt = 0;
102 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
Hal Canary10450132018-03-21 12:49:56 -0400103 fStrings.push_back_n(extensionCnt);
bsalomon@google.com1744f972013-02-26 21:46:32 +0000104 for (int i = 0; i < extensionCnt; ++i) {
105 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
Hal Canary10450132018-03-21 12:49:56 -0400106 fStrings[i] = ext;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000107 }
108 } else {
109 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
bsalomonb1a32ad2015-11-16 06:48:44 -0800110 if (!extensions) {
bsalomon@google.com1744f972013-02-26 21:46:32 +0000111 return false;
112 }
Hal Canary10450132018-03-21 12:49:56 -0400113 eat_space_sep_strings(&fStrings, extensions);
bsalomonb1a32ad2015-11-16 06:48:44 -0800114 }
115 if (queryString) {
116 const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS);
117
Hal Canary10450132018-03-21 12:49:56 -0400118 eat_space_sep_strings(&fStrings, extensions);
bsalomon@google.com1744f972013-02-26 21:46:32 +0000119 }
Hal Canary10450132018-03-21 12:49:56 -0400120 if (!fStrings.empty()) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000121 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
Hal Canary10450132018-03-21 12:49:56 -0400122 SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
bsalomon@google.coma1d27cd2013-03-08 19:01:01 +0000123 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000124 fInitialized = true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000125 return true;
126}
127
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000128bool GrGLExtensions::has(const char ext[]) const {
129 SkASSERT(fInitialized);
Hal Canary10450132018-03-21 12:49:56 -0400130 return find_string(fStrings, ext) >= 0;
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000131}
132
133bool GrGLExtensions::remove(const char ext[]) {
134 SkASSERT(fInitialized);
Hal Canary10450132018-03-21 12:49:56 -0400135 int idx = find_string(fStrings, ext);
bungeman6bd52842016-10-27 09:30:08 -0700136 if (idx < 0) {
bsalomon@google.com42db2e42013-06-11 19:22:44 +0000137 return false;
138 }
bungeman6bd52842016-10-27 09:30:08 -0700139
140 // This is not terribly effecient but we really only expect this function to be called at
141 // most a handful of times when our test programs start.
Hal Canary10450132018-03-21 12:49:56 -0400142 fStrings.removeShuffle(idx);
143 if (idx != fStrings.count()) {
Vladimir Levin70fb4792017-11-15 12:01:51 -0800144 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
Hal Canary10450132018-03-21 12:49:56 -0400145 SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), cmp);
Vladimir Levin70fb4792017-11-15 12:01:51 -0800146 }
bungeman6bd52842016-10-27 09:30:08 -0700147 return true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000148}
bsalomon@google.com00142c42013-05-02 19:42:54 +0000149
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000150void GrGLExtensions::add(const char ext[]) {
Hal Canary10450132018-03-21 12:49:56 -0400151 int idx = find_string(fStrings, ext);
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000152 if (idx < 0) {
bungeman6bd52842016-10-27 09:30:08 -0700153 // This is not the most effecient approach since we end up looking at all of the
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000154 // extensions after the add
Hal Canary10450132018-03-21 12:49:56 -0400155 fStrings.emplace_back(ext);
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000156 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
Hal Canary10450132018-03-21 12:49:56 -0400157 SkTInsertionSort(&fStrings.front(), &fStrings.back(), cmp);
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000158 }
159}
160
Kevin Lubickf4def342018-10-04 12:52:50 -0400161#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400162void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
163 writer->beginArray();
Hal Canary10450132018-03-21 12:49:56 -0400164 for (int i = 0; i < fStrings.count(); ++i) {
165 writer->appendString(fStrings[i].c_str());
bsalomon@google.com00142c42013-05-02 19:42:54 +0000166 }
Brian Osman71a18892017-08-10 10:23:25 -0400167 writer->endArray();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000168}
Kevin Lubickf4def342018-10-04 12:52:50 -0400169#else
170void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { }
171#endif