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 | |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 12 | #include "SkTSearch.h" |
| 13 | #include "SkTSort.h" |
| 14 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 15 | 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] | 16 | inline bool extension_compare(const SkString& a, const SkString& b) { |
| 17 | return strcmp(a.c_str(), b.c_str()) < 0; |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 18 | } |
| 19 | } |
| 20 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 21 | // finds the index of ext in strings or a negative result if ext is not found. |
| 22 | static 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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 34 | GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(new SkTArray<SkString>) { |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 35 | *this = that; |
| 36 | } |
| 37 | |
| 38 | GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) { |
| 39 | *fStrings = *that.fStrings; |
| 40 | fInitialized = that.fInitialized; |
| 41 | return *this; |
| 42 | } |
| 43 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 44 | static 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.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 64 | bool GrGLExtensions::init(GrGLStandard standard, |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 65 | GrGLGetStringProc getString, |
| 66 | GrGLGetStringiProc getStringi, |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 67 | GrGLGetIntegervProc getIntegerv, |
| 68 | GrEGLQueryStringProc queryString, |
| 69 | GrEGLDisplay eglDisplay) { |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 70 | fInitialized = false; |
| 71 | fStrings->reset(); |
| 72 | |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 73 | if (!getString) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 76 | |
| 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.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 79 | GrGLVersion version = GrGLGetVersionFromString((const char*) verString); |
| 80 | if (GR_GL_INVALID_VER == version) { |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 81 | return false; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 82 | } |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | 726e621 | 2013-08-23 20:55:46 +0000 | [diff] [blame] | 84 | bool indexed = version >= GR_GL_VER(3, 0); |
| 85 | |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 86 | if (indexed) { |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 87 | if (!getStringi || !getIntegerv) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | GrGLint extensionCnt = 0; |
| 91 | getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 92 | fStrings->push_back_n(extensionCnt); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 93 | for (int i = 0; i < extensionCnt; ++i) { |
| 94 | const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 95 | (*fStrings)[i] = ext; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 96 | } |
| 97 | } else { |
| 98 | const char* extensions = (const char*) getString(GR_GL_EXTENSIONS); |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 99 | if (!extensions) { |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame^] | 102 | 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.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 109 | if (!fStrings->empty()) { |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 110 | SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp; |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 111 | SkTQSort(&fStrings->front(), &fStrings->back(), cmp); |
bsalomon@google.com | a1d27cd | 2013-03-08 19:01:01 +0000 | [diff] [blame] | 112 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 113 | fInitialized = true; |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 117 | bool GrGLExtensions::has(const char ext[]) const { |
| 118 | SkASSERT(fInitialized); |
| 119 | return find_string(*fStrings, ext) >= 0; |
| 120 | } |
| 121 | |
| 122 | bool 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()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 129 | fStrings.reset(new SkTArray<SkString>(oldStrings->count() - 1)); |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 130 | 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.com | 42db2e4 | 2013-06-11 19:22:44 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
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) { |
| 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.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 149 | void GrGLExtensions::print(const char* sep) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 150 | if (nullptr == sep) { |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 151 | sep = " "; |
| 152 | } |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 153 | int cnt = fStrings->count(); |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 154 | for (int i = 0; i < cnt; ++i) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 155 | SkDebugf("%s%s", (*fStrings)[i].c_str(), (i < cnt - 1) ? sep : ""); |
bsalomon@google.com | 00142c4 | 2013-05-02 19:42:54 +0000 | [diff] [blame] | 156 | } |
| 157 | } |