Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / gles2_cmd_decoder_unittest_base.cc
1 // Copyright (c) 2012 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.
4
5 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_base.h"
6
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
14 #include "gpu/command_buffer/common/gles2_cmd_utils.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/logger.h"
18 #include "gpu/command_buffer/service/program_manager.h"
19 #include "gpu/command_buffer/service/test_helper.h"
20 #include "gpu/command_buffer/service/vertex_attrib_manager.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/gl/gl_implementation.h"
23 #include "ui/gl/gl_mock.h"
24 #include "ui/gl/gl_surface.h"
25
26 using ::gfx::MockGLInterface;
27 using ::testing::_;
28 using ::testing::DoAll;
29 using ::testing::InSequence;
30 using ::testing::Invoke;
31 using ::testing::InvokeWithoutArgs;
32 using ::testing::MatcherCast;
33 using ::testing::Pointee;
34 using ::testing::Return;
35 using ::testing::SetArrayArgument;
36 using ::testing::SetArgPointee;
37 using ::testing::SetArgumentPointee;
38 using ::testing::StrEq;
39 using ::testing::StrictMock;
40 using ::testing::WithArg;
41
42 namespace {
43
44 void NormalizeInitState(gpu::gles2::GLES2DecoderTestBase::InitState* init) {
45   CHECK(init);
46   const char* kVAOExtensions[] = {
47       "GL_OES_vertex_array_object",
48       "GL_ARB_vertex_array_object",
49       "GL_APPLE_vertex_array_object"
50   };
51   bool contains_vao_extension = false;
52   for (size_t ii = 0; ii < arraysize(kVAOExtensions); ++ii) {
53     if (init->extensions.find(kVAOExtensions[ii]) != std::string::npos) {
54       contains_vao_extension = true;
55       break;
56     }
57   }
58   if (init->use_native_vao) {
59     if (contains_vao_extension)
60       return;
61     if (!init->extensions.empty())
62       init->extensions += " ";
63     if (StartsWithASCII(init->gl_version, "opengl es", false)) {
64       init->extensions += kVAOExtensions[0];
65     } else {
66 #if !defined(OS_MACOSX)
67       init->extensions += kVAOExtensions[1];
68 #else
69       init->extensions += kVAOExtensions[2];
70 #endif  // OS_MACOSX
71     }
72   } else {
73     // Make sure we don't set up an invalid InitState.
74     CHECK(!contains_vao_extension);
75   }
76 }
77
78 }  // namespace Anonymous
79
80 namespace gpu {
81 namespace gles2 {
82
83 GLES2DecoderTestBase::GLES2DecoderTestBase()
84     : surface_(NULL),
85       context_(NULL),
86       memory_tracker_(NULL),
87       client_buffer_id_(100),
88       client_framebuffer_id_(101),
89       client_program_id_(102),
90       client_renderbuffer_id_(103),
91       client_shader_id_(104),
92       client_texture_id_(106),
93       client_element_buffer_id_(107),
94       client_vertex_shader_id_(121),
95       client_fragment_shader_id_(122),
96       client_query_id_(123),
97       client_vertexarray_id_(124),
98       service_renderbuffer_id_(0),
99       service_renderbuffer_valid_(false),
100       ignore_cached_state_for_test_(GetParam()),
101       cached_color_mask_red_(true),
102       cached_color_mask_green_(true),
103       cached_color_mask_blue_(true),
104       cached_color_mask_alpha_(true),
105       cached_depth_mask_(true),
106       cached_stencil_front_mask_(static_cast<GLuint>(-1)),
107       cached_stencil_back_mask_(static_cast<GLuint>(-1)) {
108   memset(immediate_buffer_, 0xEE, sizeof(immediate_buffer_));
109 }
110
111 GLES2DecoderTestBase::~GLES2DecoderTestBase() {}
112
113 void GLES2DecoderTestBase::SetUp() {
114   InitState init;
115   init.gl_version = "3.0";
116   init.has_alpha = true;
117   init.has_depth = true;
118   init.request_alpha = true;
119   init.request_depth = true;
120   init.bind_generates_resource = true;
121   InitDecoder(init);
122 }
123
124 void GLES2DecoderTestBase::AddExpectationsForVertexAttribManager() {
125   for (GLint ii = 0; ii < kNumVertexAttribs; ++ii) {
126     EXPECT_CALL(*gl_, VertexAttrib4f(ii, 0.0f, 0.0f, 0.0f, 1.0f))
127         .Times(1)
128         .RetiresOnSaturation();
129   }
130 }
131
132 GLES2DecoderTestBase::InitState::InitState()
133     : has_alpha(false),
134       has_depth(false),
135       has_stencil(false),
136       request_alpha(false),
137       request_depth(false),
138       request_stencil(false),
139       bind_generates_resource(false),
140       lose_context_when_out_of_memory(false),
141       use_native_vao(true) {
142 }
143
144 void GLES2DecoderTestBase::InitDecoder(const InitState& init) {
145   InitDecoderWithCommandLine(init, NULL);
146 }
147
148 void GLES2DecoderTestBase::InitDecoderWithCommandLine(
149     const InitState& init,
150     const base::CommandLine* command_line) {
151   InitState normalized_init = init;
152   NormalizeInitState(&normalized_init);
153   Framebuffer::ClearFramebufferCompleteComboMap();
154
155   gfx::SetGLGetProcAddressProc(gfx::MockGLInterface::GetGLProcAddress);
156   gfx::GLSurface::InitializeOneOffWithMockBindingsForTests();
157
158   gl_.reset(new StrictMock<MockGLInterface>());
159   ::gfx::MockGLInterface::SetGLInterface(gl_.get());
160
161   SetupMockGLBehaviors();
162
163   // Only create stream texture manager if extension is requested.
164   std::vector<std::string> list;
165   base::SplitString(normalized_init.extensions, ' ', &list);
166   scoped_refptr<FeatureInfo> feature_info;
167   if (command_line)
168     feature_info = new FeatureInfo(*command_line);
169   group_ = scoped_refptr<ContextGroup>(
170       new ContextGroup(NULL,
171                        memory_tracker_,
172                        new ShaderTranslatorCache,
173                        feature_info.get(),
174                        normalized_init.bind_generates_resource));
175   bool use_default_textures = normalized_init.bind_generates_resource;
176
177   InSequence sequence;
178
179   surface_ = new gfx::GLSurfaceStub;
180   surface_->SetSize(gfx::Size(kBackBufferWidth, kBackBufferHeight));
181
182   // Context needs to be created before initializing ContextGroup, which will
183   // in turn initialize FeatureInfo, which needs a context to determine
184   // extension support.
185   context_ = new gfx::GLContextStubWithExtensions;
186   context_->AddExtensionsString(normalized_init.extensions.c_str());
187   context_->SetGLVersionString(normalized_init.gl_version.c_str());
188
189   context_->MakeCurrent(surface_.get());
190   gfx::GLSurface::InitializeDynamicMockBindingsForTests(context_);
191
192   TestHelper::SetupContextGroupInitExpectations(
193       gl_.get(),
194       DisallowedFeatures(),
195       normalized_init.extensions.c_str(),
196       normalized_init.gl_version.c_str(),
197       normalized_init.bind_generates_resource);
198
199   // We initialize the ContextGroup with a MockGLES2Decoder so that
200   // we can use the ContextGroup to figure out how the real GLES2Decoder
201   // will initialize itself.
202   mock_decoder_.reset(new MockGLES2Decoder());
203   EXPECT_TRUE(
204       group_->Initialize(mock_decoder_.get(), DisallowedFeatures()));
205
206   if (group_->feature_info()->feature_flags().native_vertex_array_object) {
207     EXPECT_CALL(*gl_, GenVertexArraysOES(1, _))
208       .WillOnce(SetArgumentPointee<1>(kServiceVertexArrayId))
209         .RetiresOnSaturation();
210     EXPECT_CALL(*gl_, BindVertexArrayOES(_)).Times(1).RetiresOnSaturation();
211   }
212
213   if (group_->feature_info()->workarounds().init_vertex_attributes)
214     AddExpectationsForVertexAttribManager();
215
216   AddExpectationsForBindVertexArrayOES();
217
218   EXPECT_CALL(*gl_, EnableVertexAttribArray(0))
219       .Times(1)
220       .RetiresOnSaturation();
221   static GLuint attrib_0_id[] = {
222     kServiceAttrib0BufferId,
223   };
224   static GLuint fixed_attrib_buffer_id[] = {
225     kServiceFixedAttribBufferId,
226   };
227   EXPECT_CALL(*gl_, GenBuffersARB(arraysize(attrib_0_id), _))
228       .WillOnce(SetArrayArgument<1>(attrib_0_id,
229                                     attrib_0_id + arraysize(attrib_0_id)))
230       .RetiresOnSaturation();
231   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, kServiceAttrib0BufferId))
232       .Times(1)
233       .RetiresOnSaturation();
234   EXPECT_CALL(*gl_, VertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL))
235       .Times(1)
236       .RetiresOnSaturation();
237   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, 0))
238       .Times(1)
239       .RetiresOnSaturation();
240   EXPECT_CALL(*gl_, GenBuffersARB(arraysize(fixed_attrib_buffer_id), _))
241       .WillOnce(SetArrayArgument<1>(
242           fixed_attrib_buffer_id,
243           fixed_attrib_buffer_id + arraysize(fixed_attrib_buffer_id)))
244       .RetiresOnSaturation();
245
246   for (GLint tt = 0; tt < TestHelper::kNumTextureUnits; ++tt) {
247     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0 + tt))
248         .Times(1)
249         .RetiresOnSaturation();
250     if (group_->feature_info()->feature_flags().oes_egl_image_external) {
251       EXPECT_CALL(*gl_,
252                   BindTexture(GL_TEXTURE_EXTERNAL_OES,
253                               use_default_textures
254                                   ? TestHelper::kServiceDefaultExternalTextureId
255                                   : 0))
256           .Times(1)
257           .RetiresOnSaturation();
258     }
259     if (group_->feature_info()->feature_flags().arb_texture_rectangle) {
260       EXPECT_CALL(
261           *gl_,
262           BindTexture(GL_TEXTURE_RECTANGLE_ARB,
263                       use_default_textures
264                           ? TestHelper::kServiceDefaultRectangleTextureId
265                           : 0))
266           .Times(1)
267           .RetiresOnSaturation();
268     }
269     EXPECT_CALL(*gl_,
270                 BindTexture(GL_TEXTURE_CUBE_MAP,
271                             use_default_textures
272                                 ? TestHelper::kServiceDefaultTextureCubemapId
273                                 : 0))
274         .Times(1)
275         .RetiresOnSaturation();
276     EXPECT_CALL(
277         *gl_,
278         BindTexture(
279             GL_TEXTURE_2D,
280             use_default_textures ? TestHelper::kServiceDefaultTexture2dId : 0))
281         .Times(1)
282         .RetiresOnSaturation();
283   }
284   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
285       .Times(1)
286       .RetiresOnSaturation();
287
288   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER, 0))
289       .Times(1)
290       .RetiresOnSaturation();
291   EXPECT_CALL(*gl_, GetIntegerv(GL_ALPHA_BITS, _))
292       .WillOnce(SetArgumentPointee<1>(normalized_init.has_alpha ? 8 : 0))
293       .RetiresOnSaturation();
294   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
295       .WillOnce(SetArgumentPointee<1>(normalized_init.has_depth ? 24 : 0))
296       .RetiresOnSaturation();
297   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
298       .WillOnce(SetArgumentPointee<1>(normalized_init.has_stencil ? 8 : 0))
299       .RetiresOnSaturation();
300
301   EXPECT_CALL(*gl_, Enable(GL_VERTEX_PROGRAM_POINT_SIZE))
302       .Times(1)
303       .RetiresOnSaturation();
304
305   EXPECT_CALL(*gl_, Enable(GL_POINT_SPRITE))
306       .Times(1)
307       .RetiresOnSaturation();
308
309   static GLint max_viewport_dims[] = {
310     kMaxViewportWidth,
311     kMaxViewportHeight
312   };
313   EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_VIEWPORT_DIMS, _))
314       .WillOnce(SetArrayArgument<1>(
315           max_viewport_dims, max_viewport_dims + arraysize(max_viewport_dims)))
316       .RetiresOnSaturation();
317
318   SetupInitCapabilitiesExpectations();
319   SetupInitStateExpectations();
320
321   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
322       .Times(1)
323       .RetiresOnSaturation();
324
325   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, 0))
326       .Times(1)
327       .RetiresOnSaturation();
328   EXPECT_CALL(*gl_, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
329       .Times(1)
330       .RetiresOnSaturation();
331   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER, 0))
332       .Times(1)
333       .RetiresOnSaturation();
334   EXPECT_CALL(*gl_, BindRenderbufferEXT(GL_RENDERBUFFER, 0))
335       .Times(1)
336       .RetiresOnSaturation();
337
338   // TODO(boliu): Remove OS_ANDROID once crbug.com/259023 is fixed and the
339   // workaround has been reverted.
340 #if !defined(OS_ANDROID)
341   EXPECT_CALL(*gl_, Clear(
342       GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
343       .Times(1)
344       .RetiresOnSaturation();
345 #endif
346
347   engine_.reset(new StrictMock<MockCommandBufferEngine>());
348   scoped_refptr<gpu::Buffer> buffer =
349       engine_->GetSharedMemoryBuffer(kSharedMemoryId);
350   shared_memory_offset_ = kSharedMemoryOffset;
351   shared_memory_address_ =
352       reinterpret_cast<int8*>(buffer->memory()) + shared_memory_offset_;
353   shared_memory_id_ = kSharedMemoryId;
354   shared_memory_base_ = buffer->memory();
355
356   static const int32 kLoseContextWhenOutOfMemory = 0x10002;
357
358   int32 attributes[] = {
359       EGL_ALPHA_SIZE,
360       normalized_init.request_alpha ? 8 : 0,
361       EGL_DEPTH_SIZE,
362       normalized_init.request_depth ? 24 : 0,
363       EGL_STENCIL_SIZE,
364       normalized_init.request_stencil ? 8 : 0,
365       kLoseContextWhenOutOfMemory,
366       normalized_init.lose_context_when_out_of_memory ? 1 : 0, };
367   std::vector<int32> attribs(attributes, attributes + arraysize(attributes));
368
369   decoder_.reset(GLES2Decoder::Create(group_.get()));
370   decoder_->SetIgnoreCachedStateForTest(ignore_cached_state_for_test_);
371   decoder_->GetLogger()->set_log_synthesized_gl_errors(false);
372   decoder_->Initialize(surface_,
373                        context_,
374                        false,
375                        surface_->GetSize(),
376                        DisallowedFeatures(),
377                        attribs);
378   decoder_->MakeCurrent();
379   decoder_->set_engine(engine_.get());
380   decoder_->BeginDecoding();
381
382   EXPECT_CALL(*gl_, GenBuffersARB(_, _))
383       .WillOnce(SetArgumentPointee<1>(kServiceBufferId))
384       .RetiresOnSaturation();
385   GenHelper<cmds::GenBuffersImmediate>(client_buffer_id_);
386   EXPECT_CALL(*gl_, GenFramebuffersEXT(_, _))
387       .WillOnce(SetArgumentPointee<1>(kServiceFramebufferId))
388       .RetiresOnSaturation();
389   GenHelper<cmds::GenFramebuffersImmediate>(client_framebuffer_id_);
390   EXPECT_CALL(*gl_, GenRenderbuffersEXT(_, _))
391       .WillOnce(SetArgumentPointee<1>(kServiceRenderbufferId))
392       .RetiresOnSaturation();
393   GenHelper<cmds::GenRenderbuffersImmediate>(client_renderbuffer_id_);
394   EXPECT_CALL(*gl_, GenTextures(_, _))
395       .WillOnce(SetArgumentPointee<1>(kServiceTextureId))
396       .RetiresOnSaturation();
397   GenHelper<cmds::GenTexturesImmediate>(client_texture_id_);
398   EXPECT_CALL(*gl_, GenBuffersARB(_, _))
399       .WillOnce(SetArgumentPointee<1>(kServiceElementBufferId))
400       .RetiresOnSaturation();
401   GenHelper<cmds::GenBuffersImmediate>(client_element_buffer_id_);
402
403   DoCreateProgram(client_program_id_, kServiceProgramId);
404   DoCreateShader(GL_VERTEX_SHADER, client_shader_id_, kServiceShaderId);
405
406   EXPECT_EQ(GL_NO_ERROR, GetGLError());
407 }
408
409 void GLES2DecoderTestBase::ResetDecoder() {
410   if (!decoder_.get())
411     return;
412   // All Tests should have read all their GLErrors before getting here.
413   EXPECT_EQ(GL_NO_ERROR, GetGLError());
414
415   EXPECT_CALL(*gl_, DeleteBuffersARB(1, _))
416       .Times(2)
417       .RetiresOnSaturation();
418   if (group_->feature_info()->feature_flags().native_vertex_array_object) {
419     EXPECT_CALL(*gl_, DeleteVertexArraysOES(1, Pointee(kServiceVertexArrayId)))
420         .Times(1)
421         .RetiresOnSaturation();
422   }
423
424   decoder_->EndDecoding();
425   decoder_->Destroy(true);
426   decoder_.reset();
427   group_->Destroy(mock_decoder_.get(), false);
428   engine_.reset();
429   ::gfx::MockGLInterface::SetGLInterface(NULL);
430   gl_.reset();
431   gfx::ClearGLBindings();
432 }
433
434 void GLES2DecoderTestBase::TearDown() {
435   ResetDecoder();
436 }
437
438 void GLES2DecoderTestBase::ExpectEnableDisable(GLenum cap, bool enable) {
439   if (enable) {
440     EXPECT_CALL(*gl_, Enable(cap))
441         .Times(1)
442         .RetiresOnSaturation();
443   } else {
444     EXPECT_CALL(*gl_, Disable(cap))
445         .Times(1)
446         .RetiresOnSaturation();
447   }
448 }
449
450
451 GLint GLES2DecoderTestBase::GetGLError() {
452   EXPECT_CALL(*gl_, GetError())
453       .WillOnce(Return(GL_NO_ERROR))
454       .RetiresOnSaturation();
455   cmds::GetError cmd;
456   cmd.Init(shared_memory_id_, shared_memory_offset_);
457   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
458   return static_cast<GLint>(*GetSharedMemoryAs<GLenum*>());
459 }
460
461 void GLES2DecoderTestBase::DoCreateShader(
462     GLenum shader_type, GLuint client_id, GLuint service_id) {
463   EXPECT_CALL(*gl_, CreateShader(shader_type))
464       .Times(1)
465       .WillOnce(Return(service_id))
466       .RetiresOnSaturation();
467   cmds::CreateShader cmd;
468   cmd.Init(shader_type, client_id);
469   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
470 }
471
472 bool GLES2DecoderTestBase::DoIsShader(GLuint client_id) {
473   return IsObjectHelper<cmds::IsShader, cmds::IsShader::Result>(client_id);
474 }
475
476 void GLES2DecoderTestBase::DoDeleteShader(
477     GLuint client_id, GLuint service_id) {
478   EXPECT_CALL(*gl_, DeleteShader(service_id))
479       .Times(1)
480       .RetiresOnSaturation();
481   cmds::DeleteShader cmd;
482   cmd.Init(client_id);
483   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
484 }
485
486 void GLES2DecoderTestBase::DoCreateProgram(
487     GLuint client_id, GLuint service_id) {
488   EXPECT_CALL(*gl_, CreateProgram())
489       .Times(1)
490       .WillOnce(Return(service_id))
491       .RetiresOnSaturation();
492   cmds::CreateProgram cmd;
493   cmd.Init(client_id);
494   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
495 }
496
497 bool GLES2DecoderTestBase::DoIsProgram(GLuint client_id) {
498   return IsObjectHelper<cmds::IsProgram, cmds::IsProgram::Result>(client_id);
499 }
500
501 void GLES2DecoderTestBase::DoDeleteProgram(
502     GLuint client_id, GLuint /* service_id */) {
503   cmds::DeleteProgram cmd;
504   cmd.Init(client_id);
505   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
506 }
507
508 void GLES2DecoderTestBase::SetBucketAsCString(
509     uint32 bucket_id, const char* str) {
510   uint32 size = str ? (strlen(str) + 1) : 0;
511   cmd::SetBucketSize cmd1;
512   cmd1.Init(bucket_id, size);
513   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd1));
514   if (str) {
515     memcpy(shared_memory_address_, str, size);
516     cmd::SetBucketData cmd2;
517     cmd2.Init(bucket_id, 0, size, kSharedMemoryId, kSharedMemoryOffset);
518     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
519     ClearSharedMemory();
520   }
521 }
522
523 void GLES2DecoderTestBase::SetupClearTextureExpectations(
524       GLuint service_id,
525       GLuint old_service_id,
526       GLenum bind_target,
527       GLenum target,
528       GLint level,
529       GLenum internal_format,
530       GLenum format,
531       GLenum type,
532       GLsizei width,
533       GLsizei height) {
534   EXPECT_CALL(*gl_, BindTexture(bind_target, service_id))
535       .Times(1)
536       .RetiresOnSaturation();
537   EXPECT_CALL(*gl_, TexImage2D(
538       target, level, internal_format, width, height, 0, format, type, _))
539       .Times(1)
540       .RetiresOnSaturation();
541   EXPECT_CALL(*gl_, BindTexture(bind_target, old_service_id))
542       .Times(1)
543       .RetiresOnSaturation();
544 }
545
546 void GLES2DecoderTestBase::SetupExpectationsForFramebufferClearing(
547     GLenum target,
548     GLuint clear_bits,
549     GLclampf restore_red,
550     GLclampf restore_green,
551     GLclampf restore_blue,
552     GLclampf restore_alpha,
553     GLuint restore_stencil,
554     GLclampf restore_depth,
555     bool restore_scissor_test) {
556   SetupExpectationsForFramebufferClearingMulti(
557       0,
558       0,
559       target,
560       clear_bits,
561       restore_red,
562       restore_green,
563       restore_blue,
564       restore_alpha,
565       restore_stencil,
566       restore_depth,
567       restore_scissor_test);
568 }
569
570 void GLES2DecoderTestBase::SetupExpectationsForRestoreClearState(
571     GLclampf restore_red,
572     GLclampf restore_green,
573     GLclampf restore_blue,
574     GLclampf restore_alpha,
575     GLuint restore_stencil,
576     GLclampf restore_depth,
577     bool restore_scissor_test) {
578   EXPECT_CALL(*gl_, ClearColor(
579       restore_red, restore_green, restore_blue, restore_alpha))
580       .Times(1)
581       .RetiresOnSaturation();
582   EXPECT_CALL(*gl_, ClearStencil(restore_stencil))
583       .Times(1)
584       .RetiresOnSaturation();
585   EXPECT_CALL(*gl_, ClearDepth(restore_depth))
586       .Times(1)
587       .RetiresOnSaturation();
588   if (restore_scissor_test) {
589     EXPECT_CALL(*gl_, Enable(GL_SCISSOR_TEST))
590         .Times(1)
591         .RetiresOnSaturation();
592   }
593 }
594
595 void GLES2DecoderTestBase::SetupExpectationsForFramebufferClearingMulti(
596     GLuint read_framebuffer_service_id,
597     GLuint draw_framebuffer_service_id,
598     GLenum target,
599     GLuint clear_bits,
600     GLclampf restore_red,
601     GLclampf restore_green,
602     GLclampf restore_blue,
603     GLclampf restore_alpha,
604     GLuint restore_stencil,
605     GLclampf restore_depth,
606     bool restore_scissor_test) {
607   // TODO(gman): Figure out why InSequence stopped working.
608   // InSequence sequence;
609   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(target))
610       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
611       .RetiresOnSaturation();
612   if (target == GL_READ_FRAMEBUFFER_EXT) {
613     EXPECT_CALL(*gl_, BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0))
614         .Times(1)
615         .RetiresOnSaturation();
616     EXPECT_CALL(*gl_, BindFramebufferEXT(
617         GL_DRAW_FRAMEBUFFER_EXT, read_framebuffer_service_id))
618         .Times(1)
619         .RetiresOnSaturation();
620   }
621   if ((clear_bits & GL_COLOR_BUFFER_BIT) != 0) {
622     EXPECT_CALL(*gl_, ClearColor(0.0f, 0.0f, 0.0f, 0.0f))
623         .Times(1)
624         .RetiresOnSaturation();
625     SetupExpectationsForColorMask(true, true, true, true);
626   }
627   if ((clear_bits & GL_STENCIL_BUFFER_BIT) != 0) {
628     EXPECT_CALL(*gl_, ClearStencil(0))
629         .Times(1)
630         .RetiresOnSaturation();
631     EXPECT_CALL(*gl_, StencilMask(static_cast<GLuint>(-1)))
632         .Times(1)
633         .RetiresOnSaturation();
634   }
635   if ((clear_bits & GL_DEPTH_BUFFER_BIT) != 0) {
636     EXPECT_CALL(*gl_, ClearDepth(1.0f))
637         .Times(1)
638         .RetiresOnSaturation();
639     SetupExpectationsForDepthMask(true);
640   }
641   SetupExpectationsForEnableDisable(GL_SCISSOR_TEST, false);
642   EXPECT_CALL(*gl_, Clear(clear_bits))
643       .Times(1)
644       .RetiresOnSaturation();
645   SetupExpectationsForRestoreClearState(
646       restore_red, restore_green, restore_blue, restore_alpha,
647       restore_stencil, restore_depth, restore_scissor_test);
648   if (target == GL_READ_FRAMEBUFFER_EXT) {
649     EXPECT_CALL(*gl_, BindFramebufferEXT(
650         GL_READ_FRAMEBUFFER_EXT, read_framebuffer_service_id))
651         .Times(1)
652         .RetiresOnSaturation();
653     EXPECT_CALL(*gl_, BindFramebufferEXT(
654         GL_DRAW_FRAMEBUFFER_EXT, draw_framebuffer_service_id))
655         .Times(1)
656         .RetiresOnSaturation();
657   }
658 }
659
660 void GLES2DecoderTestBase::SetupShaderForUniform(GLenum uniform_type) {
661   static AttribInfo attribs[] = {
662     { "foo", 1, GL_FLOAT, 1, },
663     { "goo", 1, GL_FLOAT, 2, },
664   };
665   UniformInfo uniforms[] = {
666     { "bar", 1, uniform_type, 0, 2, -1, },
667     { "car", 4, uniform_type, 1, 1, -1, },
668   };
669   const GLuint kClientVertexShaderId = 5001;
670   const GLuint kServiceVertexShaderId = 6001;
671   const GLuint kClientFragmentShaderId = 5002;
672   const GLuint kServiceFragmentShaderId = 6002;
673   SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
674               client_program_id_, kServiceProgramId,
675               kClientVertexShaderId, kServiceVertexShaderId,
676               kClientFragmentShaderId, kServiceFragmentShaderId);
677
678   EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
679       .Times(1)
680       .RetiresOnSaturation();
681   cmds::UseProgram cmd;
682   cmd.Init(client_program_id_);
683   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
684 }
685
686 void GLES2DecoderTestBase::DoBindBuffer(
687     GLenum target, GLuint client_id, GLuint service_id) {
688   EXPECT_CALL(*gl_, BindBuffer(target, service_id))
689       .Times(1)
690       .RetiresOnSaturation();
691   cmds::BindBuffer cmd;
692   cmd.Init(target, client_id);
693   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
694 }
695
696 bool GLES2DecoderTestBase::DoIsBuffer(GLuint client_id) {
697   return IsObjectHelper<cmds::IsBuffer, cmds::IsBuffer::Result>(client_id);
698 }
699
700 void GLES2DecoderTestBase::DoDeleteBuffer(
701     GLuint client_id, GLuint service_id) {
702   EXPECT_CALL(*gl_, DeleteBuffersARB(1, Pointee(service_id)))
703       .Times(1)
704       .RetiresOnSaturation();
705   GenHelper<cmds::DeleteBuffersImmediate>(client_id);
706 }
707
708 void GLES2DecoderTestBase::SetupExpectationsForColorMask(bool red,
709                                                          bool green,
710                                                          bool blue,
711                                                          bool alpha) {
712   if (ignore_cached_state_for_test_ || cached_color_mask_red_ != red ||
713       cached_color_mask_green_ != green || cached_color_mask_blue_ != blue ||
714       cached_color_mask_alpha_ != alpha) {
715     cached_color_mask_red_ = red;
716     cached_color_mask_green_ = green;
717     cached_color_mask_blue_ = blue;
718     cached_color_mask_alpha_ = alpha;
719     EXPECT_CALL(*gl_, ColorMask(red, green, blue, alpha))
720         .Times(1)
721         .RetiresOnSaturation();
722   }
723 }
724
725 void GLES2DecoderTestBase::SetupExpectationsForDepthMask(bool mask) {
726   if (ignore_cached_state_for_test_ || cached_depth_mask_ != mask) {
727     cached_depth_mask_ = mask;
728     EXPECT_CALL(*gl_, DepthMask(mask)).Times(1).RetiresOnSaturation();
729   }
730 }
731
732 void GLES2DecoderTestBase::SetupExpectationsForStencilMask(GLuint front_mask,
733                                                            GLuint back_mask) {
734   if (ignore_cached_state_for_test_ ||
735       cached_stencil_front_mask_ != front_mask) {
736     cached_stencil_front_mask_ = front_mask;
737     EXPECT_CALL(*gl_, StencilMaskSeparate(GL_FRONT, front_mask))
738         .Times(1)
739         .RetiresOnSaturation();
740   }
741
742   if (ignore_cached_state_for_test_ ||
743       cached_stencil_back_mask_ != back_mask) {
744     cached_stencil_back_mask_ = back_mask;
745     EXPECT_CALL(*gl_, StencilMaskSeparate(GL_BACK, back_mask))
746         .Times(1)
747         .RetiresOnSaturation();
748   }
749 }
750
751 void GLES2DecoderTestBase::SetupExpectationsForEnableDisable(GLenum cap,
752                                                              bool enable) {
753   switch (cap) {
754     case GL_BLEND:
755       if (enable_flags_.cached_blend == enable &&
756           !ignore_cached_state_for_test_)
757         return;
758       enable_flags_.cached_blend = enable;
759       break;
760     case GL_CULL_FACE:
761       if (enable_flags_.cached_cull_face == enable &&
762           !ignore_cached_state_for_test_)
763         return;
764       enable_flags_.cached_cull_face = enable;
765       break;
766     case GL_DEPTH_TEST:
767       if (enable_flags_.cached_depth_test == enable &&
768           !ignore_cached_state_for_test_)
769         return;
770       enable_flags_.cached_depth_test = enable;
771       break;
772     case GL_DITHER:
773       if (enable_flags_.cached_dither == enable &&
774           !ignore_cached_state_for_test_)
775         return;
776       enable_flags_.cached_dither = enable;
777       break;
778     case GL_POLYGON_OFFSET_FILL:
779       if (enable_flags_.cached_polygon_offset_fill == enable &&
780           !ignore_cached_state_for_test_)
781         return;
782       enable_flags_.cached_polygon_offset_fill = enable;
783       break;
784     case GL_SAMPLE_ALPHA_TO_COVERAGE:
785       if (enable_flags_.cached_sample_alpha_to_coverage == enable &&
786           !ignore_cached_state_for_test_)
787         return;
788       enable_flags_.cached_sample_alpha_to_coverage = enable;
789       break;
790     case GL_SAMPLE_COVERAGE:
791       if (enable_flags_.cached_sample_coverage == enable &&
792           !ignore_cached_state_for_test_)
793         return;
794       enable_flags_.cached_sample_coverage = enable;
795       break;
796     case GL_SCISSOR_TEST:
797       if (enable_flags_.cached_scissor_test == enable &&
798           !ignore_cached_state_for_test_)
799         return;
800       enable_flags_.cached_scissor_test = enable;
801       break;
802     case GL_STENCIL_TEST:
803       if (enable_flags_.cached_stencil_test == enable &&
804           !ignore_cached_state_for_test_)
805         return;
806       enable_flags_.cached_stencil_test = enable;
807       break;
808     default:
809       NOTREACHED();
810       return;
811   }
812   if (enable) {
813     EXPECT_CALL(*gl_, Enable(cap)).Times(1).RetiresOnSaturation();
814   } else {
815     EXPECT_CALL(*gl_, Disable(cap)).Times(1).RetiresOnSaturation();
816   }
817 }
818
819 void GLES2DecoderTestBase::SetupExpectationsForApplyingDirtyState(
820     bool framebuffer_is_rgb,
821     bool framebuffer_has_depth,
822     bool framebuffer_has_stencil,
823     GLuint color_bits,
824     bool depth_mask,
825     bool depth_enabled,
826     GLuint front_stencil_mask,
827     GLuint back_stencil_mask,
828     bool stencil_enabled) {
829   bool color_mask_red = (color_bits & 0x1000) != 0;
830   bool color_mask_green = (color_bits & 0x0100) != 0;
831   bool color_mask_blue = (color_bits & 0x0010) != 0;
832   bool color_mask_alpha = (color_bits & 0x0001) && !framebuffer_is_rgb;
833
834   SetupExpectationsForColorMask(
835       color_mask_red, color_mask_green, color_mask_blue, color_mask_alpha);
836   SetupExpectationsForDepthMask(depth_mask);
837   SetupExpectationsForStencilMask(front_stencil_mask, back_stencil_mask);
838   SetupExpectationsForEnableDisable(GL_DEPTH_TEST,
839                                     framebuffer_has_depth && depth_enabled);
840   SetupExpectationsForEnableDisable(GL_STENCIL_TEST,
841                                     framebuffer_has_stencil && stencil_enabled);
842 }
843
844 void GLES2DecoderTestBase::SetupExpectationsForApplyingDefaultDirtyState() {
845   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
846                                          false,   // Framebuffer has depth
847                                          false,   // Framebuffer has stencil
848                                          0x1111,  // color bits
849                                          true,    // depth mask
850                                          false,   // depth enabled
851                                          0,       // front stencil mask
852                                          0,       // back stencil mask
853                                          false);  // stencil enabled
854 }
855
856 GLES2DecoderTestBase::EnableFlags::EnableFlags()
857     : cached_blend(false),
858       cached_cull_face(false),
859       cached_depth_test(false),
860       cached_dither(true),
861       cached_polygon_offset_fill(false),
862       cached_sample_alpha_to_coverage(false),
863       cached_sample_coverage(false),
864       cached_scissor_test(false),
865       cached_stencil_test(false) {
866 }
867
868 void GLES2DecoderTestBase::DoBindFramebuffer(
869     GLenum target, GLuint client_id, GLuint service_id) {
870   EXPECT_CALL(*gl_, BindFramebufferEXT(target, service_id))
871       .Times(1)
872       .RetiresOnSaturation();
873   cmds::BindFramebuffer cmd;
874   cmd.Init(target, client_id);
875   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
876 }
877
878 bool GLES2DecoderTestBase::DoIsFramebuffer(GLuint client_id) {
879   return IsObjectHelper<cmds::IsFramebuffer, cmds::IsFramebuffer::Result>(
880       client_id);
881 }
882
883 void GLES2DecoderTestBase::DoDeleteFramebuffer(
884     GLuint client_id, GLuint service_id,
885     bool reset_draw, GLenum draw_target, GLuint draw_id,
886     bool reset_read, GLenum read_target, GLuint read_id) {
887   if (reset_draw) {
888     EXPECT_CALL(*gl_, BindFramebufferEXT(draw_target, draw_id))
889         .Times(1)
890         .RetiresOnSaturation();
891   }
892   if (reset_read) {
893     EXPECT_CALL(*gl_, BindFramebufferEXT(read_target, read_id))
894         .Times(1)
895         .RetiresOnSaturation();
896   }
897   EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, Pointee(service_id)))
898       .Times(1)
899       .RetiresOnSaturation();
900   GenHelper<cmds::DeleteFramebuffersImmediate>(client_id);
901 }
902
903 void GLES2DecoderTestBase::DoBindRenderbuffer(
904     GLenum target, GLuint client_id, GLuint service_id) {
905   service_renderbuffer_id_ = service_id;
906   service_renderbuffer_valid_ = true;
907   EXPECT_CALL(*gl_, BindRenderbufferEXT(target, service_id))
908       .Times(1)
909       .RetiresOnSaturation();
910   cmds::BindRenderbuffer cmd;
911   cmd.Init(target, client_id);
912   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
913 }
914
915 void GLES2DecoderTestBase::DoRenderbufferStorageMultisampleCHROMIUM(
916     GLenum target,
917     GLsizei samples,
918     GLenum internal_format,
919     GLenum gl_format,
920     GLsizei width,
921     GLsizei height) {
922   EXPECT_CALL(*gl_, GetError())
923       .WillOnce(Return(GL_NO_ERROR))
924       .RetiresOnSaturation();
925   EXPECT_CALL(*gl_,
926               RenderbufferStorageMultisampleEXT(
927                   target, samples, gl_format, width, height))
928       .Times(1)
929       .RetiresOnSaturation();
930   EXPECT_CALL(*gl_, GetError())
931       .WillOnce(Return(GL_NO_ERROR))
932       .RetiresOnSaturation();
933   cmds::RenderbufferStorageMultisampleCHROMIUM cmd;
934   cmd.Init(target, samples, internal_format, width, height);
935   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
936   EXPECT_EQ(GL_NO_ERROR, GetGLError());
937 }
938
939 void GLES2DecoderTestBase::RestoreRenderbufferBindings() {
940   GetDecoder()->RestoreRenderbufferBindings();
941   service_renderbuffer_valid_ = false;
942 }
943
944 void GLES2DecoderTestBase::EnsureRenderbufferBound(bool expect_bind) {
945   EXPECT_NE(expect_bind, service_renderbuffer_valid_);
946
947   if (expect_bind) {
948     service_renderbuffer_valid_ = true;
949     EXPECT_CALL(*gl_,
950                 BindRenderbufferEXT(GL_RENDERBUFFER, service_renderbuffer_id_))
951         .Times(1)
952         .RetiresOnSaturation();
953   } else {
954     EXPECT_CALL(*gl_, BindRenderbufferEXT(_, _)).Times(0);
955   }
956 }
957
958 bool GLES2DecoderTestBase::DoIsRenderbuffer(GLuint client_id) {
959   return IsObjectHelper<cmds::IsRenderbuffer, cmds::IsRenderbuffer::Result>(
960       client_id);
961 }
962
963 void GLES2DecoderTestBase::DoDeleteRenderbuffer(
964     GLuint client_id, GLuint service_id) {
965   EXPECT_CALL(*gl_, DeleteRenderbuffersEXT(1, Pointee(service_id)))
966       .Times(1)
967       .RetiresOnSaturation();
968   GenHelper<cmds::DeleteRenderbuffersImmediate>(client_id);
969 }
970
971 void GLES2DecoderTestBase::DoBindTexture(
972     GLenum target, GLuint client_id, GLuint service_id) {
973   EXPECT_CALL(*gl_, BindTexture(target, service_id))
974       .Times(1)
975       .RetiresOnSaturation();
976   cmds::BindTexture cmd;
977   cmd.Init(target, client_id);
978   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
979 }
980
981 bool GLES2DecoderTestBase::DoIsTexture(GLuint client_id) {
982   return IsObjectHelper<cmds::IsTexture, cmds::IsTexture::Result>(client_id);
983 }
984
985 void GLES2DecoderTestBase::DoDeleteTexture(
986     GLuint client_id, GLuint service_id) {
987   EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(service_id)))
988       .Times(1)
989       .RetiresOnSaturation();
990   GenHelper<cmds::DeleteTexturesImmediate>(client_id);
991 }
992
993 void GLES2DecoderTestBase::DoTexImage2D(
994     GLenum target, GLint level, GLenum internal_format,
995     GLsizei width, GLsizei height, GLint border,
996     GLenum format, GLenum type,
997     uint32 shared_memory_id, uint32 shared_memory_offset) {
998   EXPECT_CALL(*gl_, GetError())
999       .WillOnce(Return(GL_NO_ERROR))
1000       .RetiresOnSaturation();
1001   EXPECT_CALL(*gl_, TexImage2D(target, level, internal_format,
1002                                width, height, border, format, type, _))
1003       .Times(1)
1004       .RetiresOnSaturation();
1005   EXPECT_CALL(*gl_, GetError())
1006       .WillOnce(Return(GL_NO_ERROR))
1007       .RetiresOnSaturation();
1008   cmds::TexImage2D cmd;
1009   cmd.Init(target, level, internal_format, width, height, format,
1010            type, shared_memory_id, shared_memory_offset);
1011   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1012 }
1013
1014 void GLES2DecoderTestBase::DoTexImage2DConvertInternalFormat(
1015     GLenum target, GLint level, GLenum requested_internal_format,
1016     GLsizei width, GLsizei height, GLint border,
1017     GLenum format, GLenum type,
1018     uint32 shared_memory_id, uint32 shared_memory_offset,
1019     GLenum expected_internal_format) {
1020   EXPECT_CALL(*gl_, GetError())
1021       .WillOnce(Return(GL_NO_ERROR))
1022       .RetiresOnSaturation();
1023   EXPECT_CALL(*gl_, TexImage2D(target, level, expected_internal_format,
1024                                width, height, border, format, type, _))
1025       .Times(1)
1026       .RetiresOnSaturation();
1027   EXPECT_CALL(*gl_, GetError())
1028       .WillOnce(Return(GL_NO_ERROR))
1029       .RetiresOnSaturation();
1030   cmds::TexImage2D cmd;
1031   cmd.Init(target, level, requested_internal_format, width, height,
1032            format, type, shared_memory_id, shared_memory_offset);
1033   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1034 }
1035
1036 void GLES2DecoderTestBase::DoCompressedTexImage2D(
1037     GLenum target, GLint level, GLenum format,
1038     GLsizei width, GLsizei height, GLint border,
1039     GLsizei size, uint32 bucket_id) {
1040   EXPECT_CALL(*gl_, GetError())
1041       .WillOnce(Return(GL_NO_ERROR))
1042       .RetiresOnSaturation();
1043   EXPECT_CALL(*gl_, CompressedTexImage2D(
1044       target, level, format, width, height, border, size, _))
1045       .Times(1)
1046       .RetiresOnSaturation();
1047   EXPECT_CALL(*gl_, GetError())
1048       .WillOnce(Return(GL_NO_ERROR))
1049       .RetiresOnSaturation();
1050   CommonDecoder::Bucket* bucket = decoder_->CreateBucket(bucket_id);
1051   bucket->SetSize(size);
1052   cmds::CompressedTexImage2DBucket cmd;
1053   cmd.Init(
1054       target, level, format, width, height,
1055       bucket_id);
1056   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1057 }
1058
1059 void GLES2DecoderTestBase::DoRenderbufferStorage(
1060     GLenum target, GLenum internal_format, GLenum actual_format,
1061     GLsizei width, GLsizei height,  GLenum error) {
1062   EXPECT_CALL(*gl_, GetError())
1063       .WillOnce(Return(GL_NO_ERROR))
1064       .RetiresOnSaturation();
1065   EXPECT_CALL(*gl_, RenderbufferStorageEXT(
1066       target, actual_format, width, height))
1067       .Times(1)
1068       .RetiresOnSaturation();
1069   EXPECT_CALL(*gl_, GetError())
1070       .WillOnce(Return(error))
1071       .RetiresOnSaturation();
1072   cmds::RenderbufferStorage cmd;
1073   cmd.Init(target, internal_format, width, height);
1074   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1075 }
1076
1077 void GLES2DecoderTestBase::DoFramebufferTexture2D(
1078     GLenum target, GLenum attachment, GLenum textarget,
1079     GLuint texture_client_id, GLuint texture_service_id, GLint level,
1080     GLenum error) {
1081   EXPECT_CALL(*gl_, GetError())
1082       .WillOnce(Return(GL_NO_ERROR))
1083       .RetiresOnSaturation();
1084   EXPECT_CALL(*gl_, FramebufferTexture2DEXT(
1085       target, attachment, textarget, texture_service_id, level))
1086       .Times(1)
1087       .RetiresOnSaturation();
1088   EXPECT_CALL(*gl_, GetError())
1089       .WillOnce(Return(error))
1090       .RetiresOnSaturation();
1091   cmds::FramebufferTexture2D cmd;
1092   cmd.Init(target, attachment, textarget, texture_client_id);
1093   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1094 }
1095
1096 void GLES2DecoderTestBase::DoFramebufferRenderbuffer(
1097     GLenum target,
1098     GLenum attachment,
1099     GLenum renderbuffer_target,
1100     GLuint renderbuffer_client_id,
1101     GLuint renderbuffer_service_id,
1102     GLenum error) {
1103   EXPECT_CALL(*gl_, GetError())
1104       .WillOnce(Return(GL_NO_ERROR))
1105       .RetiresOnSaturation();
1106   EXPECT_CALL(*gl_, FramebufferRenderbufferEXT(
1107       target, attachment, renderbuffer_target, renderbuffer_service_id))
1108       .Times(1)
1109       .RetiresOnSaturation();
1110   EXPECT_CALL(*gl_, GetError())
1111       .WillOnce(Return(error))
1112       .RetiresOnSaturation();
1113   cmds::FramebufferRenderbuffer cmd;
1114   cmd.Init(target, attachment, renderbuffer_target, renderbuffer_client_id);
1115   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1116 }
1117
1118 void GLES2DecoderTestBase::DoVertexAttribPointer(
1119     GLuint index, GLint size, GLenum type, GLsizei stride, GLuint offset) {
1120   EXPECT_CALL(*gl_,
1121               VertexAttribPointer(index, size, type, GL_FALSE, stride,
1122                                   BufferOffset(offset)))
1123       .Times(1)
1124       .RetiresOnSaturation();
1125   cmds::VertexAttribPointer cmd;
1126   cmd.Init(index, size, GL_FLOAT, GL_FALSE, stride, offset);
1127   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1128 }
1129
1130 void GLES2DecoderTestBase::DoVertexAttribDivisorANGLE(
1131     GLuint index, GLuint divisor) {
1132   EXPECT_CALL(*gl_,
1133               VertexAttribDivisorANGLE(index, divisor))
1134       .Times(1)
1135       .RetiresOnSaturation();
1136   cmds::VertexAttribDivisorANGLE cmd;
1137   cmd.Init(index, divisor);
1138   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1139 }
1140
1141 void GLES2DecoderTestBase::AddExpectationsForGenVertexArraysOES(){
1142   if (group_->feature_info()->feature_flags().native_vertex_array_object) {
1143       EXPECT_CALL(*gl_, GenVertexArraysOES(1, _))
1144           .WillOnce(SetArgumentPointee<1>(kServiceVertexArrayId))
1145           .RetiresOnSaturation();
1146   }
1147 }
1148
1149 void GLES2DecoderTestBase::AddExpectationsForDeleteVertexArraysOES(){
1150   if (group_->feature_info()->feature_flags().native_vertex_array_object) {
1151       EXPECT_CALL(*gl_, DeleteVertexArraysOES(1, _))
1152           .Times(1)
1153           .RetiresOnSaturation();
1154   }
1155 }
1156
1157 void GLES2DecoderTestBase::AddExpectationsForDeleteBoundVertexArraysOES() {
1158   // Expectations are the same as a delete, followed by binding VAO 0.
1159   AddExpectationsForDeleteVertexArraysOES();
1160   AddExpectationsForBindVertexArrayOES();
1161 }
1162
1163 void GLES2DecoderTestBase::AddExpectationsForBindVertexArrayOES() {
1164   if (group_->feature_info()->feature_flags().native_vertex_array_object) {
1165     EXPECT_CALL(*gl_, BindVertexArrayOES(_))
1166       .Times(1)
1167       .RetiresOnSaturation();
1168   } else {
1169     for (uint32 vv = 0; vv < group_->max_vertex_attribs(); ++vv) {
1170       AddExpectationsForRestoreAttribState(vv);
1171     }
1172
1173     EXPECT_CALL(*gl_, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, _))
1174       .Times(1)
1175       .RetiresOnSaturation();
1176   }
1177 }
1178
1179 void GLES2DecoderTestBase::AddExpectationsForRestoreAttribState(GLuint attrib) {
1180   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, _))
1181       .Times(1)
1182       .RetiresOnSaturation();
1183
1184   EXPECT_CALL(*gl_, VertexAttribPointer(attrib, _, _, _, _, _))
1185       .Times(1)
1186       .RetiresOnSaturation();
1187
1188   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(attrib, _))
1189         .Times(testing::AtMost(1))
1190         .RetiresOnSaturation();
1191
1192   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, _))
1193       .Times(1)
1194       .RetiresOnSaturation();
1195
1196   if (attrib != 0 ||
1197       gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
1198
1199       // TODO(bajones): Not sure if I can tell which of these will be called
1200       EXPECT_CALL(*gl_, EnableVertexAttribArray(attrib))
1201           .Times(testing::AtMost(1))
1202           .RetiresOnSaturation();
1203
1204       EXPECT_CALL(*gl_, DisableVertexAttribArray(attrib))
1205           .Times(testing::AtMost(1))
1206           .RetiresOnSaturation();
1207   }
1208 }
1209
1210 // GCC requires these declarations, but MSVC requires they not be present
1211 #ifndef COMPILER_MSVC
1212 const int GLES2DecoderTestBase::kBackBufferWidth;
1213 const int GLES2DecoderTestBase::kBackBufferHeight;
1214
1215 const GLint GLES2DecoderTestBase::kMaxTextureSize;
1216 const GLint GLES2DecoderTestBase::kMaxCubeMapTextureSize;
1217 const GLint GLES2DecoderTestBase::kNumVertexAttribs;
1218 const GLint GLES2DecoderTestBase::kNumTextureUnits;
1219 const GLint GLES2DecoderTestBase::kMaxTextureImageUnits;
1220 const GLint GLES2DecoderTestBase::kMaxVertexTextureImageUnits;
1221 const GLint GLES2DecoderTestBase::kMaxFragmentUniformVectors;
1222 const GLint GLES2DecoderTestBase::kMaxVaryingVectors;
1223 const GLint GLES2DecoderTestBase::kMaxVertexUniformVectors;
1224 const GLint GLES2DecoderTestBase::kMaxViewportWidth;
1225 const GLint GLES2DecoderTestBase::kMaxViewportHeight;
1226
1227 const GLint GLES2DecoderTestBase::kViewportX;
1228 const GLint GLES2DecoderTestBase::kViewportY;
1229 const GLint GLES2DecoderTestBase::kViewportWidth;
1230 const GLint GLES2DecoderTestBase::kViewportHeight;
1231
1232 const GLuint GLES2DecoderTestBase::kServiceAttrib0BufferId;
1233 const GLuint GLES2DecoderTestBase::kServiceFixedAttribBufferId;
1234
1235 const GLuint GLES2DecoderTestBase::kServiceBufferId;
1236 const GLuint GLES2DecoderTestBase::kServiceFramebufferId;
1237 const GLuint GLES2DecoderTestBase::kServiceRenderbufferId;
1238 const GLuint GLES2DecoderTestBase::kServiceTextureId;
1239 const GLuint GLES2DecoderTestBase::kServiceProgramId;
1240 const GLuint GLES2DecoderTestBase::kServiceShaderId;
1241 const GLuint GLES2DecoderTestBase::kServiceElementBufferId;
1242 const GLuint GLES2DecoderTestBase::kServiceQueryId;
1243 const GLuint GLES2DecoderTestBase::kServiceVertexArrayId;
1244
1245 const int32 GLES2DecoderTestBase::kSharedMemoryId;
1246 const size_t GLES2DecoderTestBase::kSharedBufferSize;
1247 const uint32 GLES2DecoderTestBase::kSharedMemoryOffset;
1248 const int32 GLES2DecoderTestBase::kInvalidSharedMemoryId;
1249 const uint32 GLES2DecoderTestBase::kInvalidSharedMemoryOffset;
1250 const uint32 GLES2DecoderTestBase::kInitialResult;
1251 const uint8 GLES2DecoderTestBase::kInitialMemoryValue;
1252
1253 const uint32 GLES2DecoderTestBase::kNewClientId;
1254 const uint32 GLES2DecoderTestBase::kNewServiceId;
1255 const uint32 GLES2DecoderTestBase::kInvalidClientId;
1256
1257 const GLuint GLES2DecoderTestBase::kServiceVertexShaderId;
1258 const GLuint GLES2DecoderTestBase::kServiceFragmentShaderId;
1259
1260 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumShaderId;
1261 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumProgramId;
1262
1263 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumTextureBufferId;
1264 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumVertexBufferId;
1265 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumFBOId;
1266 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumPositionAttrib;
1267 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumTexAttrib;
1268 const GLuint GLES2DecoderTestBase::kServiceCopyTextureChromiumSamplerLocation;
1269
1270 const GLsizei GLES2DecoderTestBase::kNumVertices;
1271 const GLsizei GLES2DecoderTestBase::kNumIndices;
1272 const int GLES2DecoderTestBase::kValidIndexRangeStart;
1273 const int GLES2DecoderTestBase::kValidIndexRangeCount;
1274 const int GLES2DecoderTestBase::kInvalidIndexRangeStart;
1275 const int GLES2DecoderTestBase::kInvalidIndexRangeCount;
1276 const int GLES2DecoderTestBase::kOutOfRangeIndexRangeEnd;
1277 const GLuint GLES2DecoderTestBase::kMaxValidIndex;
1278
1279 const GLint GLES2DecoderTestBase::kMaxAttribLength;
1280 const GLint GLES2DecoderTestBase::kAttrib1Size;
1281 const GLint GLES2DecoderTestBase::kAttrib2Size;
1282 const GLint GLES2DecoderTestBase::kAttrib3Size;
1283 const GLint GLES2DecoderTestBase::kAttrib1Location;
1284 const GLint GLES2DecoderTestBase::kAttrib2Location;
1285 const GLint GLES2DecoderTestBase::kAttrib3Location;
1286 const GLenum GLES2DecoderTestBase::kAttrib1Type;
1287 const GLenum GLES2DecoderTestBase::kAttrib2Type;
1288 const GLenum GLES2DecoderTestBase::kAttrib3Type;
1289 const GLint GLES2DecoderTestBase::kInvalidAttribLocation;
1290 const GLint GLES2DecoderTestBase::kBadAttribIndex;
1291
1292 const GLint GLES2DecoderTestBase::kMaxUniformLength;
1293 const GLint GLES2DecoderTestBase::kUniform1Size;
1294 const GLint GLES2DecoderTestBase::kUniform2Size;
1295 const GLint GLES2DecoderTestBase::kUniform3Size;
1296 const GLint GLES2DecoderTestBase::kUniform1RealLocation;
1297 const GLint GLES2DecoderTestBase::kUniform2RealLocation;
1298 const GLint GLES2DecoderTestBase::kUniform2ElementRealLocation;
1299 const GLint GLES2DecoderTestBase::kUniform3RealLocation;
1300 const GLint GLES2DecoderTestBase::kUniform1FakeLocation;
1301 const GLint GLES2DecoderTestBase::kUniform2FakeLocation;
1302 const GLint GLES2DecoderTestBase::kUniform2ElementFakeLocation;
1303 const GLint GLES2DecoderTestBase::kUniform3FakeLocation;
1304 const GLint GLES2DecoderTestBase::kUniform1DesiredLocation;
1305 const GLint GLES2DecoderTestBase::kUniform2DesiredLocation;
1306 const GLint GLES2DecoderTestBase::kUniform3DesiredLocation;
1307 const GLenum GLES2DecoderTestBase::kUniform1Type;
1308 const GLenum GLES2DecoderTestBase::kUniform2Type;
1309 const GLenum GLES2DecoderTestBase::kUniform3Type;
1310 const GLenum GLES2DecoderTestBase::kUniformCubemapType;
1311 const GLint GLES2DecoderTestBase::kInvalidUniformLocation;
1312 const GLint GLES2DecoderTestBase::kBadUniformIndex;
1313
1314 #endif
1315
1316 const char* GLES2DecoderTestBase::kAttrib1Name = "attrib1";
1317 const char* GLES2DecoderTestBase::kAttrib2Name = "attrib2";
1318 const char* GLES2DecoderTestBase::kAttrib3Name = "attrib3";
1319 const char* GLES2DecoderTestBase::kUniform1Name = "uniform1";
1320 const char* GLES2DecoderTestBase::kUniform2Name = "uniform2[0]";
1321 const char* GLES2DecoderTestBase::kUniform3Name = "uniform3[0]";
1322
1323 void GLES2DecoderTestBase::SetupDefaultProgram() {
1324   {
1325     static AttribInfo attribs[] = {
1326       { kAttrib1Name, kAttrib1Size, kAttrib1Type, kAttrib1Location, },
1327       { kAttrib2Name, kAttrib2Size, kAttrib2Type, kAttrib2Location, },
1328       { kAttrib3Name, kAttrib3Size, kAttrib3Type, kAttrib3Location, },
1329     };
1330     static UniformInfo uniforms[] = {
1331       { kUniform1Name, kUniform1Size, kUniform1Type,
1332         kUniform1FakeLocation, kUniform1RealLocation,
1333         kUniform1DesiredLocation },
1334       { kUniform2Name, kUniform2Size, kUniform2Type,
1335         kUniform2FakeLocation, kUniform2RealLocation,
1336         kUniform2DesiredLocation },
1337       { kUniform3Name, kUniform3Size, kUniform3Type,
1338         kUniform3FakeLocation, kUniform3RealLocation,
1339         kUniform3DesiredLocation },
1340     };
1341     SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
1342                 client_program_id_, kServiceProgramId,
1343                 client_vertex_shader_id_, kServiceVertexShaderId,
1344                 client_fragment_shader_id_, kServiceFragmentShaderId);
1345   }
1346
1347   {
1348     EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
1349         .Times(1)
1350         .RetiresOnSaturation();
1351     cmds::UseProgram cmd;
1352     cmd.Init(client_program_id_);
1353     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1354   }
1355 }
1356
1357 void GLES2DecoderTestBase::SetupCubemapProgram() {
1358   {
1359     static AttribInfo attribs[] = {
1360       { kAttrib1Name, kAttrib1Size, kAttrib1Type, kAttrib1Location, },
1361       { kAttrib2Name, kAttrib2Size, kAttrib2Type, kAttrib2Location, },
1362       { kAttrib3Name, kAttrib3Size, kAttrib3Type, kAttrib3Location, },
1363     };
1364     static UniformInfo uniforms[] = {
1365       { kUniform1Name, kUniform1Size, kUniformCubemapType,
1366         kUniform1FakeLocation, kUniform1RealLocation,
1367         kUniform1DesiredLocation, },
1368       { kUniform2Name, kUniform2Size, kUniform2Type,
1369         kUniform2FakeLocation, kUniform2RealLocation,
1370         kUniform2DesiredLocation, },
1371       { kUniform3Name, kUniform3Size, kUniform3Type,
1372         kUniform3FakeLocation, kUniform3RealLocation,
1373         kUniform3DesiredLocation, },
1374     };
1375     SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
1376                 client_program_id_, kServiceProgramId,
1377                 client_vertex_shader_id_, kServiceVertexShaderId,
1378                 client_fragment_shader_id_, kServiceFragmentShaderId);
1379   }
1380
1381   {
1382     EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
1383         .Times(1)
1384         .RetiresOnSaturation();
1385     cmds::UseProgram cmd;
1386     cmd.Init(client_program_id_);
1387     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1388   }
1389 }
1390
1391 void GLES2DecoderTestBase::SetupSamplerExternalProgram() {
1392   {
1393     static AttribInfo attribs[] = {
1394       { kAttrib1Name, kAttrib1Size, kAttrib1Type, kAttrib1Location, },
1395       { kAttrib2Name, kAttrib2Size, kAttrib2Type, kAttrib2Location, },
1396       { kAttrib3Name, kAttrib3Size, kAttrib3Type, kAttrib3Location, },
1397     };
1398     static UniformInfo uniforms[] = {
1399       { kUniform1Name, kUniform1Size, kUniformSamplerExternalType,
1400         kUniform1FakeLocation, kUniform1RealLocation,
1401         kUniform1DesiredLocation, },
1402       { kUniform2Name, kUniform2Size, kUniform2Type,
1403         kUniform2FakeLocation, kUniform2RealLocation,
1404         kUniform2DesiredLocation, },
1405       { kUniform3Name, kUniform3Size, kUniform3Type,
1406         kUniform3FakeLocation, kUniform3RealLocation,
1407         kUniform3DesiredLocation, },
1408     };
1409     SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
1410                 client_program_id_, kServiceProgramId,
1411                 client_vertex_shader_id_, kServiceVertexShaderId,
1412                 client_fragment_shader_id_, kServiceFragmentShaderId);
1413   }
1414
1415   {
1416     EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
1417         .Times(1)
1418         .RetiresOnSaturation();
1419     cmds::UseProgram cmd;
1420     cmd.Init(client_program_id_);
1421     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1422   }
1423 }
1424
1425 void GLES2DecoderWithShaderTestBase::TearDown() {
1426   GLES2DecoderTestBase::TearDown();
1427 }
1428
1429 void GLES2DecoderTestBase::SetupShader(
1430     GLES2DecoderTestBase::AttribInfo* attribs, size_t num_attribs,
1431     GLES2DecoderTestBase::UniformInfo* uniforms, size_t num_uniforms,
1432     GLuint program_client_id, GLuint program_service_id,
1433     GLuint vertex_shader_client_id, GLuint vertex_shader_service_id,
1434     GLuint fragment_shader_client_id, GLuint fragment_shader_service_id) {
1435   {
1436     InSequence s;
1437
1438     EXPECT_CALL(*gl_,
1439                 AttachShader(program_service_id, vertex_shader_service_id))
1440         .Times(1)
1441         .RetiresOnSaturation();
1442     EXPECT_CALL(*gl_,
1443                 AttachShader(program_service_id, fragment_shader_service_id))
1444         .Times(1)
1445         .RetiresOnSaturation();
1446     TestHelper::SetupShader(
1447         gl_.get(), attribs, num_attribs, uniforms, num_uniforms,
1448         program_service_id);
1449   }
1450
1451   DoCreateShader(
1452       GL_VERTEX_SHADER, vertex_shader_client_id, vertex_shader_service_id);
1453   DoCreateShader(
1454       GL_FRAGMENT_SHADER, fragment_shader_client_id,
1455       fragment_shader_service_id);
1456
1457   GetShader(vertex_shader_client_id)->SetStatus(true, "", NULL);
1458   GetShader(fragment_shader_client_id)->SetStatus(true, "", NULL);
1459
1460   cmds::AttachShader attach_cmd;
1461   attach_cmd.Init(program_client_id, vertex_shader_client_id);
1462   EXPECT_EQ(error::kNoError, ExecuteCmd(attach_cmd));
1463
1464   attach_cmd.Init(program_client_id, fragment_shader_client_id);
1465   EXPECT_EQ(error::kNoError, ExecuteCmd(attach_cmd));
1466
1467   cmds::LinkProgram link_cmd;
1468   link_cmd.Init(program_client_id);
1469
1470   EXPECT_EQ(error::kNoError, ExecuteCmd(link_cmd));
1471 }
1472
1473 void GLES2DecoderTestBase::DoEnableDisable(GLenum cap, bool enable) {
1474   SetupExpectationsForEnableDisable(cap, enable);
1475   if (enable) {
1476     cmds::Enable cmd;
1477     cmd.Init(cap);
1478     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1479   } else {
1480     cmds::Disable cmd;
1481     cmd.Init(cap);
1482     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1483   }
1484 }
1485
1486 void GLES2DecoderTestBase::DoEnableVertexAttribArray(GLint index) {
1487   EXPECT_CALL(*gl_, EnableVertexAttribArray(index))
1488       .Times(1)
1489       .RetiresOnSaturation();
1490   cmds::EnableVertexAttribArray cmd;
1491   cmd.Init(index);
1492   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1493 }
1494
1495 void GLES2DecoderTestBase::DoBufferData(GLenum target, GLsizei size) {
1496   EXPECT_CALL(*gl_, GetError())
1497       .WillOnce(Return(GL_NO_ERROR))
1498       .RetiresOnSaturation();
1499   EXPECT_CALL(*gl_, BufferData(target, size, _, GL_STREAM_DRAW))
1500       .Times(1)
1501       .RetiresOnSaturation();
1502   EXPECT_CALL(*gl_, GetError())
1503       .WillOnce(Return(GL_NO_ERROR))
1504       .RetiresOnSaturation();
1505   cmds::BufferData cmd;
1506   cmd.Init(target, size, 0, 0, GL_STREAM_DRAW);
1507   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1508 }
1509
1510 void GLES2DecoderTestBase::DoBufferSubData(
1511     GLenum target, GLint offset, GLsizei size, const void* data) {
1512   EXPECT_CALL(*gl_, BufferSubData(target, offset, size,
1513                                   shared_memory_address_))
1514       .Times(1)
1515       .RetiresOnSaturation();
1516   memcpy(shared_memory_address_, data, size);
1517   cmds::BufferSubData cmd;
1518   cmd.Init(target, offset, size, shared_memory_id_, shared_memory_offset_);
1519   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
1520 }
1521
1522 void GLES2DecoderTestBase::SetupVertexBuffer() {
1523   DoEnableVertexAttribArray(1);
1524   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
1525   GLfloat f = 0;
1526   DoBufferData(GL_ARRAY_BUFFER, kNumVertices * 2 * sizeof(f));
1527 }
1528
1529 void GLES2DecoderTestBase::SetupAllNeededVertexBuffers() {
1530   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
1531   DoBufferData(GL_ARRAY_BUFFER, kNumVertices * 16 * sizeof(float));
1532   DoEnableVertexAttribArray(0);
1533   DoEnableVertexAttribArray(1);
1534   DoEnableVertexAttribArray(2);
1535   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
1536   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
1537   DoVertexAttribPointer(2, 2, GL_FLOAT, 0, 0);
1538 }
1539
1540 void GLES2DecoderTestBase::SetupIndexBuffer() {
1541   DoBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
1542                client_element_buffer_id_,
1543                kServiceElementBufferId);
1544   static const GLshort indices[] = {100, 1, 2, 3, 4, 5, 6, 7, 100, 9};
1545   COMPILE_ASSERT(arraysize(indices) == kNumIndices, Indices_is_not_10);
1546   DoBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices));
1547   DoBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 2, indices);
1548   DoBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2, sizeof(indices) - 2, &indices[1]);
1549 }
1550
1551 void GLES2DecoderTestBase::SetupTexture() {
1552   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
1553   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1554                kSharedMemoryId, kSharedMemoryOffset);
1555 };
1556
1557 void GLES2DecoderTestBase::DeleteVertexBuffer() {
1558   DoDeleteBuffer(client_buffer_id_, kServiceBufferId);
1559 }
1560
1561 void GLES2DecoderTestBase::DeleteIndexBuffer() {
1562   DoDeleteBuffer(client_element_buffer_id_, kServiceElementBufferId);
1563 }
1564
1565 void GLES2DecoderTestBase::AddExpectationsForSimulatedAttrib0WithError(
1566     GLsizei num_vertices, GLuint buffer_id, GLenum error) {
1567   if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
1568     return;
1569   }
1570
1571   EXPECT_CALL(*gl_, GetError())
1572       .WillOnce(Return(GL_NO_ERROR))
1573       .WillOnce(Return(error))
1574       .RetiresOnSaturation();
1575   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, kServiceAttrib0BufferId))
1576       .Times(1)
1577       .RetiresOnSaturation();
1578   EXPECT_CALL(*gl_, BufferData(GL_ARRAY_BUFFER,
1579                                num_vertices * sizeof(GLfloat) * 4,
1580                                _, GL_DYNAMIC_DRAW))
1581       .Times(1)
1582       .RetiresOnSaturation();
1583   if (error == GL_NO_ERROR) {
1584     EXPECT_CALL(*gl_, BufferSubData(
1585         GL_ARRAY_BUFFER, 0, num_vertices * sizeof(GLfloat) * 4, _))
1586         .Times(1)
1587         .RetiresOnSaturation();
1588     EXPECT_CALL(*gl_, VertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL))
1589         .Times(1)
1590         .RetiresOnSaturation();
1591     EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, buffer_id))
1592         .Times(1)
1593         .RetiresOnSaturation();
1594   }
1595 }
1596
1597 void GLES2DecoderTestBase::AddExpectationsForSimulatedAttrib0(
1598     GLsizei num_vertices, GLuint buffer_id) {
1599   AddExpectationsForSimulatedAttrib0WithError(
1600       num_vertices, buffer_id, GL_NO_ERROR);
1601 }
1602
1603 void GLES2DecoderTestBase::SetupMockGLBehaviors() {
1604   ON_CALL(*gl_, BindVertexArrayOES(_))
1605       .WillByDefault(Invoke(
1606           &gl_states_,
1607           &GLES2DecoderTestBase::MockGLStates::OnBindVertexArrayOES));
1608   ON_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, _))
1609       .WillByDefault(WithArg<1>(Invoke(
1610           &gl_states_,
1611           &GLES2DecoderTestBase::MockGLStates::OnBindArrayBuffer)));
1612   ON_CALL(*gl_, VertexAttribPointer(_, _, _, _, _, NULL))
1613       .WillByDefault(InvokeWithoutArgs(
1614           &gl_states_,
1615           &GLES2DecoderTestBase::MockGLStates::OnVertexAttribNullPointer));
1616 }
1617
1618 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::
1619 MockCommandBufferEngine() {
1620
1621   scoped_ptr<base::SharedMemory> shm(new base::SharedMemory());
1622   shm->CreateAndMapAnonymous(kSharedBufferSize);
1623   valid_buffer_ = MakeBufferFromSharedMemory(shm.Pass(), kSharedBufferSize);
1624
1625   ClearSharedMemory();
1626 }
1627
1628 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::
1629 ~MockCommandBufferEngine() {}
1630
1631 scoped_refptr<gpu::Buffer>
1632 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::GetSharedMemoryBuffer(
1633     int32 shm_id) {
1634   return shm_id == kSharedMemoryId ? valid_buffer_ : invalid_buffer_;
1635 }
1636
1637 void GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::set_token(
1638     int32 token) {
1639   DCHECK(false);
1640 }
1641
1642 bool GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::SetGetBuffer(
1643     int32 /* transfer_buffer_id */) {
1644   DCHECK(false);
1645   return false;
1646 }
1647
1648 bool GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::SetGetOffset(
1649    int32 offset) {
1650   DCHECK(false);
1651   return false;
1652 }
1653
1654 int32 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::GetGetOffset() {
1655   DCHECK(false);
1656   return 0;
1657 }
1658
1659 void GLES2DecoderWithShaderTestBase::SetUp() {
1660   GLES2DecoderTestBase::SetUp();
1661   SetupDefaultProgram();
1662 }
1663
1664 // Include the auto-generated part of this file. We split this because it means
1665 // we can easily edit the non-auto generated parts right here in this file
1666 // instead of having to edit some template or the code generator.
1667 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_0_autogen.h"
1668
1669 }  // namespace gles2
1670 }  // namespace gpu