dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 1 | // Copyright 2015 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 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 5 | #include "ui/gl/gpu_timing.h" |
| 6 | |
avi | 739878c | 2015-12-24 18:06:17 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 11 | #include "base/bind.h" |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
Geoff Lang | ceb64f7 | 2017-01-30 17:02:53 | [diff] [blame^] | 13 | #include "ui/gl/gl_context_stub.h" |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 14 | #include "ui/gl/gl_implementation.h" |
| 15 | #include "ui/gl/gl_mock.h" |
piman | 39f6769 | 2016-05-12 03:25:14 | [diff] [blame] | 16 | #include "ui/gl/gl_surface_stub.h" |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 17 | #include "ui/gl/gpu_preference.h" |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 18 | #include "ui/gl/gpu_timing_fake.h" |
kylechar | 1666624 | 2016-07-04 20:54:45 | [diff] [blame] | 19 | #include "ui/gl/init/gl_factory.h" |
sadrul | ba162cd | 2015-07-20 22:34:26 | [diff] [blame] | 20 | #include "ui/gl/test/gl_surface_test_support.h" |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 21 | |
kylechar | 7a46384 | 2016-05-26 14:46:12 | [diff] [blame] | 22 | namespace gl { |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 23 | |
ewell | 71c79ec | 2016-02-18 00:45:41 | [diff] [blame] | 24 | using ::testing::Exactly; |
| 25 | using ::testing::NotNull; |
| 26 | using ::testing::DoAll; |
| 27 | using ::testing::Return; |
| 28 | using ::testing::SetArgPointee; |
| 29 | |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 30 | class GPUTimingTest : public testing::Test { |
| 31 | public: |
| 32 | void SetUp() override { |
| 33 | setup_ = false; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 34 | cpu_time_bounded_ = false; |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void TearDown() override { |
piman | 39f6769 | 2016-05-12 03:25:14 | [diff] [blame] | 38 | context_ = nullptr; |
| 39 | surface_ = nullptr; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 40 | if (setup_) { |
| 41 | MockGLInterface::SetGLInterface(NULL); |
cwallez | 3f8acfc | 2016-12-02 23:10:02 | [diff] [blame] | 42 | init::ShutdownGL(); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 43 | } |
| 44 | setup_ = false; |
| 45 | cpu_time_bounded_ = false; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 46 | gl_.reset(); |
| 47 | gpu_timing_fake_queries_.Reset(); |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void SetupGLContext(const char* gl_version, const char* gl_extensions) { |
| 51 | ASSERT_FALSE(setup_) << "Cannot setup GL context twice."; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 52 | SetGLGetProcAddressProc(MockGLInterface::GetGLProcAddress); |
sadrul | ba162cd | 2015-07-20 22:34:26 | [diff] [blame] | 53 | GLSurfaceTestSupport::InitializeOneOffWithMockBindings(); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 54 | gl_.reset(new ::testing::StrictMock<MockGLInterface>()); |
| 55 | MockGLInterface::SetGLInterface(gl_.get()); |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 56 | |
Geoff Lang | ceb64f7 | 2017-01-30 17:02:53 | [diff] [blame^] | 57 | context_ = new GLContextStub; |
| 58 | context_->SetExtensionsString(gl_extensions); |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 59 | context_->SetGLVersionString(gl_version); |
kylechar | fd72190 | 2016-05-27 16:00:24 | [diff] [blame] | 60 | surface_ = new GLSurfaceStub; |
piman | 39f6769 | 2016-05-12 03:25:14 | [diff] [blame] | 61 | context_->MakeCurrent(surface_.get()); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 62 | gpu_timing_fake_queries_.Reset(); |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 63 | |
| 64 | setup_ = true; |
| 65 | } |
| 66 | |
| 67 | scoped_refptr<GPUTimingClient> CreateGPUTimingClient() { |
| 68 | if (!setup_) { |
| 69 | SetupGLContext("2.0", ""); |
| 70 | } |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 71 | |
| 72 | scoped_refptr<GPUTimingClient> client = context_->CreateGPUTimingClient(); |
| 73 | if (!cpu_time_bounded_) { |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 74 | client->SetCpuTimeForTesting(base::Bind(&GPUTimingFake::GetFakeCPUTime)); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 75 | cpu_time_bounded_ = true; |
| 76 | } |
| 77 | return client; |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 78 | } |
| 79 | |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 80 | protected: |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 81 | bool setup_ = false; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 82 | bool cpu_time_bounded_ = false; |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 83 | std::unique_ptr<::testing::StrictMock<MockGLInterface>> gl_; |
Geoff Lang | ceb64f7 | 2017-01-30 17:02:53 | [diff] [blame^] | 84 | scoped_refptr<GLContextStub> context_; |
kylechar | fd72190 | 2016-05-27 16:00:24 | [diff] [blame] | 85 | scoped_refptr<GLSurfaceStub> surface_; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 86 | GPUTimingFake gpu_timing_fake_queries_; |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 87 | }; |
| 88 | |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 89 | TEST_F(GPUTimingTest, FakeTimerTest) { |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 90 | scoped_refptr<GPUTimingClient> gpu_timing_client = CreateGPUTimingClient(); |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 91 | |
| 92 | // Tests that we can properly set fake cpu times. |
| 93 | gpu_timing_fake_queries_.SetCurrentCPUTime(123); |
dyen | 26bd4c4 | 2015-06-10 01:39:43 | [diff] [blame] | 94 | EXPECT_EQ(123, gpu_timing_client->GetCurrentCPUTime()); |
| 95 | |
| 96 | base::Callback<int64_t(void)> empty; |
| 97 | gpu_timing_client->SetCpuTimeForTesting(empty); |
| 98 | EXPECT_NE(123, gpu_timing_client->GetCurrentCPUTime()); |
| 99 | } |
| 100 | |
dyen | 1fa42196 | 2015-06-17 21:25:39 | [diff] [blame] | 101 | TEST_F(GPUTimingTest, ForceTimeElapsedQuery) { |
| 102 | // Test that forcing time elapsed query affects all clients. |
martina.kollarova | ab30699 | 2016-02-26 11:01:10 | [diff] [blame] | 103 | SetupGLContext("3.2", "GL_ARB_timer_query"); |
dyen | 1fa42196 | 2015-06-17 21:25:39 | [diff] [blame] | 104 | scoped_refptr<GPUTimingClient> client1 = CreateGPUTimingClient(); |
| 105 | EXPECT_FALSE(client1->IsForceTimeElapsedQuery()); |
| 106 | |
| 107 | scoped_refptr<GPUTimingClient> client_force = CreateGPUTimingClient(); |
| 108 | EXPECT_FALSE(client1->IsForceTimeElapsedQuery()); |
| 109 | client_force->ForceTimeElapsedQuery(); |
| 110 | EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); |
| 111 | |
| 112 | EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); |
| 113 | |
| 114 | scoped_refptr<GPUTimingClient> client2 = CreateGPUTimingClient(); |
| 115 | EXPECT_TRUE(client2->IsForceTimeElapsedQuery()); |
| 116 | } |
| 117 | |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 118 | TEST_F(GPUTimingTest, QueryTimeStampTest) { |
| 119 | SetupGLContext("3.2", "GL_ARB_timer_query"); |
| 120 | scoped_refptr<GPUTimingClient> client = CreateGPUTimingClient(); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 121 | std::unique_ptr<GPUTimer> gpu_timer = client->CreateGPUTimer(false); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 122 | |
dyen | ea6d840 | 2016-02-02 00:55:01 | [diff] [blame] | 123 | const int64_t begin_cpu_time = 1230; |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 124 | const int64_t begin_gl_time = 10 * base::Time::kNanosecondsPerMicrosecond; |
| 125 | const int64_t cpu_gl_offset = |
| 126 | begin_gl_time / base::Time::kNanosecondsPerMicrosecond - begin_cpu_time; |
| 127 | gpu_timing_fake_queries_.SetCPUGLOffset(cpu_gl_offset); |
| 128 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 129 | gpu_timing_fake_queries_.ExpectGPUTimeStampQuery(*gl_, false); |
| 130 | |
| 131 | gpu_timer->QueryTimeStamp(); |
| 132 | |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 133 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time - 1); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 134 | EXPECT_FALSE(gpu_timer->IsAvailable()); |
| 135 | |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 136 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 137 | EXPECT_TRUE(gpu_timer->IsAvailable()); |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 138 | |
| 139 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time + 1); |
| 140 | EXPECT_TRUE(gpu_timer->IsAvailable()); |
| 141 | |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 142 | EXPECT_EQ(0, gpu_timer->GetDeltaElapsed()); |
| 143 | |
avi | 739878c | 2015-12-24 18:06:17 | [diff] [blame] | 144 | int64_t start, end; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 145 | gpu_timer->GetStartEndTimestamps(&start, &end); |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 146 | EXPECT_EQ(begin_cpu_time, start); |
| 147 | EXPECT_EQ(begin_cpu_time, end); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST_F(GPUTimingTest, QueryTimeStampUsingElapsedTest) { |
| 151 | // Test timestamp queries using GL_EXT_timer_query which does not support |
| 152 | // timestamp queries. Internally we fall back to time elapsed queries. |
| 153 | SetupGLContext("3.2", "GL_EXT_timer_query"); |
| 154 | scoped_refptr<GPUTimingClient> client = CreateGPUTimingClient(); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 155 | std::unique_ptr<GPUTimer> gpu_timer = client->CreateGPUTimer(false); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 156 | ASSERT_TRUE(client->IsForceTimeElapsedQuery()); |
| 157 | |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 158 | const int64_t begin_cpu_time = 123; |
| 159 | const int64_t begin_gl_time = 10 * base::Time::kNanosecondsPerMicrosecond; |
| 160 | const int64_t cpu_gl_offset = begin_gl_time - begin_cpu_time; |
| 161 | gpu_timing_fake_queries_.SetCPUGLOffset(cpu_gl_offset); |
| 162 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 163 | gpu_timing_fake_queries_.ExpectGPUTimeStampQuery(*gl_, true); |
| 164 | |
| 165 | gpu_timer->QueryTimeStamp(); |
| 166 | |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 167 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time - 1); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 168 | EXPECT_FALSE(gpu_timer->IsAvailable()); |
| 169 | |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 170 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time + 1); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 171 | EXPECT_TRUE(gpu_timer->IsAvailable()); |
| 172 | EXPECT_EQ(0, gpu_timer->GetDeltaElapsed()); |
| 173 | |
avi | 739878c | 2015-12-24 18:06:17 | [diff] [blame] | 174 | int64_t start, end; |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 175 | gpu_timer->GetStartEndTimestamps(&start, &end); |
dyen | b245d37 | 2015-07-25 01:18:38 | [diff] [blame] | 176 | EXPECT_EQ(begin_cpu_time, start); |
| 177 | EXPECT_EQ(begin_cpu_time, end); |
dyen | da06bae | 2015-07-16 22:59:30 | [diff] [blame] | 178 | } |
| 179 | |
ewell | 71c79ec | 2016-02-18 00:45:41 | [diff] [blame] | 180 | TEST_F(GPUTimingTest, QueryTimestampUsingElapsedARBTest) { |
| 181 | // Test timestamp queries on platforms with GL_ARB_timer_query but still lack |
| 182 | // support for timestamp queries |
| 183 | SetupGLContext("3.2", "GL_ARB_timer_query"); |
| 184 | scoped_refptr<GPUTimingClient> client = CreateGPUTimingClient(); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 185 | std::unique_ptr<GPUTimer> gpu_timer = client->CreateGPUTimer(false); |
ewell | 71c79ec | 2016-02-18 00:45:41 | [diff] [blame] | 186 | |
| 187 | const int64_t begin_cpu_time = 123; |
| 188 | const int64_t begin_gl_time = 10 * base::Time::kNanosecondsPerMicrosecond; |
| 189 | const int64_t cpu_gl_offset = begin_gl_time - begin_cpu_time; |
| 190 | gpu_timing_fake_queries_.SetCPUGLOffset(cpu_gl_offset); |
| 191 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time); |
| 192 | |
| 193 | gpu_timing_fake_queries_.ExpectGPUTimeStampQuery(*gl_, true); |
| 194 | |
| 195 | // Custom mock override to ensure the timestamp bits are 0 |
| 196 | EXPECT_CALL(*gl_, GetQueryiv(GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, NotNull())) |
| 197 | .Times(Exactly(1)) |
| 198 | .WillRepeatedly(DoAll(SetArgPointee<2>(0), Return())); |
| 199 | |
| 200 | gpu_timer->QueryTimeStamp(); |
| 201 | |
| 202 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time - 1); |
| 203 | EXPECT_FALSE(gpu_timer->IsAvailable()); |
| 204 | |
| 205 | gpu_timing_fake_queries_.SetCurrentCPUTime(begin_cpu_time + 1); |
| 206 | EXPECT_TRUE(gpu_timer->IsAvailable()); |
| 207 | EXPECT_EQ(0, gpu_timer->GetDeltaElapsed()); |
| 208 | |
| 209 | int64_t start, end; |
| 210 | gpu_timer->GetStartEndTimestamps(&start, &end); |
| 211 | // Force time elapsed won't be set until a query is actually attempted |
| 212 | ASSERT_TRUE(client->IsForceTimeElapsedQuery()); |
| 213 | EXPECT_EQ(begin_cpu_time, start); |
| 214 | EXPECT_EQ(begin_cpu_time, end); |
| 215 | } |
| 216 | |
kylechar | fd72190 | 2016-05-27 16:00:24 | [diff] [blame] | 217 | } // namespace gl |