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   InitDecoderWithCommandLine(extensions,
     92                              has_alpha,
     93                              has_depth,
     94                              has_stencil,
     95                              request_alpha,
     96                              request_depth,
     97                              request_stencil,
     98                              bind_generates_resource,
     99                              NULL);
    100 }
    101 
    102 void GLES2DecoderTestBase::InitDecoderWithCommandLine(
    103     const char* extensions,
    104     bool has_alpha,
    105     bool has_depth,
    106     bool has_stencil,
    107     bool request_alpha,
    108     bool request_depth,
    109     bool request_stencil,
    110     bool bind_generates_resource,
    111     const CommandLine* command_line) {
    112   Framebuffer::ClearFramebufferCompleteComboMap();
    113   gl_.reset(new StrictMock<MockGLInterface>());
    114   ::gfx::GLInterface::SetGLInterface(gl_.get());
    115 
    116   // Only create stream texture manager if extension is requested.
    117   std::vector<std::string> list;
    118   base::SplitString(std::string(extensions), ' ', &list);
    119   if (std::find(list.begin(), list.end(),
    120                 "GL_CHROMIUM_stream_texture") != list.end())
    121       stream_texture_manager_.reset(new StrictMock<MockStreamTextureManager>);
    122   scoped_refptr<FeatureInfo> feature_info;
    123   if (command_line)
    124     feature_info = new FeatureInfo(*command_line);
    125   group_ = scoped_refptr<ContextGroup>(new ContextGroup(
    126       NULL,
    127       NULL,
    128       memory_tracker_,
    129       stream_texture_manager_.get(),
    130       feature_info.get(),
    131       bind_generates_resource));
    132   // These two workarounds are always turned on.
    133   group_->feature_info(
    134       )->workarounds_.set_texture_filter_before_generating_mipmap = true;
    135   group_->feature_info()->workarounds_.clear_alpha_in_readpixels = true;
    136 
    137   InSequence sequence;
    138 
    139   TestHelper::SetupContextGroupInitExpectations(gl_.get(),
    140       DisallowedFeatures(), extensions);
    141 
    142   // We initialize the ContextGroup with a MockGLES2Decoder so that
    143   // we can use the ContextGroup to figure out how the real GLES2Decoder
    144   // will initialize itself.
    145   mock_decoder_.reset(new MockGLES2Decoder());
    146   EXPECT_TRUE(
    147       group_->Initialize(mock_decoder_.get(), DisallowedFeatures()));
    148 
    149   AddExpectationsForVertexAttribManager();
    150 
    151   AddExpectationsForBindVertexArrayOES();
    152 
    153   EXPECT_CALL(*gl_, EnableVertexAttribArray(0))
    154       .Times(1)
    155       .RetiresOnSaturation();
    156   static GLuint attrib_0_id[] = {
    157     kServiceAttrib0BufferId,
    158   };
    159   static GLuint fixed_attrib_buffer_id[] = {
    160     kServiceFixedAttribBufferId,
    161   };
    162   EXPECT_CALL(*gl_, GenBuffersARB(arraysize(attrib_0_id), _))
    163       .WillOnce(SetArrayArgument<1>(attrib_0_id,
    164                                     attrib_0_id + arraysize(attrib_0_id)))
    165       .RetiresOnSaturation();
    166   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, kServiceAttrib0BufferId))
    167       .Times(1)
    168       .RetiresOnSaturation();
    169   EXPECT_CALL(*gl_, VertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL))
    170       .Times(1)
    171       .RetiresOnSaturation();
    172   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, 0))
    173       .Times(1)
    174       .RetiresOnSaturation();
    175   EXPECT_CALL(*gl_, GenBuffersARB(arraysize(fixed_attrib_buffer_id), _))
    176       .WillOnce(SetArrayArgument<1>(
    177           fixed_attrib_buffer_id,
    178           fixed_attrib_buffer_id + arraysize(fixed_attrib_buffer_id)))
    179       .RetiresOnSaturation();
    180 
    181   for (GLint tt = 0; tt < TestHelper::kNumTextureUnits; ++tt) {
    182     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0 + tt))
    183         .Times(1)
    184         .RetiresOnSaturation();
    185     if (group_->feature_info()->feature_flags().oes_egl_image_external) {
    186       EXPECT_CALL(*gl_, BindTexture(
    187               GL_TEXTURE_EXTERNAL_OES,
    188               TestHelper::kServiceDefaultExternalTextureId))
    189           .Times(1)
    190           .RetiresOnSaturation();
    191     }
    192     if (group_->feature_info()->feature_flags().arb_texture_rectangle) {
    193       EXPECT_CALL(*gl_, BindTexture(
    194               GL_TEXTURE_RECTANGLE_ARB,
    195               TestHelper::kServiceDefaultRectangleTextureId))
    196           .Times(1)
    197           .RetiresOnSaturation();
    198     }
    199     EXPECT_CALL(*gl_, BindTexture(
    200         GL_TEXTURE_CUBE_MAP, TestHelper::kServiceDefaultTextureCubemapId))
    201         .Times(1)
    202         .RetiresOnSaturation();
    203     EXPECT_CALL(*gl_, BindTexture(
    204         GL_TEXTURE_2D, TestHelper::kServiceDefaultTexture2dId))
    205         .Times(1)
    206         .RetiresOnSaturation();
    207   }
    208   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    209       .Times(1)
    210       .RetiresOnSaturation();
    211 
    212   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER, 0))
    213       .Times(1)
    214       .RetiresOnSaturation();
    215   EXPECT_CALL(*gl_, GetIntegerv(GL_ALPHA_BITS, _))
    216        .WillOnce(SetArgumentPointee<1>(has_alpha ? 8 : 0))
    217        .RetiresOnSaturation();
    218   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_BITS, _))
    219        .WillOnce(SetArgumentPointee<1>(has_depth ? 24 : 0))
    220        .RetiresOnSaturation();
    221   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_BITS, _))
    222        .WillOnce(SetArgumentPointee<1>(has_stencil ? 8 : 0))
    223        .RetiresOnSaturation();
    224 
    225   EXPECT_CALL(*gl_, Enable(GL_VERTEX_PROGRAM_POINT_SIZE))
    226       .Times(1)
    227       .RetiresOnSaturation();
    228 
    229   EXPECT_CALL(*gl_, Enable(GL_POINT_SPRITE))
    230       .Times(1)
    231       .RetiresOnSaturation();
    232 
    233   static GLint max_viewport_dims[] = {
    234     kMaxViewportWidth,
    235     kMaxViewportHeight
    236   };
    237   EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_VIEWPORT_DIMS, _))
    238       .WillOnce(SetArrayArgument<1>(
    239           max_viewport_dims, max_viewport_dims + arraysize(max_viewport_dims)))
    240       .RetiresOnSaturation();
    241 
    242   SetupInitCapabilitiesExpectations();
    243   SetupInitStateExpectations();
    244 
    245   EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    246       .Times(1)
    247       .RetiresOnSaturation();
    248 
    249   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, 0))
    250       .Times(1)
    251       .RetiresOnSaturation();
    252   EXPECT_CALL(*gl_, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
    253       .Times(1)
    254       .RetiresOnSaturation();
    255   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_FRAMEBUFFER, 0))
    256       .Times(1)
    257       .RetiresOnSaturation();
    258   EXPECT_CALL(*gl_, BindRenderbufferEXT(GL_RENDERBUFFER, 0))
    259       .Times(1)
    260       .RetiresOnSaturation();
    261 
    262   // TODO(boliu): Remove OS_ANDROID once crbug.com/259023 is fixed and the
    263   // workaround has been reverted.
    264 #if !defined(OS_ANDROID)
    265   EXPECT_CALL(*gl_, Clear(
    266       GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
    267       .Times(1)
    268       .RetiresOnSaturation();
    269 #endif
    270 
    271   engine_.reset(new StrictMock<MockCommandBufferEngine>());
    272   gpu::Buffer buffer = engine_->GetSharedMemoryBuffer(kSharedMemoryId);
    273   shared_memory_offset_ = kSharedMemoryOffset;
    274   shared_memory_address_ = reinterpret_cast<int8*>(buffer.ptr) +
    275       shared_memory_offset_;
    276   shared_memory_id_ = kSharedMemoryId;
    277   shared_memory_base_ = buffer.ptr;
    278 
    279   surface_ = new gfx::GLSurfaceStub;
    280   surface_->SetSize(gfx::Size(kBackBufferWidth, kBackBufferHeight));
    281 
    282   context_ = new gfx::GLContextStub;
    283 
    284   context_->MakeCurrent(surface_.get());
    285 
    286   int32 attributes[] = {
    287     EGL_ALPHA_SIZE, request_alpha ? 8 : 0,
    288     EGL_DEPTH_SIZE, request_depth ? 24 : 0,
    289     EGL_STENCIL_SIZE, request_stencil ? 8 : 0,
    290   };
    291   std::vector<int32> attribs(attributes, attributes + arraysize(attributes));
    292 
    293   decoder_.reset(GLES2Decoder::Create(group_.get()));
    294   decoder_->GetLogger()->set_log_synthesized_gl_errors(false);
    295   decoder_->Initialize(surface_,
    296                        context_,
    297                        false,
    298                        surface_->GetSize(),
    299                        DisallowedFeatures(),
    300                        attribs);
    301   decoder_->MakeCurrent();
    302   decoder_->set_engine(engine_.get());
    303 
    304   EXPECT_CALL(*gl_, GenBuffersARB(_, _))
    305       .WillOnce(SetArgumentPointee<1>(kServiceBufferId))
    306       .RetiresOnSaturation();
    307   GenHelper<cmds::GenBuffersImmediate>(client_buffer_id_);
    308   EXPECT_CALL(*gl_, GenFramebuffersEXT(_, _))
    309       .WillOnce(SetArgumentPointee<1>(kServiceFramebufferId))
    310       .RetiresOnSaturation();
    311   GenHelper<cmds::GenFramebuffersImmediate>(client_framebuffer_id_);
    312   EXPECT_CALL(*gl_, GenRenderbuffersEXT(_, _))
    313       .WillOnce(SetArgumentPointee<1>(kServiceRenderbufferId))
    314       .RetiresOnSaturation();
    315   GenHelper<cmds::GenRenderbuffersImmediate>(client_renderbuffer_id_);
    316   EXPECT_CALL(*gl_, GenTextures(_, _))
    317       .WillOnce(SetArgumentPointee<1>(kServiceTextureId))
    318       .RetiresOnSaturation();
    319   GenHelper<cmds::GenTexturesImmediate>(client_texture_id_);
    320   EXPECT_CALL(*gl_, GenBuffersARB(_, _))
    321       .WillOnce(SetArgumentPointee<1>(kServiceElementBufferId))
    322       .RetiresOnSaturation();
    323   GenHelper<cmds::GenBuffersImmediate>(client_element_buffer_id_);
    324 
    325   DoCreateProgram(client_program_id_, kServiceProgramId);
    326   DoCreateShader(GL_VERTEX_SHADER, client_shader_id_, kServiceShaderId);
    327 
    328   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    329 }
    330 
    331 void GLES2DecoderTestBase::TearDown() {
    332   // All Tests should have read all their GLErrors before getting here.
    333   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    334 
    335   EXPECT_CALL(*gl_, DeleteBuffersARB(1, _))
    336       .Times(2)
    337       .RetiresOnSaturation();
    338 
    339   decoder_->Destroy(true);
    340   decoder_.reset();
    341   group_->Destroy(mock_decoder_.get(), false);
    342   engine_.reset();
    343   ::gfx::GLInterface::SetGLInterface(NULL);
    344   gl_.reset();
    345 }
    346 
    347 void GLES2DecoderTestBase::ExpectEnableDisable(GLenum cap, bool enable) {
    348   if (enable) {
    349     EXPECT_CALL(*gl_, Enable(cap))
    350         .Times(1)
    351         .RetiresOnSaturation();
    352   } else {
    353     EXPECT_CALL(*gl_, Disable(cap))
    354         .Times(1)
    355         .RetiresOnSaturation();
    356   }
    357 }
    358 
    359 
    360 GLint GLES2DecoderTestBase::GetGLError() {
    361   EXPECT_CALL(*gl_, GetError())
    362       .WillOnce(Return(GL_NO_ERROR))
    363       .RetiresOnSaturation();
    364   cmds::GetError cmd;
    365   cmd.Init(shared_memory_id_, shared_memory_offset_);
    366   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    367   return static_cast<GLint>(*GetSharedMemoryAs<GLenum*>());
    368 }
    369 
    370 void GLES2DecoderTestBase::DoCreateShader(
    371     GLenum shader_type, GLuint client_id, GLuint service_id) {
    372   EXPECT_CALL(*gl_, CreateShader(shader_type))
    373       .Times(1)
    374       .WillOnce(Return(service_id))
    375       .RetiresOnSaturation();
    376   cmds::CreateShader cmd;
    377   cmd.Init(shader_type, client_id);
    378   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    379 }
    380 
    381 bool GLES2DecoderTestBase::DoIsShader(GLuint client_id) {
    382   return IsObjectHelper<cmds::IsShader, cmds::IsShader::Result>(client_id);
    383 }
    384 
    385 void GLES2DecoderTestBase::DoDeleteShader(
    386     GLuint client_id, GLuint service_id) {
    387   EXPECT_CALL(*gl_, DeleteShader(service_id))
    388       .Times(1)
    389       .RetiresOnSaturation();
    390   cmds::DeleteShader cmd;
    391   cmd.Init(client_id);
    392   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    393 }
    394 
    395 void GLES2DecoderTestBase::DoCreateProgram(
    396     GLuint client_id, GLuint service_id) {
    397   EXPECT_CALL(*gl_, CreateProgram())
    398       .Times(1)
    399       .WillOnce(Return(service_id))
    400       .RetiresOnSaturation();
    401   cmds::CreateProgram cmd;
    402   cmd.Init(client_id);
    403   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    404 }
    405 
    406 bool GLES2DecoderTestBase::DoIsProgram(GLuint client_id) {
    407   return IsObjectHelper<cmds::IsProgram, cmds::IsProgram::Result>(client_id);
    408 }
    409 
    410 void GLES2DecoderTestBase::DoDeleteProgram(
    411     GLuint client_id, GLuint /* service_id */) {
    412   cmds::DeleteProgram cmd;
    413   cmd.Init(client_id);
    414   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    415 }
    416 
    417 void GLES2DecoderTestBase::SetBucketAsCString(
    418     uint32 bucket_id, const char* str) {
    419   uint32 size = str ? (strlen(str) + 1) : 0;
    420   cmd::SetBucketSize cmd1;
    421   cmd1.Init(bucket_id, size);
    422   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd1));
    423   if (str) {
    424     memcpy(shared_memory_address_, str, size);
    425     cmd::SetBucketData cmd2;
    426     cmd2.Init(bucket_id, 0, size, kSharedMemoryId, kSharedMemoryOffset);
    427     EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    428     ClearSharedMemory();
    429   }
    430 }
    431 
    432 void GLES2DecoderTestBase::SetupClearTextureExpections(
    433       GLuint service_id,
    434       GLuint old_service_id,
    435       GLenum bind_target,
    436       GLenum target,
    437       GLint level,
    438       GLenum format,
    439       GLenum type,
    440       GLsizei width,
    441       GLsizei height) {
    442   EXPECT_CALL(*gl_, BindTexture(bind_target, service_id))
    443       .Times(1)
    444       .RetiresOnSaturation();
    445   EXPECT_CALL(*gl_, TexImage2D(
    446       target, level, format, width, height, 0, format, type, _))
    447       .Times(1)
    448       .RetiresOnSaturation();
    449   EXPECT_CALL(*gl_, BindTexture(bind_target, old_service_id))
    450       .Times(1)
    451       .RetiresOnSaturation();
    452 }
    453 
    454 void GLES2DecoderTestBase::SetupExpectationsForFramebufferClearing(
    455     GLenum target,
    456     GLuint clear_bits,
    457     GLclampf restore_red,
    458     GLclampf restore_green,
    459     GLclampf restore_blue,
    460     GLclampf restore_alpha,
    461     GLuint restore_stencil,
    462     GLclampf restore_depth,
    463     bool restore_scissor_test) {
    464   SetupExpectationsForFramebufferClearingMulti(
    465       0,
    466       0,
    467       target,
    468       clear_bits,
    469       restore_red,
    470       restore_green,
    471       restore_blue,
    472       restore_alpha,
    473       restore_stencil,
    474       restore_depth,
    475       restore_scissor_test);
    476 }
    477 
    478 void GLES2DecoderTestBase::SetupExpectationsForRestoreClearState(
    479     GLclampf restore_red,
    480     GLclampf restore_green,
    481     GLclampf restore_blue,
    482     GLclampf restore_alpha,
    483     GLuint restore_stencil,
    484     GLclampf restore_depth,
    485     bool restore_scissor_test) {
    486   EXPECT_CALL(*gl_, ClearColor(
    487       restore_red, restore_green, restore_blue, restore_alpha))
    488       .Times(1)
    489       .RetiresOnSaturation();
    490   EXPECT_CALL(*gl_, ClearStencil(restore_stencil))
    491       .Times(1)
    492       .RetiresOnSaturation();
    493   EXPECT_CALL(*gl_, ClearDepth(restore_depth))
    494       .Times(1)
    495       .RetiresOnSaturation();
    496   if (restore_scissor_test) {
    497     EXPECT_CALL(*gl_, Enable(GL_SCISSOR_TEST))
    498         .Times(1)
    499         .RetiresOnSaturation();
    500   }
    501 }
    502 
    503 void GLES2DecoderTestBase::SetupExpectationsForFramebufferClearingMulti(
    504     GLuint read_framebuffer_service_id,
    505     GLuint draw_framebuffer_service_id,
    506     GLenum target,
    507     GLuint clear_bits,
    508     GLclampf restore_red,
    509     GLclampf restore_green,
    510     GLclampf restore_blue,
    511     GLclampf restore_alpha,
    512     GLuint restore_stencil,
    513     GLclampf restore_depth,
    514     bool restore_scissor_test) {
    515   // TODO(gman): Figure out why InSequence stopped working.
    516   // InSequence sequence;
    517   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(target))
    518       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
    519       .RetiresOnSaturation();
    520   if (target == GL_READ_FRAMEBUFFER_EXT) {
    521     EXPECT_CALL(*gl_, BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0))
    522         .Times(1)
    523         .RetiresOnSaturation();
    524     EXPECT_CALL(*gl_, BindFramebufferEXT(
    525         GL_DRAW_FRAMEBUFFER_EXT, read_framebuffer_service_id))
    526         .Times(1)
    527         .RetiresOnSaturation();
    528   }
    529   if ((clear_bits & GL_COLOR_BUFFER_BIT) != 0) {
    530     EXPECT_CALL(*gl_, ClearColor(0.0f, 0.0f, 0.0f, 0.0f))
    531         .Times(1)
    532         .RetiresOnSaturation();
    533     EXPECT_CALL(*gl_, ColorMask(true, true, true, true))
    534         .Times(1)
    535         .RetiresOnSaturation();
    536   }
    537   if ((clear_bits & GL_STENCIL_BUFFER_BIT) != 0) {
    538     EXPECT_CALL(*gl_, ClearStencil(0))
    539         .Times(1)
    540         .RetiresOnSaturation();
    541     EXPECT_CALL(*gl_, StencilMask(static_cast<GLuint>(-1)))
    542         .Times(1)
    543         .RetiresOnSaturation();
    544   }
    545   if ((clear_bits & GL_DEPTH_BUFFER_BIT) != 0) {
    546     EXPECT_CALL(*gl_, ClearDepth(1.0f))
    547         .Times(1)
    548         .RetiresOnSaturation();
    549     EXPECT_CALL(*gl_, DepthMask(1))
    550         .Times(1)
    551         .RetiresOnSaturation();
    552   }
    553   EXPECT_CALL(*gl_, Disable(GL_SCISSOR_TEST))
    554       .Times(1)
    555       .RetiresOnSaturation();
    556   EXPECT_CALL(*gl_, Clear(clear_bits))
    557       .Times(1)
    558       .RetiresOnSaturation();
    559   SetupExpectationsForRestoreClearState(
    560       restore_red, restore_green, restore_blue, restore_alpha,
    561       restore_stencil, restore_depth, restore_scissor_test);
    562   if (target == GL_READ_FRAMEBUFFER_EXT) {
    563     EXPECT_CALL(*gl_, BindFramebufferEXT(
    564         GL_READ_FRAMEBUFFER_EXT, read_framebuffer_service_id))
    565         .Times(1)
    566         .RetiresOnSaturation();
    567     EXPECT_CALL(*gl_, BindFramebufferEXT(
    568         GL_DRAW_FRAMEBUFFER_EXT, draw_framebuffer_service_id))
    569         .Times(1)
    570         .RetiresOnSaturation();
    571   }
    572 }
    573 
    574 void GLES2DecoderTestBase::SetupShaderForUniform(GLenum uniform_type) {
    575   static AttribInfo attribs[] = {
    576     { "foo", 1, GL_FLOAT, 1, },
    577     { "goo", 1, GL_FLOAT, 2, },
    578   };
    579   UniformInfo uniforms[] = {
    580     { "bar", 1, uniform_type, 0, 2, -1, },
    581     { "car", 4, uniform_type, 1, 1, -1, },
    582   };
    583   const GLuint kClientVertexShaderId = 5001;
    584   const GLuint kServiceVertexShaderId = 6001;
    585   const GLuint kClientFragmentShaderId = 5002;
    586   const GLuint kServiceFragmentShaderId = 6002;
    587   SetupShader(attribs, arraysize(attribs), uniforms, arraysize(uniforms),
    588               client_program_id_, kServiceProgramId,
    589               kClientVertexShaderId, kServiceVertexShaderId,
    590               kClientFragmentShaderId, kServiceFragmentShaderId);
    591 
    592   EXPECT_CALL(*gl_, UseProgram(kServiceProgramId))
    593       .Times(1)
    594       .RetiresOnSaturation();
    595   cmds::UseProgram cmd;
    596   cmd.Init(client_program_id_);
    597   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    598 }
    599 
    600 void GLES2DecoderTestBase::DoBindBuffer(
    601     GLenum target, GLuint client_id, GLuint service_id) {
    602   EXPECT_CALL(*gl_, BindBuffer(target, service_id))
    603       .Times(1)
    604       .RetiresOnSaturation();
    605   cmds::BindBuffer cmd;
    606   cmd.Init(target, client_id);
    607   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    608 }
    609 
    610 bool GLES2DecoderTestBase::DoIsBuffer(GLuint client_id) {
    611   return IsObjectHelper<cmds::IsBuffer, cmds::IsBuffer::Result>(client_id);
    612 }
    613 
    614 void GLES2DecoderTestBase::DoDeleteBuffer(
    615     GLuint client_id, GLuint service_id) {
    616   EXPECT_CALL(*gl_, DeleteBuffersARB(1, Pointee(service_id)))
    617       .Times(1)
    618       .RetiresOnSaturation();
    619   cmds::DeleteBuffers cmd;
    620   cmd.Init(1, shared_memory_id_, shared_memory_offset_);
    621   memcpy(shared_memory_address_, &client_id, sizeof(client_id));
    622   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    623 }
    624 
    625 void GLES2DecoderTestBase::SetupExpectationsForApplyingDirtyState(
    626     bool framebuffer_is_rgb,
    627     bool framebuffer_has_depth,
    628     bool framebuffer_has_stencil,
    629     GLuint color_bits,
    630     bool depth_mask,
    631     bool depth_enabled,
    632     GLuint front_stencil_mask,
    633     GLuint back_stencil_mask,
    634     bool stencil_enabled,
    635     bool cull_face_enabled,
    636     bool scissor_test_enabled,
    637     bool blend_enabled) {
    638   EXPECT_CALL(*gl_, ColorMask(
    639       (color_bits & 0x1000) != 0,
    640       (color_bits & 0x0100) != 0,
    641       (color_bits & 0x0010) != 0,
    642       (color_bits & 0x0001) && !framebuffer_is_rgb))
    643       .Times(1)
    644       .RetiresOnSaturation();
    645   EXPECT_CALL(*gl_, DepthMask(depth_mask))
    646       .Times(1)
    647       .RetiresOnSaturation();
    648   if (framebuffer_has_depth && depth_enabled) {
    649     EXPECT_CALL(*gl_, Enable(GL_DEPTH_TEST))
    650         .Times(1)
    651         .RetiresOnSaturation();
    652   } else {
    653     EXPECT_CALL(*gl_, Disable(GL_DEPTH_TEST))
    654         .Times(1)
    655         .RetiresOnSaturation();
    656   }
    657   EXPECT_CALL(*gl_, StencilMaskSeparate(GL_FRONT, front_stencil_mask))
    658       .Times(1)
    659       .RetiresOnSaturation();
    660   EXPECT_CALL(*gl_, StencilMaskSeparate(GL_BACK, back_stencil_mask))
    661       .Times(1)
    662       .RetiresOnSaturation();
    663   if (framebuffer_has_stencil && stencil_enabled) {
    664     EXPECT_CALL(*gl_, Enable(GL_STENCIL_TEST))
    665         .Times(1)
    666         .RetiresOnSaturation();
    667   } else {
    668     EXPECT_CALL(*gl_, Disable(GL_STENCIL_TEST))
    669         .Times(1)
    670         .RetiresOnSaturation();
    671   }
    672   if (cull_face_enabled) {
    673     EXPECT_CALL(*gl_, Enable(GL_CULL_FACE))
    674         .Times(1)
    675         .RetiresOnSaturation();
    676   } else {
    677     EXPECT_CALL(*gl_, Disable(GL_CULL_FACE))
    678         .Times(1)
    679         .RetiresOnSaturation();
    680   }
    681   if (scissor_test_enabled) {
    682     EXPECT_CALL(*gl_, Enable(GL_SCISSOR_TEST))
    683         .Times(1)
    684         .RetiresOnSaturation();
    685   } else {
    686     EXPECT_CALL(*gl_, Disable(GL_SCISSOR_TEST))
    687         .Times(1)
    688         .RetiresOnSaturation();
    689   }
    690   if (blend_enabled) {
    691     EXPECT_CALL(*gl_, Enable(GL_BLEND))
    692         .Times(1)
    693         .RetiresOnSaturation();
    694   } else {
    695     EXPECT_CALL(*gl_, Disable(GL_BLEND))
    696         .Times(1)
    697         .RetiresOnSaturation();
    698   }
    699 }
    700 
    701 void GLES2DecoderTestBase::SetupExpectationsForApplyingDefaultDirtyState() {
    702   SetupExpectationsForApplyingDirtyState(
    703       false,   // Framebuffer is RGB
    704       false,   // Framebuffer has depth
    705       false,   // Framebuffer has stencil
    706       0x1111,  // color bits
    707       true,    // depth mask
    708       false,   // depth enabled
    709       0,       // front stencil mask
    710       0,       // back stencil mask
    711       false,   // stencil enabled
    712       false,   // cull_face_enabled
    713       false,   // scissor_test_enabled
    714       false);  // blend_enabled
    715 }
    716 
    717 void GLES2DecoderTestBase::DoBindFramebuffer(
    718     GLenum target, GLuint client_id, GLuint service_id) {
    719   EXPECT_CALL(*gl_, BindFramebufferEXT(target, service_id))
    720       .Times(1)
    721       .RetiresOnSaturation();
    722   cmds::BindFramebuffer cmd;
    723   cmd.Init(target, client_id);
    724   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    725 }
    726 
    727 bool GLES2DecoderTestBase::DoIsFramebuffer(GLuint client_id) {
    728   return IsObjectHelper<cmds::IsFramebuffer, cmds::IsFramebuffer::Result>(
    729       client_id);
    730 }
    731 
    732 void GLES2DecoderTestBase::DoDeleteFramebuffer(
    733     GLuint client_id, GLuint service_id,
    734     bool reset_draw, GLenum draw_target, GLuint draw_id,
    735     bool reset_read, GLenum read_target, GLuint read_id) {
    736   if (reset_draw) {
    737     EXPECT_CALL(*gl_, BindFramebufferEXT(draw_target, draw_id))
    738         .Times(1)
    739         .RetiresOnSaturation();
    740   }
    741   if (reset_read) {
    742     EXPECT_CALL(*gl_, BindFramebufferEXT(read_target, read_id))
    743         .Times(1)
    744         .RetiresOnSaturation();
    745   }
    746   EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, Pointee(service_id)))
    747       .Times(1)
    748       .RetiresOnSaturation();
    749   cmds::DeleteFramebuffers cmd;
    750   cmd.Init(1, shared_memory_id_, shared_memory_offset_);
    751   memcpy(shared_memory_address_, &client_id, sizeof(client_id));
    752   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    753 }
    754 
    755 void GLES2DecoderTestBase::DoBindRenderbuffer(
    756     GLenum target, GLuint client_id, GLuint service_id) {
    757   EXPECT_CALL(*gl_, BindRenderbufferEXT(target, service_id))
    758       .Times(1)
    759       .RetiresOnSaturation();
    760   cmds::BindRenderbuffer cmd;
    761   cmd.Init(target, client_id);
    762   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    763 }
    764 
    765 bool GLES2DecoderTestBase::DoIsRenderbuffer(GLuint client_id) {
    766   return IsObjectHelper<cmds::IsRenderbuffer, cmds::IsRenderbuffer::Result>(
    767       client_id);
    768 }
    769 
    770 void GLES2DecoderTestBase::DoDeleteRenderbuffer(
    771     GLuint client_id, GLuint service_id) {
    772   EXPECT_CALL(*gl_, DeleteRenderbuffersEXT(1, Pointee(service_id)))
    773       .Times(1)
    774       .RetiresOnSaturation();
    775   cmds::DeleteRenderbuffers cmd;
    776   cmd.Init(1, shared_memory_id_, shared_memory_offset_);
    777   memcpy(shared_memory_address_, &client_id, sizeof(client_id));
    778   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    779 }
    780 
    781 void GLES2DecoderTestBase::DoBindTexture(
    782     GLenum target, GLuint client_id, GLuint service_id) {
    783   EXPECT_CALL(*gl_, BindTexture(target, service_id))
    784       .Times(1)
    785       .RetiresOnSaturation();
    786   cmds::BindTexture cmd;
    787   cmd.Init(target, client_id);
    788   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    789 }
    790 
    791 bool GLES2DecoderTestBase::DoIsTexture(GLuint client_id) {
    792   return IsObjectHelper<cmds::IsTexture, cmds::IsTexture::Result>(client_id);
    793 }
    794 
    795 void GLES2DecoderTestBase::DoDeleteTexture(
    796     GLuint client_id, GLuint service_id) {
    797   EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(service_id)))
    798       .Times(1)
    799       .RetiresOnSaturation();
    800   cmds::DeleteTextures cmd;
    801   cmd.Init(1, shared_memory_id_, shared_memory_offset_);
    802   memcpy(shared_memory_address_, &client_id, sizeof(client_id));
    803   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    804 }
    805 
    806 void GLES2DecoderTestBase::DoTexImage2D(
    807     GLenum target, GLint level, GLenum internal_format,
    808     GLsizei width, GLsizei height, GLint border,
    809     GLenum format, GLenum type,
    810     uint32 shared_memory_id, uint32 shared_memory_offset) {
    811   EXPECT_CALL(*gl_, GetError())
    812       .WillOnce(Return(GL_NO_ERROR))
    813       .RetiresOnSaturation();
    814   EXPECT_CALL(*gl_, TexImage2D(target, level, internal_format,
    815                                width, height, border, format, type, _))
    816       .Times(1)
    817       .RetiresOnSaturation();
    818   EXPECT_CALL(*gl_, GetError())
    819       .WillOnce(Return(GL_NO_ERROR))
    820       .RetiresOnSaturation();
    821   cmds::TexImage2D cmd;
    822   cmd.Init(target, level, internal_format, width, height, border, format,
    823            type, shared_memory_id, shared_memory_offset);
    824   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    825 }
    826 
    827 void GLES2DecoderTestBase::DoCompressedTexImage2D(
    828     GLenum target, GLint level, GLenum format,
    829     GLsizei width, GLsizei height, GLint border,
    830     GLsizei size, uint32 bucket_id) {
    831   EXPECT_CALL(*gl_, GetError())
    832       .WillOnce(Return(GL_NO_ERROR))
    833       .RetiresOnSaturation();
    834   EXPECT_CALL(*gl_, CompressedTexImage2D(
    835       target, level, format, width, height, border, size, _))
    836       .Times(1)
    837       .RetiresOnSaturation();
    838   EXPECT_CALL(*gl_, GetError())
    839       .WillOnce(Return(GL_NO_ERROR))
    840       .RetiresOnSaturation();
    841   CommonDecoder::Bucket* bucket = decoder_->CreateBucket(bucket_id);
    842   bucket->SetSize(size);
    843   cmds::CompressedTexImage2DBucket cmd;
    844   cmd.Init(
    845       target, level, format, width, height, border,
    846       bucket_id);
    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 
   1258 void GLES2DecoderTestBase::DoEnableVertexAttribArray(GLint index) {
   1259   EXPECT_CALL(*gl_, EnableVertexAttribArray(index))
   1260       .Times(1)
   1261       .RetiresOnSaturation();
   1262   cmds::EnableVertexAttribArray cmd;
   1263   cmd.Init(index);
   1264   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1265 }
   1266 
   1267 void GLES2DecoderTestBase::DoBufferData(GLenum target, GLsizei size) {
   1268   EXPECT_CALL(*gl_, GetError())
   1269       .WillOnce(Return(GL_NO_ERROR))
   1270       .RetiresOnSaturation();
   1271   EXPECT_CALL(*gl_, BufferData(target, size, _, GL_STREAM_DRAW))
   1272       .Times(1)
   1273       .RetiresOnSaturation();
   1274   EXPECT_CALL(*gl_, GetError())
   1275       .WillOnce(Return(GL_NO_ERROR))
   1276       .RetiresOnSaturation();
   1277   cmds::BufferData cmd;
   1278   cmd.Init(target, size, 0, 0, GL_STREAM_DRAW);
   1279   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1280 }
   1281 
   1282 void GLES2DecoderTestBase::DoBufferSubData(
   1283     GLenum target, GLint offset, GLsizei size, const void* data) {
   1284   EXPECT_CALL(*gl_, BufferSubData(target, offset, size,
   1285                                   shared_memory_address_))
   1286       .Times(1)
   1287       .RetiresOnSaturation();
   1288   memcpy(shared_memory_address_, data, size);
   1289   cmds::BufferSubData cmd;
   1290   cmd.Init(target, offset, size, shared_memory_id_, shared_memory_offset_);
   1291   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1292 }
   1293 
   1294 void GLES2DecoderTestBase::SetupVertexBuffer() {
   1295   DoEnableVertexAttribArray(1);
   1296   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
   1297   GLfloat f = 0;
   1298   DoBufferData(GL_ARRAY_BUFFER, kNumVertices * 2 * sizeof(f));
   1299 }
   1300 
   1301 void GLES2DecoderTestBase::SetupAllNeededVertexBuffers() {
   1302   DoBindBuffer(GL_ARRAY_BUFFER, client_buffer_id_, kServiceBufferId);
   1303   DoBufferData(GL_ARRAY_BUFFER, kNumVertices * 16 * sizeof(float));
   1304   DoEnableVertexAttribArray(0);
   1305   DoEnableVertexAttribArray(1);
   1306   DoEnableVertexAttribArray(2);
   1307   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1308   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1309   DoVertexAttribPointer(2, 2, GL_FLOAT, 0, 0);
   1310 }
   1311 
   1312 void GLES2DecoderTestBase::SetupIndexBuffer() {
   1313   DoBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
   1314                client_element_buffer_id_,
   1315                kServiceElementBufferId);
   1316   static const GLshort indices[] = {100, 1, 2, 3, 4, 5, 6, 7, 100, 9};
   1317   COMPILE_ASSERT(arraysize(indices) == kNumIndices, Indices_is_not_10);
   1318   DoBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices));
   1319   DoBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 2, indices);
   1320   DoBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 2, sizeof(indices) - 2, &indices[1]);
   1321 }
   1322 
   1323 void GLES2DecoderTestBase::SetupTexture() {
   1324   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1325   DoTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
   1326                kSharedMemoryId, kSharedMemoryOffset);
   1327 };
   1328 
   1329 void GLES2DecoderTestBase::DeleteVertexBuffer() {
   1330   DoDeleteBuffer(client_buffer_id_, kServiceBufferId);
   1331 }
   1332 
   1333 void GLES2DecoderTestBase::DeleteIndexBuffer() {
   1334   DoDeleteBuffer(client_element_buffer_id_, kServiceElementBufferId);
   1335 }
   1336 
   1337 void GLES2DecoderTestBase::AddExpectationsForSimulatedAttrib0WithError(
   1338     GLsizei num_vertices, GLuint buffer_id, GLenum error) {
   1339   if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
   1340     return;
   1341   }
   1342 
   1343   EXPECT_CALL(*gl_, GetError())
   1344       .WillOnce(Return(GL_NO_ERROR))
   1345       .WillOnce(Return(error))
   1346       .RetiresOnSaturation();
   1347   EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, kServiceAttrib0BufferId))
   1348       .Times(1)
   1349       .RetiresOnSaturation();
   1350   EXPECT_CALL(*gl_, BufferData(GL_ARRAY_BUFFER,
   1351                                num_vertices * sizeof(GLfloat) * 4,
   1352                                _, GL_DYNAMIC_DRAW))
   1353       .Times(1)
   1354       .RetiresOnSaturation();
   1355   if (error == GL_NO_ERROR) {
   1356     EXPECT_CALL(*gl_, BufferSubData(
   1357         GL_ARRAY_BUFFER, 0, num_vertices * sizeof(GLfloat) * 4, _))
   1358         .Times(1)
   1359         .RetiresOnSaturation();
   1360     EXPECT_CALL(*gl_, VertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL))
   1361         .Times(1)
   1362         .RetiresOnSaturation();
   1363     EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, 0))
   1364         .Times(1)
   1365         .RetiresOnSaturation();
   1366     EXPECT_CALL(*gl_, VertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL))
   1367         .Times(1)
   1368         .RetiresOnSaturation();
   1369     EXPECT_CALL(*gl_, BindBuffer(GL_ARRAY_BUFFER, buffer_id))
   1370         .Times(1)
   1371         .RetiresOnSaturation();
   1372   }
   1373 }
   1374 
   1375 void GLES2DecoderTestBase::AddExpectationsForSimulatedAttrib0(
   1376     GLsizei num_vertices, GLuint buffer_id) {
   1377   AddExpectationsForSimulatedAttrib0WithError(
   1378       num_vertices, buffer_id, GL_NO_ERROR);
   1379 }
   1380 
   1381 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::
   1382 MockCommandBufferEngine() {
   1383   data_.reset(new int8[kSharedBufferSize]);
   1384   ClearSharedMemory();
   1385   valid_buffer_.ptr = data_.get();
   1386   valid_buffer_.size = kSharedBufferSize;
   1387 }
   1388 
   1389 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::
   1390 ~MockCommandBufferEngine() {}
   1391 
   1392 gpu::Buffer
   1393 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::GetSharedMemoryBuffer(
   1394     int32 shm_id) {
   1395   return shm_id == kSharedMemoryId ? valid_buffer_ : invalid_buffer_;
   1396 }
   1397 
   1398 void GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::set_token(
   1399     int32 token) {
   1400   DCHECK(false);
   1401 }
   1402 
   1403 bool GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::SetGetBuffer(
   1404     int32 /* transfer_buffer_id */) {
   1405   DCHECK(false);
   1406   return false;
   1407 }
   1408 
   1409 bool GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::SetGetOffset(
   1410    int32 offset) {
   1411   DCHECK(false);
   1412   return false;
   1413 }
   1414 
   1415 int32 GLES2DecoderWithShaderTestBase::MockCommandBufferEngine::GetGetOffset() {
   1416   DCHECK(false);
   1417   return 0;
   1418 }
   1419 
   1420 void GLES2DecoderWithShaderTestBase::SetUp() {
   1421   GLES2DecoderTestBase::SetUp();
   1422   SetupDefaultProgram();
   1423 }
   1424 
   1425 // Include the auto-generated part of this file. We split this because it means
   1426 // we can easily edit the non-auto generated parts right here in this file
   1427 // instead of having to edit some template or the code generator.
   1428 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest_0_autogen.h"
   1429 
   1430 }  // namespace gles2
   1431 }  // namespace gpu
   1432