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