blob: c4f64752934f259229520f7541c9a3f5209e3ad1 [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
Brian Osman71a18892017-08-10 10:23:25 -040012#include "SkJSONWriter.h"
bungeman6bd52842016-10-27 09:30:08 -070013#include "SkMakeUnique.h"
bsalomon@google.comff436612013-02-27 19:07:32 +000014#include "SkTSearch.h"
15#include "SkTSort.h"
16
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
halcanary385fe4d2015-08-26 13:07:48 -070036GrGLExtensions::GrGLExtensions(const GrGLExtensions& that) : fStrings(new SkTArray<SkString>) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000037 *this = that;
38}
39
40GrGLExtensions& GrGLExtensions::operator=(const GrGLExtensions& that) {
41 *fStrings = *that.fStrings;
42 fInitialized = that.fInitialized;
43 return *this;
44}
45
bsalomonb1a32ad2015-11-16 06:48:44 -080046static void eat_space_sep_strings(SkTArray<SkString>* out, const char in[]) {
47 if (!in) {
48 return;
49 }
50 while (true) {
51 // skip over multiple spaces between extensions
52 while (' ' == *in) {
53 ++in;
54 }
55 // quit once we reach the end of the string.
56 if ('\0' == *in) {
57 break;
58 }
59 // we found an extension
60 size_t length = strcspn(in, " ");
61 out->push_back().set(in, length);
62 in += length;
63 }
64}
65
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000066bool GrGLExtensions::init(GrGLStandard standard,
bsalomon9f2dc272016-02-08 07:22:17 -080067 GrGLFunction<GrGLGetStringProc> getString,
68 GrGLFunction<GrGLGetStringiProc> getStringi,
69 GrGLFunction<GrGLGetIntegervProc> getIntegerv,
70 GrGLFunction<GrEGLQueryStringProc> queryString,
bsalomonb1a32ad2015-11-16 06:48:44 -080071 GrEGLDisplay eglDisplay) {
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000072 fInitialized = false;
73 fStrings->reset();
74
bsalomonb1a32ad2015-11-16 06:48:44 -080075 if (!getString) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000076 return false;
77 }
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000078
79 // glGetStringi and indexed extensions were added in version 3.0 of desktop GL and ES.
80 const GrGLubyte* verString = getString(GR_GL_VERSION);
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000081 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
82 if (GR_GL_INVALID_VER == version) {
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000083 return false;
bsalomon@google.com1744f972013-02-26 21:46:32 +000084 }
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000085
commit-bot@chromium.org726e6212013-08-23 20:55:46 +000086 bool indexed = version >= GR_GL_VER(3, 0);
87
bsalomon@google.com1744f972013-02-26 21:46:32 +000088 if (indexed) {
bsalomonb1a32ad2015-11-16 06:48:44 -080089 if (!getStringi || !getIntegerv) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000090 return false;
91 }
92 GrGLint extensionCnt = 0;
93 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000094 fStrings->push_back_n(extensionCnt);
bsalomon@google.com1744f972013-02-26 21:46:32 +000095 for (int i = 0; i < extensionCnt; ++i) {
96 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000097 (*fStrings)[i] = ext;
bsalomon@google.com1744f972013-02-26 21:46:32 +000098 }
99 } else {
100 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
bsalomonb1a32ad2015-11-16 06:48:44 -0800101 if (!extensions) {
bsalomon@google.com1744f972013-02-26 21:46:32 +0000102 return false;
103 }
bungeman6bd52842016-10-27 09:30:08 -0700104 eat_space_sep_strings(fStrings.get(), extensions);
bsalomonb1a32ad2015-11-16 06:48:44 -0800105 }
106 if (queryString) {
107 const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS);
108
bungeman6bd52842016-10-27 09:30:08 -0700109 eat_space_sep_strings(fStrings.get(), extensions);
bsalomon@google.com1744f972013-02-26 21:46:32 +0000110 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000111 if (!fStrings->empty()) {
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000112 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000113 SkTQSort(&fStrings->front(), &fStrings->back(), cmp);
bsalomon@google.coma1d27cd2013-03-08 19:01:01 +0000114 }
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000115 fInitialized = true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000116 return true;
117}
118
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000119bool GrGLExtensions::has(const char ext[]) const {
120 SkASSERT(fInitialized);
121 return find_string(*fStrings, ext) >= 0;
122}
123
124bool GrGLExtensions::remove(const char ext[]) {
125 SkASSERT(fInitialized);
126 int idx = find_string(*fStrings, ext);
bungeman6bd52842016-10-27 09:30:08 -0700127 if (idx < 0) {
bsalomon@google.com42db2e42013-06-11 19:22:44 +0000128 return false;
129 }
bungeman6bd52842016-10-27 09:30:08 -0700130
131 // This is not terribly effecient but we really only expect this function to be called at
132 // most a handful of times when our test programs start.
133 fStrings->removeShuffle(idx);
134 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
135 SkTInsertionSort(&(fStrings->operator[](idx)), &fStrings->back(), cmp);
136 return true;
bsalomon@google.com1744f972013-02-26 21:46:32 +0000137}
bsalomon@google.com00142c42013-05-02 19:42:54 +0000138
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000139void GrGLExtensions::add(const char ext[]) {
140 int idx = find_string(*fStrings, ext);
141 if (idx < 0) {
bungeman6bd52842016-10-27 09:30:08 -0700142 // 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 +0000143 // extensions after the add
bungeman6bd52842016-10-27 09:30:08 -0700144 fStrings->emplace_back(ext);
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000145 SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
bungeman6bd52842016-10-27 09:30:08 -0700146 SkTInsertionSort(&fStrings->front(), &fStrings->back(), cmp);
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000147 }
148}
149
Brian Osman71a18892017-08-10 10:23:25 -0400150void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
151 writer->beginArray();
152 for (int i = 0; i < fStrings->count(); ++i) {
153 writer->appendString((*fStrings)[i].c_str());
bsalomon@google.com00142c42013-05-02 19:42:54 +0000154 }
Brian Osman71a18892017-08-10 10:23:25 -0400155 writer->endArray();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000156}