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.
5 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "gpu/command_buffer/common/gles2_cmd_format.h"
10 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
11 #include "gpu/command_buffer/common/id_allocator.h"
12 #include "gpu/command_buffer/service/async_pixel_transfer_delegate_mock.h"
13 #include "gpu/command_buffer/service/async_pixel_transfer_manager.h"
14 #include "gpu/command_buffer/service/async_pixel_transfer_manager_mock.h"
15 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
16 #include "gpu/command_buffer/service/context_group.h"
17 #include "gpu/command_buffer/service/context_state.h"
18 #include "gpu/command_buffer/service/gl_surface_mock.h"
19 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
21 #include "gpu/command_buffer/service/gpu_switches.h"
22 #include "gpu/command_buffer/service/image_manager.h"
23 #include "gpu/command_buffer/service/mailbox_manager.h"
24 #include "gpu/command_buffer/service/mocks.h"
25 #include "gpu/command_buffer/service/program_manager.h"
26 #include "gpu/command_buffer/service/test_helper.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gl/gl_implementation.h"
29 #include "ui/gl/gl_mock.h"
30 #include "ui/gl/gl_surface_stub.h"
32 #if !defined(GL_DEPTH24_STENCIL8)
33 #define GL_DEPTH24_STENCIL8 0x88F0
36 using ::gfx::MockGLInterface
;
38 using ::testing::DoAll
;
39 using ::testing::InSequence
;
40 using ::testing::Invoke
;
41 using ::testing::MatcherCast
;
42 using ::testing::Mock
;
43 using ::testing::Pointee
;
44 using ::testing::Return
;
45 using ::testing::SaveArg
;
46 using ::testing::SetArrayArgument
;
47 using ::testing::SetArgumentPointee
;
48 using ::testing::SetArgPointee
;
49 using ::testing::StrEq
;
50 using ::testing::StrictMock
;
57 class GLES2DecoderRestoreStateTest
: public GLES2DecoderManualInitTest
{
59 GLES2DecoderRestoreStateTest() {}
62 void AddExpectationsForActiveTexture(GLenum unit
);
63 void AddExpectationsForBindTexture(GLenum target
, GLuint id
);
64 void InitializeContextState(ContextState
* state
,
65 uint32 non_default_unit
,
69 INSTANTIATE_TEST_CASE_P(Service
,
70 GLES2DecoderRestoreStateTest
,
73 void GLES2DecoderRestoreStateTest::AddExpectationsForActiveTexture(
75 EXPECT_CALL(*gl_
, ActiveTexture(unit
)).Times(1).RetiresOnSaturation();
78 void GLES2DecoderRestoreStateTest::AddExpectationsForBindTexture(GLenum target
,
80 EXPECT_CALL(*gl_
, BindTexture(target
, id
)).Times(1).RetiresOnSaturation();
83 void GLES2DecoderRestoreStateTest::InitializeContextState(
85 uint32 non_default_unit
,
87 state
->texture_units
.resize(group().max_texture_units());
88 for (uint32 tt
= 0; tt
< state
->texture_units
.size(); ++tt
) {
89 TextureRef
* ref_cube_map
=
90 group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP
);
91 state
->texture_units
[tt
].bound_texture_cube_map
= ref_cube_map
;
93 (tt
== non_default_unit
)
94 ? group().texture_manager()->GetTexture(client_texture_id_
)
95 : group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_2D
);
96 state
->texture_units
[tt
].bound_texture_2d
= ref_2d
;
98 state
->active_texture_unit
= active_unit
;
101 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousStateBGR
) {
103 init
.gl_version
= "3.0";
104 init
.bind_generates_resource
= true;
109 // Expect to restore texture bindings for unit GL_TEXTURE0.
110 AddExpectationsForActiveTexture(GL_TEXTURE0
);
111 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
112 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
113 TestHelper::kServiceDefaultTextureCubemapId
);
115 // Expect to restore texture bindings for remaining units.
116 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
117 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
118 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
119 TestHelper::kServiceDefaultTexture2dId
);
120 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
121 TestHelper::kServiceDefaultTextureCubemapId
);
124 // Expect to restore the active texture unit to GL_TEXTURE0.
125 AddExpectationsForActiveTexture(GL_TEXTURE0
);
127 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
130 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousState
) {
132 init
.gl_version
= "3.0";
137 // Expect to restore texture bindings for unit GL_TEXTURE0.
138 AddExpectationsForActiveTexture(GL_TEXTURE0
);
139 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
140 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
142 // Expect to restore texture bindings for remaining units.
143 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
144 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
145 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
146 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
149 // Expect to restore the active texture unit to GL_TEXTURE0.
150 AddExpectationsForActiveTexture(GL_TEXTURE0
);
152 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
155 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousStateBGR
) {
157 init
.gl_version
= "3.0";
158 init
.bind_generates_resource
= true;
162 // Construct a previous ContextState with all texture bindings
163 // set to default textures.
164 ContextState
prev_state(NULL
, NULL
, NULL
);
165 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
168 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
169 // since the rest of the bindings haven't changed between the current
170 // state and the |prev_state|.
171 AddExpectationsForActiveTexture(GL_TEXTURE0
);
172 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
174 // Expect to restore active texture unit to GL_TEXTURE0.
175 AddExpectationsForActiveTexture(GL_TEXTURE0
);
177 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
180 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousState
) {
182 init
.gl_version
= "3.0";
186 // Construct a previous ContextState with all texture bindings
187 // set to default textures.
188 ContextState
prev_state(NULL
, NULL
, NULL
);
189 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
192 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
193 // since the rest of the bindings haven't changed between the current
194 // state and the |prev_state|.
195 AddExpectationsForActiveTexture(GL_TEXTURE0
);
196 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
198 // Expect to restore active texture unit to GL_TEXTURE0.
199 AddExpectationsForActiveTexture(GL_TEXTURE0
);
201 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
204 TEST_P(GLES2DecoderRestoreStateTest
, ActiveUnit1
) {
206 init
.gl_version
= "3.0";
209 // Bind a non-default texture to GL_TEXTURE1 unit.
210 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
212 cmd
.Init(GL_TEXTURE1
);
213 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
214 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
217 // Construct a previous ContextState with all texture bindings
218 // set to default textures.
219 ContextState
prev_state(NULL
, NULL
, NULL
);
220 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
223 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE1 unit,
224 // since the rest of the bindings haven't changed between the current
225 // state and the |prev_state|.
226 AddExpectationsForActiveTexture(GL_TEXTURE1
);
227 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
229 // Expect to restore active texture unit to GL_TEXTURE1.
230 AddExpectationsForActiveTexture(GL_TEXTURE1
);
232 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
235 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit0BGR
) {
237 init
.gl_version
= "3.0";
238 init
.bind_generates_resource
= true;
241 // Bind a non-default texture to GL_TEXTURE1 unit.
242 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
243 SpecializedSetup
<ActiveTexture
, 0>(true);
245 cmd
.Init(GL_TEXTURE1
);
246 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
247 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
250 // Construct a previous ContextState with GL_TEXTURE_2D target in
251 // GL_TEXTURE0 unit bound to a non-default texture and the rest
252 // set to default textures.
253 ContextState
prev_state(NULL
, NULL
, NULL
);
254 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
257 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
258 // a default texture.
259 AddExpectationsForActiveTexture(GL_TEXTURE0
);
260 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
261 TestHelper::kServiceDefaultTexture2dId
);
263 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
265 AddExpectationsForActiveTexture(GL_TEXTURE1
);
266 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
268 // Expect to restore active texture unit to GL_TEXTURE1.
269 AddExpectationsForActiveTexture(GL_TEXTURE1
);
271 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
274 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit1BGR
) {
276 init
.gl_version
= "3.0";
277 init
.bind_generates_resource
= true;
280 // Bind a non-default texture to GL_TEXTURE0 unit.
283 // Construct a previous ContextState with GL_TEXTURE_2D target in
284 // GL_TEXTURE1 unit bound to a non-default texture and the rest
285 // set to default textures.
286 ContextState
prev_state(NULL
, NULL
, NULL
);
287 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
290 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
291 // for GL_TEXTURE0 unit.
292 AddExpectationsForActiveTexture(GL_TEXTURE0
);
293 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
295 // Expect to restore GL_TEXTURE_2D binding to the default texture
296 // for GL_TEXTURE1 unit.
297 AddExpectationsForActiveTexture(GL_TEXTURE1
);
298 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
299 TestHelper::kServiceDefaultTexture2dId
);
301 // Expect to restore active texture unit to GL_TEXTURE0.
302 AddExpectationsForActiveTexture(GL_TEXTURE0
);
304 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
307 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit0
) {
309 init
.gl_version
= "3.0";
312 // Bind a non-default texture to GL_TEXTURE1 unit.
313 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
314 SpecializedSetup
<ActiveTexture
, 0>(true);
316 cmd
.Init(GL_TEXTURE1
);
317 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
318 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
321 // Construct a previous ContextState with GL_TEXTURE_2D target in
322 // GL_TEXTURE0 unit bound to a non-default texture and the rest
323 // set to default textures.
324 ContextState
prev_state(NULL
, NULL
, NULL
);
325 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
328 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
330 AddExpectationsForActiveTexture(GL_TEXTURE0
);
331 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
333 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
335 AddExpectationsForActiveTexture(GL_TEXTURE1
);
336 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
338 // Expect to restore active texture unit to GL_TEXTURE1.
339 AddExpectationsForActiveTexture(GL_TEXTURE1
);
341 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
344 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit1
) {
346 init
.gl_version
= "3.0";
349 // Bind a non-default texture to GL_TEXTURE0 unit.
352 // Construct a previous ContextState with GL_TEXTURE_2D target in
353 // GL_TEXTURE1 unit bound to a non-default texture and the rest
354 // set to default textures.
355 ContextState
prev_state(NULL
, NULL
, NULL
);
356 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
359 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
360 // for GL_TEXTURE0 unit.
361 AddExpectationsForActiveTexture(GL_TEXTURE0
);
362 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
364 // Expect to restore GL_TEXTURE_2D binding to the 0 texture
365 // for GL_TEXTURE1 unit.
366 AddExpectationsForActiveTexture(GL_TEXTURE1
);
367 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
369 // Expect to restore active texture unit to GL_TEXTURE0.
370 AddExpectationsForActiveTexture(GL_TEXTURE0
);
372 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
375 TEST_P(GLES2DecoderManualInitTest
, ContextStateCapabilityCaching
) {
382 // TODO(vmiura): Should autogen this to match build_gles2_cmd_buffer.py.
383 TestInfo test
[] = {{GL_BLEND
, false, true},
384 {GL_CULL_FACE
, false, true},
385 {GL_DEPTH_TEST
, false, false},
386 {GL_DITHER
, true, true},
387 {GL_POLYGON_OFFSET_FILL
, false, true},
388 {GL_SAMPLE_ALPHA_TO_COVERAGE
, false, true},
389 {GL_SAMPLE_COVERAGE
, false, true},
390 {GL_SCISSOR_TEST
, false, true},
391 {GL_STENCIL_TEST
, false, false},
395 init
.gl_version
= "2.1";
398 for (int i
= 0; test
[i
].gl_enum
; i
++) {
399 bool enable_state
= test
[i
].default_state
;
401 // Test setting default state initially is ignored.
402 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
404 // Test new and cached state changes.
405 for (int n
= 0; n
< 3; n
++) {
406 enable_state
= !enable_state
;
407 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
408 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
413 // TODO(vmiura): Tests for VAO restore.
415 // TODO(vmiura): Tests for ContextState::RestoreAttribute().
417 // TODO(vmiura): Tests for ContextState::RestoreBufferBindings().
419 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
421 // TODO(vmiura): Tests for ContextState::RestoreRenderbufferBindings().
423 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
425 // TODO(vmiura): Tests for ContextState::RestoreGlobalState().