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