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.
8 #include "gpu/command_buffer/service/gpu_service_test.h"
9 #include "gpu/command_buffer/service/gpu_tracer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gl/gl_mock.h"
16 using ::testing::InvokeWithoutArgs
;
17 using ::testing::Return
;
18 using ::testing::ReturnRef
;
19 using ::testing::ReturnPointee
;
20 using ::testing::NotNull
;
21 using ::testing::ElementsAreArray
;
22 using ::testing::ElementsAre
;
23 using ::testing::SetArrayArgument
;
24 using ::testing::AtLeast
;
25 using ::testing::SetArgPointee
;
26 using ::testing::Pointee
;
27 using ::testing::Unused
;
28 using ::testing::Invoke
;
31 class MockOutputter
: public Outputter
{
35 void(const std::string
& name
, int64 start_time
, int64 end_time
));
48 alloced_queries_
.clear();
49 query_timestamp_
.clear();
52 void SetCurrentGLTime(GLint64 current_time
) { current_time_
= current_time
; }
54 void GenQueriesARB(GLsizei n
, GLuint
* ids
) {
55 for (GLsizei i
= 0; i
< n
; i
++) {
56 ids
[i
] = next_query_id_
++;
57 alloced_queries_
.insert(ids
[i
]);
61 void DeleteQueriesARB(GLsizei n
, const GLuint
* ids
) {
62 for (GLsizei i
= 0; i
< n
; i
++) {
63 alloced_queries_
.erase(ids
[i
]);
64 query_timestamp_
.erase(ids
[i
]);
68 void GetQueryObjectiv(GLuint id
, GLenum pname
, GLint
* params
) {
70 case GL_QUERY_RESULT_AVAILABLE
: {
71 std::map
<GLuint
, GLint64
>::iterator it
= query_timestamp_
.find(id
);
72 if (it
!= query_timestamp_
.end() && it
->second
<= current_time_
)
83 void QueryCounter(GLuint id
, GLenum target
) {
86 ASSERT_TRUE(alloced_queries_
.find(id
) != alloced_queries_
.end());
87 query_timestamp_
[id
] = current_time_
;
94 void GetQueryObjectui64v(GLuint id
, GLenum pname
, GLuint64
* params
) {
97 ASSERT_TRUE(query_timestamp_
.find(id
) != query_timestamp_
.end());
98 *params
= query_timestamp_
.find(id
)->second
;
106 GLint64 current_time_
;
107 GLuint next_query_id_
;
108 std::set
<GLuint
> alloced_queries_
;
109 std::map
<GLuint
, GLint64
> query_timestamp_
;
112 class BaseGpuTracerTest
: public GpuServiceTest
{
114 BaseGpuTracerTest() {}
116 ///////////////////////////////////////////////////////////////////////////
119 MockOutputter
* outputter
= new MockOutputter();
120 scoped_refptr
<Outputter
> outputter_ref
= outputter
;
122 SetupTimerQueryMocks();
125 const std::string
trace_name("trace_test");
126 const int64 offset_time
= 3231;
127 const GLint64 start_timestamp
= 7 * base::Time::kNanosecondsPerMicrosecond
;
128 const GLint64 end_timestamp
= 32 * base::Time::kNanosecondsPerMicrosecond
;
129 const int64 expect_start_time
=
130 (start_timestamp
/ base::Time::kNanosecondsPerMicrosecond
) +
132 const int64 expect_end_time
=
133 (end_timestamp
/ base::Time::kNanosecondsPerMicrosecond
) + offset_time
;
135 // Expected Outputter::Trace call
136 EXPECT_CALL(*outputter
,
137 Trace(trace_name
, expect_start_time
, expect_end_time
));
139 scoped_refptr
<GPUTrace
> trace
=
140 new GPUTrace(outputter_ref
, trace_name
, offset_time
,
143 gl_fake_queries_
.SetCurrentGLTime(start_timestamp
);
146 // Shouldn't be available before End() call
147 gl_fake_queries_
.SetCurrentGLTime(end_timestamp
);
148 EXPECT_FALSE(trace
->IsAvailable());
152 // Shouldn't be available until the queries complete
153 gl_fake_queries_
.SetCurrentGLTime(end_timestamp
-
154 base::Time::kNanosecondsPerMicrosecond
);
155 EXPECT_FALSE(trace
->IsAvailable());
157 // Now it should be available
158 gl_fake_queries_
.SetCurrentGLTime(end_timestamp
);
159 EXPECT_TRUE(trace
->IsAvailable());
161 // Proces should output expected Trace results to MockOutputter
166 virtual void SetUp() {
167 GpuServiceTest::SetUp();
168 gl_fake_queries_
.Reset();
171 virtual void TearDown() {
173 gl_fake_queries_
.Reset();
174 GpuServiceTest::TearDown();
177 virtual void SetupTimerQueryMocks() {
178 // Delegate query APIs used by GPUTrace to a GlFakeQueries
179 EXPECT_CALL(*gl_
, GenQueriesARB(_
, NotNull())).Times(AtLeast(1)).WillOnce(
180 Invoke(&gl_fake_queries_
, &GlFakeQueries::GenQueriesARB
));
182 EXPECT_CALL(*gl_
, GetQueryObjectiv(_
, GL_QUERY_RESULT_AVAILABLE
, NotNull()))
185 Invoke(&gl_fake_queries_
, &GlFakeQueries::GetQueryObjectiv
));
187 EXPECT_CALL(*gl_
, QueryCounter(_
, GL_TIMESTAMP
))
190 Invoke(&gl_fake_queries_
, &GlFakeQueries::QueryCounter
));
192 EXPECT_CALL(*gl_
, GetQueryObjectui64v(_
, GL_QUERY_RESULT
, NotNull()))
195 Invoke(&gl_fake_queries_
, &GlFakeQueries::GetQueryObjectui64v
));
197 EXPECT_CALL(*gl_
, DeleteQueriesARB(2, NotNull()))
200 Invoke(&gl_fake_queries_
, &GlFakeQueries::DeleteQueriesARB
));
203 virtual GpuTracerType
GetTracerType() = 0;
205 GlFakeQueries gl_fake_queries_
;
208 class GpuARBTimerTracerTest
: public BaseGpuTracerTest
{
210 virtual GpuTracerType
GetTracerType() OVERRIDE
{
211 return kTracerTypeARBTimer
;
215 class GpuDisjointTimerTracerTest
: public BaseGpuTracerTest
{
217 virtual GpuTracerType
GetTracerType() OVERRIDE
{
218 return kTracerTypeDisjointTimer
;
222 TEST_F(GpuARBTimerTracerTest
, GPUTrace
) {
223 // Test basic timer query functionality
229 TEST_F(GpuDisjointTimerTracerTest
, GPUTrace
) {
230 // Test basic timer query functionality