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.
5 #include "ui/gl/gpu_timing_fake.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gl/gl_mock.h"
13 using ::testing::AtLeast
;
14 using ::testing::AtMost
;
15 using ::testing::Exactly
;
16 using ::testing::Invoke
;
17 using ::testing::NotNull
;
19 GPUTimingFake::GPUTimingFake() {
23 GPUTimingFake::~GPUTimingFake() {
26 void GPUTimingFake::Reset() {
29 allocated_queries_
.clear();
30 query_results_
.clear();
31 current_elapsed_query_
.Reset();
34 void GPUTimingFake::SetCurrentGLTime(GLint64 current_time
) {
35 current_time_
= current_time
;
38 void GPUTimingFake::SetDisjoint() {
42 void GPUTimingFake::ExpectGetErrorCalls(MockGLInterface
& gl
) {
43 EXPECT_CALL(gl
, GetError()).Times(AtLeast(0))
44 .WillRepeatedly(Invoke(this, &GPUTimingFake::FakeGLGetError
));
47 void GPUTimingFake::ExpectDisjointCalls(MockGLInterface
& gl
) {
48 EXPECT_CALL(gl
, GetIntegerv(GL_GPU_DISJOINT_EXT
, _
)).Times(AtLeast(1))
49 .WillRepeatedly(Invoke(this, &GPUTimingFake::FakeGLGetIntegerv
));
52 void GPUTimingFake::ExpectNoDisjointCalls(MockGLInterface
& gl
) {
53 EXPECT_CALL(gl
, GetIntegerv(GL_GPU_DISJOINT_EXT
, _
)).Times(Exactly(0));
56 void GPUTimingFake::ExpectGPUTimerQuery(
57 MockGLInterface
& gl
, bool elapsed_query
) {
58 EXPECT_CALL(gl
, GenQueries(1, NotNull())).Times(AtLeast(2))
59 .WillRepeatedly(Invoke(this, &GPUTimingFake::FakeGLGenQueries
));
62 // Time Stamp based queries.
63 EXPECT_CALL(gl
, GetInteger64v(GL_TIMESTAMP
, _
))
65 Invoke(this, &GPUTimingFake::FakeGLGetInteger64v
));
67 EXPECT_CALL(gl
, QueryCounter(_
, GL_TIMESTAMP
)).Times(AtLeast(1))
69 Invoke(this, &GPUTimingFake::FakeGLQueryCounter
));
72 // Time Elapsed based queries.
73 EXPECT_CALL(gl
, BeginQuery(GL_TIME_ELAPSED
, _
))
75 Invoke(this, &GPUTimingFake::FakeGLBeginQuery
));
77 EXPECT_CALL(gl
, EndQuery(GL_TIME_ELAPSED
))
78 .WillRepeatedly(Invoke(this, &GPUTimingFake::FakeGLEndQuery
));
80 EXPECT_CALL(gl
, GetQueryObjectiv(_
, GL_QUERY_RESULT_AVAILABLE
,
83 Invoke(this, &GPUTimingFake::FakeGLGetQueryObjectiv
));
85 EXPECT_CALL(gl
, GetQueryObjectui64v(_
, GL_QUERY_RESULT
, NotNull()))
87 Invoke(this, &GPUTimingFake::FakeGLGetQueryObjectui64v
));
89 EXPECT_CALL(gl
, DeleteQueries(1, NotNull())).Times(AtLeast(2))
91 Invoke(this, &GPUTimingFake::FakeGLDeleteQueries
));
94 void GPUTimingFake::ExpectOffsetCalculationQuery(
95 MockGLInterface
& gl
) {
96 EXPECT_CALL(gl
, GetInteger64v(GL_TIMESTAMP
, NotNull()))
99 Invoke(this, &GPUTimingFake::FakeGLGetInteger64v
));
102 void GPUTimingFake::ExpectNoOffsetCalculationQuery(
103 MockGLInterface
& gl
) {
104 EXPECT_CALL(gl
, GetInteger64v(GL_TIMESTAMP
, NotNull())).Times(Exactly(0));
107 void GPUTimingFake::FakeGLGenQueries(GLsizei n
, GLuint
* ids
) {
108 for (GLsizei i
= 0; i
< n
; i
++) {
109 ids
[i
] = next_query_id_
++;
110 allocated_queries_
.insert(ids
[i
]);
114 void GPUTimingFake::FakeGLDeleteQueries(GLsizei n
, const GLuint
* ids
) {
115 for (GLsizei i
= 0; i
< n
; i
++) {
116 allocated_queries_
.erase(ids
[i
]);
117 query_results_
.erase(ids
[i
]);
118 if (current_elapsed_query_
.query_id_
== ids
[i
])
119 current_elapsed_query_
.Reset();
123 void GPUTimingFake::FakeGLBeginQuery(GLenum target
, GLuint id
) {
125 case GL_TIME_ELAPSED
:
126 ASSERT_FALSE(current_elapsed_query_
.active_
);
127 current_elapsed_query_
.Reset();
128 current_elapsed_query_
.active_
= true;
129 current_elapsed_query_
.query_id_
= id
;
130 current_elapsed_query_
.begin_time_
= current_time_
;
133 FAIL() << "Invalid target passed to BeginQuery: " << target
;
137 void GPUTimingFake::FakeGLEndQuery(GLenum target
) {
139 case GL_TIME_ELAPSED
: {
140 ASSERT_TRUE(current_elapsed_query_
.active_
);
141 QueryResult
& query
= query_results_
[current_elapsed_query_
.query_id_
];
142 query
.type_
= QueryResult::kQueryResultType_Elapsed
;
143 query
.begin_time_
= current_elapsed_query_
.begin_time_
;
144 query
.value_
= current_time_
;
145 current_elapsed_query_
.active_
= false;
148 FAIL() << "Invalid target passed to BeginQuery: " << target
;
152 void GPUTimingFake::FakeGLGetQueryObjectiv(GLuint id
, GLenum pname
,
155 case GL_QUERY_RESULT_AVAILABLE
: {
156 std::map
<GLuint
, QueryResult
>::iterator it
= query_results_
.find(id
);
157 if (it
!= query_results_
.end() && it
->second
.value_
<= current_time_
)
163 FAIL() << "Invalid variable passed to GetQueryObjectiv: " << pname
;
167 void GPUTimingFake::FakeGLQueryCounter(GLuint id
, GLenum target
) {
170 ASSERT_TRUE(allocated_queries_
.find(id
) != allocated_queries_
.end());
171 QueryResult
& query
= query_results_
[id
];
172 query
.type_
= QueryResult::kQueryResultType_TimeStamp
;
173 query
.value_
= current_time_
;
177 FAIL() << "Invalid variable passed to QueryCounter: " << target
;
181 void GPUTimingFake::FakeGLGetInteger64v(GLenum pname
, GLint64
* data
) {
184 *data
= current_time_
;
187 FAIL() << "Invalid variable passed to GetInteger64v: " << pname
;
191 void GPUTimingFake::FakeGLGetQueryObjectui64v(GLuint id
, GLenum pname
,
194 case GL_QUERY_RESULT
: {
195 std::map
<GLuint
, QueryResult
>::iterator it
= query_results_
.find(id
);
196 ASSERT_TRUE(it
!= query_results_
.end());
197 switch (it
->second
.type_
) {
198 case QueryResult::kQueryResultType_TimeStamp
:
199 *params
= it
->second
.value_
;
201 case QueryResult::kQueryResultType_Elapsed
:
202 *params
= it
->second
.value_
- it
->second
.begin_time_
;
205 FAIL() << "Invalid Query Result Type: " << it
->second
.type_
;
209 FAIL() << "Invalid variable passed to GetQueryObjectui64v: " << pname
;
213 void GPUTimingFake::FakeGLGetIntegerv(GLenum pname
, GLint
* params
) {
215 case GL_GPU_DISJOINT_EXT
:
216 *params
= static_cast<GLint
>(disjointed_
);
220 FAIL() << "Invalid variable passed to GetIntegerv: " << pname
;
224 GLenum
GPUTimingFake::FakeGLGetError() {