Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrGLExtensions.cpp
Go to the documentation of this file.
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
11
12#include "src/base/SkTSearch.h"
13#include "src/base/SkTSort.h"
14
15using namespace skia_private;
16
17namespace { // This cannot be static because it is used as a template parameter.
18inline bool extension_compare(const SkString& a, const SkString& b) {
19 return strcmp(a.c_str(), b.c_str()) < 0;
20}
21} // namespace
22
23// finds the index of ext in strings or a negative result if ext is not found.
24static int find_string(const TArray<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.size(),
31 extensionStr,
32 sizeof(SkString));
33 return idx;
34}
35
37 *this = that;
38}
39
41 if (this != &that) {
42 fStrings = that.fStrings;
43 fInitialized = that.fInitialized;
44 }
45 return *this;
46}
47
48static void eat_space_sep_strings(TArray<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
73 GrEGLDisplay eglDisplay) {
74 fInitialized = false;
75 fStrings.clear();
76
77 if (!getString) {
78 return false;
79 }
80
81 const GrGLubyte* verString = getString(GR_GL_VERSION);
82 GrGLVersion version = GrGLGetVersionFromString((const char*) verString);
83 if (GR_GL_INVALID_VER == version) {
84 return false;
85 }
86
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 }
96
97 if (indexed) {
98 if (!getStringi || !getIntegerv) {
99 return false;
100 }
101 GrGLint extensionCnt = 0;
102 getIntegerv(GR_GL_NUM_EXTENSIONS, &extensionCnt);
103 fStrings.push_back_n(extensionCnt);
104 for (int i = 0; i < extensionCnt; ++i) {
105 const char* ext = (const char*) getStringi(GR_GL_EXTENSIONS, i);
106 fStrings[i] = ext;
107 }
108 } else {
109 const char* extensions = (const char*) getString(GR_GL_EXTENSIONS);
110 if (!extensions) {
111 return false;
112 }
113 eat_space_sep_strings(&fStrings, extensions);
114 }
115 if (queryString) {
116 const char* extensions = queryString(eglDisplay, GR_EGL_EXTENSIONS);
117
118 eat_space_sep_strings(&fStrings, extensions);
119 }
120 if (!fStrings.empty()) {
121 SkTQSort(fStrings.begin(), fStrings.end(), extension_compare);
122 }
123 fInitialized = true;
124 return true;
125}
126
127bool GrGLExtensions::has(const char ext[]) const {
128 SkASSERT(fInitialized);
129 return find_string(fStrings, ext) >= 0;
130}
131
132bool GrGLExtensions::remove(const char ext[]) {
133 SkASSERT(fInitialized);
134 int idx = find_string(fStrings, ext);
135 if (idx < 0) {
136 return false;
137 }
138
139 // This is not terribly effecient but we really only expect this function to be called at
140 // most a handful of times when our test programs start.
141 fStrings.removeShuffle(idx);
142 if (idx != fStrings.size()) {
143 SkTInsertionSort(fStrings.begin() + idx, fStrings.size() - idx, extension_compare);
144 }
145 return true;
146}
147
148void GrGLExtensions::add(const char ext[]) {
149 int idx = find_string(fStrings, ext);
150 if (idx < 0) {
151 // This is not the most effecient approach since we end up looking at all of the
152 // extensions after the add
153 fStrings.emplace_back(ext);
154 SkTInsertionSort(fStrings.begin(), fStrings.size(), extension_compare);
155 }
156}
157
158#ifdef SK_ENABLE_DUMP_GPU
160
161void GrGLExtensions::dumpJSON(SkJSONWriter* writer) const {
162 writer->beginArray();
163 for (int i = 0; i < fStrings.size(); ++i) {
164 writer->appendString(fStrings[i]);
165 }
166 writer->endArray();
167}
168#else
170#endif
#define GR_GL_NUM_EXTENSIONS
#define GR_EGL_EXTENSIONS
#define GR_GL_EXTENSIONS
#define GR_GL_VERSION
static int find_string(const TArray< SkString > &strings, const char ext[])
static void eat_space_sep_strings(TArray< SkString > *out, const char in[])
GrGLStandard
Definition GrGLTypes.h:19
#define GR_IS_GR_WEBGL(standard)
Definition GrGLTypes.h:50
int GrGLint
Definition GrGLTypes.h:108
void * GrEGLDisplay
Definition GrGLTypes.h:166
unsigned char GrGLubyte
Definition GrGLTypes.h:111
#define GR_IS_GR_GL(standard)
Definition GrGLTypes.h:48
#define GR_IS_GR_GL_ES(standard)
Definition GrGLTypes.h:49
GrGLVersion GrGLGetVersionFromString(const char *versionString)
Definition GrGLUtil.cpp:68
#define GR_GL_INVALID_VER
Definition GrGLUtil.h:37
#define GR_GL_VER(major, minor)
Definition GrGLUtil.h:26
uint32_t GrGLVersion
Definition GrGLUtil.h:22
#define SkASSERT(cond)
Definition SkAssert.h:116
void SkTInsertionSort(T *left, int count, const C &lessThan)
Definition SkTSort.h:114
void SkTQSort(T *begin, T *end, const C &lessThan)
Definition SkTSort.h:194
void add(const char[])
bool has(const char[]) const
bool init(GrGLStandard standard, GrGLFunction< GrGLGetStringFn > getString, GrGLFunction< GrGLGetStringiFn > getStringi, GrGLFunction< GrGLGetIntegervFn > getIntegerv, GrGLFunction< GrEGLQueryStringFn > queryString=nullptr, GrEGLDisplay eglDisplay=nullptr)
GrGLExtensions & operator=(const GrGLExtensions &)
void dumpJSON(SkJSONWriter *) const
bool remove(const char[])
void beginArray(const char *name=nullptr, bool multiline=true)
void appendString(const char *value, size_t size)
T * push_back_n(int n)
Definition SkTArray.h:262
bool empty() const
Definition SkTArray.h:194
void removeShuffle(int n)
Definition SkTArray.h:183
int size() const
Definition SkTArray.h:416
T & emplace_back(Args &&... args)
Definition SkTArray.h:243
static bool b
struct MyStruct a[10]
size_t length