gl_query_unittest.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <GLES2/gl2extchromium.h>
  7. #include <stdint.h>
  8. #include "base/logging.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"
  14. namespace gpu {
  15. class QueryTest : public testing::Test {
  16. protected:
  17. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  18. void TearDown() override { gl_.Destroy(); }
  19. GLManager gl_;
  20. };
  21. TEST_F(QueryTest, MultipleQueries) {
  22. EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
  23. EXPECT_TRUE(GLTestHelper::HasExtension(
  24. "GL_CHROMIUM_command_buffer_latency_query"));
  25. GLuint error_query = 0;
  26. GLuint commands_issue_query = 0;
  27. glGenQueriesEXT(1, &error_query);
  28. glGenQueriesEXT(1, &commands_issue_query);
  29. GLuint available;
  30. GLuint result;
  31. base::TimeTicks before = base::TimeTicks::Now();
  32. // Begin two queries of different types
  33. glBeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, commands_issue_query);
  34. glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM, error_query);
  35. glEnable(GL_TEXTURE_2D); // Generates an INVALID_ENUM error
  36. // End the two queries
  37. glEndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
  38. glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM);
  39. glFinish();
  40. base::TimeTicks after = base::TimeTicks::Now();
  41. // Check that we got result on both queries.
  42. available = 0;
  43. result = 0;
  44. glGetQueryObjectuivEXT(commands_issue_query,
  45. GL_QUERY_RESULT_AVAILABLE_EXT,
  46. &available);
  47. EXPECT_TRUE(available);
  48. glGetQueryObjectuivEXT(commands_issue_query, GL_QUERY_RESULT_EXT, &result);
  49. // Sanity check - the resulting delta is shorter than the time it took to
  50. // run this test.
  51. EXPECT_LE(result, base::TimeDelta(after - before).InMicroseconds());
  52. result = 0;
  53. available = 0;
  54. glGetQueryObjectuivEXT(error_query,
  55. GL_QUERY_RESULT_AVAILABLE_EXT,
  56. &available);
  57. EXPECT_TRUE(available);
  58. glGetQueryObjectuivEXT(error_query, GL_QUERY_RESULT_EXT, &result);
  59. EXPECT_EQ(static_cast<uint32_t>(GL_INVALID_ENUM), result);
  60. }
  61. TEST_F(QueryTest, GetErrorBasic) {
  62. EXPECT_TRUE(GLTestHelper::HasExtension("GL_CHROMIUM_get_error_query"));
  63. GLuint query = 0;
  64. glGenQueriesEXT(1, &query);
  65. GLuint query_status = 0;
  66. GLuint result = 0;
  67. glBeginQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM, query);
  68. glEnable(GL_TEXTURE_2D); // Generates an INVALID_ENUM error
  69. glEndQueryEXT(GL_GET_ERROR_QUERY_CHROMIUM);
  70. glFinish();
  71. query_status = 0;
  72. result = 0;
  73. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
  74. EXPECT_TRUE(result);
  75. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_status);
  76. EXPECT_EQ(static_cast<uint32_t>(GL_INVALID_ENUM), query_status);
  77. }
  78. TEST_F(QueryTest, DISABLED_LatencyQueryBasic) {
  79. EXPECT_TRUE(GLTestHelper::HasExtension(
  80. "GL_CHROMIUM_command_buffer_latency_query"));
  81. GLuint query = 0;
  82. glGenQueriesEXT(1, &query);
  83. GLuint query_result = 0;
  84. GLuint available = 0;
  85. // First test a query with a ~1ms "latency".
  86. const unsigned int kExpectedLatencyMicroseconds = 2000;
  87. const unsigned int kTimePrecisionMicroseconds = 1000;
  88. glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM, query);
  89. // Usually, we want to measure gpu-side latency, but we fake it by
  90. // adding client side latency for our test because it's easier.
  91. base::PlatformThread::Sleep(base::Microseconds(kExpectedLatencyMicroseconds));
  92. glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM);
  93. glFinish();
  94. query_result = 0;
  95. available = 0;
  96. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
  97. EXPECT_TRUE(available);
  98. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_result);
  99. EXPECT_GE(query_result, kExpectedLatencyMicroseconds
  100. - kTimePrecisionMicroseconds);
  101. EXPECT_LE(query_result, kExpectedLatencyMicroseconds
  102. + kTimePrecisionMicroseconds);
  103. // Then test a query with the lowest latency possible.
  104. glBeginQueryEXT(GL_LATENCY_QUERY_CHROMIUM, query);
  105. glEndQueryEXT(GL_LATENCY_QUERY_CHROMIUM);
  106. glFinish();
  107. query_result = 0;
  108. available = 0;
  109. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
  110. EXPECT_TRUE(available);
  111. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_result);
  112. EXPECT_LE(query_result, kTimePrecisionMicroseconds);
  113. }
  114. TEST_F(QueryTest, CommandsCompleted) {
  115. if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
  116. LOG(INFO) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
  117. return;
  118. }
  119. base::TimeTicks before = base::TimeTicks::Now();
  120. GLuint query;
  121. glGenQueriesEXT(1, &query);
  122. glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, query);
  123. glClearColor(0.0, 0.0, 1.0, 1.0);
  124. glClear(GL_COLOR_BUFFER_BIT);
  125. glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM);
  126. glFlush();
  127. GLuint result = 0;
  128. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &result);
  129. base::TimeTicks after = base::TimeTicks::Now();
  130. // Sanity check - the resulting delta is shorter than the time it took to
  131. // run this test.
  132. EXPECT_LE(result, base::TimeDelta(after - before).InMicroseconds());
  133. glDeleteQueriesEXT(1, &query);
  134. GLTestHelper::CheckGLError("no errors", __LINE__);
  135. }
  136. TEST_F(QueryTest, CommandsCompletedWithFinish) {
  137. if (!GLTestHelper::HasExtension("GL_CHROMIUM_sync_query")) {
  138. LOG(INFO) << "GL_CHROMIUM_sync_query not supported. Skipping test...";
  139. return;
  140. }
  141. GLuint query;
  142. glGenQueriesEXT(1, &query);
  143. glBeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, query);
  144. glClearColor(0.0, 0.0, 1.0, 1.0);
  145. glClear(GL_COLOR_BUFFER_BIT);
  146. glEndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM);
  147. glFinish();
  148. GLuint result = 0;
  149. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
  150. EXPECT_EQ(1u, result);
  151. glDeleteQueriesEXT(1, &query);
  152. GLTestHelper::CheckGLError("no errors", __LINE__);
  153. }
  154. } // namespace gpu