| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/gl/gl_context_stub_with_extensions.h" | 8 #include "ui/gl/gl_context_stub_with_extensions.h" |
| 9 #include "ui/gl/gl_implementation.h" | 9 #include "ui/gl/gl_implementation.h" |
| 10 #include "ui/gl/gl_mock.h" | 10 #include "ui/gl/gl_mock.h" |
| 11 #include "ui/gl/gl_surface.h" | 11 #include "ui/gl/gl_surface.h" |
| 12 #include "ui/gl/gpu_preference.h" | 12 #include "ui/gl/gpu_preference.h" |
| 13 #include "ui/gl/gpu_timing.h" | 13 #include "ui/gl/gpu_timing.h" |
| 14 #include "ui/gl/gpu_timing_fake.h" |
| 14 | 15 |
| 15 namespace gfx { | 16 namespace gfx { |
| 16 | 17 |
| 17 class GPUTimingTest : public testing::Test { | 18 class GPUTimingTest : public testing::Test { |
| 18 public: | 19 public: |
| 19 void SetUp() override { | 20 void SetUp() override { |
| 20 setup_ = false; | 21 setup_ = false; |
| 21 fake_cpu_time_ = 0; | 22 fake_cpu_time_ = 0; |
| 22 | 23 cpu_time_bounded_ = false; |
| 23 CreateGPUTimingClient()->SetCpuTimeForTesting(base::Bind(&GetFakeCPUTime)); | |
| 24 } | 24 } |
| 25 | 25 |
| 26 void TearDown() override { | 26 void TearDown() override { |
| 27 if (setup_) { |
| 28 MockGLInterface::SetGLInterface(NULL); |
| 29 gfx::ClearGLBindings(); |
| 30 } |
| 31 setup_ = false; |
| 32 cpu_time_bounded_ = false; |
| 27 context_ = nullptr; | 33 context_ = nullptr; |
| 34 gl_.reset(); |
| 35 gpu_timing_fake_queries_.Reset(); |
| 28 } | 36 } |
| 29 | 37 |
| 30 void SetupGLContext(const char* gl_version, const char* gl_extensions) { | 38 void SetupGLContext(const char* gl_version, const char* gl_extensions) { |
| 31 ASSERT_FALSE(setup_) << "Cannot setup GL context twice."; | 39 ASSERT_FALSE(setup_) << "Cannot setup GL context twice."; |
| 32 gfx::SetGLGetProcAddressProc(gfx::MockGLInterface::GetGLProcAddress); | 40 SetGLGetProcAddressProc(MockGLInterface::GetGLProcAddress); |
| 33 gfx::GLSurface::InitializeOneOffWithMockBindingsForTests(); | 41 GLSurface::InitializeOneOffWithMockBindingsForTests(); |
| 34 gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>()); | 42 gl_.reset(new ::testing::StrictMock<MockGLInterface>()); |
| 35 ::gfx::MockGLInterface::SetGLInterface(gl_.get()); | 43 MockGLInterface::SetGLInterface(gl_.get()); |
| 36 | 44 |
| 37 context_ = new gfx::GLContextStubWithExtensions; | 45 context_ = new GLContextStubWithExtensions; |
| 38 context_->AddExtensionsString(gl_extensions); | 46 context_->AddExtensionsString(gl_extensions); |
| 39 context_->SetGLVersionString(gl_version); | 47 context_->SetGLVersionString(gl_version); |
| 48 gpu_timing_fake_queries_.Reset(); |
| 49 GLSurface::InitializeDynamicMockBindingsForTests(context_.get()); |
| 40 | 50 |
| 41 setup_ = true; | 51 setup_ = true; |
| 42 } | 52 } |
| 43 | 53 |
| 44 scoped_refptr<GPUTimingClient> CreateGPUTimingClient() { | 54 scoped_refptr<GPUTimingClient> CreateGPUTimingClient() { |
| 45 if (!setup_) { | 55 if (!setup_) { |
| 46 SetupGLContext("2.0", ""); | 56 SetupGLContext("2.0", ""); |
| 47 } | 57 } |
| 48 return context_->CreateGPUTimingClient(); | 58 |
| 59 scoped_refptr<GPUTimingClient> client = context_->CreateGPUTimingClient(); |
| 60 if (!cpu_time_bounded_) { |
| 61 client->SetCpuTimeForTesting(base::Bind(&GetFakeCPUTime)); |
| 62 cpu_time_bounded_ = true; |
| 63 } |
| 64 return client; |
| 49 } | 65 } |
| 50 | 66 |
| 51 void SetFakeCPUTime(int64_t fake_cpu_time) { | 67 void SetFakeCPUTime(int64_t fake_cpu_time) { |
| 52 fake_cpu_time_ = fake_cpu_time; | 68 fake_cpu_time_ = fake_cpu_time; |
| 53 } | 69 } |
| 54 | 70 |
| 55 protected: | 71 protected: |
| 56 static int64_t GetFakeCPUTime() { | 72 static int64_t GetFakeCPUTime() { |
| 57 return fake_cpu_time_; | 73 return fake_cpu_time_; |
| 58 } | 74 } |
| 59 | 75 |
| 60 private: | |
| 61 static int64_t fake_cpu_time_; | 76 static int64_t fake_cpu_time_; |
| 62 | 77 |
| 63 bool setup_ = false; | 78 bool setup_ = false; |
| 64 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_; | 79 bool cpu_time_bounded_ = false; |
| 65 scoped_refptr<gfx::GLContextStubWithExtensions> context_; | 80 scoped_ptr< ::testing::StrictMock<MockGLInterface> > gl_; |
| 81 scoped_refptr<GLContextStubWithExtensions> context_; |
| 82 GPUTimingFake gpu_timing_fake_queries_; |
| 66 }; | 83 }; |
| 67 | 84 |
| 68 int64_t GPUTimingTest::fake_cpu_time_ = 0; | 85 int64_t GPUTimingTest::fake_cpu_time_ = 0; |
| 69 | 86 |
| 70 TEST_F(GPUTimingTest, FakeTimerTest) { | 87 TEST_F(GPUTimingTest, FakeTimerTest) { |
| 71 // Tests that we can properly set fake cpu times. | 88 // Tests that we can properly set fake cpu times. |
| 72 SetFakeCPUTime(123); | 89 SetFakeCPUTime(123); |
| 73 | 90 |
| 74 scoped_refptr<GPUTimingClient> gpu_timing_client = CreateGPUTimingClient(); | 91 scoped_refptr<GPUTimingClient> gpu_timing_client = CreateGPUTimingClient(); |
| 75 EXPECT_EQ(123, gpu_timing_client->GetCurrentCPUTime()); | 92 EXPECT_EQ(123, gpu_timing_client->GetCurrentCPUTime()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 88 EXPECT_FALSE(client1->IsForceTimeElapsedQuery()); | 105 EXPECT_FALSE(client1->IsForceTimeElapsedQuery()); |
| 89 client_force->ForceTimeElapsedQuery(); | 106 client_force->ForceTimeElapsedQuery(); |
| 90 EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); | 107 EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); |
| 91 | 108 |
| 92 EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); | 109 EXPECT_TRUE(client1->IsForceTimeElapsedQuery()); |
| 93 | 110 |
| 94 scoped_refptr<GPUTimingClient> client2 = CreateGPUTimingClient(); | 111 scoped_refptr<GPUTimingClient> client2 = CreateGPUTimingClient(); |
| 95 EXPECT_TRUE(client2->IsForceTimeElapsedQuery()); | 112 EXPECT_TRUE(client2->IsForceTimeElapsedQuery()); |
| 96 } | 113 } |
| 97 | 114 |
| 115 TEST_F(GPUTimingTest, QueryTimeStampTest) { |
| 116 SetupGLContext("3.2", "GL_ARB_timer_query"); |
| 117 scoped_refptr<GPUTimingClient> client = CreateGPUTimingClient(); |
| 118 scoped_ptr<GPUTimer> gpu_timer = client->CreateGPUTimer(false); |
| 119 |
| 120 SetFakeCPUTime(123); |
| 121 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 122 10 * base::Time::kNanosecondsPerMicrosecond); |
| 123 gpu_timing_fake_queries_.ExpectGPUTimeStampQuery(*gl_, false); |
| 124 |
| 125 gpu_timer->QueryTimeStamp(); |
| 126 |
| 127 SetFakeCPUTime(122); |
| 128 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 129 9 * base::Time::kNanosecondsPerMicrosecond); |
| 130 EXPECT_FALSE(gpu_timer->IsAvailable()); |
| 131 |
| 132 SetFakeCPUTime(124); |
| 133 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 134 11 * base::Time::kNanosecondsPerMicrosecond); |
| 135 EXPECT_TRUE(gpu_timer->IsAvailable()); |
| 136 EXPECT_EQ(0, gpu_timer->GetDeltaElapsed()); |
| 137 |
| 138 int64 start, end; |
| 139 gpu_timer->GetStartEndTimestamps(&start, &end); |
| 140 EXPECT_EQ(123, start); |
| 141 EXPECT_EQ(123, end); |
| 142 } |
| 143 |
| 144 TEST_F(GPUTimingTest, QueryTimeStampUsingElapsedTest) { |
| 145 // Test timestamp queries using GL_EXT_timer_query which does not support |
| 146 // timestamp queries. Internally we fall back to time elapsed queries. |
| 147 SetupGLContext("3.2", "GL_EXT_timer_query"); |
| 148 scoped_refptr<GPUTimingClient> client = CreateGPUTimingClient(); |
| 149 scoped_ptr<GPUTimer> gpu_timer = client->CreateGPUTimer(false); |
| 150 ASSERT_TRUE(client->IsForceTimeElapsedQuery()); |
| 151 |
| 152 SetFakeCPUTime(123); |
| 153 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 154 10 * base::Time::kNanosecondsPerMicrosecond); |
| 155 gpu_timing_fake_queries_.ExpectGPUTimeStampQuery(*gl_, true); |
| 156 |
| 157 gpu_timer->QueryTimeStamp(); |
| 158 |
| 159 SetFakeCPUTime(122); |
| 160 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 161 9 * base::Time::kNanosecondsPerMicrosecond); |
| 162 EXPECT_FALSE(gpu_timer->IsAvailable()); |
| 163 |
| 164 SetFakeCPUTime(124); |
| 165 gpu_timing_fake_queries_.SetCurrentGLTime( |
| 166 11 * base::Time::kNanosecondsPerMicrosecond); |
| 167 EXPECT_TRUE(gpu_timer->IsAvailable()); |
| 168 EXPECT_EQ(0, gpu_timer->GetDeltaElapsed()); |
| 169 |
| 170 int64 start, end; |
| 171 gpu_timer->GetStartEndTimestamps(&start, &end); |
| 172 EXPECT_EQ(123, start); |
| 173 EXPECT_EQ(123, end); |
| 174 } |
| 175 |
| 98 } // namespace gpu | 176 } // namespace gpu |
| OLD | NEW |