1. With introduction of compressed formats, e.g. ETC1, the number of
[chromium-blink-merge.git] / ui / gl / gl_api_unittest.cc
blob9c9409a05a17abdabd7ef3d78d5f7c3fa4ef4669
1 // Copyright 2015 The Chromium 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.
5 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gl/gl_context.h"
9 #include "ui/gl/gl_gl_api_implementation.h"
10 #include "ui/gl/gl_implementation.h"
11 #include "ui/gl/gl_switches.h"
12 #include "ui/gl/gpu_timing.h"
14 namespace gfx {
16 class GLContextFake : public GLContext {
17 public:
18 bool Initialize(GLSurface* compatible_surface,
19 GpuPreference gpu_preference) override {
20 return true;
22 bool MakeCurrent(GLSurface* surface) override { return true; }
23 void ReleaseCurrent(GLSurface* surface) override {}
24 bool IsCurrent(GLSurface* surface) override { return true; }
25 void* GetHandle() override { return NULL; }
26 scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() override {
27 return NULL;
29 void OnSetSwapInterval(int interval) override {}
30 GLContextFake() : GLContext(NULL) {}
31 protected:
32 ~GLContextFake() override {}
35 class GLApiTest : public testing::Test {
36 public:
37 void SetUp() override {
38 fake_extension_string_ = "";
39 num_fake_extension_strings_ = 0;
40 fake_extension_strings_ = nullptr;
42 g_current_gl_context_tls = new base::ThreadLocalPointer<GLApi>;
43 api_.reset(new RealGLApi());
45 g_driver_gl.ClearBindings();
46 g_driver_gl.fn.glGetStringFn = &FakeGetString;
47 g_driver_gl.fn.glGetStringiFn = &FakeGetStringi;
48 g_driver_gl.fn.glGetIntegervFn = &FakeGetIntegervFn;
50 SetGLGetProcAddressProc(
51 static_cast<GLGetProcAddressProc>(&FakeGLGetProcAddress));
54 static void* GL_BINDING_CALL FakeGLGetProcAddress(const char *proc) {
55 return reinterpret_cast<void*>(0x1);
58 void TearDown() override {
59 api_.reset(nullptr);
60 delete g_current_gl_context_tls;
62 SetGLImplementation(kGLImplementationNone);
63 fake_extension_string_ = "";
64 num_fake_extension_strings_ = 0;
65 fake_extension_strings_ = nullptr;
68 void InitializeAPI(base::CommandLine* command_line) {
69 api_.reset(new RealGLApi());
70 g_current_gl_context_tls->Set(api_.get());
72 fake_context_ = new GLContextFake();
73 if (command_line)
74 api_->InitializeWithCommandLine(&g_driver_gl, command_line);
75 else
76 api_->Initialize(&g_driver_gl);
77 api_->InitializeFilteredExtensions();
78 g_driver_gl.InitializeCustomDynamicBindings(fake_context_.get());
81 void SetFakeExtensionString(const char* fake_string) {
82 SetGLImplementation(kGLImplementationDesktopGL);
83 fake_extension_string_ = fake_string;
86 void SetFakeExtensionStrings(const char** fake_strings, uint32_t count) {
87 SetGLImplementation(kGLImplementationDesktopGLCoreProfile);
88 num_fake_extension_strings_ = count;
89 fake_extension_strings_ = fake_strings;
92 static const GLubyte* GL_BINDING_CALL FakeGetString(GLenum name) {
93 return reinterpret_cast<const GLubyte*>(fake_extension_string_);
96 static void GL_BINDING_CALL FakeGetIntegervFn(GLenum pname, GLint* params) {
97 *params = num_fake_extension_strings_;
100 static const GLubyte* GL_BINDING_CALL FakeGetStringi(GLenum name,
101 GLuint index) {
102 return (index < num_fake_extension_strings_) ?
103 reinterpret_cast<const GLubyte*>(fake_extension_strings_[index]) :
104 nullptr;
107 const char* GetExtensions() {
108 return reinterpret_cast<const char*>(api_->glGetStringFn(GL_EXTENSIONS));
111 uint32_t GetNumExtensions() {
112 GLint num_extensions = 0;
113 api_->glGetIntegervFn(GL_NUM_EXTENSIONS, &num_extensions);
114 return static_cast<uint32_t>(num_extensions);
117 const char* GetExtensioni(uint32_t index) {
118 return reinterpret_cast<const char*>(api_->glGetStringiFn(GL_EXTENSIONS,
119 index));
122 protected:
123 static const char* fake_extension_string_;
125 static uint32_t num_fake_extension_strings_;
126 static const char** fake_extension_strings_;
128 scoped_refptr<GLContext> fake_context_;
129 scoped_ptr<DriverGL> driver_;
130 scoped_ptr<RealGLApi> api_;
133 const char* GLApiTest::fake_extension_string_ = "";
135 uint32_t GLApiTest::num_fake_extension_strings_ = 0;
136 const char** GLApiTest::fake_extension_strings_ = nullptr;
138 TEST_F(GLApiTest, DisabledExtensionStringTest) {
139 static const char* kFakeExtensions = "GL_EXT_1 GL_EXT_2 GL_EXT_3 GL_EXT_4";
140 static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
141 static const char* kFilteredExtensions = "GL_EXT_3 GL_EXT_4";
143 SetFakeExtensionString(kFakeExtensions);
144 InitializeAPI(nullptr);
146 EXPECT_STREQ(kFakeExtensions, GetExtensions());
148 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
149 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
150 kFakeDisabledExtensions);
151 InitializeAPI(&command_line);
153 EXPECT_STREQ(kFilteredExtensions, GetExtensions());
156 TEST_F(GLApiTest, DisabledExtensionBitTest) {
157 static const char* kFakeExtensions[] = {
158 "GL_ARB_timer_query"
160 static const char* kFakeDisabledExtensions = "GL_ARB_timer_query";
162 SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
163 InitializeAPI(nullptr);
165 EXPECT_TRUE(g_driver_gl.ext.b_GL_ARB_timer_query);
167 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
168 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
169 kFakeDisabledExtensions);
170 InitializeAPI(&command_line);
172 EXPECT_FALSE(g_driver_gl.ext.b_GL_ARB_timer_query);
175 TEST_F(GLApiTest, DisabledExtensionStringIndexTest) {
176 static const char* kFakeExtensions[] = {
177 "GL_EXT_1",
178 "GL_EXT_2",
179 "GL_EXT_3",
180 "GL_EXT_4"
182 static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
183 static const char* kFilteredExtensions[] = {
184 "GL_EXT_3",
185 "GL_EXT_4"
188 SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
189 InitializeAPI(nullptr);
191 EXPECT_EQ(arraysize(kFakeExtensions), GetNumExtensions());
192 for (uint32_t i = 0; i < arraysize(kFakeExtensions); ++i) {
193 EXPECT_STREQ(kFakeExtensions[i], GetExtensioni(i));
196 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
197 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
198 kFakeDisabledExtensions);
199 InitializeAPI(&command_line);
201 EXPECT_EQ(arraysize(kFilteredExtensions), GetNumExtensions());
202 for (uint32_t i = 0; i < arraysize(kFilteredExtensions); ++i) {
203 EXPECT_STREQ(kFilteredExtensions[i], GetExtensioni(i));
207 } // namespace gfx