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 virtual void SetUp() {
20 gl_
.Initialize(GLManager::Options());
23 virtual void TearDown() {
30 TEST_F(QueryTest
, MultipleQueries
) {
31 EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
32 EXPECT_TRUE(GLTestHelper::HasExtension(
33 "GL_CHROMIUM_command_buffer_latency_query"));
35 GLuint error_query
= 0;
36 GLuint commands_issue_query
= 0;
37 glGenQueriesEXT(1, &error_query
);
38 glGenQueriesEXT(1, &commands_issue_query
);
43 base::TimeTicks before
= base::TimeTicks::HighResNow();
45 // Begin two queries of different types
46 glBeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
, commands_issue_query
);
47 glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
, error_query
);
49 glEnable(GL_TEXTURE_2D
); // Generates an INVALID_ENUM error
51 // End the two queries
52 glEndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
);
53 glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
);
57 base::TimeTicks after
= base::TimeTicks::HighResNow();
59 // Check that we got result on both queries.
63 glGetQueryObjectuivEXT(commands_issue_query
,
64 GL_QUERY_RESULT_AVAILABLE_EXT
,
66 EXPECT_TRUE(available
);
67 glGetQueryObjectuivEXT(commands_issue_query
, GL_QUERY_RESULT_EXT
, &result
);
68 // Sanity check - the resulting delta is shorter than the time it took to
70 EXPECT_LT(result
, base::TimeDelta(after
- before
).InMicroseconds());
74 glGetQueryObjectuivEXT(error_query
,
75 GL_QUERY_RESULT_AVAILABLE_EXT
,
77 EXPECT_TRUE(available
);
78 glGetQueryObjectuivEXT(error_query
, GL_QUERY_RESULT_EXT
, &result
);
79 EXPECT_EQ(static_cast<uint32
>(GL_INVALID_ENUM
), result
);
82 TEST_F(QueryTest
, GetErrorBasic
) {
83 EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
86 glGenQueriesEXT(1, &query
);
88 GLuint query_status
= 0;
91 glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
, query
);
92 glEnable(GL_TEXTURE_2D
); // Generates an INVALID_ENUM error
93 glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM
);
99 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
101 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_status
);
102 EXPECT_EQ(static_cast<uint32
>(GL_INVALID_ENUM
), query_status
);
105 TEST_F(QueryTest
, DISABLED_LatencyQueryBasic
) {
106 EXPECT_TRUE(GLTestHelper::HasExtension(
107 "GL_CHROMIUM_command_buffer_latency_query"));
110 glGenQueriesEXT(1, &query
);
112 GLuint query_result
= 0;
113 GLuint available
= 0;
115 // First test a query with a ~1ms "latency".
116 const unsigned int kExpectedLatencyMicroseconds
= 2000;
117 const unsigned int kTimePrecisionMicroseconds
= 1000;
119 glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM
, query
);
120 // Usually, we want to measure gpu-side latency, but we fake it by
121 // adding client side latency for our test because it's easier.
122 base::PlatformThread::Sleep(
123 base::TimeDelta::FromMicroseconds(kExpectedLatencyMicroseconds
));
124 glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM
);
130 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &available
);
131 EXPECT_TRUE(available
);
132 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_result
);
133 EXPECT_GE(query_result
, kExpectedLatencyMicroseconds
134 - kTimePrecisionMicroseconds
);
135 EXPECT_LE(query_result
, kExpectedLatencyMicroseconds
136 + kTimePrecisionMicroseconds
);
138 // Then test a query with the lowest latency possible.
139 glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM
, query
);
140 glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM
);
146 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &available
);
147 EXPECT_TRUE(available
);
148 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &query_result
);
150 EXPECT_LE(query_result
, kTimePrecisionMicroseconds
);
153 TEST_F(QueryTest
, CommandsCompleted
) {
154 if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
155 LOG(INFO
) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
160 glGenQueriesEXT(1, &query
);
161 glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
, query
);
162 glClearColor(0.0, 0.0, 1.0, 1.0);
163 glClear(GL_COLOR_BUFFER_BIT
);
164 glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
);
167 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_EXT
, &result
);
168 EXPECT_EQ(0u, result
);
169 glDeleteQueriesEXT(1, &query
);
170 GLTestHelper::CheckGLError("no errors", __LINE__
);
173 TEST_F(QueryTest
, CommandsCompletedWithFinish
) {
174 if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
175 LOG(INFO
) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
180 glGenQueriesEXT(1, &query
);
181 glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
, query
);
182 glClearColor(0.0, 0.0, 1.0, 1.0);
183 glClear(GL_COLOR_BUFFER_BIT
);
184 glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
);
187 glGetQueryObjectuivEXT(query
, GL_QUERY_RESULT_AVAILABLE_EXT
, &result
);
188 EXPECT_EQ(1u, result
);
189 glDeleteQueriesEXT(1, &query
);
190 GLTestHelper::CheckGLError("no errors", __LINE__
);