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 virtual void SetUp() {
18 GLManager::Options options
;
19 options
.size
= gfx::Size(512, 512);
20 gl_
.Initialize(options
);
23 virtual void TearDown() {
27 void DrawRect(float x
, float z
, float scale
, float* color
);
36 static void SetMatrix(float x
, float z
, float scale
, float* matrix
) {
58 void OcclusionQueryTest::DrawRect(float x
, float z
, float scale
, float* color
) {
61 SetMatrix(x
, z
, scale
, matrix
);
63 // Set up the model matrix
64 glUniformMatrix4fv(matrix_loc_
, 1, GL_FALSE
, matrix
);
65 glUniform4fv(color_loc_
, 1, color
);
67 glDrawArrays(GL_TRIANGLES
, 0, 6);
70 TEST_F(OcclusionQueryTest
, Occlusion
) {
71 #if defined(OS_MACOSX)
72 EXPECT_TRUE(GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean"))
73 << "GL_EXT_occlusion_query_boolean is required on OSX";
76 if (!GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean")) {
80 static const char* v_shader_str
=
81 "uniform mat4 worldMatrix;\n"
82 "attribute vec3 g_Position;\n"
85 " gl_Position = worldMatrix *\n"
86 " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n"
88 static const char* f_shader_str
=
89 "precision mediump float;"
90 "uniform vec4 color;\n"
93 " gl_FragColor = color;\n"
96 GLuint program
= GLTestHelper::LoadProgram(v_shader_str
, f_shader_str
);
98 position_loc_
= glGetAttribLocation(program
, "g_Position");
99 matrix_loc_
= glGetUniformLocation(program
, "worldMatrix");
100 color_loc_
= glGetUniformLocation(program
, "color");
102 GLTestHelper::SetupUnitQuad(position_loc_
);
105 glGenQueriesEXT(1, &query
);
107 glEnable(GL_DEPTH_TEST
);
108 glClearColor(0.0f
, 0.1f
, 0.2f
, 1.0f
);
110 // Use the program object
111 glUseProgram(program
);
113 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
114 static float red
[] = { 1.0f
, 0.0f
, 0.0f
, 1.0f
};
115 DrawRect(0, 0.0f
, 0.50f
, red
);
117 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT
, query
);
118 static float blue
[] = { 0.0f
, 0.0f
, 1.0f
, 1.0f
};
119 DrawRect(-0.125f
, 0.1f
, 0.25f
, blue
);
120 glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT
);
124 GLuint query_status
= 0;
126 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
128 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_status
);
129 EXPECT_FALSE(query_status
);
131 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
132 DrawRect(1, 0.0f
, 0.50f
, red
);
134 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT
, query
);
135 DrawRect(-0.125f
, 0.1f
, 0.25f
, blue
);
136 glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT
);
142 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
144 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_status
);
145 EXPECT_TRUE(query_status
);
146 GLTestHelper::CheckGLError("no errors", __LINE__
);