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 "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/gl_surface_mock.h"
14 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
15 #include "gpu/command_buffer/service/gpu_switches.h"
16 #include "gpu/command_buffer/service/mocks.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gl/gl_mock.h"
20 using ::gfx::MockGLInterface
;
22 using ::testing::DoAll
;
23 using ::testing::InSequence
;
24 using ::testing::Invoke
;
25 using ::testing::MatcherCast
;
26 using ::testing::Mock
;
27 using ::testing::Pointee
;
28 using ::testing::Return
;
29 using ::testing::SaveArg
;
30 using ::testing::SetArrayArgument
;
31 using ::testing::SetArgumentPointee
;
32 using ::testing::SetArgPointee
;
33 using ::testing::StrEq
;
34 using ::testing::StrictMock
;
41 class GLES2DecoderDrawOOMTest
: public GLES2DecoderManualInitTest
{
43 void Init(bool has_robustness
) {
45 init
.lose_context_when_out_of_memory
= true;
47 init
.extensions
= "GL_ARB_robustness";
49 SetupDefaultProgram();
52 void Draw(GLenum reset_status
,
53 error::ContextLostReason expected_other_reason
) {
54 const GLsizei kFakeLargeCount
= 0x1234;
56 if (context_
->WasAllocatedUsingRobustnessExtension()) {
57 EXPECT_CALL(*gl_
, GetGraphicsResetStatusARB())
58 .WillOnce(Return(reset_status
));
60 AddExpectationsForSimulatedAttrib0WithError(kFakeLargeCount
, 0,
62 EXPECT_CALL(*gl_
, DrawArrays(_
, _
, _
)).Times(0).RetiresOnSaturation();
63 // Other contexts in the group should be lost also.
64 EXPECT_CALL(*mock_decoder_
, MarkContextLost(expected_other_reason
))
66 .RetiresOnSaturation();
68 cmd
.Init(GL_TRIANGLES
, 0, kFakeLargeCount
);
69 EXPECT_EQ(error::kLostContext
, ExecuteCmd(cmd
));
73 // Test that we lose context.
74 TEST_P(GLES2DecoderDrawOOMTest
, ContextLostReasonOOM
) {
75 Init(false); // without robustness
76 const error::ContextLostReason expected_reason_for_other_contexts
=
78 Draw(GL_NO_ERROR
, expected_reason_for_other_contexts
);
79 EXPECT_EQ(GL_OUT_OF_MEMORY
, GetGLError());
80 EXPECT_TRUE(decoder_
->WasContextLost());
81 EXPECT_EQ(error::kOutOfMemory
, decoder_
->GetContextLostReason());
84 TEST_P(GLES2DecoderDrawOOMTest
, ContextLostReasonWhenStatusIsNoError
) {
85 Init(true); // with robustness
86 // If the reset status is NO_ERROR, we should be signaling kOutOfMemory.
87 const error::ContextLostReason expected_reason_for_other_contexts
=
89 Draw(GL_NO_ERROR
, expected_reason_for_other_contexts
);
90 EXPECT_EQ(GL_OUT_OF_MEMORY
, GetGLError());
91 EXPECT_TRUE(decoder_
->WasContextLost());
92 EXPECT_EQ(error::kOutOfMemory
, decoder_
->GetContextLostReason());
95 TEST_P(GLES2DecoderDrawOOMTest
, ContextLostReasonWhenStatusIsGuilty
) {
97 // If there was a reset, it should override kOutOfMemory.
98 const error::ContextLostReason expected_reason_for_other_contexts
=
100 Draw(GL_GUILTY_CONTEXT_RESET_ARB
, expected_reason_for_other_contexts
);
101 EXPECT_EQ(GL_OUT_OF_MEMORY
, GetGLError());
102 EXPECT_TRUE(decoder_
->WasContextLost());
103 EXPECT_EQ(error::kGuilty
, decoder_
->GetContextLostReason());
106 TEST_P(GLES2DecoderDrawOOMTest
, ContextLostReasonWhenStatusIsUnknown
) {
108 // If there was a reset, it should override kOutOfMemory.
109 const error::ContextLostReason expected_reason_for_other_contexts
=
111 Draw(GL_UNKNOWN_CONTEXT_RESET_ARB
, expected_reason_for_other_contexts
);
112 EXPECT_EQ(GL_OUT_OF_MEMORY
, GetGLError());
113 EXPECT_TRUE(decoder_
->WasContextLost());
114 EXPECT_EQ(error::kUnknown
, decoder_
->GetContextLostReason());
117 INSTANTIATE_TEST_CASE_P(Service
, GLES2DecoderDrawOOMTest
, ::testing::Bool());
119 class GLES2DecoderLostContextTest
: public GLES2DecoderManualInitTest
{
121 void Init(bool has_robustness
) {
123 init
.gl_version
= "opengl es 2.0";
125 init
.extensions
= "GL_KHR_robustness";
129 void InitWithVirtualContextsAndRobustness() {
130 base::CommandLine
command_line(0, NULL
);
131 command_line
.AppendSwitchASCII(
132 switches::kGpuDriverBugWorkarounds
,
133 base::IntToString(USE_VIRTUALIZED_GL_CONTEXTS
));
135 init
.gl_version
= "opengl es 2.0";
136 init
.extensions
= "GL_KHR_robustness";
137 InitDecoderWithCommandLine(init
, &command_line
);
140 void DoGetErrorWithContextLost(GLenum reset_status
) {
141 DCHECK(context_
->HasExtension("GL_KHR_robustness"));
142 EXPECT_CALL(*gl_
, GetError())
143 .WillOnce(Return(GL_CONTEXT_LOST_KHR
))
144 .RetiresOnSaturation();
145 EXPECT_CALL(*gl_
, GetGraphicsResetStatusARB())
146 .WillOnce(Return(reset_status
));
148 cmd
.Init(shared_memory_id_
, shared_memory_offset_
);
149 EXPECT_EQ(error::kLostContext
, ExecuteCmd(cmd
));
150 EXPECT_EQ(static_cast<GLuint
>(GL_NO_ERROR
), *GetSharedMemoryAs
<GLenum
*>());
153 void ClearCurrentDecoderError() {
154 DCHECK(decoder_
->WasContextLost());
155 EXPECT_CALL(*gl_
, GetError())
156 .WillOnce(Return(GL_CONTEXT_LOST_KHR
))
157 .RetiresOnSaturation();
159 cmd
.Init(shared_memory_id_
, shared_memory_offset_
);
160 EXPECT_EQ(error::kLostContext
, ExecuteCmd(cmd
));
164 TEST_P(GLES2DecoderLostContextTest
, LostFromMakeCurrent
) {
165 Init(false); // without robustness
166 EXPECT_CALL(*context_
, MakeCurrent(surface_
.get())).WillOnce(Return(false));
167 // Expect the group to be lost.
168 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
)).Times(1);
169 decoder_
->MakeCurrent();
170 EXPECT_TRUE(decoder_
->WasContextLost());
171 EXPECT_EQ(error::kMakeCurrentFailed
, decoder_
->GetContextLostReason());
173 // We didn't process commands, so we need to clear the decoder error,
174 // so that we can shut down cleanly.
175 ClearCurrentDecoderError();
178 TEST_P(GLES2DecoderLostContextTest
, LostFromMakeCurrentWithRobustness
) {
179 Init(true); // with robustness
180 // If we can't make the context current, we cannot query the robustness
182 EXPECT_CALL(*gl_
, GetGraphicsResetStatusARB()).Times(0);
183 EXPECT_CALL(*context_
, MakeCurrent(surface_
.get())).WillOnce(Return(false));
184 // Expect the group to be lost.
185 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
)).Times(1);
186 decoder_
->MakeCurrent();
187 EXPECT_TRUE(decoder_
->WasContextLost());
188 EXPECT_FALSE(decoder_
->WasContextLostByRobustnessExtension());
189 EXPECT_EQ(error::kMakeCurrentFailed
, decoder_
->GetContextLostReason());
191 // We didn't process commands, so we need to clear the decoder error,
192 // so that we can shut down cleanly.
193 ClearCurrentDecoderError();
196 TEST_P(GLES2DecoderLostContextTest
, LostFromResetAfterMakeCurrent
) {
197 Init(true); // with robustness
199 EXPECT_CALL(*context_
, MakeCurrent(surface_
.get())).WillOnce(Return(true));
200 EXPECT_CALL(*gl_
, GetGraphicsResetStatusARB())
201 .WillOnce(Return(GL_GUILTY_CONTEXT_RESET_KHR
));
202 // Expect the group to be lost.
203 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
)).Times(1);
204 decoder_
->MakeCurrent();
205 EXPECT_TRUE(decoder_
->WasContextLost());
206 EXPECT_TRUE(decoder_
->WasContextLostByRobustnessExtension());
207 EXPECT_EQ(error::kGuilty
, decoder_
->GetContextLostReason());
209 // We didn't process commands, so we need to clear the decoder error,
210 // so that we can shut down cleanly.
211 ClearCurrentDecoderError();
214 TEST_P(GLES2DecoderLostContextTest
, LoseGuiltyFromGLError
) {
216 // Always expect other contexts to be signaled as 'kUnknown' since we can't
217 // query their status without making them current.
218 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
))
220 DoGetErrorWithContextLost(GL_GUILTY_CONTEXT_RESET_KHR
);
221 EXPECT_TRUE(decoder_
->WasContextLost());
222 EXPECT_TRUE(decoder_
->WasContextLostByRobustnessExtension());
223 EXPECT_EQ(error::kGuilty
, decoder_
->GetContextLostReason());
226 TEST_P(GLES2DecoderLostContextTest
, LoseInnocentFromGLError
) {
228 // Always expect other contexts to be signaled as 'kUnknown' since we can't
229 // query their status without making them current.
230 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
))
232 DoGetErrorWithContextLost(GL_INNOCENT_CONTEXT_RESET_KHR
);
233 EXPECT_TRUE(decoder_
->WasContextLost());
234 EXPECT_TRUE(decoder_
->WasContextLostByRobustnessExtension());
235 EXPECT_EQ(error::kInnocent
, decoder_
->GetContextLostReason());
238 TEST_P(GLES2DecoderLostContextTest
, LoseVirtualContextWithRobustness
) {
239 InitWithVirtualContextsAndRobustness();
240 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
))
243 DoGetErrorWithContextLost(GL_GUILTY_CONTEXT_RESET_KHR
);
244 EXPECT_TRUE(decoder_
->WasContextLost());
245 EXPECT_TRUE(decoder_
->WasContextLostByRobustnessExtension());
246 // ...but make sure we don't pretend, since for virtual contexts we don't
247 // know if this was really the guilty client.
248 EXPECT_EQ(error::kUnknown
, decoder_
->GetContextLostReason());
251 TEST_P(GLES2DecoderLostContextTest
, LoseGroupFromRobustness
) {
252 // If one context in a group is lost through robustness,
253 // the other ones should also get lost and query the reset status.
255 EXPECT_CALL(*mock_decoder_
, MarkContextLost(error::kUnknown
))
257 // There should be no GL calls, since we might not have a current context.
258 EXPECT_CALL(*gl_
, GetGraphicsResetStatusARB()).Times(0);
259 LoseContexts(error::kUnknown
);
260 EXPECT_TRUE(decoder_
->WasContextLost());
261 EXPECT_EQ(error::kUnknown
, decoder_
->GetContextLostReason());
263 // We didn't process commands, so we need to clear the decoder error,
264 // so that we can shut down cleanly.
265 ClearCurrentDecoderError();
268 INSTANTIATE_TEST_CASE_P(Service
,
269 GLES2DecoderLostContextTest
,