bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/gl/GrGLExtensions.h" |
| 9 | #include "src/gpu/gl/GrGLDefines.h" |
| 10 | #include "src/gpu/gl/GrGLUtil.h" |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/core/SkMakeUnique.h" |
Ben Wagner | 8bd6e8f | 2019-05-15 09:28:52 -0400 | [diff] [blame] | 13 | #include "src/core/SkTSearch.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/core/SkTSort.h" |
| 15 | #include "src/utils/SkJSONWriter.h" |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 17 | namespace { // This cannot be static because it is used as a template parameter. |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 18 | inline bool extension_compare(const SkString& a, const SkString& b) { |
| 19 | return strcmp(a.c_str(), b.c_str()) < 0; |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 23 | // finds the index of ext in strings or a negative result if ext is not found. |
| 24 | static 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 Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 36 | GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) { |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 37 | *this = that; |
| 38 | } |
| 39 | |
| 40 | GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) { |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 41 | if (this != &that) { |
| 42 | fStrings = that.fStrings; |
| 43 | fInitialized = that.fInitialized; |
| 44 | } |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 45 | return *this; |
| 46 | } |
| 47 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 48 | static 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.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 68 | bool GrGLExtensions::init(GrGLStandard standard, |
Brian Salomon | 2424ec4 | 2018-08-24 10:26:42 -0400 | [diff] [blame] | 69 | GrGLFunction<GrGLGetStringFn> getString, |
| 70 | GrGLFunction<GrGLGetStringiFn> getStringi, |
| 71 | GrGLFunction<GrGLGetIntegervFn> getIntegerv, |
| 72 | GrGLFunction<GrEGLQueryStringFn> queryString, |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 73 | GrEGLDisplay eglDisplay) { |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 74 | fInitialized = false; |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 75 | fStrings.reset(); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 76 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 77 | if (!getString) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 81 | const GrGLubyte* verString = getString(GR_GL_VERSION); |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 82 | GrGLVersion version = GrGLGetVersionFromString((const char*) verString); |
| 83 | if (GR_GL_INVALID_VER == version) { |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 84 | return false; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 85 | } |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 86 | |
Kevin Lubick | 3902628 | 2019-03-28 12:46:40 -0400 | [diff] [blame] | 87 | 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.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 97 | if (indexed) { |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 98 | if (!getStringi || !getIntegerv) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | GrGLint extensionCnt = 0; |
| 102 | getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt); |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 103 | fStrings.push_back_n(extensionCnt); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 104 | for (int i = 0; i < extensionCnt; ++i) { |
| 105 | const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i); |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 106 | fStrings[i] = ext; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 107 | } |
| 108 | } else { |
| 109 | const char* extensions = (const char*) getString(GR_GL_EXTENSIONS); |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 110 | if (!extensions) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 113 | eat_space_sep_strings(&fStrings, extensions); |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 114 | } |
| 115 | if (queryString) { |
| 116 | const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS); |
| 117 | |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 118 | eat_space_sep_strings(&fStrings, extensions); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 119 | } |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 120 | if (!fStrings.empty()) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 121 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 122 | SkTQSort(&fStrings.front(), &fStrings.back(), cmp); |
bsalomon@google.com | a1d27cd | 2013-03-08 19:01:01 +0000 | [diff] [blame] | 123 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 124 | fInitialized = true; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 128 | bool GrGLExtensions::has(const char ext[]) const { |
| 129 | SkASSERT(fInitialized); |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 130 | return find_string(fStrings, ext) >= 0; |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool GrGLExtensions::remove(const char ext[]) { |
| 134 | SkASSERT(fInitialized); |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 135 | int idx = find_string(fStrings, ext); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 136 | if (idx < 0) { |
bsalomon@google.com | 42db2e4 | 2013-06-11 19:22:44 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 139 | |
| 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 Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 142 | fStrings.removeShuffle(idx); |
| 143 | if (idx != fStrings.count()) { |
Vladimir Levin | 70fb479 | 2017-11-15 12:01:51 -0800 | [diff] [blame] | 144 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 145 | SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), cmp); |
Vladimir Levin | 70fb479 | 2017-11-15 12:01:51 -0800 | [diff] [blame] | 146 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 147 | return true; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 148 | } |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 149 | |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 150 | void GrGLExtensions::add(const char ext[]) { |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 151 | int idx = find_string(fStrings, ext); |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 152 | if (idx < 0) { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 153 | // This is not the most effecient approach since we end up looking at all of the |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 154 | // extensions after the add |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 155 | fStrings.emplace_back(ext); |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 156 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 157 | SkTInsertionSort(&fStrings.front(), &fStrings.back(), cmp); |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 161 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 162 | void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { |
| 163 | writer->beginArray(); |
Hal Canary | 1045013 | 2018-03-21 12:49:56 -0400 | [diff] [blame] | 164 | for (int i = 0; i < fStrings.count(); ++i) { |
| 165 | writer->appendString(fStrings[i].c_str()); |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 166 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 167 | writer->endArray(); |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 168 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 169 | #else |
| 170 | void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const { } |
| 171 | #endif |