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