OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <map> | 5 #include <map> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "gpu/command_buffer/service/gpu_service_test.h" | 8 #include "gpu/command_buffer/service/gpu_service_test.h" |
9 #include "gpu/command_buffer/service/gpu_tracer.h" | 9 #include "gpu/command_buffer/service/gpu_tracer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/gl/gl_context_stub.h" |
11 #include "ui/gl/gl_mock.h" | 12 #include "ui/gl/gl_mock.h" |
| 13 #include "ui/gl/gl_surface_stub.h" |
12 | 14 |
13 namespace gpu { | 15 namespace gpu { |
14 namespace gles2 { | 16 namespace gles2 { |
15 | 17 |
| 18 using ::testing::_; |
| 19 using ::testing::AtLeast; |
| 20 using ::testing::Exactly; |
| 21 using ::testing::Invoke; |
| 22 using ::testing::NotNull; |
16 using ::testing::Return; | 23 using ::testing::Return; |
17 using ::testing::NotNull; | 24 |
18 using ::testing::AtLeast; | 25 class FakeCPUTime : public CPUTime { |
19 using ::testing::Invoke; | 26 public: |
20 using ::testing::_; | 27 FakeCPUTime() |
| 28 : current_cpu_time_(0) { |
| 29 } |
| 30 |
| 31 int64 GetCurrentTime() override { |
| 32 return current_cpu_time_; |
| 33 } |
| 34 |
| 35 void SetFakeCPUTime(int64 cpu_time) { |
| 36 current_cpu_time_ = cpu_time; |
| 37 } |
| 38 |
| 39 protected: |
| 40 ~FakeCPUTime() {} |
| 41 |
| 42 int64 current_cpu_time_; |
| 43 }; |
21 | 44 |
22 class MockOutputter : public Outputter { | 45 class MockOutputter : public Outputter { |
23 public: | 46 public: |
24 MockOutputter() {} | 47 MockOutputter() {} |
25 MOCK_METHOD4(Trace, | 48 MOCK_METHOD4(TraceDevice, |
26 void(const std::string& category, const std::string& name, | 49 void(const std::string& category, const std::string& name, |
27 int64 start_time, int64 end_time)); | 50 int64 start_time, int64 end_time)); |
28 | 51 |
| 52 MOCK_METHOD3(TraceServiceBegin, |
| 53 void(const std::string& category, const std::string& name, |
| 54 void* id)); |
| 55 |
| 56 MOCK_METHOD3(TraceServiceEnd, |
| 57 void(const std::string& category, const std::string& name, |
| 58 void* id)); |
| 59 |
29 protected: | 60 protected: |
30 ~MockOutputter() {} | 61 ~MockOutputter() {} |
31 }; | 62 }; |
32 | 63 |
33 class GlFakeQueries { | 64 class GlFakeQueries { |
34 public: | 65 public: |
35 GlFakeQueries() {} | 66 GlFakeQueries() {} |
36 | 67 |
37 void Reset() { | 68 void Reset() { |
38 current_time_ = 0; | 69 current_time_ = 0; |
39 next_query_id_ = 23; | 70 next_query_id_ = 23; |
40 alloced_queries_.clear(); | 71 alloced_queries_.clear(); |
41 query_timestamp_.clear(); | 72 query_timestamp_.clear(); |
42 } | 73 } |
43 | 74 |
44 void SetCurrentGLTime(GLint64 current_time) { current_time_ = current_time; } | 75 void SetCurrentGLTime(GLint64 current_time) { current_time_ = current_time; } |
| 76 void SetDisjoint() { disjointed_ = true; } |
45 | 77 |
46 void GenQueriesARB(GLsizei n, GLuint* ids) { | 78 void GenQueriesARB(GLsizei n, GLuint* ids) { |
47 for (GLsizei i = 0; i < n; i++) { | 79 for (GLsizei i = 0; i < n; i++) { |
48 ids[i] = next_query_id_++; | 80 ids[i] = next_query_id_++; |
49 alloced_queries_.insert(ids[i]); | 81 alloced_queries_.insert(ids[i]); |
50 } | 82 } |
51 } | 83 } |
52 | 84 |
53 void DeleteQueriesARB(GLsizei n, const GLuint* ids) { | 85 void DeleteQueriesARB(GLsizei n, const GLuint* ids) { |
54 for (GLsizei i = 0; i < n; i++) { | 86 for (GLsizei i = 0; i < n; i++) { |
55 alloced_queries_.erase(ids[i]); | 87 alloced_queries_.erase(ids[i]); |
56 query_timestamp_.erase(ids[i]); | 88 query_timestamp_.erase(ids[i]); |
57 } | 89 } |
58 } | 90 } |
59 | 91 |
60 void GetQueryObjectiv(GLuint id, GLenum pname, GLint* params) { | 92 void GetQueryObjectiv(GLuint id, GLenum pname, GLint* params) { |
61 switch (pname) { | 93 switch (pname) { |
62 case GL_QUERY_RESULT_AVAILABLE: { | 94 case GL_QUERY_RESULT_AVAILABLE: { |
63 std::map<GLuint, GLint64>::iterator it = query_timestamp_.find(id); | 95 std::map<GLuint, GLint64>::iterator it = query_timestamp_.find(id); |
64 if (it != query_timestamp_.end() && it->second <= current_time_) | 96 if (it != query_timestamp_.end() && it->second <= current_time_) |
65 *params = 1; | 97 *params = 1; |
66 else | 98 else |
67 *params = 0; | 99 *params = 0; |
68 break; | 100 break; |
69 } | 101 } |
70 default: | 102 default: |
71 ASSERT_TRUE(false); | 103 FAIL() << "Invalid variable passed to GetQueryObjectiv: " << pname; |
72 } | 104 } |
73 } | 105 } |
74 | 106 |
75 void QueryCounter(GLuint id, GLenum target) { | 107 void QueryCounter(GLuint id, GLenum target) { |
76 switch (target) { | 108 switch (target) { |
77 case GL_TIMESTAMP: | 109 case GL_TIMESTAMP: |
78 ASSERT_TRUE(alloced_queries_.find(id) != alloced_queries_.end()); | 110 ASSERT_TRUE(alloced_queries_.find(id) != alloced_queries_.end()); |
79 query_timestamp_[id] = current_time_; | 111 query_timestamp_[id] = current_time_; |
80 break; | 112 break; |
81 default: | 113 default: |
82 ASSERT_TRUE(false); | 114 FAIL() << "Invalid variable passed to QueryCounter: " << target; |
83 } | 115 } |
84 } | 116 } |
85 | 117 |
| 118 void GetInteger64v(GLenum pname, GLint64 * data) { |
| 119 switch (pname) { |
| 120 case GL_TIMESTAMP: |
| 121 *data = current_time_; |
| 122 break; |
| 123 default: |
| 124 FAIL() << "Invalid variable passed to GetInteger64v: " << pname; |
| 125 } |
| 126 } |
| 127 |
86 void GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64* params) { | 128 void GetQueryObjectui64v(GLuint id, GLenum pname, GLuint64* params) { |
87 switch (pname) { | 129 switch (pname) { |
88 case GL_QUERY_RESULT: | 130 case GL_QUERY_RESULT: |
89 ASSERT_TRUE(query_timestamp_.find(id) != query_timestamp_.end()); | 131 ASSERT_TRUE(query_timestamp_.find(id) != query_timestamp_.end()); |
90 *params = query_timestamp_.find(id)->second; | 132 *params = query_timestamp_.find(id)->second; |
91 break; | 133 break; |
92 default: | 134 default: |
93 ASSERT_TRUE(false); | 135 FAIL() << "Invalid variable passed to GetQueryObjectui64v: " << pname; |
94 } | 136 } |
| 137 } |
| 138 |
| 139 void GetIntegerv(GLenum pname, GLint* params) { |
| 140 switch (pname) { |
| 141 case GL_GPU_DISJOINT_EXT: |
| 142 *params = static_cast<GLint>(disjointed_); |
| 143 disjointed_ = false; |
| 144 break; |
| 145 default: |
| 146 FAIL() << "Invalid variable passed to GetIntegerv: " << pname; |
| 147 } |
| 148 } |
| 149 |
| 150 void Finish() { |
| 151 } |
| 152 |
| 153 GLenum GetError() { |
| 154 return GL_NO_ERROR; |
95 } | 155 } |
96 | 156 |
97 protected: | 157 protected: |
| 158 bool disjointed_; |
98 GLint64 current_time_; | 159 GLint64 current_time_; |
99 GLuint next_query_id_; | 160 GLuint next_query_id_; |
100 std::set<GLuint> alloced_queries_; | 161 std::set<GLuint> alloced_queries_; |
101 std::map<GLuint, GLint64> query_timestamp_; | 162 std::map<GLuint, GLint64> query_timestamp_; |
102 }; | 163 }; |
103 | 164 |
104 class BaseGpuTracerTest : public GpuServiceTest { | 165 class GPUTracerTester : public GPUTracer { |
105 public: | 166 public: |
106 BaseGpuTracerTest() {} | 167 GPUTracerTester(GpuTracerType tracer_type) |
107 | 168 : GPUTracer(NULL), |
108 /////////////////////////////////////////////////////////////////////////// | 169 tracing_enabled_(0), |
| 170 test_tracer_type_(tracer_type) { |
| 171 // Force tracing to be dependent on our mock variable here. |
| 172 gpu_trace_srv_category = &tracing_enabled_; |
| 173 gpu_trace_dev_category = &tracing_enabled_; |
| 174 } |
| 175 |
| 176 virtual ~GPUTracerTester() { |
| 177 } |
| 178 |
| 179 void SetTracingEnabled(bool enabled) { |
| 180 tracing_enabled_ = enabled ? 1 : 0; |
| 181 } |
| 182 |
| 183 void SetOutputter(scoped_refptr<Outputter> outputter) { |
| 184 set_outputter_ = outputter; |
| 185 } |
| 186 |
| 187 void SetCPUTime(scoped_refptr<CPUTime> cputime) { |
| 188 set_cputime_ = cputime; |
| 189 } |
| 190 |
| 191 protected: |
| 192 scoped_refptr<Outputter> CreateOutputter(const std::string& name) override { |
| 193 if (set_outputter_.get()) { |
| 194 return set_outputter_; |
| 195 } |
| 196 return new MockOutputter(); |
| 197 } |
| 198 |
| 199 scoped_refptr<CPUTime> CreateCPUTime() override { |
| 200 if (set_cputime_.get()) { |
| 201 return set_cputime_; |
| 202 } |
| 203 return new FakeCPUTime(); |
| 204 } |
| 205 |
| 206 GpuTracerType DetermineTracerType() override { |
| 207 return test_tracer_type_; |
| 208 } |
| 209 |
| 210 void PostTask() override { |
| 211 // Process synchronously. |
| 212 Process(); |
| 213 } |
| 214 |
| 215 unsigned char tracing_enabled_; |
| 216 GpuTracerType test_tracer_type_; |
| 217 |
| 218 scoped_refptr<Outputter> set_outputter_; |
| 219 scoped_refptr<CPUTime> set_cputime_; |
| 220 }; |
| 221 |
| 222 class BaseGpuTest : public GpuServiceTest { |
| 223 public: |
| 224 BaseGpuTest(GpuTracerType test_tracer_type) |
| 225 : test_tracer_type_(test_tracer_type) { |
| 226 } |
| 227 |
| 228 protected: |
| 229 virtual void SetUp() override { |
| 230 GpuServiceTest::SetUp(); |
| 231 gl_fake_queries_.Reset(); |
| 232 gl_surface_ = new gfx::GLSurfaceStub(); |
| 233 gl_context_ = new gfx::GLContextStub(); |
| 234 gl_context_->MakeCurrent(gl_surface_.get()); |
| 235 |
| 236 outputter_ref_ = new MockOutputter(); |
| 237 cpu_time_ref_ = new FakeCPUTime; |
| 238 } |
| 239 |
| 240 virtual void TearDown() override { |
| 241 outputter_ref_ = NULL; |
| 242 cpu_time_ref_ = NULL; |
| 243 |
| 244 gl_context_->ReleaseCurrent(gl_surface_.get()); |
| 245 gl_context_ = NULL; |
| 246 gl_surface_ = NULL; |
| 247 |
| 248 gl_.reset(); |
| 249 gl_fake_queries_.Reset(); |
| 250 GpuServiceTest::TearDown(); |
| 251 } |
| 252 |
| 253 void ExpectTraceQueryMocks() { |
| 254 if (GetTracerType() != kTracerTypeInvalid) { |
| 255 // Delegate query APIs used by GPUTrace to a GlFakeQueries |
| 256 EXPECT_CALL(*gl_, GenQueriesARB(2, NotNull())).Times(AtLeast(1)) |
| 257 .WillRepeatedly( |
| 258 Invoke(&gl_fake_queries_, &GlFakeQueries::GenQueriesARB)); |
| 259 |
| 260 EXPECT_CALL(*gl_, GetQueryObjectiv(_, GL_QUERY_RESULT_AVAILABLE, |
| 261 NotNull())) |
| 262 .WillRepeatedly( |
| 263 Invoke(&gl_fake_queries_, &GlFakeQueries::GetQueryObjectiv)); |
| 264 |
| 265 if (GetTracerType() == kTracerTypeDisjointTimer) { |
| 266 EXPECT_CALL(*gl_, GetInteger64v(GL_TIMESTAMP, _)) |
| 267 .WillRepeatedly( |
| 268 Invoke(&gl_fake_queries_, &GlFakeQueries::GetInteger64v)); |
| 269 } |
| 270 |
| 271 EXPECT_CALL(*gl_, QueryCounter(_, GL_TIMESTAMP)).Times(AtLeast(2)) |
| 272 .WillRepeatedly( |
| 273 Invoke(&gl_fake_queries_, &GlFakeQueries::QueryCounter)); |
| 274 |
| 275 EXPECT_CALL(*gl_, GetQueryObjectui64v(_, GL_QUERY_RESULT, NotNull())) |
| 276 .WillRepeatedly( |
| 277 Invoke(&gl_fake_queries_, |
| 278 &GlFakeQueries::GetQueryObjectui64v)); |
| 279 |
| 280 EXPECT_CALL(*gl_, DeleteQueriesARB(2, NotNull())).Times(AtLeast(1)) |
| 281 .WillRepeatedly( |
| 282 Invoke(&gl_fake_queries_, &GlFakeQueries::DeleteQueriesARB)); |
| 283 } |
| 284 } |
| 285 |
| 286 void ExpectOutputterBeginMocks(MockOutputter* outputter, |
| 287 const std::string& category, |
| 288 const std::string& name) { |
| 289 EXPECT_CALL(*outputter, |
| 290 TraceServiceBegin(category, name, NotNull())); |
| 291 } |
| 292 |
| 293 void ExpectOutputterEndMocks(MockOutputter* outputter, |
| 294 const std::string& category, |
| 295 const std::string& name, int64 expect_start_time, |
| 296 int64 expect_end_time, |
| 297 bool trace_device) { |
| 298 EXPECT_CALL(*outputter, |
| 299 TraceServiceEnd(category, name, NotNull())); |
| 300 |
| 301 if (trace_device) { |
| 302 EXPECT_CALL(*outputter, |
| 303 TraceDevice(category, name, |
| 304 expect_start_time, expect_end_time)) |
| 305 .Times(Exactly(1)); |
| 306 } else { |
| 307 EXPECT_CALL(*outputter, TraceDevice(category, name, |
| 308 expect_start_time, expect_end_time)) |
| 309 .Times(Exactly(0)); |
| 310 } |
| 311 } |
| 312 |
| 313 void ExpectOutputterMocks(MockOutputter* outputter, |
| 314 const std::string& category, |
| 315 const std::string& name, int64 expect_start_time, |
| 316 int64 expect_end_time) { |
| 317 ExpectOutputterBeginMocks(outputter, category, name); |
| 318 ExpectOutputterEndMocks(outputter, category, name, |
| 319 expect_start_time, expect_end_time, |
| 320 GetTracerType() != kTracerTypeInvalid); |
| 321 } |
| 322 |
| 323 void ExpectTracerOffsetQueryMocks() { |
| 324 // Disjoint check should only be called by kTracerTypeDisjointTimer type. |
| 325 if (GetTracerType() == kTracerTypeDisjointTimer) { |
| 326 EXPECT_CALL(*gl_, GetIntegerv(GL_GPU_DISJOINT_EXT, _)).Times(AtLeast(1)) |
| 327 .WillRepeatedly( |
| 328 Invoke(&gl_fake_queries_, &GlFakeQueries::GetIntegerv)); |
| 329 } else { |
| 330 EXPECT_CALL(*gl_, GetIntegerv(GL_GPU_DISJOINT_EXT, _)).Times(Exactly(0)); |
| 331 } |
| 332 |
| 333 // Timer offset calculation should only happen for the regular timer. |
| 334 if (GetTracerType() != kTracerTypeARBTimer) { |
| 335 EXPECT_CALL(*gl_, GenQueriesARB(_, NotNull())).Times(Exactly(0)); |
| 336 EXPECT_CALL(*gl_, Finish()).Times(Exactly(0)); |
| 337 EXPECT_CALL(*gl_, QueryCounter(_, GL_TIMESTAMP)).Times(Exactly(0)); |
| 338 EXPECT_CALL(*gl_, GetQueryObjectui64v(_, GL_QUERY_RESULT, NotNull())) |
| 339 .Times(Exactly(0)); |
| 340 EXPECT_CALL(*gl_, DeleteQueriesARB(_, NotNull())).Times(Exactly(0)); |
| 341 } else { |
| 342 EXPECT_CALL(*gl_, GenQueriesARB(_, NotNull())).Times(AtLeast(1)) |
| 343 .WillRepeatedly( |
| 344 Invoke(&gl_fake_queries_, &GlFakeQueries::GenQueriesARB)); |
| 345 |
| 346 EXPECT_CALL(*gl_, Finish()).Times(AtLeast(2)) |
| 347 .WillRepeatedly( |
| 348 Invoke(&gl_fake_queries_, &GlFakeQueries::Finish)); |
| 349 |
| 350 EXPECT_CALL(*gl_, QueryCounter(_, GL_TIMESTAMP)) |
| 351 .Times(AtLeast(1)) |
| 352 .WillRepeatedly( |
| 353 Invoke(&gl_fake_queries_, &GlFakeQueries::QueryCounter)); |
| 354 |
| 355 EXPECT_CALL(*gl_, GetQueryObjectui64v(_, GL_QUERY_RESULT, NotNull())) |
| 356 .Times(AtLeast(1)) |
| 357 .WillRepeatedly( |
| 358 Invoke(&gl_fake_queries_, |
| 359 &GlFakeQueries::GetQueryObjectui64v)); |
| 360 |
| 361 EXPECT_CALL(*gl_, DeleteQueriesARB(1, NotNull())) |
| 362 .Times(AtLeast(1)) |
| 363 .WillRepeatedly( |
| 364 Invoke(&gl_fake_queries_, &GlFakeQueries::DeleteQueriesARB)); |
| 365 } |
| 366 } |
| 367 |
| 368 GpuTracerType GetTracerType() { return test_tracer_type_; } |
| 369 |
| 370 GpuTracerType test_tracer_type_; |
| 371 GlFakeQueries gl_fake_queries_; |
| 372 |
| 373 scoped_refptr<MockOutputter> outputter_ref_; |
| 374 scoped_refptr<FakeCPUTime> cpu_time_ref_; |
| 375 |
| 376 scoped_refptr<gfx::GLSurface> gl_surface_; |
| 377 scoped_refptr<gfx::GLContext> gl_context_; |
| 378 }; |
| 379 |
| 380 // Test GPUTrace calls all the correct gl calls. |
| 381 class BaseGpuTraceTest : public BaseGpuTest { |
| 382 public: |
| 383 BaseGpuTraceTest(GpuTracerType test_tracer_type) |
| 384 : BaseGpuTest(test_tracer_type) { |
| 385 } |
109 | 386 |
110 void DoTraceTest() { | 387 void DoTraceTest() { |
111 MockOutputter* outputter = new MockOutputter(); | |
112 scoped_refptr<Outputter> outputter_ref = outputter; | |
113 | |
114 SetupTimerQueryMocks(); | |
115 | |
116 // Expected results | 388 // Expected results |
117 const std::string category_name("trace_category"); | 389 const std::string category_name("trace_category"); |
118 const std::string trace_name("trace_test"); | 390 const std::string trace_name("trace_test"); |
119 const int64 offset_time = 3231; | 391 const int64 offset_time = 3231; |
120 const GLint64 start_timestamp = 7 * base::Time::kNanosecondsPerMicrosecond; | 392 const GLint64 start_timestamp = 7 * base::Time::kNanosecondsPerMicrosecond; |
121 const GLint64 end_timestamp = 32 * base::Time::kNanosecondsPerMicrosecond; | 393 const GLint64 end_timestamp = 32 * base::Time::kNanosecondsPerMicrosecond; |
122 const int64 expect_start_time = | 394 const int64 expect_start_time = |
123 (start_timestamp / base::Time::kNanosecondsPerMicrosecond) + | 395 (start_timestamp / base::Time::kNanosecondsPerMicrosecond) + |
124 offset_time; | 396 offset_time; |
125 const int64 expect_end_time = | 397 const int64 expect_end_time = |
126 (end_timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_time; | 398 (end_timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_time; |
127 | 399 |
128 // Expected Outputter::Trace call | 400 ExpectTraceQueryMocks(); |
129 EXPECT_CALL(*outputter, | 401 ExpectOutputterMocks(outputter_ref_.get(), category_name, trace_name, |
130 Trace(category_name, trace_name, | 402 expect_start_time, expect_end_time); |
131 expect_start_time, expect_end_time)); | |
132 | 403 |
133 scoped_refptr<GPUTrace> trace = | 404 scoped_refptr<GPUTrace> trace = |
134 new GPUTrace(outputter_ref, category_name, trace_name, | 405 new GPUTrace(outputter_ref_, cpu_time_ref_, category_name, trace_name, |
135 offset_time, GetTracerType()); | 406 offset_time, GetTracerType()); |
136 | 407 |
137 gl_fake_queries_.SetCurrentGLTime(start_timestamp); | 408 gl_fake_queries_.SetCurrentGLTime(start_timestamp); |
138 trace->Start(true); | 409 trace->Start(true); |
139 | 410 |
140 // Shouldn't be available before End() call | 411 // Shouldn't be available before End() call |
141 gl_fake_queries_.SetCurrentGLTime(end_timestamp); | 412 gl_fake_queries_.SetCurrentGLTime(end_timestamp); |
142 EXPECT_FALSE(trace->IsAvailable()); | 413 EXPECT_FALSE(trace->IsAvailable()); |
143 | 414 |
144 trace->End(true); | 415 trace->End(true); |
145 | 416 |
146 // Shouldn't be available until the queries complete | 417 // Shouldn't be available until the queries complete |
147 gl_fake_queries_.SetCurrentGLTime(end_timestamp - | 418 gl_fake_queries_.SetCurrentGLTime(end_timestamp - |
148 base::Time::kNanosecondsPerMicrosecond); | 419 base::Time::kNanosecondsPerMicrosecond); |
149 EXPECT_FALSE(trace->IsAvailable()); | 420 EXPECT_FALSE(trace->IsAvailable()); |
150 | 421 |
151 // Now it should be available | 422 // Now it should be available |
152 gl_fake_queries_.SetCurrentGLTime(end_timestamp); | 423 gl_fake_queries_.SetCurrentGLTime(end_timestamp); |
153 EXPECT_TRUE(trace->IsAvailable()); | 424 EXPECT_TRUE(trace->IsAvailable()); |
154 | 425 |
155 // Proces should output expected Trace results to MockOutputter | 426 // Proces should output expected Trace results to MockOutputter |
156 trace->Process(); | 427 trace->Process(); |
157 } | 428 |
158 | 429 outputter_ref_ = NULL; |
159 protected: | 430 cpu_time_ref_ = NULL; |
160 void SetUp() override { | 431 } |
161 GpuServiceTest::SetUp(); | 432 }; |
162 gl_fake_queries_.Reset(); | 433 |
163 } | 434 class GpuARBTimerTraceTest : public BaseGpuTraceTest { |
164 | 435 public: |
165 void TearDown() override { | 436 GpuARBTimerTraceTest() |
166 gl_.reset(); | 437 : BaseGpuTraceTest(kTracerTypeARBTimer) { |
167 gl_fake_queries_.Reset(); | 438 } |
168 GpuServiceTest::TearDown(); | 439 }; |
169 } | 440 |
170 | 441 class GpuDisjointTimerTraceTest : public BaseGpuTraceTest { |
171 virtual void SetupTimerQueryMocks() { | 442 public: |
172 // Delegate query APIs used by GPUTrace to a GlFakeQueries | 443 GpuDisjointTimerTraceTest() |
173 EXPECT_CALL(*gl_, GenQueriesARB(_, NotNull())).Times(AtLeast(1)).WillOnce( | 444 : BaseGpuTraceTest(kTracerTypeDisjointTimer) { |
174 Invoke(&gl_fake_queries_, &GlFakeQueries::GenQueriesARB)); | 445 } |
175 | 446 }; |
176 EXPECT_CALL(*gl_, GetQueryObjectiv(_, GL_QUERY_RESULT_AVAILABLE, NotNull())) | 447 |
177 .Times(AtLeast(2)) | 448 TEST_F(GpuARBTimerTraceTest, ARBTimerTraceTest) { |
178 .WillRepeatedly( | 449 DoTraceTest(); |
179 Invoke(&gl_fake_queries_, &GlFakeQueries::GetQueryObjectiv)); | 450 } |
180 | 451 |
181 EXPECT_CALL(*gl_, QueryCounter(_, GL_TIMESTAMP)) | 452 TEST_F(GpuDisjointTimerTraceTest, DisjointTimerTraceTest) { |
182 .Times(AtLeast(2)) | 453 DoTraceTest(); |
183 .WillRepeatedly( | 454 } |
184 Invoke(&gl_fake_queries_, &GlFakeQueries::QueryCounter)); | 455 |
185 | 456 // Test GPUTracer calls all the correct gl calls. |
186 EXPECT_CALL(*gl_, GetQueryObjectui64v(_, GL_QUERY_RESULT, NotNull())) | 457 class BaseGpuTracerTest : public BaseGpuTest { |
187 .Times(AtLeast(2)) | 458 public: |
188 .WillRepeatedly( | 459 BaseGpuTracerTest(GpuTracerType test_tracer_type) |
189 Invoke(&gl_fake_queries_, &GlFakeQueries::GetQueryObjectui64v)); | 460 : BaseGpuTest(test_tracer_type) { |
190 | 461 } |
191 EXPECT_CALL(*gl_, DeleteQueriesARB(2, NotNull())) | 462 |
192 .Times(AtLeast(1)) | 463 void DoBasicTracerTest() { |
193 .WillRepeatedly( | 464 ExpectTracerOffsetQueryMocks(); |
194 Invoke(&gl_fake_queries_, &GlFakeQueries::DeleteQueriesARB)); | 465 |
195 } | 466 GPUTracerTester tracer(test_tracer_type_); |
196 | 467 tracer.SetTracingEnabled(true); |
197 virtual GpuTracerType GetTracerType() = 0; | 468 |
198 | 469 tracer.SetOutputter(outputter_ref_); |
199 GlFakeQueries gl_fake_queries_; | 470 tracer.SetCPUTime(cpu_time_ref_); |
| 471 |
| 472 ASSERT_TRUE(tracer.BeginDecoding()); |
| 473 ASSERT_TRUE(tracer.EndDecoding()); |
| 474 |
| 475 outputter_ref_ = NULL; |
| 476 cpu_time_ref_ = NULL; |
| 477 } |
| 478 |
| 479 void DoTracerMarkersTest() { |
| 480 ExpectTracerOffsetQueryMocks(); |
| 481 |
| 482 EXPECT_CALL(*gl_, GetError()).Times(AtLeast(0)) |
| 483 .WillRepeatedly( |
| 484 Invoke(&gl_fake_queries_, &GlFakeQueries::GetError)); |
| 485 |
| 486 const std::string category_name("trace_category"); |
| 487 const std::string trace_name("trace_test"); |
| 488 const int64 offset_time = 3231; |
| 489 const GLint64 start_timestamp = 7 * base::Time::kNanosecondsPerMicrosecond; |
| 490 const GLint64 end_timestamp = 32 * base::Time::kNanosecondsPerMicrosecond; |
| 491 const int64 expect_start_time = |
| 492 (start_timestamp / base::Time::kNanosecondsPerMicrosecond) + |
| 493 offset_time; |
| 494 const int64 expect_end_time = |
| 495 (end_timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_time; |
| 496 |
| 497 GPUTracerTester tracer(test_tracer_type_); |
| 498 tracer.SetTracingEnabled(true); |
| 499 |
| 500 tracer.SetOutputter(outputter_ref_); |
| 501 tracer.SetCPUTime(cpu_time_ref_); |
| 502 |
| 503 gl_fake_queries_.SetCurrentGLTime(start_timestamp); |
| 504 cpu_time_ref_->SetFakeCPUTime(expect_start_time); |
| 505 |
| 506 ASSERT_TRUE(tracer.BeginDecoding()); |
| 507 |
| 508 ExpectTraceQueryMocks(); |
| 509 |
| 510 // This will test multiple marker sources which overlap one another. |
| 511 for (int i = 0; i < NUM_TRACER_SOURCES; ++i) { |
| 512 // Set times so each source has a different time. |
| 513 gl_fake_queries_.SetCurrentGLTime( |
| 514 start_timestamp + |
| 515 (i * base::Time::kNanosecondsPerMicrosecond)); |
| 516 cpu_time_ref_->SetFakeCPUTime(expect_start_time + i); |
| 517 |
| 518 // Each trace name should be different to differentiate. |
| 519 std::string source_category = category_name + std::to_string(i); |
| 520 std::string source_trace_name = trace_name + std::to_string(i); |
| 521 |
| 522 ExpectOutputterBeginMocks(outputter_ref_.get(), |
| 523 source_category, source_trace_name); |
| 524 |
| 525 const GpuTracerSource source = static_cast<GpuTracerSource>(i); |
| 526 ASSERT_TRUE(tracer.Begin(source_category, source_trace_name, source)); |
| 527 |
| 528 ASSERT_EQ(source_category, tracer.CurrentCategory()); |
| 529 ASSERT_EQ(source_trace_name, tracer.CurrentName()); |
| 530 } |
| 531 |
| 532 for (int i = 0; i < NUM_TRACER_SOURCES; ++i) { |
| 533 // Set times so each source has a different time. |
| 534 gl_fake_queries_.SetCurrentGLTime( |
| 535 end_timestamp + |
| 536 (i * base::Time::kNanosecondsPerMicrosecond)); |
| 537 cpu_time_ref_->SetFakeCPUTime(expect_end_time + i); |
| 538 |
| 539 // Each trace name should be different to differentiate. |
| 540 std::string source_category = category_name + std::to_string(i); |
| 541 std::string source_trace_name = trace_name + std::to_string(i); |
| 542 |
| 543 ExpectOutputterEndMocks(outputter_ref_.get(), source_category, |
| 544 source_trace_name, |
| 545 expect_start_time + i, expect_end_time + i, |
| 546 GetTracerType() != kTracerTypeInvalid); |
| 547 |
| 548 const GpuTracerSource source = static_cast<GpuTracerSource>(i); |
| 549 ASSERT_TRUE(tracer.End(source)); |
| 550 } |
| 551 |
| 552 ASSERT_TRUE(tracer.EndDecoding()); |
| 553 |
| 554 outputter_ref_ = NULL; |
| 555 cpu_time_ref_ = NULL; |
| 556 } |
| 557 |
| 558 void DoDisjointTest() { |
| 559 // Cause a disjoint in a middle of a trace and expect no output calls. |
| 560 ExpectTracerOffsetQueryMocks(); |
| 561 |
| 562 EXPECT_CALL(*gl_, GetError()).Times(AtLeast(0)) |
| 563 .WillRepeatedly( |
| 564 Invoke(&gl_fake_queries_, &GlFakeQueries::GetError)); |
| 565 |
| 566 const std::string category_name("trace_category"); |
| 567 const std::string trace_name("trace_test"); |
| 568 const GpuTracerSource source = static_cast<GpuTracerSource>(0); |
| 569 const int64 offset_time = 3231; |
| 570 const GLint64 start_timestamp = 7 * base::Time::kNanosecondsPerMicrosecond; |
| 571 const GLint64 end_timestamp = 32 * base::Time::kNanosecondsPerMicrosecond; |
| 572 const int64 expect_start_time = |
| 573 (start_timestamp / base::Time::kNanosecondsPerMicrosecond) + |
| 574 offset_time; |
| 575 const int64 expect_end_time = |
| 576 (end_timestamp / base::Time::kNanosecondsPerMicrosecond) + offset_time; |
| 577 |
| 578 GPUTracerTester tracer(test_tracer_type_); |
| 579 tracer.SetTracingEnabled(true); |
| 580 |
| 581 tracer.SetOutputter(outputter_ref_); |
| 582 tracer.SetCPUTime(cpu_time_ref_); |
| 583 |
| 584 gl_fake_queries_.SetCurrentGLTime(start_timestamp); |
| 585 cpu_time_ref_->SetFakeCPUTime(expect_start_time); |
| 586 |
| 587 ASSERT_TRUE(tracer.BeginDecoding()); |
| 588 |
| 589 ExpectTraceQueryMocks(); |
| 590 |
| 591 ExpectOutputterBeginMocks(outputter_ref_.get(), |
| 592 category_name, trace_name); |
| 593 ASSERT_TRUE(tracer.Begin(category_name, trace_name, source)); |
| 594 |
| 595 gl_fake_queries_.SetCurrentGLTime(end_timestamp); |
| 596 cpu_time_ref_->SetFakeCPUTime(expect_end_time); |
| 597 gl_fake_queries_.SetDisjoint(); |
| 598 |
| 599 ExpectOutputterEndMocks(outputter_ref_.get(), category_name, trace_name, |
| 600 expect_start_time, expect_end_time, false); |
| 601 |
| 602 ASSERT_TRUE(tracer.End(source)); |
| 603 ASSERT_TRUE(tracer.EndDecoding()); |
| 604 |
| 605 outputter_ref_ = NULL; |
| 606 cpu_time_ref_ = NULL; |
| 607 } |
| 608 }; |
| 609 |
| 610 class InvalidTimerTracerTest : public BaseGpuTracerTest { |
| 611 public: |
| 612 InvalidTimerTracerTest() |
| 613 : BaseGpuTracerTest(kTracerTypeInvalid) { |
| 614 } |
200 }; | 615 }; |
201 | 616 |
202 class GpuARBTimerTracerTest : public BaseGpuTracerTest { | 617 class GpuARBTimerTracerTest : public BaseGpuTracerTest { |
203 protected: | 618 public: |
204 GpuTracerType GetTracerType() override { return kTracerTypeARBTimer; } | 619 GpuARBTimerTracerTest() |
| 620 : BaseGpuTracerTest(kTracerTypeARBTimer) { |
| 621 } |
205 }; | 622 }; |
206 | 623 |
207 class GpuDisjointTimerTracerTest : public BaseGpuTracerTest { | 624 class GpuDisjointTimerTracerTest : public BaseGpuTracerTest { |
208 protected: | 625 public: |
209 GpuTracerType GetTracerType() override { return kTracerTypeDisjointTimer; } | 626 GpuDisjointTimerTracerTest() |
210 }; | 627 : BaseGpuTracerTest(kTracerTypeDisjointTimer) { |
211 | 628 } |
212 TEST_F(GpuARBTimerTracerTest, GPUTrace) { | 629 }; |
213 // Test basic timer query functionality | 630 |
214 { | 631 TEST_F(InvalidTimerTracerTest, InvalidTimerBasicTracerTest) { |
215 DoTraceTest(); | 632 DoBasicTracerTest(); |
216 } | 633 } |
217 } | 634 |
218 | 635 TEST_F(GpuARBTimerTracerTest, ARBTimerBasicTracerTest) { |
219 TEST_F(GpuDisjointTimerTracerTest, GPUTrace) { | 636 DoBasicTracerTest(); |
220 // Test basic timer query functionality | 637 } |
221 { | 638 |
222 DoTraceTest(); | 639 TEST_F(GpuDisjointTimerTracerTest, DisjointTimerBasicTracerTest) { |
223 } | 640 DoBasicTracerTest(); |
| 641 } |
| 642 |
| 643 TEST_F(InvalidTimerTracerTest, InvalidTimerTracerMarkersTest) { |
| 644 DoTracerMarkersTest(); |
| 645 } |
| 646 |
| 647 TEST_F(GpuARBTimerTracerTest, ARBTimerBasicTracerMarkersTest) { |
| 648 DoTracerMarkersTest(); |
| 649 } |
| 650 |
| 651 TEST_F(GpuDisjointTimerTracerTest, DisjointTimerBasicTracerMarkersTest) { |
| 652 DoTracerMarkersTest(); |
| 653 } |
| 654 |
| 655 TEST_F(GpuDisjointTimerTracerTest, DisjointTimerDisjointTraceTest) { |
| 656 DoDisjointTest(); |
| 657 } |
| 658 |
| 659 // Test basic functionality of the GPUTracerTester. |
| 660 TEST(GPUTracerTester, IsTracingTest) { |
| 661 GPUTracerTester tracer_tester(kTracerTypeInvalid); |
| 662 EXPECT_FALSE(tracer_tester.IsTracing()); |
| 663 tracer_tester.SetTracingEnabled(true); |
| 664 EXPECT_TRUE(tracer_tester.IsTracing()); |
| 665 } |
| 666 |
| 667 TEST(GPUTracerTester, DecodeTest) { |
| 668 GPUTracerTester tracer_tester(kTracerTypeInvalid); |
| 669 ASSERT_TRUE(tracer_tester.BeginDecoding()); |
| 670 EXPECT_FALSE(tracer_tester.BeginDecoding()); |
| 671 ASSERT_TRUE(tracer_tester.EndDecoding()); |
| 672 EXPECT_FALSE(tracer_tester.EndDecoding()); |
| 673 } |
| 674 |
| 675 TEST(GPUTracerTester, TraceDuringDecodeTest) { |
| 676 GPUTracerTester tracer_tester(kTracerTypeInvalid); |
| 677 const std::string category_name("trace_category"); |
| 678 const std::string trace_name("trace_test"); |
| 679 |
| 680 EXPECT_FALSE(tracer_tester.Begin(category_name, trace_name, |
| 681 kTraceGroupMarker)); |
| 682 |
| 683 ASSERT_TRUE(tracer_tester.BeginDecoding()); |
| 684 EXPECT_TRUE(tracer_tester.Begin(category_name, trace_name, |
| 685 kTraceGroupMarker)); |
| 686 ASSERT_TRUE(tracer_tester.EndDecoding()); |
224 } | 687 } |
225 | 688 |
226 } // namespace gles2 | 689 } // namespace gles2 |
227 } // namespace gpu | 690 } // namespace gpu |
OLD | NEW |