Infobar material design refresh: bg color
[chromium-blink-merge.git] / gpu / command_buffer / tests / occlusion_query_unittest.cc
blob2f749c7797808a05e9b06af595b50a08a95c8a11
1 // Copyright 2014 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 <GLES2/gl2.h>
6 #include <GLES2/gl2ext.h>
8 #include "gpu/command_buffer/tests/gl_manager.h"
9 #include "gpu/command_buffer/tests/gl_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace gpu {
15 class OcclusionQueryTest : public testing::Test {
16 protected:
17 void SetUp() override {
18 GLManager::Options options;
19 options.size = gfx::Size(512, 512);
20 gl_.Initialize(options);
23 void TearDown() override { gl_.Destroy(); }
25 void DrawRect(float x, float z, float scale, float* color);
27 GLManager gl_;
29 GLint position_loc_;
30 GLint matrix_loc_;
31 GLint color_loc_;
34 static void SetMatrix(float x, float z, float scale, float* matrix) {
35 matrix[0] = scale;
36 matrix[1] = 0.0f;
37 matrix[2] = 0.0f;
38 matrix[3] = 0.0f;
40 matrix[4] = 0.0f;
41 matrix[5] = scale;
42 matrix[6] = 0.0f;
43 matrix[7] = 0.0f;
45 matrix[8] = 0.0f;
46 matrix[9] = 0.0f;
47 matrix[10] = scale;
48 matrix[11] = 0.0f;
50 matrix[12] = x;
51 matrix[13] = 0.0f;
52 matrix[14] = z;
53 matrix[15] = 1.0f;
56 void OcclusionQueryTest::DrawRect(float x, float z, float scale, float* color) {
57 GLfloat matrix[16];
59 SetMatrix(x, z, scale, matrix);
61 // Set up the model matrix
62 glUniformMatrix4fv(matrix_loc_, 1, GL_FALSE, matrix);
63 glUniform4fv(color_loc_, 1, color);
65 glDrawArrays(GL_TRIANGLES, 0, 6);
68 TEST_F(OcclusionQueryTest, Occlusion) {
69 #if defined(OS_MACOSX)
70 EXPECT_TRUE(GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean"))
71 << "GL_EXT_occlusion_query_boolean is required on OSX";
72 #endif
74 if (!GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean")) {
75 return;
78 static const char* v_shader_str =
79 "uniform mat4 worldMatrix;\n"
80 "attribute vec3 g_Position;\n"
81 "void main()\n"
82 "{\n"
83 " gl_Position = worldMatrix *\n"
84 " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n"
85 "}\n";
86 static const char* f_shader_str =
87 "precision mediump float;"
88 "uniform vec4 color;\n"
89 "void main()\n"
90 "{\n"
91 " gl_FragColor = color;\n"
92 "}\n";
94 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
96 position_loc_ = glGetAttribLocation(program, "g_Position");
97 matrix_loc_ = glGetUniformLocation(program, "worldMatrix");
98 color_loc_ = glGetUniformLocation(program, "color");
100 GLTestHelper::SetupUnitQuad(position_loc_);
102 GLuint query = 0;
103 glGenQueriesEXT(1, &query);
105 glEnable(GL_DEPTH_TEST);
106 glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
108 // Use the program object
109 glUseProgram(program);
111 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
112 static float red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
113 DrawRect(0, 0.0f, 0.50f, red);
115 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query);
116 static float blue[] = { 0.0f, 0.0f, 1.0f, 1.0f };
117 DrawRect(-0.125f, 0.1f, 0.25f, blue);
118 glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT);
120 glFinish();
122 GLuint query_status = 0;
123 GLuint result = 0;
124 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
125 EXPECT_TRUE(result);
126 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_status);
127 EXPECT_FALSE(query_status);
129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
130 DrawRect(1, 0.0f, 0.50f, red);
132 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query);
133 DrawRect(-0.125f, 0.1f, 0.25f, blue);
134 glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT);
136 glFinish();
138 query_status = 0;
139 result = 0;
140 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
141 EXPECT_TRUE(result);
142 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_status);
143 EXPECT_TRUE(query_status);
144 GLTestHelper::CheckGLError("no errors", __LINE__);
147 } // namespace gpu