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 | |
| 8 | #include "gl/GrGLExtensions.h" |
| 9 | #include "gl/GrGLDefines.h" |
| 10 | #include "gl/GrGLUtil.h" |
| 11 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 12 | #include "SkMakeUnique.h" |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 13 | #include "SkTSearch.h" |
| 14 | #include "SkTSort.h" |
| 15 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 16 | 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] | 17 | inline bool extension_compare(const SkString& a, const SkString& b) { |
| 18 | return strcmp(a.c_str(), b.c_str()) < 0; |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 19 | } |
| 20 | } |
| 21 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 22 | // finds the index of ext in strings or a negative result if ext is not found. |
| 23 | static int find_string(const SkTArray<SkString>& strings, const char ext[]) { |
| 24 | if (strings.empty()) { |
| 25 | return -1; |
| 26 | } |
| 27 | SkString extensionStr(ext); |
| 28 | int idx = SkTSearch<SkString, extension_compare>(&strings.front(), |
| 29 | strings.count(), |
| 30 | extensionStr, |
| 31 | sizeof(SkString)); |
| 32 | return idx; |
| 33 | } |
| 34 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 35 | GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(new SkTArray<SkString>) { |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 36 | *this = that; |
| 37 | } |
| 38 | |
| 39 | GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) { |
| 40 | *fStrings = *that.fStrings; |
| 41 | fInitialized = that.fInitialized; |
| 42 | return *this; |
| 43 | } |
| 44 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 45 | static void eat_space_sep_strings(SkTArray<SkString>* out, const char in[]) { |
| 46 | if (!in) { |
| 47 | return; |
| 48 | } |
| 49 | while (true) { |
| 50 | // skip over multiple spaces between extensions |
| 51 | while (' ' == *in) { |
| 52 | ++in; |
| 53 | } |
| 54 | // quit once we reach the end of the string. |
| 55 | if ('\0' == *in) { |
| 56 | break; |
| 57 | } |
| 58 | // we found an extension |
| 59 | size_t length = strcspn(in, " "); |
| 60 | out->push_back().set(in, length); |
| 61 | in += length; |
| 62 | } |
| 63 | } |
| 64 | |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 65 | bool GrGLExtensions::init(GrGLStandard standard, |
bsalomon | 9f2dc27 | 2016-02-08 07:22:17 -0800 | [diff] [blame] | 66 | GrGLFunction<GrGLGetStringProc> getString, |
| 67 | GrGLFunction<GrGLGetStringiProc> getStringi, |
| 68 | GrGLFunction<GrGLGetIntegervProc> getIntegerv, |
| 69 | GrGLFunction<GrEGLQueryStringProc> queryString, |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 70 | GrEGLDisplay eglDisplay) { |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 71 | fInitialized = false; |
| 72 | fStrings->reset(); |
| 73 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 74 | if (!getString) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 77 | |
| 78 | // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES. |
| 79 | const GrGLubyte* verString = getString(GR_GL_VERSION); |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 80 | GrGLVersion version = GrGLGetVersionFromString((const char*) verString); |
| 81 | if (GR_GL_INVALID_VER == version) { |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 82 | return false; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 83 | } |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 85 | bool indexed = version >= GR_GL_VER(3, 0); |
| 86 | |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 87 | if (indexed) { |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 88 | if (!getStringi || !getIntegerv) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | GrGLint extensionCnt = 0; |
| 92 | getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 93 | fStrings->push_back_n(extensionCnt); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 94 | for (int i = 0; i < extensionCnt; ++i) { |
| 95 | const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 96 | (*fStrings)[i] = ext; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 97 | } |
| 98 | } else { |
| 99 | const char* extensions = (const char*) getString(GR_GL_EXTENSIONS); |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 100 | if (!extensions) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 103 | eat_space_sep_strings(fStrings.get(), extensions); |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 104 | } |
| 105 | if (queryString) { |
| 106 | const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS); |
| 107 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 108 | eat_space_sep_strings(fStrings.get(), extensions); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 109 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 110 | if (!fStrings->empty()) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 111 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 112 | SkTQSort(&fStrings->front(), &fStrings->back(), cmp); |
bsalomon@google.com | a1d27cd | 2013-03-08 19:01:01 +0000 | [diff] [blame] | 113 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 114 | fInitialized = true; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 118 | bool GrGLExtensions::has(const char ext[]) const { |
| 119 | SkASSERT(fInitialized); |
| 120 | return find_string(*fStrings, ext) >= 0; |
| 121 | } |
| 122 | |
| 123 | bool GrGLExtensions::remove(const char ext[]) { |
| 124 | SkASSERT(fInitialized); |
| 125 | int idx = find_string(*fStrings, ext); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 126 | if (idx < 0) { |
bsalomon@google.com | 42db2e4 | 2013-06-11 19:22:44 +0000 | [diff] [blame] | 127 | return false; |
| 128 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 129 | |
| 130 | // This is not terribly effecient but we really only expect this function to be called at |
| 131 | // most a handful of times when our test programs start. |
| 132 | fStrings->removeShuffle(idx); |
| 133 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
| 134 | SkTInsertionSort(&(fStrings->operator[](idx)), &fStrings->back(), cmp); |
| 135 | return true; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 136 | } |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 138 | void GrGLExtensions::add(const char ext[]) { |
| 139 | int idx = find_string(*fStrings, ext); |
| 140 | if (idx < 0) { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 141 | // 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] | 142 | // extensions after the add |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 143 | fStrings->emplace_back(ext); |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 144 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 145 | SkTInsertionSort(&fStrings->front(), &fStrings->back(), cmp); |
commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Brian Osman | 0f450ac | 2017-08-09 20:45:52 +0000 | [diff] [blame] | 149 | void GrGLExtensions::print(const char* sep) const { |
| 150 | if (nullptr == sep) { |
| 151 | sep = " "; |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 152 | } |
Brian Osman | 0f450ac | 2017-08-09 20:45:52 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 157 | } |