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>
7 #include <GLES2/gl2extchromium.h>
9 #include "base/threading/platform_thread.h"
10 #include "gpu/command_buffer/tests/gl_manager.h"
11 #include "gpu/command_buffer/tests/gl_test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class QueryTest
: public testing::Test
{
19 void SetUp() override
{ gl_
.Initialize(GLManager::Options()); }
21 void TearDown() override
{ gl_
.Destroy(); }
26 TEST_F(QueryTest
, MultipleQueries
) {
27 EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
28 EXPECT_TRUE(GLTestHelper::HasExtension(
29 "GL_CHROMIUM_command_buffer_latency_query"));
31 GLuint error_query
= 0;
32 GLuint commands_issue_query
= 0;
33 glGenQueriesEXT(1, &error_query
);
34 glGenQueriesEXT(1, &commands_issue_query
);
39 base::TimeTicks before
= base::TimeTicks::Now();
41 // Begin two queries of different types
42 glBeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
, commands_issue_query
);
43 glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
, error_query
);
45 glEnable(GL_TEXTURE_2D
); // Generates an INVALID_ENUM error
47 // End the two queries
48 glEndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
);
49 glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
);
53 base::TimeTicks after
= base::TimeTicks::Now();
55 // Check that we got result on both queries.
59 glGetQueryObjectuivEXT(commands_issue_query
,
60 GL_QUERY_RESULT_AVAILABLE_EXT
,
62 EXPECT_TRUE(available
);
63 glGetQueryObjectuivEXT(commands_issue_query
, GL_QUERY_RESULT_EXT
, &result
);
64 // Sanity check - the resulting delta is shorter than the time it took to
66 EXPECT_LE(result
, base::TimeDelta(after
- before
).InMicroseconds());
70 glGetQueryObjectuivEXT(error_query
,
71 GL_QUERY_RESULT_AVAILABLE_EXT
,
73 EXPECT_TRUE(available
);
74 glGetQueryObjectuivEXT(error_query
, GL_QUERY_RESULT_EXT
, &result
);
75 EXPECT_EQ(static_cast<uint32
>(GL_INVALID_ENUM
), result
);
78 TEST_F(QueryTest
, GetErrorBasic
) {
79 EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
82 glGenQueriesEXT(1, &query
);
84 GLuint query_status
= 0;
87 glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
, query
);
88 glEnable(GL_TEXTURE_2D
); // Generates an INVALID_ENUM error
89 glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
);
95 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
97 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_status
);
98 EXPECT_EQ(static_cast<uint32
>(GL_INVALID_ENUM
), query_status
);
101 TEST_F(QueryTest
, DISABLED_LatencyQueryBasic
) {
102 EXPECT_TRUE(GLTestHelper::HasExtension(
103 "GL_CHROMIUM_command_buffer_latency_query"));
106 glGenQueriesEXT(1, &query
);
108 GLuint query_result
= 0;
109 GLuint available
= 0;
111 // First test a query with a ~1ms "latency".
112 const unsigned int kExpectedLatencyMicroseconds
= 2000;
113 const unsigned int kTimePrecisionMicroseconds
= 1000;
115 glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM
, query
);
116 // Usually, we want to measure gpu-side latency, but we fake it by
117 // adding client side latency for our test because it's easier.
118 base::PlatformThread::Sleep(
119 base::TimeDelta::FromMicroseconds(kExpectedLatencyMicroseconds
));
120 glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM
);
126 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &available
);
127 EXPECT_TRUE(available
);
128 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_result
);
129 EXPECT_GE(query_result
, kExpectedLatencyMicroseconds
130 - kTimePrecisionMicroseconds
);
131 EXPECT_LE(query_result
, kExpectedLatencyMicroseconds
132 + kTimePrecisionMicroseconds
);
134 // Then test a query with the lowest latency possible.
135 glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM
, query
);
136 glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM
);
142 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &available
);
143 EXPECT_TRUE(available
);
144 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_result
);
146 EXPECT_LE(query_result
, kTimePrecisionMicroseconds
);
149 TEST_F(QueryTest
, CommandsCompleted
) {
150 if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
151 LOG(INFO
) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
155 base::TimeTicks before
= base::TimeTicks::Now();
158 glGenQueriesEXT(1, &query
);
159 glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
, query
);
160 glClearColor(0.0, 0.0, 1.0, 1.0);
161 glClear(GL_COLOR_BUFFER_BIT
);
162 glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
);
165 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &result
);
167 base::TimeTicks after
= base::TimeTicks::Now();
168 // Sanity check - the resulting delta is shorter than the time it took to
170 EXPECT_LE(result
, base::TimeDelta(after
- before
).InMicroseconds());
172 glDeleteQueriesEXT(1, &query
);
173 GLTestHelper::CheckGLError("no errors", __LINE__
);
176 TEST_F(QueryTest
, CommandsCompletedWithFinish
) {
177 if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
178 LOG(INFO
) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
183 glGenQueriesEXT(1, &query
);
184 glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
, query
);
185 glClearColor(0.0, 0.0, 1.0, 1.0);
186 glClear(GL_COLOR_BUFFER_BIT
);
187 glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
);
190 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
191 EXPECT_EQ(1u, result
);
192 glDeleteQueriesEXT(1, &query
);
193 GLTestHelper::CheckGLError("no errors", __LINE__
);