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/async_pixel_transfer_delegate_mock.h"
12 #include "gpu/command_buffer/service/async_pixel_transfer_manager.h"
13 #include "gpu/command_buffer/service/async_pixel_transfer_manager_mock.h"
14 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
15 #include "gpu/command_buffer/service/context_group.h"
16 #include "gpu/command_buffer/service/context_state.h"
17 #include "gpu/command_buffer/service/gl_surface_mock.h"
18 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
20 #include "gpu/command_buffer/service/gpu_switches.h"
21 #include "gpu/command_buffer/service/image_manager.h"
22 #include "gpu/command_buffer/service/mailbox_manager.h"
23 #include "gpu/command_buffer/service/mocks.h"
24 #include "gpu/command_buffer/service/program_manager.h"
25 #include "gpu/command_buffer/service/test_helper.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/gl/gl_implementation.h"
28 #include "ui/gl/gl_mock.h"
29 #include "ui/gl/gl_surface_stub.h"
31 #if !defined(GL_DEPTH24_STENCIL8)
32 #define GL_DEPTH24_STENCIL8 0x88F0
35 using ::gfx::MockGLInterface
;
37 using ::testing::DoAll
;
38 using ::testing::InSequence
;
39 using ::testing::Invoke
;
40 using ::testing::MatcherCast
;
41 using ::testing::Mock
;
42 using ::testing::Pointee
;
43 using ::testing::Return
;
44 using ::testing::SaveArg
;
45 using ::testing::SetArrayArgument
;
46 using ::testing::SetArgumentPointee
;
47 using ::testing::SetArgPointee
;
48 using ::testing::StrEq
;
49 using ::testing::StrictMock
;
56 class GLES2DecoderRestoreStateTest
: public GLES2DecoderManualInitTest
{
58 GLES2DecoderRestoreStateTest() {}
61 void AddExpectationsForActiveTexture(GLenum unit
);
62 void AddExpectationsForBindTexture(GLenum target
, GLuint id
);
63 void InitializeContextState(ContextState
* state
,
64 uint32 non_default_unit
,
68 INSTANTIATE_TEST_CASE_P(Service
,
69 GLES2DecoderRestoreStateTest
,
72 void GLES2DecoderRestoreStateTest::AddExpectationsForActiveTexture(
74 EXPECT_CALL(*gl_
, ActiveTexture(unit
)).Times(1).RetiresOnSaturation();
77 void GLES2DecoderRestoreStateTest::AddExpectationsForBindTexture(GLenum target
,
79 EXPECT_CALL(*gl_
, BindTexture(target
, id
)).Times(1).RetiresOnSaturation();
82 void GLES2DecoderRestoreStateTest::InitializeContextState(
84 uint32 non_default_unit
,
86 state
->texture_units
.resize(group().max_texture_units());
87 for (uint32 tt
= 0; tt
< state
->texture_units
.size(); ++tt
) {
88 TextureRef
* ref_cube_map
=
89 group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_CUBE_MAP
);
90 state
->texture_units
[tt
].bound_texture_cube_map
= ref_cube_map
;
92 (tt
== non_default_unit
)
93 ? group().texture_manager()->GetTexture(client_texture_id_
)
94 : group().texture_manager()->GetDefaultTextureInfo(GL_TEXTURE_2D
);
95 state
->texture_units
[tt
].bound_texture_2d
= ref_2d
;
97 state
->active_texture_unit
= active_unit
;
100 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousStateBGR
) {
102 init
.bind_generates_resource
= true;
107 // Expect to restore texture bindings for unit GL_TEXTURE0.
108 AddExpectationsForActiveTexture(GL_TEXTURE0
);
109 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
110 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
111 TestHelper::kServiceDefaultTextureCubemapId
);
113 // Expect to restore texture bindings for remaining units.
114 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
115 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
116 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
117 TestHelper::kServiceDefaultTexture2dId
);
118 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
,
119 TestHelper::kServiceDefaultTextureCubemapId
);
122 // Expect to restore the active texture unit to GL_TEXTURE0.
123 AddExpectationsForActiveTexture(GL_TEXTURE0
);
125 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
128 TEST_P(GLES2DecoderRestoreStateTest
, NullPreviousState
) {
134 // Expect to restore texture bindings for unit GL_TEXTURE0.
135 AddExpectationsForActiveTexture(GL_TEXTURE0
);
136 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
137 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
139 // Expect to restore texture bindings for remaining units.
140 for (uint32 i
= 1; i
< group().max_texture_units(); ++i
) {
141 AddExpectationsForActiveTexture(GL_TEXTURE0
+ i
);
142 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
143 AddExpectationsForBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
146 // Expect to restore the active texture unit to GL_TEXTURE0.
147 AddExpectationsForActiveTexture(GL_TEXTURE0
);
149 GetDecoder()->RestoreAllTextureUnitBindings(NULL
);
152 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousStateBGR
) {
154 init
.bind_generates_resource
= true;
158 // Construct a previous ContextState with all texture bindings
159 // set to default textures.
160 ContextState
prev_state(NULL
, NULL
, NULL
);
161 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
164 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
165 // since the rest of the bindings haven't changed between the current
166 // state and the |prev_state|.
167 AddExpectationsForActiveTexture(GL_TEXTURE0
);
168 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
170 // Expect to restore active texture unit to GL_TEXTURE0.
171 AddExpectationsForActiveTexture(GL_TEXTURE0
);
173 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
176 TEST_P(GLES2DecoderRestoreStateTest
, WithPreviousState
) {
181 // Construct a previous ContextState with all texture bindings
182 // set to default textures.
183 ContextState
prev_state(NULL
, NULL
, NULL
);
184 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
187 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE0 unit,
188 // since the rest of the bindings haven't changed between the current
189 // state and the |prev_state|.
190 AddExpectationsForActiveTexture(GL_TEXTURE0
);
191 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
193 // Expect to restore active texture unit to GL_TEXTURE0.
194 AddExpectationsForActiveTexture(GL_TEXTURE0
);
196 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
199 TEST_P(GLES2DecoderRestoreStateTest
, ActiveUnit1
) {
203 // Bind a non-default texture to GL_TEXTURE1 unit.
204 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
206 cmd
.Init(GL_TEXTURE1
);
207 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
208 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
211 // Construct a previous ContextState with all texture bindings
212 // set to default textures.
213 ContextState
prev_state(NULL
, NULL
, NULL
);
214 InitializeContextState(&prev_state
, std::numeric_limits
<uint32
>::max(), 0);
217 // Expect to restore only GL_TEXTURE_2D binding for GL_TEXTURE1 unit,
218 // since the rest of the bindings haven't changed between the current
219 // state and the |prev_state|.
220 AddExpectationsForActiveTexture(GL_TEXTURE1
);
221 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
223 // Expect to restore active texture unit to GL_TEXTURE1.
224 AddExpectationsForActiveTexture(GL_TEXTURE1
);
226 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
229 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit0BGR
) {
231 init
.bind_generates_resource
= true;
234 // Bind a non-default texture to GL_TEXTURE1 unit.
235 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
236 SpecializedSetup
<ActiveTexture
, 0>(true);
238 cmd
.Init(GL_TEXTURE1
);
239 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
240 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
243 // Construct a previous ContextState with GL_TEXTURE_2D target in
244 // GL_TEXTURE0 unit bound to a non-default texture and the rest
245 // set to default textures.
246 ContextState
prev_state(NULL
, NULL
, NULL
);
247 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
250 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
251 // a default texture.
252 AddExpectationsForActiveTexture(GL_TEXTURE0
);
253 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
254 TestHelper::kServiceDefaultTexture2dId
);
256 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
258 AddExpectationsForActiveTexture(GL_TEXTURE1
);
259 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
261 // Expect to restore active texture unit to GL_TEXTURE1.
262 AddExpectationsForActiveTexture(GL_TEXTURE1
);
264 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
267 TEST_P(GLES2DecoderRestoreStateTest
, NonDefaultUnit1BGR
) {
269 init
.bind_generates_resource
= true;
272 // Bind a non-default texture to GL_TEXTURE0 unit.
275 // Construct a previous ContextState with GL_TEXTURE_2D target in
276 // GL_TEXTURE1 unit bound to a non-default texture and the rest
277 // set to default textures.
278 ContextState
prev_state(NULL
, NULL
, NULL
);
279 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
282 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
283 // for GL_TEXTURE0 unit.
284 AddExpectationsForActiveTexture(GL_TEXTURE0
);
285 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
287 // Expect to restore GL_TEXTURE_2D binding to the default texture
288 // for GL_TEXTURE1 unit.
289 AddExpectationsForActiveTexture(GL_TEXTURE1
);
290 AddExpectationsForBindTexture(GL_TEXTURE_2D
,
291 TestHelper::kServiceDefaultTexture2dId
);
293 // Expect to restore active texture unit to GL_TEXTURE0.
294 AddExpectationsForActiveTexture(GL_TEXTURE0
);
296 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
299 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit0
) {
303 // Bind a non-default texture to GL_TEXTURE1 unit.
304 EXPECT_CALL(*gl_
, ActiveTexture(GL_TEXTURE1
));
305 SpecializedSetup
<ActiveTexture
, 0>(true);
307 cmd
.Init(GL_TEXTURE1
);
308 EXPECT_EQ(error::kNoError
, ExecuteCmd(cmd
));
309 EXPECT_EQ(GL_NO_ERROR
, GetGLError());
312 // Construct a previous ContextState with GL_TEXTURE_2D target in
313 // GL_TEXTURE0 unit bound to a non-default texture and the rest
314 // set to default textures.
315 ContextState
prev_state(NULL
, NULL
, NULL
);
316 InitializeContextState(&prev_state
, 0, kServiceTextureId
);
319 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE0 unit to
321 AddExpectationsForActiveTexture(GL_TEXTURE0
);
322 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
324 // Expect to restore GL_TEXTURE_2D binding for GL_TEXTURE1 unit to
326 AddExpectationsForActiveTexture(GL_TEXTURE1
);
327 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
329 // Expect to restore active texture unit to GL_TEXTURE1.
330 AddExpectationsForActiveTexture(GL_TEXTURE1
);
332 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
335 TEST_P(GLES2DecoderRestoreStateTest
, DefaultUnit1
) {
339 // Bind a non-default texture to GL_TEXTURE0 unit.
342 // Construct a previous ContextState with GL_TEXTURE_2D target in
343 // GL_TEXTURE1 unit bound to a non-default texture and the rest
344 // set to default textures.
345 ContextState
prev_state(NULL
, NULL
, NULL
);
346 InitializeContextState(&prev_state
, 1, kServiceTextureId
);
349 // Expect to restore GL_TEXTURE_2D binding to the non-default texture
350 // for GL_TEXTURE0 unit.
351 AddExpectationsForActiveTexture(GL_TEXTURE0
);
352 AddExpectationsForBindTexture(GL_TEXTURE_2D
, kServiceTextureId
);
354 // Expect to restore GL_TEXTURE_2D binding to the 0 texture
355 // for GL_TEXTURE1 unit.
356 AddExpectationsForActiveTexture(GL_TEXTURE1
);
357 AddExpectationsForBindTexture(GL_TEXTURE_2D
, 0);
359 // Expect to restore active texture unit to GL_TEXTURE0.
360 AddExpectationsForActiveTexture(GL_TEXTURE0
);
362 GetDecoder()->RestoreAllTextureUnitBindings(&prev_state
);
365 TEST_P(GLES2DecoderManualInitTest
, ContextStateCapabilityCaching
) {
372 // TODO(vmiura): Should autogen this to match build_gles2_cmd_buffer.py.
373 TestInfo test
[] = {{GL_BLEND
, false, true},
374 {GL_CULL_FACE
, false, true},
375 {GL_DEPTH_TEST
, false, false},
376 {GL_DITHER
, true, true},
377 {GL_POLYGON_OFFSET_FILL
, false, true},
378 {GL_SAMPLE_ALPHA_TO_COVERAGE
, false, true},
379 {GL_SAMPLE_COVERAGE
, false, true},
380 {GL_SCISSOR_TEST
, false, true},
381 {GL_STENCIL_TEST
, false, false},
387 for (int i
= 0; test
[i
].gl_enum
; i
++) {
388 bool enable_state
= test
[i
].default_state
;
390 // Test setting default state initially is ignored.
391 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
393 // Test new and cached state changes.
394 for (int n
= 0; n
< 3; n
++) {
395 enable_state
= !enable_state
;
396 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
397 EnableDisableTest(test
[i
].gl_enum
, enable_state
, test
[i
].expect_set
);
402 // TODO(vmiura): Tests for VAO restore.
404 // TODO(vmiura): Tests for ContextState::RestoreAttribute().
406 // TODO(vmiura): Tests for ContextState::RestoreBufferBindings().
408 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
410 // TODO(vmiura): Tests for ContextState::RestoreRenderbufferBindings().
412 // TODO(vmiura): Tests for ContextState::RestoreProgramBindings().
414 // TODO(vmiura): Tests for ContextState::RestoreGlobalState().