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