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.
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"
15 class OcclusionQueryTest
: public testing::Test
{
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
);
34 static void SetMatrix(float x
, float z
, float scale
, float* matrix
) {
56 void OcclusionQueryTest::DrawRect(float x
, float z
, float scale
, float* color
) {
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";
74 if (!GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean")) {
78 static const char* v_shader_str
=
79 "uniform mat4 worldMatrix;\n"
80 "attribute vec3 g_Position;\n"
83 " gl_Position = worldMatrix *\n"
84 " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n"
86 static const char* f_shader_str
=
87 "precision mediump float;"
88 "uniform vec4 color;\n"
91 " gl_FragColor = color;\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_
);
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
);
122 GLuint query_status
= 0;
124 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &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
);
140 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
142 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_status
);
143 EXPECT_TRUE(query_status
);
144 GLTestHelper::CheckGLError("no errors", __LINE__
);