Infobar material design refresh: bg color
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_query_unittest.cc
blobb12adc333e473d66801233a040824da2cbd2e582
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>
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"
15 namespace gpu {
17 class QueryTest : public testing::Test {
18 protected:
19 void SetUp() override { gl_.Initialize(GLManager::Options()); }
21 void TearDown() override { gl_.Destroy(); }
23 GLManager gl_;
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);
36 GLuint available;
37 GLuint result;
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);
51 glFinish();
53 base::TimeTicks after = base::TimeTicks::Now();
55 // Check that we got result on both queries.
57 available = 0;
58 result = 0;
59 glGetQueryObjectuivEXT(commands_issue_query,
60 GL_QUERY_RESULT_AVAILABLE_EXT,
61 &available);
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
65 // run this test.
66 EXPECT_LE(result, base::TimeDelta(after - before).InMicroseconds());
68 result = 0;
69 available = 0;
70 glGetQueryObjectuivEXT(error_query,
71 GL_QUERY_RESULT_AVAILABLE_EXT,
72 &available);
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"));
81 GLuint query = 0;
82 glGenQueriesEXT(1, &query);
84 GLuint query_status = 0;
85 GLuint result = 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);
91 glFinish();
93 query_status = 0;
94 result = 0;
95 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
96 EXPECT_TRUE(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"));
105 GLuint query = 0;
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);
122 glFinish();
124 query_result = 0;
125 available = 0;
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);
138 glFinish();
140 query_result = 0;
141 available = 0;
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...";
152 return;
155 base::TimeTicks before = base::TimeTicks::Now();
157 GLuint query;
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);
163 glFlush();
164 GLuint result = 0;
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
169 // run this test.
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...";
179 return;
182 GLuint query;
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);
188 glFinish();
189 GLuint result = 0;
190 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
191 EXPECT_EQ(1u, result);
192 glDeleteQueriesEXT(1, &query);
193 GLTestHelper::CheckGLError("no errors", __LINE__);
196 } // namespace gpu