1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include "base/command_line.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_switches.h"
#include "ui/gl/gpu_timing.h"
namespace gfx {
class GLContextFake : public GLContext {
public:
bool Initialize(GLSurface* compatible_surface,
GpuPreference gpu_preference) override {
return true;
}
bool MakeCurrent(GLSurface* surface) override { return true; }
void ReleaseCurrent(GLSurface* surface) override {}
bool IsCurrent(GLSurface* surface) override { return true; }
void* GetHandle() override { return NULL; }
scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() override {
return NULL;
}
void OnSetSwapInterval(int interval) override {}
GLContextFake() : GLContext(NULL) {}
protected:
~GLContextFake() override {}
};
class GLApiTest : public testing::Test {
public:
void SetUp() override {
fake_extension_string_ = "";
fake_version_string_ = "";
num_fake_extension_strings_ = 0;
fake_extension_strings_ = nullptr;
DCHECK(!g_current_gl_context_tls);
g_current_gl_context_tls = new base::ThreadLocalPointer<GLApi>;
SetGLGetProcAddressProc(
static_cast<GLGetProcAddressProc>(&FakeGLGetProcAddress));
}
static void* GL_BINDING_CALL FakeGLGetProcAddress(const char *proc) {
return reinterpret_cast<void*>(0x1);
}
void TearDown() override {
api_.reset(nullptr);
delete g_current_gl_context_tls;
g_current_gl_context_tls = nullptr;
SetGLImplementation(kGLImplementationNone);
fake_extension_string_ = "";
fake_version_string_ = "";
num_fake_extension_strings_ = 0;
fake_extension_strings_ = nullptr;
}
void InitializeAPI(base::CommandLine* command_line) {
api_.reset(new RealGLApi());
g_current_gl_context_tls->Set(api_.get());
g_driver_gl.ClearBindings();
g_driver_gl.fn.glGetStringFn = &FakeGetString;
g_driver_gl.fn.glGetStringiFn = &FakeGetStringi;
g_driver_gl.fn.glGetIntegervFn = &FakeGetIntegervFn;
fake_context_ = new GLContextFake();
if (command_line)
api_->InitializeWithCommandLine(&g_driver_gl, command_line);
else
api_->Initialize(&g_driver_gl);
api_->InitializeFilteredExtensions();
g_driver_gl.InitializeCustomDynamicBindings(fake_context_.get());
}
void SetFakeExtensionString(const char* fake_string) {
SetGLImplementation(kGLImplementationDesktopGL);
fake_extension_string_ = fake_string;
fake_version_string_ = "2.1";
}
void SetFakeExtensionStrings(const char** fake_strings, uint32_t count) {
SetGLImplementation(kGLImplementationDesktopGL);
num_fake_extension_strings_ = count;
fake_extension_strings_ = fake_strings;
fake_version_string_ = "3.0";
}
static const GLubyte* GL_BINDING_CALL FakeGetString(GLenum name) {
if (name == GL_VERSION)
return reinterpret_cast<const GLubyte*>(fake_version_string_);
return reinterpret_cast<const GLubyte*>(fake_extension_string_);
}
static void GL_BINDING_CALL FakeGetIntegervFn(GLenum pname, GLint* params) {
*params = num_fake_extension_strings_;
}
static const GLubyte* GL_BINDING_CALL FakeGetStringi(GLenum name,
GLuint index) {
return (index < num_fake_extension_strings_) ?
reinterpret_cast<const GLubyte*>(fake_extension_strings_[index]) :
nullptr;
}
const char* GetExtensions() {
return reinterpret_cast<const char*>(api_->glGetStringFn(GL_EXTENSIONS));
}
uint32_t GetNumExtensions() {
GLint num_extensions = 0;
api_->glGetIntegervFn(GL_NUM_EXTENSIONS, &num_extensions);
return static_cast<uint32_t>(num_extensions);
}
const char* GetExtensioni(uint32_t index) {
return reinterpret_cast<const char*>(api_->glGetStringiFn(GL_EXTENSIONS,
index));
}
protected:
static const char* fake_extension_string_;
static const char* fake_version_string_;
static uint32_t num_fake_extension_strings_;
static const char** fake_extension_strings_;
scoped_refptr<GLContext> fake_context_;
scoped_ptr<DriverGL> driver_;
scoped_ptr<RealGLApi> api_;
};
const char* GLApiTest::fake_extension_string_ = "";
const char* GLApiTest::fake_version_string_ = "";
uint32_t GLApiTest::num_fake_extension_strings_ = 0;
const char** GLApiTest::fake_extension_strings_ = nullptr;
TEST_F(GLApiTest, DisabledExtensionStringTest) {
static const char* kFakeExtensions = "GL_EXT_1 GL_EXT_2 GL_EXT_3 GL_EXT_4";
static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
static const char* kFilteredExtensions = "GL_EXT_3 GL_EXT_4";
SetFakeExtensionString(kFakeExtensions);
InitializeAPI(nullptr);
EXPECT_STREQ(kFakeExtensions, GetExtensions());
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
kFakeDisabledExtensions);
InitializeAPI(&command_line);
EXPECT_STREQ(kFilteredExtensions, GetExtensions());
}
TEST_F(GLApiTest, DisabledExtensionBitTest) {
static const char* kFakeExtensions[] = {
"GL_ARB_timer_query"
};
static const char* kFakeDisabledExtensions = "GL_ARB_timer_query";
SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
InitializeAPI(nullptr);
EXPECT_TRUE(g_driver_gl.ext.b_GL_ARB_timer_query);
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
kFakeDisabledExtensions);
InitializeAPI(&command_line);
EXPECT_FALSE(g_driver_gl.ext.b_GL_ARB_timer_query);
}
TEST_F(GLApiTest, DisabledExtensionStringIndexTest) {
static const char* kFakeExtensions[] = {
"GL_EXT_1",
"GL_EXT_2",
"GL_EXT_3",
"GL_EXT_4"
};
static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
static const char* kFilteredExtensions[] = {
"GL_EXT_3",
"GL_EXT_4"
};
SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
InitializeAPI(nullptr);
EXPECT_EQ(arraysize(kFakeExtensions), GetNumExtensions());
for (uint32_t i = 0; i < arraysize(kFakeExtensions); ++i) {
EXPECT_STREQ(kFakeExtensions[i], GetExtensioni(i));
}
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
kFakeDisabledExtensions);
InitializeAPI(&command_line);
EXPECT_EQ(arraysize(kFilteredExtensions), GetNumExtensions());
for (uint32_t i = 0; i < arraysize(kFilteredExtensions); ++i) {
EXPECT_STREQ(kFilteredExtensions[i], GetExtensioni(i));
}
}
} // namespace gfx
|