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/service/cmd_buffer_engine.h"
12 #include "gpu/command_buffer/service/context_group.h"
13 #include "gpu/command_buffer/service/context_state.h"
14 #include "gpu/command_buffer/service/gl_surface_mock.h"
15 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
17 #include "gpu/command_buffer/service/gpu_switches.h"
18 #include "gpu/command_buffer/service/image_manager.h"
19 #include "gpu/command_buffer/service/mailbox_manager.h"
20 #include "gpu/command_buffer/service/mocks.h"
21 #include "gpu/command_buffer/service/program_manager.h"
22 #include "gpu/command_buffer/service/test_helper.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gl/gl_implementation.h"
25 #include "ui/gl/gl_mock.h"
26 #include "ui/gl/gl_surface_stub.h"
28 #if !defined(GL_DEPTH24_STENCIL8)
29 #define GL_DEPTH24_STENCIL8 0x88F0
32 using ::gfx::MockGLInterface
;
34 using ::testing::DoAll
;
35 using ::testing::InSequence
;
36 using ::testing::Invoke
;
37 using ::testing::MatcherCast
;
38 using ::testing::Mock
;
39 using ::testing::Pointee
;
40 using ::testing::Return
;
41 using ::testing::SaveArg
;
42 using ::testing::SetArrayArgument
;
43 using ::testing::SetArgumentPointee
;
44 using ::testing::SetArgPointee
;
45 using ::testing::StrEq
;
46 using ::testing::StrictMock
;
53 class GLES2DecoderRestoreStateTest
: public GLES2DecoderManualInitTest
{
55 GLES2DecoderRestoreStateTest() {}
58 void AddExpectationsForActiveTexture(GLenum unit
);
59 void AddExpectationsForBindTexture(GLenum target
, GLuint id
);
60 void InitializeContextState(ContextState
* state
,
61 uint32 non_default_unit
,
65 INSTANTIATE_TEST_CASE_P(Service
,
66 GLES2DecoderRestoreStateTest
,
69 void GLES2DecoderRestoreStateTest::AddExpectationsForActiveTexture(
71 EXPECT_CALL(*gl_
, ActiveTexture(unit
)).Times(1).RetiresOnSaturation();
74 void GLES2DecoderRestoreStateTest::AddExpectationsForBindTexture(GLenum target
,
76 EXPECT_CALL(*gl_
, BindTexture(target
, id
)).Times(1).RetiresOnSaturation();
79 void GLES2DecoderRestoreStateTest::InitializeContextState(
81 uint32 non_default_unit
,
83 state
->texture_units
.resize(group().max_texture_units());
84 for (uint32 tt
= 0; tt
< state
->texture_units
.size(); ++tt
) {
85 TextureRef
* ref_cube_map
=
86 group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP
);
87 state
->texture_units
[tt
].bound_texture_cube_map
= ref_cube_map
;
89 (tt
== non_default_unit
)
90 ? group().texture_manager()->GetTexture(client_texture_id_
)
91 : group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_2D
);
92 state
->texture_units
[tt
].bound_texture_2d
= ref_2d
;
94 state
->active_texture_unit
= active_unit
;
97 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousStateBGR
) {
99 init
.bind_generates_resource
= true;
104 // Expect to restore texture bindings for unit GL_TEXTURE0.
105 AddExpectationsForActiveTexture(GL_TEXTURE0
);
106 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
107 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
108 TestHelper::kServiceDefaultTextureCubemapId
);
110 // Expect to restore texture bindings for remaining units.
111 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
112 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
113 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
114 TestHelper::kServiceDefaultTexture2dId
);
115 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
116 TestHelper::kServiceDefaultTextureCubemapId
);
119 // Expect to restore the active texture unit to GL_TEXTURE0.
120 AddExpectationsForActiveTexture(GL_TEXTURE0
);
122 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
125 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousState
) {
131 // Expect to restore texture bindings for unit GL_TEXTURE0.
132 AddExpectationsForActiveTexture(GL_TEXTURE0
);
133 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
134 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
136 // Expect to restore texture bindings for remaining units.
137 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
138 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
139 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
140 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
143 // Expect to restore the active texture unit to GL_TEXTURE0.
144 AddExpectationsForActiveTexture(GL_TEXTURE0
);
146 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
149 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousStateBGR
) {
151 init
.bind_generates_resource
= true;
155 // Construct a previous ContextState with all texture bindings
156 // set to default textures.
157 ContextState
prev_state(NULL
, NULL
, NULL
);
158 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
161 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
162 // since the rest of the bindings haven't changed between the current
163 // state and the |prev_state|.
164 AddExpectationsForActiveTexture(GL_TEXTURE0
);
165 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
167 // Expect to restore active texture unit to GL_TEXTURE0.
168 AddExpectationsForActiveTexture(GL_TEXTURE0
);
170 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
173 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousState
) {
178 // Construct a previous ContextState with all texture bindings
179 // set to default textures.
180 ContextState
prev_state(NULL
, NULL
, NULL
);
181 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
184 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
185 // since the rest of the bindings haven't changed between the current
186 // state and the |prev_state|.
187 AddExpectationsForActiveTexture(GL_TEXTURE0
);
188 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
190 // Expect to restore active texture unit to GL_TEXTURE0.
191 AddExpectationsForActiveTexture(GL_TEXTURE0
);
193 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
196 TEST_P(GLES2DecoderRestoreStateTest
, ActiveUnit1
) {
200 // Bind a non-default texture to GL_TEXTURE1 unit.
201 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
203 cmd
.Init(GL_TEXTURE1
);
204 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
205 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
208 // Construct a previous ContextState with all texture bindings
209 // set to default textures.
210 ContextState
prev_state(NULL
, NULL
, NULL
);
211 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
214 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE1 unit,
215 // since the rest of the bindings haven't changed between the current
216 // state and the |prev_state|.
217 AddExpectationsForActiveTexture(GL_TEXTURE1
);
218 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
220 // Expect to restore active texture unit to GL_TEXTURE1.
221 AddExpectationsForActiveTexture(GL_TEXTURE1
);
223 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
226 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit0BGR
) {
228 init
.bind_generates_resource
= true;
231 // Bind a non-default texture to GL_TEXTURE1 unit.
232 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
233 SpecializedSetup
<ActiveTexture
, 0>(true);
235 cmd
.Init(GL_TEXTURE1
);
236 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
237 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
240 // Construct a previous ContextState with GL_TEXTURE_2D target in
241 // GL_TEXTURE0 unit bound to a non-default texture and the rest
242 // set to default textures.
243 ContextState
prev_state(NULL
, NULL
, NULL
);
244 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
247 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
248 // a default texture.
249 AddExpectationsForActiveTexture(GL_TEXTURE0
);
250 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
251 TestHelper::kServiceDefaultTexture2dId
);
253 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
255 AddExpectationsForActiveTexture(GL_TEXTURE1
);
256 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
258 // Expect to restore active texture unit to GL_TEXTURE1.
259 AddExpectationsForActiveTexture(GL_TEXTURE1
);
261 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
264 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit1BGR
) {
266 init
.bind_generates_resource
= true;
269 // Bind a non-default texture to GL_TEXTURE0 unit.
272 // Construct a previous ContextState with GL_TEXTURE_2D target in
273 // GL_TEXTURE1 unit bound to a non-default texture and the rest
274 // set to default textures.
275 ContextState
prev_state(NULL
, NULL
, NULL
);
276 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
279 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
280 // for GL_TEXTURE0 unit.
281 AddExpectationsForActiveTexture(GL_TEXTURE0
);
282 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
284 // Expect to restore GL_TEXTURE_2D binding to the default texture
285 // for GL_TEXTURE1 unit.
286 AddExpectationsForActiveTexture(GL_TEXTURE1
);
287 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
288 TestHelper::kServiceDefaultTexture2dId
);
290 // Expect to restore active texture unit to GL_TEXTURE0.
291 AddExpectationsForActiveTexture(GL_TEXTURE0
);
293 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
296 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit0
) {
300 // Bind a non-default texture to GL_TEXTURE1 unit.
301 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
302 SpecializedSetup
<ActiveTexture
, 0>(true);
304 cmd
.Init(GL_TEXTURE1
);
305 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
306 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
309 // Construct a previous ContextState with GL_TEXTURE_2D target in
310 // GL_TEXTURE0 unit bound to a non-default texture and the rest
311 // set to default textures.
312 ContextState
prev_state(NULL
, NULL
, NULL
);
313 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
316 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
318 AddExpectationsForActiveTexture(GL_TEXTURE0
);
319 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
321 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
323 AddExpectationsForActiveTexture(GL_TEXTURE1
);
324 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
326 // Expect to restore active texture unit to GL_TEXTURE1.
327 AddExpectationsForActiveTexture(GL_TEXTURE1
);
329 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
332 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit1
) {
336 // Bind a non-default texture to GL_TEXTURE0 unit.
339 // Construct a previous ContextState with GL_TEXTURE_2D target in
340 // GL_TEXTURE1 unit bound to a non-default texture and the rest
341 // set to default textures.
342 ContextState
prev_state(NULL
, NULL
, NULL
);
343 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
346 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
347 // for GL_TEXTURE0 unit.
348 AddExpectationsForActiveTexture(GL_TEXTURE0
);
349 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
351 // Expect to restore GL_TEXTURE_2D binding to the 0 texture
352 // for GL_TEXTURE1 unit.
353 AddExpectationsForActiveTexture(GL_TEXTURE1
);
354 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
356 // Expect to restore active texture unit to GL_TEXTURE0.
357 AddExpectationsForActiveTexture(GL_TEXTURE0
);
359 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
362 TEST_P(GLES2DecoderManualInitTest
, ContextStateCapabilityCaching
) {
369 // TODO(vmiura): Should autogen this to match build_gles2_cmd_buffer.py.
370 TestInfo test
[] = {{GL_BLEND
, false, true},
371 {GL_CULL_FACE
, false, true},
372 {GL_DEPTH_TEST
, false, false},
373 {GL_DITHER
, true, true},
374 {GL_POLYGON_OFFSET_FILL
, false, true},
375 {GL_SAMPLE_ALPHA_TO_COVERAGE
, false, true},
376 {GL_SAMPLE_COVERAGE
, false, true},
377 {GL_SCISSOR_TEST
, false, true},
378 {GL_STENCIL_TEST
, false, false},
384 for (int i
= 0; test
[i
].gl_enum
; i
++) {
385 bool enable_state
= test
[i
].default_state
;
387 // Test setting default state initially is ignored.
388 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
390 // Test new and cached state changes.
391 for (int n
= 0; n
< 3; n
++) {
392 enable_state
= !enable_state
;
393 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
394 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
399 // TODO(vmiura): Tests for VAO restore.
401 // TODO(vmiura): Tests for ContextState::RestoreAttribute().
403 // TODO(vmiura): Tests for ContextState::RestoreBufferBindings().
405 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
407 // TODO(vmiura): Tests for ContextState::RestoreRenderbufferBindings().
409 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
411 // TODO(vmiura): Tests for ContextState::RestoreGlobalState().