Flutter Engine
 
Loading...
Searching...
No Matches
mock_gles.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6
7#include "GLES3/gl3.h"
8#include "fml/logging.h"
11
12namespace impeller {
13namespace testing {
14
15// OpenGLES is not thread safe.
16//
17// This mutex is used to ensure that only one test is using the mock at a time.
18static std::mutex g_test_lock;
19
20static std::weak_ptr<MockGLES> g_mock_gles;
21
22static std::vector<const char*> g_extensions;
23
24static const char* g_version;
25
26template <typename T, typename U>
27struct CheckSameSignature : std::false_type {};
28
29template <typename Ret, typename... Args>
30struct CheckSameSignature<Ret(Args...), Ret(Args...)> : std::true_type {};
31
32// This is a stub function that does nothing/records nothing.
33void doNothing() {}
34
35auto const kMockVendor = "MockGLES";
36const auto kMockShadingLanguageVersion = "GLSL ES 1.0";
37auto const kExtensions = std::vector<const char*>{
38 "GL_KHR_debug" //
39};
40
41namespace {
42
43template <typename T>
44struct function_traits;
45
46template <typename C, typename Ret, typename... Args>
47struct function_traits<Ret (C::*)(Args...)> {
48 using return_type = Ret;
49};
50
51template <typename Func, typename... Args>
52auto CallMockMethod(Func func, Args&&... args) {
53 if (auto mock_gles = g_mock_gles.lock()) {
54 if (mock_gles->GetImpl()) {
55 return (mock_gles->GetImpl()->*func)(std::forward<Args>(args)...);
56 }
57 }
58 return typename function_traits<Func>::return_type();
59}
60} // namespace
61
62const unsigned char* mockGetString(GLenum name) {
63 switch (name) {
64 case GL_VENDOR:
65 return reinterpret_cast<const unsigned char*>(kMockVendor);
66 case GL_VERSION:
67 return reinterpret_cast<const unsigned char*>(g_version);
68 case GL_SHADING_LANGUAGE_VERSION:
69 return reinterpret_cast<const unsigned char*>(
71 default:
72 return reinterpret_cast<const unsigned char*>("");
73 }
74}
75
76static_assert(CheckSameSignature<decltype(mockGetString), //
77 decltype(glGetString)>::value);
78
79const unsigned char* mockGetStringi(GLenum name, GLuint index) {
80 switch (name) {
81 case GL_EXTENSIONS:
82 return reinterpret_cast<const unsigned char*>(g_extensions[index]);
83 default:
84 return reinterpret_cast<const unsigned char*>("");
85 }
86}
87
88static_assert(CheckSameSignature<decltype(mockGetStringi), //
89 decltype(glGetStringi)>::value);
90
91void mockGetIntegerv(GLenum name, int* value) {
92 switch (name) {
93 case GL_NUM_EXTENSIONS: {
94 *value = g_extensions.size();
95 } break;
96 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
97 *value = 8;
98 break;
99 case GL_MAX_LABEL_LENGTH_KHR:
100 *value = 64;
101 break;
102 case GL_MAX_TEXTURE_SIZE:
103 *value = 4096;
104 break;
105 default:
106 CallMockMethod(&IMockGLESImpl::GetIntegerv, name, value);
107 break;
108 }
109}
110
111static_assert(CheckSameSignature<decltype(mockGetIntegerv), //
112 decltype(glGetIntegerv)>::value);
113
114GLenum mockGetError() {
115 return GL_NO_ERROR;
116}
117
118static_assert(CheckSameSignature<decltype(mockGetError), //
119 decltype(glGetError)>::value);
120
122
123static_assert(CheckSameSignature<decltype(mockPopDebugGroupKHR), //
124 decltype(glPopDebugGroupKHR)>::value);
125
126void mockPushDebugGroupKHR(GLenum source,
127 GLuint id,
128 GLsizei length,
129 const GLchar* message) {}
130
131static_assert(CheckSameSignature<decltype(mockPushDebugGroupKHR), //
132 decltype(glPushDebugGroupKHR)>::value);
133
134void mockGenQueriesEXT(GLsizei n, GLuint* ids) {
135 CallMockMethod(&IMockGLESImpl::GenQueriesEXT, n, ids);
136}
137
138static_assert(CheckSameSignature<decltype(mockGenQueriesEXT), //
139 decltype(glGenQueriesEXT)>::value);
140
141void mockBeginQueryEXT(GLenum target, GLuint id) {
142 CallMockMethod(&IMockGLESImpl::BeginQueryEXT, target, id);
143}
144
145static_assert(CheckSameSignature<decltype(mockBeginQueryEXT), //
146 decltype(glBeginQueryEXT)>::value);
147
148void mockEndQueryEXT(GLuint id) {
149 CallMockMethod(&IMockGLESImpl::EndQueryEXT, id);
150}
151
152static_assert(CheckSameSignature<decltype(mockEndQueryEXT), //
153 decltype(glEndQueryEXT)>::value);
154
155void mockGetQueryObjectuivEXT(GLuint id, GLenum target, GLuint* result) {
156 CallMockMethod(&IMockGLESImpl::GetQueryObjectuivEXT, id, target, result);
157}
158
159static_assert(CheckSameSignature<decltype(mockGetQueryObjectuivEXT), //
160 decltype(glGetQueryObjectuivEXT)>::value);
161
162void mockGetQueryObjectui64vEXT(GLuint id, GLenum target, GLuint64* result) {
163 CallMockMethod(&IMockGLESImpl::GetQueryObjectui64vEXT, id, target, result);
164}
165
166static_assert(CheckSameSignature<decltype(mockGetQueryObjectui64vEXT), //
167 decltype(glGetQueryObjectui64vEXT)>::value);
168
169void mockDeleteQueriesEXT(GLsizei size, const GLuint* queries) {
170 CallMockMethod(&IMockGLESImpl::DeleteQueriesEXT, size, queries);
171}
172
173void mockDeleteTextures(GLsizei size, const GLuint* queries) {
174 CallMockMethod(&IMockGLESImpl::DeleteTextures, size, queries);
175}
176
177static_assert(CheckSameSignature<decltype(mockDeleteQueriesEXT), //
178 decltype(glDeleteQueriesEXT)>::value);
179
180void mockUniform1fv(GLint location, GLsizei count, const GLfloat* value) {
181 CallMockMethod(&IMockGLESImpl::Uniform1fv, location, count, value);
182}
183static_assert(CheckSameSignature<decltype(mockUniform1fv), //
184 decltype(glUniform1fv)>::value);
185
186void mockGenTextures(GLsizei n, GLuint* textures) {
187 CallMockMethod(&IMockGLESImpl::GenTextures, n, textures);
188}
189
190static_assert(CheckSameSignature<decltype(mockGenTextures), //
191 decltype(glGenTextures)>::value);
192
193void mockGenBuffers(GLsizei n, GLuint* buffers) {
194 CallMockMethod(&IMockGLESImpl::GenBuffers, n, buffers);
195}
196
197static_assert(CheckSameSignature<decltype(mockGenTextures), //
198 decltype(glGenTextures)>::value);
199
200void mockObjectLabelKHR(GLenum identifier,
201 GLuint name,
202 GLsizei length,
203 const GLchar* label) {
204 CallMockMethod(&IMockGLESImpl::ObjectLabelKHR, identifier, name, length,
205 label);
206}
207static_assert(CheckSameSignature<decltype(mockObjectLabelKHR), //
208 decltype(glObjectLabelKHR)>::value);
209
210GLboolean mockIsTexture(GLuint texture) {
211 return CallMockMethod(&IMockGLESImpl::IsTexture, texture);
212}
213
214static_assert(CheckSameSignature<decltype(mockIsTexture), //
215 decltype(glIsTexture)>::value);
216
218 return CallMockMethod(&IMockGLESImpl::CheckFramebufferStatus, target);
219}
220
221static_assert(CheckSameSignature<decltype(mockCheckFramebufferStatus), //
222 decltype(glCheckFramebufferStatus)>::value);
223
224void mockGenFramebuffers(GLsizei n, GLuint* ids) {
225 return CallMockMethod(&IMockGLESImpl::GenFramebuffers, n, ids);
226}
227
228static_assert(CheckSameSignature<decltype(mockGenFramebuffers), //
229 decltype(glGenFramebuffers)>::value);
230
231void mockBindFramebuffer(GLenum target, GLuint framebuffer) {
232 return CallMockMethod(&IMockGLESImpl::BindFramebuffer, target, framebuffer);
233}
234
235static_assert(CheckSameSignature<decltype(mockBindFramebuffer), //
236 decltype(glBindFramebuffer)>::value);
237
238void mockReadPixels(GLint x,
239 GLint y,
240 GLsizei width,
241 GLsizei height,
242 GLenum format,
243 GLenum type,
244 void* data) {
245 return CallMockMethod(&IMockGLESImpl::ReadPixels, x, y, width, height, format,
246 type, data);
247}
248
250 GLsizei numAttachments,
251 const GLenum* attachments) {
252 return CallMockMethod(&IMockGLESImpl::DiscardFramebufferEXT, target,
253 numAttachments, attachments);
254}
255
256static_assert(CheckSameSignature<decltype(mockDiscardFramebufferEXT), //
257 decltype(glDiscardFramebufferEXT)>::value);
258
259// static
260std::shared_ptr<MockGLES> MockGLES::Init(
261 std::unique_ptr<MockGLESImpl> impl,
262 const std::optional<std::vector<const char*>>& extensions) {
263 FML_CHECK(g_test_lock.try_lock())
264 << "MockGLES is already being used by another test.";
265 g_extensions = extensions.value_or(kExtensions);
266 g_version = "OpenGL ES 3.0";
267 auto mock_gles = std::shared_ptr<MockGLES>(new MockGLES());
268 mock_gles->impl_ = std::move(impl);
269 g_mock_gles = mock_gles;
270 return mock_gles;
271}
272
273std::shared_ptr<MockGLES> MockGLES::Init(
274 const std::optional<std::vector<const char*>>& extensions,
275 const char* version_string,
276 ProcTableGLES::Resolver resolver) {
277 // If we cannot obtain a lock, MockGLES is already being used elsewhere.
278 FML_CHECK(g_test_lock.try_lock())
279 << "MockGLES is already being used by another test.";
280 g_extensions = extensions.value_or(kExtensions);
281 g_version = version_string;
282 auto mock_gles = std::shared_ptr<MockGLES>(new MockGLES(std::move(resolver)));
283 g_mock_gles = mock_gles;
284 return mock_gles;
285}
286
288 if (strcmp(name, "glPopDebugGroupKHR") == 0) {
289 return reinterpret_cast<void*>(&mockPopDebugGroupKHR);
290 } else if (strcmp(name, "glPushDebugGroupKHR") == 0) {
291 return reinterpret_cast<void*>(&mockPushDebugGroupKHR);
292 } else if (strcmp(name, "glGetString") == 0) {
293 return reinterpret_cast<void*>(&mockGetString);
294 } else if (strcmp(name, "glGetStringi") == 0) {
295 return reinterpret_cast<void*>(&mockGetStringi);
296 } else if (strcmp(name, "glGetIntegerv") == 0) {
297 return reinterpret_cast<void*>(&mockGetIntegerv);
298 } else if (strcmp(name, "glGetError") == 0) {
299 return reinterpret_cast<void*>(&mockGetError);
300 } else if (strcmp(name, "glGenQueriesEXT") == 0) {
301 return reinterpret_cast<void*>(&mockGenQueriesEXT);
302 } else if (strcmp(name, "glBeginQueryEXT") == 0) {
303 return reinterpret_cast<void*>(&mockBeginQueryEXT);
304 } else if (strcmp(name, "glEndQueryEXT") == 0) {
305 return reinterpret_cast<void*>(&mockEndQueryEXT);
306 } else if (strcmp(name, "glDeleteQueriesEXT") == 0) {
307 return reinterpret_cast<void*>(&mockDeleteQueriesEXT);
308 } else if (strcmp(name, "glDeleteTextures") == 0) {
309 return reinterpret_cast<void*>(&mockDeleteTextures);
310 } else if (strcmp(name, "glGetQueryObjectui64vEXT") == 0) {
311 return reinterpret_cast<void*>(mockGetQueryObjectui64vEXT);
312 } else if (strcmp(name, "glGetQueryObjectuivEXT") == 0) {
313 return reinterpret_cast<void*>(mockGetQueryObjectuivEXT);
314 } else if (strcmp(name, "glUniform1fv") == 0) {
315 return reinterpret_cast<void*>(mockUniform1fv);
316 } else if (strcmp(name, "glGenTextures") == 0) {
317 return reinterpret_cast<void*>(mockGenTextures);
318 } else if (strcmp(name, "glObjectLabelKHR") == 0) {
319 return reinterpret_cast<void*>(mockObjectLabelKHR);
320 } else if (strcmp(name, "glGenBuffers") == 0) {
321 return reinterpret_cast<void*>(mockGenBuffers);
322 } else if (strcmp(name, "glIsTexture") == 0) {
323 return reinterpret_cast<void*>(mockIsTexture);
324 } else if (strcmp(name, "glCheckFramebufferStatus") == 0) {
325 return reinterpret_cast<void*>(mockCheckFramebufferStatus);
326 } else if (strcmp(name, "glReadPixels") == 0) {
327 return reinterpret_cast<void*>(mockReadPixels);
328 } else if (strcmp(name, "glGenFramebuffers") == 0) {
329 return reinterpret_cast<void*>(mockGenFramebuffers);
330 } else if (strcmp(name, "glBindFramebuffer") == 0) {
331 return reinterpret_cast<void*>(mockBindFramebuffer);
332 } else if (strcmp(name, "glDiscardFramebufferEXT") == 0) {
333 return reinterpret_cast<void*>(mockDiscardFramebufferEXT);
334 } else {
335 return reinterpret_cast<void*>(&doNothing);
336 }
337};
338
339MockGLES::MockGLES(ProcTableGLES::Resolver resolver)
340 : proc_table_(std::move(resolver)) {}
341
342MockGLES::~MockGLES() {
343 g_test_lock.unlock();
344}
345
346} // namespace testing
347} // namespace impeller
GLenum type
std::function< void *(const char *function_name)> Resolver
virtual void GenFramebuffers(GLsizei n, GLuint *framebuffers)
Definition mock_gles.h:34
virtual void GenBuffers(GLsizei n, GLuint *buffers)
Definition mock_gles.h:66
virtual void GetQueryObjectui64vEXT(GLuint id, GLenum target, GLuint64 *result)
Definition mock_gles.h:62
virtual void GetQueryObjectuivEXT(GLuint id, GLenum target, GLuint *result)
Definition mock_gles.h:61
virtual void DeleteQueriesEXT(GLsizei size, const GLuint *queries)
Definition mock_gles.h:65
virtual void ObjectLabelKHR(GLenum identifier, GLuint name, GLsizei length, const GLchar *label)
Definition mock_gles.h:52
virtual void EndQueryEXT(GLuint id)
Definition mock_gles.h:60
virtual void DiscardFramebufferEXT(GLenum target, GLsizei numAttachments, const GLenum *attachments)
Definition mock_gles.h:69
virtual void DeleteTextures(GLsizei size, const GLuint *queries)
Definition mock_gles.h:22
virtual void GenQueriesEXT(GLsizei n, GLuint *ids)
Definition mock_gles.h:58
virtual void BeginQueryEXT(GLenum target, GLuint id)
Definition mock_gles.h:59
virtual void BindFramebuffer(GLenum target, GLuint framebuffer)
Definition mock_gles.h:35
virtual void GetIntegerv(GLenum name, GLint *attachments)
Definition mock_gles.h:72
virtual GLboolean IsTexture(GLuint texture)
Definition mock_gles.h:68
virtual void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels)
Definition mock_gles.h:44
virtual void Uniform1fv(GLint location, GLsizei count, const GLfloat *value)
Definition mock_gles.h:56
virtual void GenTextures(GLsizei n, GLuint *textures)
Definition mock_gles.h:23
virtual GLenum CheckFramebufferStatus(GLenum target)
Definition mock_gles.h:41
Provides a mocked version of the |ProcTableGLES| class.
Definition mock_gles.h:173
static std::shared_ptr< MockGLES > Init(std::unique_ptr< MockGLESImpl > impl, const std::optional< std::vector< const char * > > &extensions=std::nullopt)
Definition mock_gles.cc:260
int32_t value
int32_t x
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_BEGIN_DECLS GBytes * message
uint32_t uint32_t * format
uint32_t * target
#define FML_CHECK(condition)
Definition logging.h:104
size_t length
FlTexture * texture
double y
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
void mockDeleteQueriesEXT(GLsizei size, const GLuint *queries)
Definition mock_gles.cc:169
void mockGetQueryObjectuivEXT(GLuint id, GLenum target, GLuint *result)
Definition mock_gles.cc:155
static std::mutex g_test_lock
Definition mock_gles.cc:18
GLenum mockGetError()
Definition mock_gles.cc:114
void mockDiscardFramebufferEXT(GLenum target, GLsizei numAttachments, const GLenum *attachments)
Definition mock_gles.cc:249
const auto kMockShadingLanguageVersion
Definition mock_gles.cc:36
void mockPopDebugGroupKHR()
Definition mock_gles.cc:121
const unsigned char * mockGetStringi(GLenum name, GLuint index)
Definition mock_gles.cc:79
void mockGetIntegerv(GLenum name, int *value)
Definition mock_gles.cc:91
static std::vector< const char * > g_extensions
Definition mock_gles.cc:22
void mockGetQueryObjectui64vEXT(GLuint id, GLenum target, GLuint64 *result)
Definition mock_gles.cc:162
GLboolean mockIsTexture(GLuint texture)
Definition mock_gles.cc:210
auto const kExtensions
Definition mock_gles.cc:37
void mockReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data)
Definition mock_gles.cc:238
static std::weak_ptr< MockGLES > g_mock_gles
Definition mock_gles.cc:20
void mockDeleteTextures(GLsizei size, const GLuint *queries)
Definition mock_gles.cc:173
GLenum mockCheckFramebufferStatus(GLenum target)
Definition mock_gles.cc:217
void mockUniform1fv(GLint location, GLsizei count, const GLfloat *value)
Definition mock_gles.cc:180
void mockObjectLabelKHR(GLenum identifier, GLuint name, GLsizei length, const GLchar *label)
Definition mock_gles.cc:200
auto const kMockVendor
Definition mock_gles.cc:35
static const char * g_version
Definition mock_gles.cc:24
const ProcTableGLES::Resolver kMockResolverGLES
Definition mock_gles.cc:287
void mockPushDebugGroupKHR(GLenum source, GLuint id, GLsizei length, const GLchar *message)
Definition mock_gles.cc:126
void mockBindFramebuffer(GLenum target, GLuint framebuffer)
Definition mock_gles.cc:231
void mockEndQueryEXT(GLuint id)
Definition mock_gles.cc:148
void mockGenBuffers(GLsizei n, GLuint *buffers)
Definition mock_gles.cc:193
void mockGenFramebuffers(GLsizei n, GLuint *ids)
Definition mock_gles.cc:224
void mockGenQueriesEXT(GLsizei n, GLuint *ids)
Definition mock_gles.cc:134
const unsigned char * mockGetString(GLenum name)
Definition mock_gles.cc:62
void mockBeginQueryEXT(GLenum target, GLuint id)
Definition mock_gles.cc:141
void mockGenTextures(GLsizei n, GLuint *textures)
Definition mock_gles.cc:186
Definition ref_ptr.h:261
int32_t height
int32_t width