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