Home | History | Annotate | Download | only in service
      1 // Copyright 2014 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.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "gpu/command_buffer/common/gles2_cmd_format.h"
     10 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
     11 #include "gpu/command_buffer/common/id_allocator.h"
     12 #include "gpu/command_buffer/service/async_pixel_transfer_delegate_mock.h"
     13 #include "gpu/command_buffer/service/async_pixel_transfer_manager.h"
     14 #include "gpu/command_buffer/service/async_pixel_transfer_manager_mock.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/context_state.h"
     18 #include "gpu/command_buffer/service/gl_surface_mock.h"
     19 #include "gpu/command_buffer/service/gles2_cmd_decoder_unittest.h"
     20 
     21 #include "gpu/command_buffer/service/gpu_switches.h"
     22 #include "gpu/command_buffer/service/image_manager.h"
     23 #include "gpu/command_buffer/service/mailbox_manager.h"
     24 #include "gpu/command_buffer/service/mocks.h"
     25 #include "gpu/command_buffer/service/program_manager.h"
     26 #include "gpu/command_buffer/service/test_helper.h"
     27 #include "testing/gtest/include/gtest/gtest.h"
     28 #include "ui/gl/gl_implementation.h"
     29 #include "ui/gl/gl_mock.h"
     30 #include "ui/gl/gl_surface_stub.h"
     31 
     32 #if !defined(GL_DEPTH24_STENCIL8)
     33 #define GL_DEPTH24_STENCIL8 0x88F0
     34 #endif
     35 
     36 using ::gfx::MockGLInterface;
     37 using ::testing::_;
     38 using ::testing::DoAll;
     39 using ::testing::InSequence;
     40 using ::testing::Invoke;
     41 using ::testing::MatcherCast;
     42 using ::testing::Mock;
     43 using ::testing::Pointee;
     44 using ::testing::Return;
     45 using ::testing::SaveArg;
     46 using ::testing::SetArrayArgument;
     47 using ::testing::SetArgumentPointee;
     48 using ::testing::SetArgPointee;
     49 using ::testing::StrEq;
     50 using ::testing::StrictMock;
     51 
     52 namespace gpu {
     53 namespace gles2 {
     54 
     55 using namespace cmds;
     56 
     57 class GLES2DecoderGeometryInstancingTest : public GLES2DecoderWithShaderTest {
     58  public:
     59   GLES2DecoderGeometryInstancingTest() : GLES2DecoderWithShaderTest() {}
     60 
     61   virtual void SetUp() {
     62     InitState init;
     63     init.extensions = "GL_ANGLE_instanced_arrays";
     64     init.gl_version = "opengl es 2.0";
     65     init.has_alpha = true;
     66     init.has_depth = true;
     67     init.request_alpha = true;
     68     init.request_depth = true;
     69     init.bind_generates_resource = true;
     70     InitDecoder(init);
     71     SetupDefaultProgram();
     72   }
     73 };
     74 
     75 INSTANTIATE_TEST_CASE_P(Service,
     76                         GLES2DecoderGeometryInstancingTest,
     77                         ::testing::Bool());
     78 
     79 void GLES2DecoderManualInitTest::DirtyStateMaskTest(GLuint color_bits,
     80                                                     bool depth_mask,
     81                                                     GLuint front_stencil_mask,
     82                                                     GLuint back_stencil_mask) {
     83   ColorMask color_mask_cmd;
     84   color_mask_cmd.Init((color_bits & 0x1000) != 0,
     85                       (color_bits & 0x0100) != 0,
     86                       (color_bits & 0x0010) != 0,
     87                       (color_bits & 0x0001) != 0);
     88   EXPECT_EQ(error::kNoError, ExecuteCmd(color_mask_cmd));
     89   EXPECT_EQ(GL_NO_ERROR, GetGLError());
     90 
     91   DepthMask depth_mask_cmd;
     92   depth_mask_cmd.Init(depth_mask);
     93   EXPECT_EQ(error::kNoError, ExecuteCmd(depth_mask_cmd));
     94   EXPECT_EQ(GL_NO_ERROR, GetGLError());
     95 
     96   StencilMaskSeparate front_stencil_mask_cmd;
     97   front_stencil_mask_cmd.Init(GL_FRONT, front_stencil_mask);
     98   EXPECT_EQ(error::kNoError, ExecuteCmd(front_stencil_mask_cmd));
     99   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    100 
    101   StencilMaskSeparate back_stencil_mask_cmd;
    102   back_stencil_mask_cmd.Init(GL_BACK, back_stencil_mask);
    103   EXPECT_EQ(error::kNoError, ExecuteCmd(back_stencil_mask_cmd));
    104   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    105 
    106   SetupExpectationsForApplyingDirtyState(
    107       false,               // Framebuffer is RGB
    108       true,                // Framebuffer has depth
    109       true,                // Framebuffer has stencil
    110       color_bits,          // color bits
    111       depth_mask,          // depth mask
    112       false,               // depth enabled
    113       front_stencil_mask,  // front stencil mask
    114       back_stencil_mask,   // back stencil mask
    115       false);              // stencil enabled
    116 
    117   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    118       .Times(1)
    119       .RetiresOnSaturation();
    120   DrawArrays draw_cmd;
    121   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    122   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    123   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    124 }
    125 
    126 // Test that with an RGB backbuffer if we set the color mask to 1,1,1,1 it is
    127 // set to 1,1,1,0 at Draw time but is 1,1,1,1 at query time.
    128 TEST_P(GLES2DecoderRGBBackbufferTest, RGBBackbufferColorMask) {
    129   ColorMask cmd;
    130   cmd.Init(true, true, true, true);
    131   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    132   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    133 
    134   SetupTexture();
    135   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    136   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    137                                          false,   // Framebuffer has depth
    138                                          false,   // Framebuffer has stencil
    139                                          0x1110,  // color bits
    140                                          false,   // depth mask
    141                                          false,   // depth enabled
    142                                          0,       // front stencil mask
    143                                          0,       // back stencil mask
    144                                          false);  // stencil enabled
    145 
    146   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    147       .Times(1)
    148       .RetiresOnSaturation();
    149   DrawArrays draw_cmd;
    150   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    151   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    152   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    153 
    154   EXPECT_CALL(*gl_, GetError())
    155       .WillOnce(Return(GL_NO_ERROR))
    156       .WillOnce(Return(GL_NO_ERROR))
    157       .RetiresOnSaturation();
    158   typedef GetIntegerv::Result Result;
    159   Result* result = static_cast<Result*>(shared_memory_address_);
    160   EXPECT_CALL(*gl_, GetIntegerv(GL_COLOR_WRITEMASK, result->GetData()))
    161       .Times(0);
    162   result->size = 0;
    163   GetIntegerv cmd2;
    164   cmd2.Init(GL_COLOR_WRITEMASK, shared_memory_id_, shared_memory_offset_);
    165   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    166   EXPECT_EQ(
    167       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_COLOR_WRITEMASK),
    168       result->GetNumResults());
    169   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    170   EXPECT_EQ(1, result->GetData()[0]);
    171   EXPECT_EQ(1, result->GetData()[1]);
    172   EXPECT_EQ(1, result->GetData()[2]);
    173   EXPECT_EQ(1, result->GetData()[3]);
    174 }
    175 
    176 // Test that with no depth if we set DepthMask true that it's set to false at
    177 // draw time but querying it returns true.
    178 TEST_P(GLES2DecoderRGBBackbufferTest, RGBBackbufferDepthMask) {
    179   EXPECT_CALL(*gl_, DepthMask(true)).Times(0).RetiresOnSaturation();
    180   DepthMask cmd;
    181   cmd.Init(true);
    182   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    183   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    184 
    185   SetupTexture();
    186   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    187   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    188                                          false,   // Framebuffer has depth
    189                                          false,   // Framebuffer has stencil
    190                                          0x1110,  // color bits
    191                                          false,   // depth mask
    192                                          false,   // depth enabled
    193                                          0,       // front stencil mask
    194                                          0,       // back stencil mask
    195                                          false);  // stencil enabled
    196 
    197   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    198       .Times(1)
    199       .RetiresOnSaturation();
    200   DrawArrays draw_cmd;
    201   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    202   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    203   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    204 
    205   EXPECT_CALL(*gl_, GetError())
    206       .WillOnce(Return(GL_NO_ERROR))
    207       .WillOnce(Return(GL_NO_ERROR))
    208       .RetiresOnSaturation();
    209   typedef GetIntegerv::Result Result;
    210   Result* result = static_cast<Result*>(shared_memory_address_);
    211   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_WRITEMASK, result->GetData()))
    212       .Times(0);
    213   result->size = 0;
    214   GetIntegerv cmd2;
    215   cmd2.Init(GL_DEPTH_WRITEMASK, shared_memory_id_, shared_memory_offset_);
    216   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    217   EXPECT_EQ(
    218       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_WRITEMASK),
    219       result->GetNumResults());
    220   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    221   EXPECT_EQ(1, result->GetData()[0]);
    222 }
    223 
    224 // Test that with no stencil if we set the stencil mask it's still set to 0 at
    225 // draw time but gets our value if we query.
    226 TEST_P(GLES2DecoderRGBBackbufferTest, RGBBackbufferStencilMask) {
    227   const GLint kMask = 123;
    228   EXPECT_CALL(*gl_, StencilMask(kMask)).Times(0).RetiresOnSaturation();
    229   StencilMask cmd;
    230   cmd.Init(kMask);
    231   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    232   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    233 
    234   SetupTexture();
    235   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    236   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    237                                          false,   // Framebuffer has depth
    238                                          false,   // Framebuffer has stencil
    239                                          0x1110,  // color bits
    240                                          false,   // depth mask
    241                                          false,   // depth enabled
    242                                          0,       // front stencil mask
    243                                          0,       // back stencil mask
    244                                          false);  // stencil enabled
    245 
    246   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    247       .Times(1)
    248       .RetiresOnSaturation();
    249   DrawArrays draw_cmd;
    250   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    251   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    252   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    253 
    254   EXPECT_CALL(*gl_, GetError())
    255       .WillOnce(Return(GL_NO_ERROR))
    256       .WillOnce(Return(GL_NO_ERROR))
    257       .RetiresOnSaturation();
    258   typedef GetIntegerv::Result Result;
    259   Result* result = static_cast<Result*>(shared_memory_address_);
    260   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_WRITEMASK, result->GetData()))
    261       .Times(0);
    262   result->size = 0;
    263   GetIntegerv cmd2;
    264   cmd2.Init(GL_STENCIL_WRITEMASK, shared_memory_id_, shared_memory_offset_);
    265   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    266   EXPECT_EQ(
    267       decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_WRITEMASK),
    268       result->GetNumResults());
    269   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    270   EXPECT_EQ(kMask, result->GetData()[0]);
    271 }
    272 
    273 // Test that if an FBO is bound we get the correct masks.
    274 TEST_P(GLES2DecoderRGBBackbufferTest, RGBBackbufferColorMaskFBO) {
    275   ColorMask cmd;
    276   cmd.Init(true, true, true, true);
    277   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    278   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    279 
    280   SetupTexture();
    281   SetupVertexBuffer();
    282   DoEnableVertexAttribArray(0);
    283   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
    284   DoEnableVertexAttribArray(1);
    285   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    286   DoEnableVertexAttribArray(2);
    287   DoVertexAttribPointer(2, 2, GL_FLOAT, 0, 0);
    288   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    289                                          false,   // Framebuffer has depth
    290                                          false,   // Framebuffer has stencil
    291                                          0x1110,  // color bits
    292                                          false,   // depth mask
    293                                          false,   // depth enabled
    294                                          0,       // front stencil mask
    295                                          0,       // back stencil mask
    296                                          false);  // stencil enabled
    297 
    298   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    299       .Times(1)
    300       .RetiresOnSaturation();
    301   DrawArrays draw_cmd;
    302   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    303   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    304   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    305 
    306   // Check that no extra calls are made on the next draw.
    307   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    308       .Times(1)
    309       .RetiresOnSaturation();
    310   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    311   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    312 
    313   // Setup Frame buffer.
    314   // needs to be 1x1 or else it's not renderable.
    315   const GLsizei kWidth = 1;
    316   const GLsizei kHeight = 1;
    317   const GLenum kFormat = GL_RGB;
    318   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
    319   // Pass some data so the texture will be marked as cleared.
    320   DoTexImage2D(GL_TEXTURE_2D,
    321                0,
    322                kFormat,
    323                kWidth,
    324                kHeight,
    325                0,
    326                kFormat,
    327                GL_UNSIGNED_BYTE,
    328                kSharedMemoryId,
    329                kSharedMemoryOffset);
    330   DoBindFramebuffer(
    331       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
    332   DoFramebufferTexture2D(GL_FRAMEBUFFER,
    333                          GL_COLOR_ATTACHMENT0,
    334                          GL_TEXTURE_2D,
    335                          client_texture_id_,
    336                          kServiceTextureId,
    337                          0,
    338                          GL_NO_ERROR);
    339   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
    340       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
    341       .RetiresOnSaturation();
    342 
    343   // This time state needs to be set.
    344   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
    345                                          false,   // Framebuffer has depth
    346                                          false,   // Framebuffer has stencil
    347                                          0x1110,  // color bits
    348                                          false,   // depth mask
    349                                          false,   // depth enabled
    350                                          0,       // front stencil mask
    351                                          0,       // back stencil mask
    352                                          false);  // stencil enabled
    353 
    354   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    355       .Times(1)
    356       .RetiresOnSaturation();
    357   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    358   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    359 
    360   // Check that no extra calls are made on the next draw.
    361   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    362       .Times(1)
    363       .RetiresOnSaturation();
    364   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    365   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    366 
    367   // Unbind
    368   DoBindFramebuffer(GL_FRAMEBUFFER, 0, 0);
    369 
    370   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    371                                          false,   // Framebuffer has depth
    372                                          false,   // Framebuffer has stencil
    373                                          0x1110,  // color bits
    374                                          false,   // depth mask
    375                                          false,   // depth enabled
    376                                          0,       // front stencil mask
    377                                          0,       // back stencil mask
    378                                          false);  // stencil enabled
    379 
    380   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    381       .Times(1)
    382       .RetiresOnSaturation();
    383   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    384   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    385 }
    386 
    387 TEST_P(GLES2DecoderManualInitTest, DepthEnableWithDepth) {
    388   InitState init;
    389   init.gl_version = "3.0";
    390   init.has_depth = true;
    391   init.request_depth = true;
    392   init.bind_generates_resource = true;
    393   InitDecoder(init);
    394 
    395   Enable cmd;
    396   cmd.Init(GL_DEPTH_TEST);
    397   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    398   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    399 
    400   SetupDefaultProgram();
    401   SetupTexture();
    402   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    403   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    404                                          true,    // Framebuffer has depth
    405                                          false,   // Framebuffer has stencil
    406                                          0x1110,  // color bits
    407                                          true,    // depth mask
    408                                          true,    // depth enabled
    409                                          0,       // front stencil mask
    410                                          0,       // back stencil mask
    411                                          false);  // stencil enabled
    412 
    413   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    414       .Times(1)
    415       .RetiresOnSaturation();
    416   DrawArrays draw_cmd;
    417   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    418   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    419   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    420 
    421   EXPECT_CALL(*gl_, GetError())
    422       .WillOnce(Return(GL_NO_ERROR))
    423       .WillOnce(Return(GL_NO_ERROR))
    424       .RetiresOnSaturation();
    425   typedef GetIntegerv::Result Result;
    426   Result* result = static_cast<Result*>(shared_memory_address_);
    427   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_TEST, _))
    428       .Times(0)
    429       .RetiresOnSaturation();
    430   result->size = 0;
    431   GetIntegerv cmd2;
    432   cmd2.Init(GL_DEPTH_TEST, shared_memory_id_, shared_memory_offset_);
    433   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    434   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_TEST),
    435             result->GetNumResults());
    436   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    437   EXPECT_EQ(1, result->GetData()[0]);
    438 }
    439 
    440 TEST_P(GLES2DecoderManualInitTest, DepthEnableWithoutRequestedDepth) {
    441   InitState init;
    442   init.gl_version = "3.0";
    443   init.has_depth = true;
    444   init.bind_generates_resource = true;
    445   InitDecoder(init);
    446 
    447   Enable cmd;
    448   cmd.Init(GL_DEPTH_TEST);
    449   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    450   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    451 
    452   SetupDefaultProgram();
    453   SetupTexture();
    454   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    455   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    456                                          false,   // Framebuffer has depth
    457                                          false,   // Framebuffer has stencil
    458                                          0x1110,  // color bits
    459                                          false,   // depth mask
    460                                          false,   // depth enabled
    461                                          0,       // front stencil mask
    462                                          0,       // back stencil mask
    463                                          false);  // stencil enabled
    464 
    465   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    466       .Times(1)
    467       .RetiresOnSaturation();
    468   DrawArrays draw_cmd;
    469   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    470   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    471   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    472 
    473   EXPECT_CALL(*gl_, GetError())
    474       .WillOnce(Return(GL_NO_ERROR))
    475       .WillOnce(Return(GL_NO_ERROR))
    476       .RetiresOnSaturation();
    477   typedef GetIntegerv::Result Result;
    478   Result* result = static_cast<Result*>(shared_memory_address_);
    479   EXPECT_CALL(*gl_, GetIntegerv(GL_DEPTH_TEST, _))
    480       .Times(0)
    481       .RetiresOnSaturation();
    482   result->size = 0;
    483   GetIntegerv cmd2;
    484   cmd2.Init(GL_DEPTH_TEST, shared_memory_id_, shared_memory_offset_);
    485   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    486   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_DEPTH_TEST),
    487             result->GetNumResults());
    488   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    489   EXPECT_EQ(1, result->GetData()[0]);
    490 }
    491 
    492 TEST_P(GLES2DecoderManualInitTest, StencilEnableWithStencil) {
    493   InitState init;
    494   init.gl_version = "3.0";
    495   init.has_stencil = true;
    496   init.request_stencil = true;
    497   init.bind_generates_resource = true;
    498   InitDecoder(init);
    499 
    500   Enable cmd;
    501   cmd.Init(GL_STENCIL_TEST);
    502   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    503   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    504 
    505   SetupDefaultProgram();
    506   SetupTexture();
    507   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    508   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    509                                          false,   // Framebuffer has depth
    510                                          true,    // Framebuffer has stencil
    511                                          0x1110,  // color bits
    512                                          false,   // depth mask
    513                                          false,   // depth enabled
    514                                          -1,      // front stencil mask
    515                                          -1,      // back stencil mask
    516                                          true);   // stencil enabled
    517 
    518   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    519       .Times(1)
    520       .RetiresOnSaturation();
    521   DrawArrays draw_cmd;
    522   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    523   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    524   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    525 
    526   EXPECT_CALL(*gl_, GetError())
    527       .WillOnce(Return(GL_NO_ERROR))
    528       .WillOnce(Return(GL_NO_ERROR))
    529       .RetiresOnSaturation();
    530   typedef GetIntegerv::Result Result;
    531   Result* result = static_cast<Result*>(shared_memory_address_);
    532   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_TEST, _))
    533       .Times(0)
    534       .RetiresOnSaturation();
    535   result->size = 0;
    536   GetIntegerv cmd2;
    537   cmd2.Init(GL_STENCIL_TEST, shared_memory_id_, shared_memory_offset_);
    538   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    539   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_TEST),
    540             result->GetNumResults());
    541   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    542   EXPECT_EQ(1, result->GetData()[0]);
    543 }
    544 
    545 TEST_P(GLES2DecoderManualInitTest, StencilEnableWithoutRequestedStencil) {
    546   InitState init;
    547   init.gl_version = "3.0";
    548   init.has_stencil = true;
    549   init.bind_generates_resource = true;
    550   InitDecoder(init);
    551 
    552   Enable cmd;
    553   cmd.Init(GL_STENCIL_TEST);
    554   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    555   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    556 
    557   SetupDefaultProgram();
    558   SetupTexture();
    559   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    560   SetupExpectationsForApplyingDirtyState(true,    // Framebuffer is RGB
    561                                          false,   // Framebuffer has depth
    562                                          false,   // Framebuffer has stencil
    563                                          0x1110,  // color bits
    564                                          false,   // depth mask
    565                                          false,   // depth enabled
    566                                          0,       // front stencil mask
    567                                          0,       // back stencil mask
    568                                          false);  // stencil enabled
    569 
    570   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    571       .Times(1)
    572       .RetiresOnSaturation();
    573   DrawArrays draw_cmd;
    574   draw_cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    575   EXPECT_EQ(error::kNoError, ExecuteCmd(draw_cmd));
    576   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    577 
    578   EXPECT_CALL(*gl_, GetError())
    579       .WillOnce(Return(GL_NO_ERROR))
    580       .WillOnce(Return(GL_NO_ERROR))
    581       .RetiresOnSaturation();
    582   typedef GetIntegerv::Result Result;
    583   Result* result = static_cast<Result*>(shared_memory_address_);
    584   EXPECT_CALL(*gl_, GetIntegerv(GL_STENCIL_TEST, _))
    585       .Times(0)
    586       .RetiresOnSaturation();
    587   result->size = 0;
    588   GetIntegerv cmd2;
    589   cmd2.Init(GL_STENCIL_TEST, shared_memory_id_, shared_memory_offset_);
    590   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd2));
    591   EXPECT_EQ(decoder_->GetGLES2Util()->GLGetNumValuesReturned(GL_STENCIL_TEST),
    592             result->GetNumResults());
    593   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    594   EXPECT_EQ(1, result->GetData()[0]);
    595 }
    596 
    597 TEST_P(GLES2DecoderManualInitTest, CachedColorMask) {
    598   InitState init;
    599   init.gl_version = "3.0";
    600   init.has_alpha = true;
    601   init.has_depth = true;
    602   init.has_stencil = true;
    603   init.request_alpha = true;
    604   init.request_depth = true;
    605   init.request_stencil = true;
    606   init.bind_generates_resource = true;
    607   InitDecoder(init);
    608 
    609   SetupDefaultProgram();
    610   SetupAllNeededVertexBuffers();
    611   SetupTexture();
    612 
    613   // Test all color_bits combinations twice.
    614   for (int i = 0; i < 32; i++) {
    615     GLuint color_bits = (i & 1 ? 0x0001 : 0x0000) | (i & 2 ? 0x0010 : 0x0000) |
    616                         (i & 4 ? 0x0100 : 0x0000) | (i & 8 ? 0x1000 : 0x0000);
    617 
    618     // Toggle depth_test to force ApplyDirtyState each time.
    619     DirtyStateMaskTest(color_bits, false, 0xffffffff, 0xffffffff);
    620     DirtyStateMaskTest(color_bits, true, 0xffffffff, 0xffffffff);
    621     DirtyStateMaskTest(color_bits, false, 0xffffffff, 0xffffffff);
    622   }
    623 }
    624 
    625 TEST_P(GLES2DecoderManualInitTest, CachedDepthMask) {
    626   InitState init;
    627   init.gl_version = "3.0";
    628   init.has_alpha = true;
    629   init.has_depth = true;
    630   init.has_stencil = true;
    631   init.request_alpha = true;
    632   init.request_depth = true;
    633   init.request_stencil = true;
    634   init.bind_generates_resource = true;
    635   InitDecoder(init);
    636 
    637   SetupDefaultProgram();
    638   SetupAllNeededVertexBuffers();
    639   SetupTexture();
    640 
    641   // Test all depth_mask combinations twice.
    642   for (int i = 0; i < 4; i++) {
    643     bool depth_mask = (i & 1) == 1;
    644 
    645     // Toggle color masks to force ApplyDirtyState each time.
    646     DirtyStateMaskTest(0x1010, depth_mask, 0xffffffff, 0xffffffff);
    647     DirtyStateMaskTest(0x0101, depth_mask, 0xffffffff, 0xffffffff);
    648     DirtyStateMaskTest(0x1010, depth_mask, 0xffffffff, 0xffffffff);
    649   }
    650 }
    651 
    652 TEST_P(GLES2DecoderManualInitTest, CachedStencilMask) {
    653   InitState init;
    654   init.gl_version = "3.0";
    655   init.has_alpha = true;
    656   init.has_depth = true;
    657   init.has_stencil = true;
    658   init.request_alpha = true;
    659   init.request_depth = true;
    660   init.request_stencil = true;
    661   init.bind_generates_resource = true;
    662   InitDecoder(init);
    663 
    664   SetupDefaultProgram();
    665   SetupAllNeededVertexBuffers();
    666   SetupTexture();
    667 
    668   // Test all stencil_mask combinations twice.
    669   for (int i = 0; i < 4; i++) {
    670     GLuint stencil_mask = (i & 1) ? 0xf0f0f0f0 : 0x0f0f0f0f;
    671 
    672     // Toggle color masks to force ApplyDirtyState each time.
    673     DirtyStateMaskTest(0x1010, true, stencil_mask, 0xffffffff);
    674     DirtyStateMaskTest(0x0101, true, stencil_mask, 0xffffffff);
    675     DirtyStateMaskTest(0x1010, true, stencil_mask, 0xffffffff);
    676   }
    677 
    678   for (int i = 0; i < 4; i++) {
    679     GLuint stencil_mask = (i & 1) ? 0xf0f0f0f0 : 0x0f0f0f0f;
    680 
    681     // Toggle color masks to force ApplyDirtyState each time.
    682     DirtyStateMaskTest(0x1010, true, 0xffffffff, stencil_mask);
    683     DirtyStateMaskTest(0x0101, true, 0xffffffff, stencil_mask);
    684     DirtyStateMaskTest(0x1010, true, 0xffffffff, stencil_mask);
    685   }
    686 }
    687 
    688 TEST_P(GLES2DecoderWithShaderTest, DrawArraysNoAttributesSucceeds) {
    689   SetupTexture();
    690   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    691   SetupExpectationsForApplyingDefaultDirtyState();
    692 
    693   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    694       .Times(1)
    695       .RetiresOnSaturation();
    696   DrawArrays cmd;
    697   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    698   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    699   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    700 }
    701 
    702 // Tests when the math overflows (0x40000000 * sizeof GLfloat)
    703 TEST_P(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0OverflowFails) {
    704   const GLsizei kLargeCount = 0x40000000;
    705   SetupTexture();
    706   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0).RetiresOnSaturation();
    707   DrawArrays cmd;
    708   cmd.Init(GL_TRIANGLES, 0, kLargeCount);
    709   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    710   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    711   EXPECT_FALSE(GetDecoder()->WasContextLost());
    712 }
    713 
    714 // Tests when the math overflows (0x7FFFFFFF + 1 = 0x8000000 verts)
    715 TEST_P(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0PosToNegFails) {
    716   const GLsizei kLargeCount = 0x7FFFFFFF;
    717   SetupTexture();
    718   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0).RetiresOnSaturation();
    719   DrawArrays cmd;
    720   cmd.Init(GL_TRIANGLES, 0, kLargeCount);
    721   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    722   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    723   EXPECT_FALSE(GetDecoder()->WasContextLost());
    724 }
    725 
    726 // Tests when the driver returns an error
    727 TEST_P(GLES2DecoderWithShaderTest, DrawArraysSimulatedAttrib0OOMFails) {
    728   const GLsizei kFakeLargeCount = 0x1234;
    729   SetupTexture();
    730   AddExpectationsForSimulatedAttrib0WithError(
    731       kFakeLargeCount, 0, GL_OUT_OF_MEMORY);
    732   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0).RetiresOnSaturation();
    733   DrawArrays cmd;
    734   cmd.Init(GL_TRIANGLES, 0, kFakeLargeCount);
    735   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    736   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    737   EXPECT_FALSE(GetDecoder()->WasContextLost());
    738 }
    739 
    740 // Test that we lose context.
    741 TEST_P(GLES2DecoderManualInitTest, LoseContextWhenOOM) {
    742   InitState init;
    743   init.gl_version = "3.0";
    744   init.has_alpha = true;
    745   init.has_depth = true;
    746   init.request_alpha = true;
    747   init.request_depth = true;
    748   init.bind_generates_resource = true;
    749   init.lose_context_when_out_of_memory = true;
    750   InitDecoder(init);
    751   SetupDefaultProgram();
    752 
    753   const GLsizei kFakeLargeCount = 0x1234;
    754   SetupTexture();
    755   AddExpectationsForSimulatedAttrib0WithError(
    756       kFakeLargeCount, 0, GL_OUT_OF_MEMORY);
    757   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0).RetiresOnSaturation();
    758   // Other contexts in the group should be lost also.
    759   EXPECT_CALL(*mock_decoder_, LoseContext(GL_UNKNOWN_CONTEXT_RESET_ARB))
    760       .Times(1)
    761       .RetiresOnSaturation();
    762   DrawArrays cmd;
    763   cmd.Init(GL_TRIANGLES, 0, kFakeLargeCount);
    764   // This context should be lost.
    765   EXPECT_EQ(error::kLostContext, ExecuteCmd(cmd));
    766   EXPECT_EQ(GL_OUT_OF_MEMORY, GetGLError());
    767   EXPECT_TRUE(decoder_->WasContextLost());
    768 }
    769 
    770 TEST_P(GLES2DecoderWithShaderTest, DrawArraysBadTextureUsesBlack) {
    771   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
    772   // This is an NPOT texture. As the default filtering requires mips
    773   // this should trigger replacing with black textures before rendering.
    774   DoTexImage2D(GL_TEXTURE_2D,
    775                0,
    776                GL_RGBA,
    777                3,
    778                1,
    779                0,
    780                GL_RGBA,
    781                GL_UNSIGNED_BYTE,
    782                kSharedMemoryId,
    783                kSharedMemoryOffset);
    784   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    785   {
    786     InSequence sequence;
    787     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    788         .Times(1)
    789         .RetiresOnSaturation();
    790     EXPECT_CALL(
    791         *gl_, BindTexture(GL_TEXTURE_2D, TestHelper::kServiceBlackTexture2dId))
    792         .Times(1)
    793         .RetiresOnSaturation();
    794     EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    795         .Times(1)
    796         .RetiresOnSaturation();
    797     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    798         .Times(1)
    799         .RetiresOnSaturation();
    800     EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kServiceTextureId))
    801         .Times(1)
    802         .RetiresOnSaturation();
    803     EXPECT_CALL(*gl_, ActiveTexture(GL_TEXTURE0))
    804         .Times(1)
    805         .RetiresOnSaturation();
    806   }
    807   SetupExpectationsForApplyingDefaultDirtyState();
    808   DrawArrays cmd;
    809   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    810   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    811   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    812 }
    813 
    814 TEST_P(GLES2DecoderWithShaderTest, DrawArraysMissingAttributesFails) {
    815   DoEnableVertexAttribArray(1);
    816 
    817   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    818   DrawArrays cmd;
    819   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    820   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    821   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    822 }
    823 
    824 TEST_P(GLES2DecoderWithShaderTest,
    825        DrawArraysMissingAttributesZeroCountSucceeds) {
    826   DoEnableVertexAttribArray(1);
    827 
    828   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    829   DrawArrays cmd;
    830   cmd.Init(GL_TRIANGLES, 0, 0);
    831   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    832   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    833 }
    834 
    835 TEST_P(GLES2DecoderWithShaderTest, DrawArraysValidAttributesSucceeds) {
    836   SetupTexture();
    837   SetupVertexBuffer();
    838   DoEnableVertexAttribArray(1);
    839   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    840   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
    841   SetupExpectationsForApplyingDefaultDirtyState();
    842 
    843   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    844       .Times(1)
    845       .RetiresOnSaturation();
    846   DrawArrays cmd;
    847   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    848   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    849   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    850 }
    851 
    852 // Same as DrawArraysValidAttributesSucceeds, but with workaround
    853 // |init_vertex_attributes|.
    854 TEST_P(GLES2DecoderManualInitTest, InitVertexAttributes) {
    855   CommandLine command_line(0, NULL);
    856   command_line.AppendSwitchASCII(
    857       switches::kGpuDriverBugWorkarounds,
    858       base::IntToString(gpu::INIT_VERTEX_ATTRIBUTES));
    859   InitState init;
    860   init.gl_version = "3.0";
    861   init.has_alpha = true;
    862   init.has_depth = true;
    863   init.request_alpha = true;
    864   init.request_depth = true;
    865   init.bind_generates_resource = true;
    866   InitDecoderWithCommandLine(init, &command_line);
    867   SetupDefaultProgram();
    868   SetupTexture();
    869   SetupVertexBuffer();
    870   DoEnableVertexAttribArray(1);
    871   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    872   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
    873   SetupExpectationsForApplyingDefaultDirtyState();
    874 
    875   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
    876       .Times(1)
    877       .RetiresOnSaturation();
    878   DrawArrays cmd;
    879   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    880   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    881   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    882 }
    883 
    884 TEST_P(GLES2DecoderWithShaderTest, DrawArraysDeletedBufferFails) {
    885   SetupVertexBuffer();
    886   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    887   DeleteVertexBuffer();
    888 
    889   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    890   DrawArrays cmd;
    891   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    892   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    893   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    894 }
    895 
    896 TEST_P(GLES2DecoderWithShaderTest, DrawArraysDeletedProgramSucceeds) {
    897   SetupTexture();
    898   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
    899   SetupExpectationsForApplyingDefaultDirtyState();
    900   DoDeleteProgram(client_program_id_, kServiceProgramId);
    901 
    902   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(1).RetiresOnSaturation();
    903   EXPECT_CALL(*gl_, DeleteProgram(kServiceProgramId)).Times(1);
    904   DrawArrays cmd;
    905   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    906   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    907   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    908 }
    909 
    910 TEST_P(GLES2DecoderWithShaderTest, DrawArraysWithInvalidModeFails) {
    911   SetupVertexBuffer();
    912   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    913 
    914   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    915   DrawArrays cmd;
    916   cmd.Init(GL_QUADS, 0, 1);
    917   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    918   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    919   cmd.Init(GL_POLYGON, 0, 1);
    920   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    921   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
    922 }
    923 
    924 TEST_P(GLES2DecoderWithShaderTest, DrawArraysInvalidCountFails) {
    925   SetupVertexBuffer();
    926   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    927 
    928   // Try start > 0
    929   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0);
    930   DrawArrays cmd;
    931   cmd.Init(GL_TRIANGLES, 1, kNumVertices);
    932   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    933   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    934   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    935 
    936   // Try with count > size
    937   cmd.Init(GL_TRIANGLES, 0, kNumVertices + 1);
    938   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    939   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    940   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    941 
    942   // Try with attrib offset > 0
    943   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
    944   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 4);
    945   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    946   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    947   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    948 
    949   // Try with size > 2 (ie, vec3 instead of vec2)
    950   DoVertexAttribPointer(1, 3, GL_FLOAT, 0, 0);
    951   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    952   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    953   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    954 
    955   // Try with stride > 8 (vec2 + vec2 byte)
    956   DoVertexAttribPointer(1, 2, GL_FLOAT, sizeof(GLfloat) * 3, 0);
    957   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    958   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    959   EXPECT_EQ(GL_NO_ERROR, GetGLError());
    960 }
    961 
    962 TEST_P(GLES2DecoderWithShaderTest, DrawArraysInstancedANGLEFails) {
    963   SetupTexture();
    964   SetupVertexBuffer();
    965   DoEnableVertexAttribArray(1);
    966   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    967 
    968   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    969       .Times(0)
    970       .RetiresOnSaturation();
    971   DrawArraysInstancedANGLE cmd;
    972   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
    973   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    974   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    975 }
    976 
    977 TEST_P(GLES2DecoderWithShaderTest, VertexAttribDivisorANGLEFails) {
    978   SetupTexture();
    979   SetupVertexBuffer();
    980   DoEnableVertexAttribArray(1);
    981   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
    982 
    983   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(_, _))
    984       .Times(0)
    985       .RetiresOnSaturation();
    986 
    987   VertexAttribDivisorANGLE cmd;
    988   cmd.Init(0, 1);
    989   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
    990   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
    991 }
    992 
    993 TEST_P(GLES2DecoderGeometryInstancingTest,
    994        DrawArraysInstancedANGLENoAttributesFails) {
    995   SetupTexture();
    996 
    997   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
    998       .Times(0)
    999       .RetiresOnSaturation();
   1000   DrawArraysInstancedANGLE cmd;
   1001   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
   1002   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1003   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1004 }
   1005 
   1006 TEST_P(GLES2DecoderGeometryInstancingTest,
   1007        DrawArraysInstancedANGLESimulatedAttrib0) {
   1008   SetupTexture();
   1009   SetupVertexBuffer();
   1010   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1011 
   1012   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
   1013   SetupExpectationsForApplyingDefaultDirtyState();
   1014 
   1015   DoVertexAttribDivisorANGLE(0, 1);
   1016   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, 3))
   1017       .Times(1)
   1018       .RetiresOnSaturation();
   1019   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 0))
   1020       .Times(1)
   1021       .RetiresOnSaturation();
   1022   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 1))
   1023       .Times(1)
   1024       .RetiresOnSaturation();
   1025   DrawArraysInstancedANGLE cmd;
   1026   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 3);
   1027   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1028   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1029 }
   1030 
   1031 TEST_P(GLES2DecoderGeometryInstancingTest,
   1032        DrawArraysInstancedANGLEMissingAttributesFails) {
   1033   DoEnableVertexAttribArray(1);
   1034 
   1035   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _)).Times(0);
   1036   DrawArraysInstancedANGLE cmd;
   1037   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
   1038   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1039   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1040 }
   1041 
   1042 TEST_P(GLES2DecoderGeometryInstancingTest,
   1043        DrawArraysInstancedANGLEMissingAttributesZeroCountSucceeds) {
   1044   DoEnableVertexAttribArray(1);
   1045 
   1046   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _)).Times(0);
   1047   DrawArraysInstancedANGLE cmd;
   1048   cmd.Init(GL_TRIANGLES, 0, 0, 1);
   1049   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1050   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1051 }
   1052 
   1053 TEST_P(GLES2DecoderGeometryInstancingTest,
   1054        DrawArraysInstancedANGLEValidAttributesSucceeds) {
   1055   SetupTexture();
   1056   SetupVertexBuffer();
   1057   DoEnableVertexAttribArray(1);
   1058   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1059   AddExpectationsForSimulatedAttrib0(kNumVertices, kServiceBufferId);
   1060   SetupExpectationsForApplyingDefaultDirtyState();
   1061 
   1062   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, 1))
   1063       .Times(1)
   1064       .RetiresOnSaturation();
   1065   DrawArraysInstancedANGLE cmd;
   1066   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
   1067   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1068   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1069 }
   1070 
   1071 TEST_P(GLES2DecoderGeometryInstancingTest,
   1072        DrawArraysInstancedANGLEWithInvalidModeFails) {
   1073   SetupVertexBuffer();
   1074   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1075 
   1076   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _)).Times(0);
   1077   DrawArraysInstancedANGLE cmd;
   1078   cmd.Init(GL_QUADS, 0, 1, 1);
   1079   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1080   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1081   cmd.Init(GL_POLYGON, 0, 1, 1);
   1082   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1083   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1084 }
   1085 
   1086 TEST_P(GLES2DecoderGeometryInstancingTest,
   1087        DrawArraysInstancedANGLEInvalidPrimcountFails) {
   1088   SetupVertexBuffer();
   1089   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1090 
   1091   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _)).Times(0);
   1092   DrawArraysInstancedANGLE cmd;
   1093   cmd.Init(GL_TRIANGLES, 0, 1, -1);
   1094   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1095   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1096 }
   1097 
   1098 // Per-instance data is twice as large, but number of instances is half
   1099 TEST_P(GLES2DecoderGeometryInstancingTest,
   1100        DrawArraysInstancedANGLELargeInstanceSucceeds) {
   1101   SetupTexture();
   1102   SetupVertexBuffer();
   1103   SetupExpectationsForApplyingDefaultDirtyState();
   1104   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1105 
   1106   DoEnableVertexAttribArray(0);
   1107   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
   1108   DoVertexAttribDivisorANGLE(0, 1);
   1109   EXPECT_CALL(
   1110       *gl_,
   1111       DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, kNumVertices / 2))
   1112       .Times(1)
   1113       .RetiresOnSaturation();
   1114   DrawArraysInstancedANGLE cmd;
   1115   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices / 2);
   1116   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1117   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1118 }
   1119 
   1120 // Per-instance data is twice as large, but divisor is twice
   1121 TEST_P(GLES2DecoderGeometryInstancingTest,
   1122        DrawArraysInstancedANGLELargeDivisorSucceeds) {
   1123   SetupTexture();
   1124   SetupVertexBuffer();
   1125   SetupExpectationsForApplyingDefaultDirtyState();
   1126   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1127 
   1128   DoEnableVertexAttribArray(0);
   1129   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
   1130   DoVertexAttribDivisorANGLE(0, 2);
   1131   EXPECT_CALL(
   1132       *gl_,
   1133       DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices, kNumVertices))
   1134       .Times(1)
   1135       .RetiresOnSaturation();
   1136   DrawArraysInstancedANGLE cmd;
   1137   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices);
   1138   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1139   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1140 }
   1141 
   1142 TEST_P(GLES2DecoderGeometryInstancingTest, DrawArraysInstancedANGLELargeFails) {
   1143   SetupTexture();
   1144   SetupVertexBuffer();
   1145   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1146 
   1147   DoEnableVertexAttribArray(0);
   1148   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1149   DoVertexAttribDivisorANGLE(0, 1);
   1150   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
   1151       .Times(0)
   1152       .RetiresOnSaturation();
   1153   DrawArraysInstancedANGLE cmd;
   1154   cmd.Init(GL_TRIANGLES, 0, kNumVertices, kNumVertices + 1);
   1155   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1156   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1157   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1158 
   1159   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
   1160       .Times(0)
   1161       .RetiresOnSaturation();
   1162   cmd.Init(GL_TRIANGLES, 0, kNumVertices + 1, kNumVertices);
   1163   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1164   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1165   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1166 }
   1167 
   1168 // Per-index data is twice as large, but number of indices is half
   1169 TEST_P(GLES2DecoderGeometryInstancingTest,
   1170        DrawArraysInstancedANGLELargeIndexSucceeds) {
   1171   SetupTexture();
   1172   SetupVertexBuffer();
   1173   SetupExpectationsForApplyingDefaultDirtyState();
   1174   DoVertexAttribPointer(1, 4, GL_FLOAT, 0, 0);
   1175 
   1176   DoEnableVertexAttribArray(0);
   1177   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1178   DoVertexAttribDivisorANGLE(0, 1);
   1179   EXPECT_CALL(
   1180       *gl_,
   1181       DrawArraysInstancedANGLE(GL_TRIANGLES, 0, kNumVertices / 2, kNumVertices))
   1182       .Times(1)
   1183       .RetiresOnSaturation();
   1184   DrawArraysInstancedANGLE cmd;
   1185   cmd.Init(GL_TRIANGLES, 0, kNumVertices / 2, kNumVertices);
   1186   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1187   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1188 }
   1189 
   1190 TEST_P(GLES2DecoderGeometryInstancingTest,
   1191        DrawArraysInstancedANGLENoDivisor0Fails) {
   1192   SetupTexture();
   1193   SetupVertexBuffer();
   1194   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1195 
   1196   DoEnableVertexAttribArray(0);
   1197   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1198   DoVertexAttribDivisorANGLE(0, 1);
   1199   DoVertexAttribDivisorANGLE(1, 1);
   1200   EXPECT_CALL(*gl_, DrawArraysInstancedANGLE(_, _, _, _))
   1201       .Times(0)
   1202       .RetiresOnSaturation();
   1203   DrawArraysInstancedANGLE cmd;
   1204   cmd.Init(GL_TRIANGLES, 0, kNumVertices, 1);
   1205   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1206   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1207   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1208 }
   1209 
   1210 TEST_P(GLES2DecoderWithShaderTest, DrawElementsNoAttributesSucceeds) {
   1211   SetupTexture();
   1212   SetupIndexBuffer();
   1213   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, 0);
   1214   SetupExpectationsForApplyingDefaultDirtyState();
   1215   EXPECT_CALL(*gl_,
   1216               DrawElements(GL_TRIANGLES,
   1217                            kValidIndexRangeCount,
   1218                            GL_UNSIGNED_SHORT,
   1219                            BufferOffset(kValidIndexRangeStart * 2)))
   1220       .Times(1)
   1221       .RetiresOnSaturation();
   1222   DrawElements cmd;
   1223   cmd.Init(GL_TRIANGLES,
   1224            kValidIndexRangeCount,
   1225            GL_UNSIGNED_SHORT,
   1226            kValidIndexRangeStart * 2);
   1227   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1228   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1229 }
   1230 
   1231 TEST_P(GLES2DecoderWithShaderTest, DrawElementsMissingAttributesFails) {
   1232   SetupIndexBuffer();
   1233   DoEnableVertexAttribArray(1);
   1234 
   1235   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1236   DrawElements cmd;
   1237   cmd.Init(GL_TRIANGLES,
   1238            kValidIndexRangeCount,
   1239            GL_UNSIGNED_SHORT,
   1240            kValidIndexRangeStart * 2);
   1241   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1242   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1243 }
   1244 
   1245 TEST_P(GLES2DecoderWithShaderTest,
   1246        DrawElementsMissingAttributesZeroCountSucceeds) {
   1247   SetupIndexBuffer();
   1248   DoEnableVertexAttribArray(1);
   1249 
   1250   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1251   DrawElements cmd;
   1252   cmd.Init(GL_TRIANGLES, 0, GL_UNSIGNED_SHORT, kValidIndexRangeStart * 2);
   1253   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1254   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1255 }
   1256 
   1257 TEST_P(GLES2DecoderWithShaderTest, DrawElementsExtraAttributesFails) {
   1258   SetupIndexBuffer();
   1259   DoEnableVertexAttribArray(6);
   1260 
   1261   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1262   DrawElements cmd;
   1263   cmd.Init(GL_TRIANGLES,
   1264            kValidIndexRangeCount,
   1265            GL_UNSIGNED_SHORT,
   1266            kValidIndexRangeStart * 2);
   1267   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1268   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1269 }
   1270 
   1271 TEST_P(GLES2DecoderWithShaderTest, DrawElementsValidAttributesSucceeds) {
   1272   SetupTexture();
   1273   SetupVertexBuffer();
   1274   SetupIndexBuffer();
   1275   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1276   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
   1277   SetupExpectationsForApplyingDefaultDirtyState();
   1278 
   1279   EXPECT_CALL(*gl_,
   1280               DrawElements(GL_TRIANGLES,
   1281                            kValidIndexRangeCount,
   1282                            GL_UNSIGNED_SHORT,
   1283                            BufferOffset(kValidIndexRangeStart * 2)))
   1284       .Times(1)
   1285       .RetiresOnSaturation();
   1286   DrawElements cmd;
   1287   cmd.Init(GL_TRIANGLES,
   1288            kValidIndexRangeCount,
   1289            GL_UNSIGNED_SHORT,
   1290            kValidIndexRangeStart * 2);
   1291   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1292   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1293 }
   1294 
   1295 TEST_P(GLES2DecoderWithShaderTest, DrawElementsDeletedBufferFails) {
   1296   SetupVertexBuffer();
   1297   SetupIndexBuffer();
   1298   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1299   DeleteIndexBuffer();
   1300 
   1301   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1302   DrawElements cmd;
   1303   cmd.Init(GL_TRIANGLES,
   1304            kValidIndexRangeCount,
   1305            GL_UNSIGNED_SHORT,
   1306            kValidIndexRangeStart * 2);
   1307   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1308   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1309 }
   1310 
   1311 TEST_P(GLES2DecoderWithShaderTest, DrawElementsDeletedProgramSucceeds) {
   1312   SetupTexture();
   1313   SetupIndexBuffer();
   1314   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, 0);
   1315   SetupExpectationsForApplyingDefaultDirtyState();
   1316   DoDeleteProgram(client_program_id_, kServiceProgramId);
   1317 
   1318   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(1);
   1319   EXPECT_CALL(*gl_, DeleteProgram(kServiceProgramId)).Times(1);
   1320   DrawElements cmd;
   1321   cmd.Init(GL_TRIANGLES,
   1322            kValidIndexRangeCount,
   1323            GL_UNSIGNED_SHORT,
   1324            kValidIndexRangeStart * 2);
   1325   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1326   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1327 }
   1328 
   1329 TEST_P(GLES2DecoderWithShaderTest, DrawElementsWithInvalidModeFails) {
   1330   SetupVertexBuffer();
   1331   SetupIndexBuffer();
   1332   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1333 
   1334   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1335   DrawElements cmd;
   1336   cmd.Init(GL_QUADS,
   1337            kValidIndexRangeCount,
   1338            GL_UNSIGNED_SHORT,
   1339            kValidIndexRangeStart * 2);
   1340   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1341   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1342   cmd.Init(GL_POLYGON,
   1343            kValidIndexRangeCount,
   1344            GL_UNSIGNED_SHORT,
   1345            kValidIndexRangeStart);
   1346   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1347   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1348 }
   1349 
   1350 TEST_P(GLES2DecoderWithShaderTest, DrawElementsInvalidCountFails) {
   1351   SetupVertexBuffer();
   1352   SetupIndexBuffer();
   1353   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1354 
   1355   // Try start > 0
   1356   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1357   DrawElements cmd;
   1358   cmd.Init(GL_TRIANGLES, kNumIndices, GL_UNSIGNED_SHORT, 2);
   1359   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1360   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1361   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1362 
   1363   // Try with count > size
   1364   cmd.Init(GL_TRIANGLES, kNumIndices + 1, GL_UNSIGNED_SHORT, 0);
   1365   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1366   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1367   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1368 }
   1369 
   1370 TEST_P(GLES2DecoderWithShaderTest, DrawElementsOutOfRangeIndicesFails) {
   1371   SetupVertexBuffer();
   1372   SetupIndexBuffer();
   1373   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1374 
   1375   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1376   DrawElements cmd;
   1377   cmd.Init(GL_TRIANGLES,
   1378            kInvalidIndexRangeCount,
   1379            GL_UNSIGNED_SHORT,
   1380            kInvalidIndexRangeStart * 2);
   1381   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1382   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1383   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1384 }
   1385 
   1386 TEST_P(GLES2DecoderWithShaderTest, DrawElementsOddOffsetForUint16Fails) {
   1387   SetupVertexBuffer();
   1388   SetupIndexBuffer();
   1389   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1390 
   1391   EXPECT_CALL(*gl_, DrawElements(_, _, _, _)).Times(0);
   1392   DrawElements cmd;
   1393   cmd.Init(GL_TRIANGLES, kInvalidIndexRangeCount, GL_UNSIGNED_SHORT, 1);
   1394   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1395   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1396   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1397 }
   1398 
   1399 TEST_P(GLES2DecoderWithShaderTest, DrawElementsInstancedANGLEFails) {
   1400   SetupTexture();
   1401   SetupVertexBuffer();
   1402   SetupIndexBuffer();
   1403   DoEnableVertexAttribArray(1);
   1404   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1405 
   1406   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1407       .Times(0)
   1408       .RetiresOnSaturation();
   1409   DrawElementsInstancedANGLE cmd;
   1410   cmd.Init(GL_TRIANGLES,
   1411            kValidIndexRangeCount,
   1412            GL_UNSIGNED_SHORT,
   1413            kValidIndexRangeStart * 2,
   1414            1);
   1415   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1416   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1417 }
   1418 
   1419 TEST_P(GLES2DecoderGeometryInstancingTest,
   1420        DrawElementsInstancedANGLENoAttributesFails) {
   1421   SetupTexture();
   1422   SetupIndexBuffer();
   1423 
   1424   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1425       .Times(0)
   1426       .RetiresOnSaturation();
   1427   DrawElementsInstancedANGLE cmd;
   1428   cmd.Init(GL_TRIANGLES,
   1429            kValidIndexRangeCount,
   1430            GL_UNSIGNED_SHORT,
   1431            kValidIndexRangeStart * 2,
   1432            1);
   1433   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1434   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1435 }
   1436 
   1437 TEST_P(GLES2DecoderGeometryInstancingTest,
   1438        DrawElementsInstancedANGLESimulatedAttrib0) {
   1439   SetupTexture();
   1440   SetupVertexBuffer();
   1441   SetupIndexBuffer();
   1442   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1443 
   1444   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
   1445   SetupExpectationsForApplyingDefaultDirtyState();
   1446 
   1447   DoVertexAttribDivisorANGLE(0, 1);
   1448   EXPECT_CALL(
   1449       *gl_,
   1450       DrawElementsInstancedANGLE(GL_TRIANGLES,
   1451                                  kValidIndexRangeCount,
   1452                                  GL_UNSIGNED_SHORT,
   1453                                  BufferOffset(kValidIndexRangeStart * 2),
   1454                                  3))
   1455       .Times(1)
   1456       .RetiresOnSaturation();
   1457   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 0))
   1458       .Times(1)
   1459       .RetiresOnSaturation();
   1460   EXPECT_CALL(*gl_, VertexAttribDivisorANGLE(0, 1))
   1461       .Times(1)
   1462       .RetiresOnSaturation();
   1463   DrawElementsInstancedANGLE cmd;
   1464   cmd.Init(GL_TRIANGLES,
   1465            kValidIndexRangeCount,
   1466            GL_UNSIGNED_SHORT,
   1467            kValidIndexRangeStart * 2,
   1468            3);
   1469   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1470   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1471 }
   1472 
   1473 TEST_P(GLES2DecoderGeometryInstancingTest,
   1474        DrawElementsInstancedANGLEMissingAttributesFails) {
   1475   SetupIndexBuffer();
   1476   DoEnableVertexAttribArray(1);
   1477 
   1478   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _)).Times(0);
   1479   DrawElementsInstancedANGLE cmd;
   1480   cmd.Init(GL_TRIANGLES,
   1481            kValidIndexRangeCount,
   1482            GL_UNSIGNED_SHORT,
   1483            kValidIndexRangeStart * 2,
   1484            1);
   1485   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1486   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1487 }
   1488 
   1489 TEST_P(GLES2DecoderGeometryInstancingTest,
   1490        DrawElementsInstancedANGLEMissingAttributesZeroCountSucceeds) {
   1491   SetupIndexBuffer();
   1492   DoEnableVertexAttribArray(1);
   1493 
   1494   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _)).Times(0);
   1495   DrawElementsInstancedANGLE cmd;
   1496   cmd.Init(GL_TRIANGLES, 0, GL_UNSIGNED_SHORT, kValidIndexRangeStart * 2, 1);
   1497   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1498   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1499 }
   1500 
   1501 TEST_P(GLES2DecoderGeometryInstancingTest,
   1502        DrawElementsInstancedANGLEValidAttributesSucceeds) {
   1503   SetupIndexBuffer();
   1504   SetupTexture();
   1505   SetupVertexBuffer();
   1506   DoEnableVertexAttribArray(1);
   1507   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1508   AddExpectationsForSimulatedAttrib0(kMaxValidIndex + 1, kServiceBufferId);
   1509   SetupExpectationsForApplyingDefaultDirtyState();
   1510 
   1511   EXPECT_CALL(
   1512       *gl_,
   1513       DrawElementsInstancedANGLE(GL_TRIANGLES,
   1514                                  kValidIndexRangeCount,
   1515                                  GL_UNSIGNED_SHORT,
   1516                                  BufferOffset(kValidIndexRangeStart * 2),
   1517                                  1))
   1518       .Times(1)
   1519       .RetiresOnSaturation();
   1520   DrawElementsInstancedANGLE cmd;
   1521   cmd.Init(GL_TRIANGLES,
   1522            kValidIndexRangeCount,
   1523            GL_UNSIGNED_SHORT,
   1524            kValidIndexRangeStart * 2,
   1525            1);
   1526   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1527   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1528 }
   1529 
   1530 TEST_P(GLES2DecoderGeometryInstancingTest,
   1531        DrawElementsInstancedANGLEWithInvalidModeFails) {
   1532   SetupIndexBuffer();
   1533   SetupVertexBuffer();
   1534   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1535 
   1536   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _)).Times(0);
   1537   DrawElementsInstancedANGLE cmd;
   1538   cmd.Init(GL_QUADS,
   1539            kValidIndexRangeCount,
   1540            GL_UNSIGNED_SHORT,
   1541            kValidIndexRangeStart * 2,
   1542            1);
   1543   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1544   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1545   cmd.Init(GL_INVALID_ENUM,
   1546            kValidIndexRangeCount,
   1547            GL_UNSIGNED_SHORT,
   1548            kValidIndexRangeStart * 2,
   1549            1);
   1550   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1551   EXPECT_EQ(GL_INVALID_ENUM, GetGLError());
   1552 }
   1553 
   1554 // Per-instance data is twice as large, but number of instances is half
   1555 TEST_P(GLES2DecoderGeometryInstancingTest,
   1556        DrawElementsInstancedANGLELargeInstanceSucceeds) {
   1557   SetupTexture();
   1558   SetupIndexBuffer();
   1559   SetupVertexBuffer();
   1560   SetupExpectationsForApplyingDefaultDirtyState();
   1561   // Add offset so we're sure we're accessing data near the end of the buffer.
   1562   DoVertexAttribPointer(
   1563       1,
   1564       2,
   1565       GL_FLOAT,
   1566       0,
   1567       (kNumVertices - kMaxValidIndex - 1) * 2 * sizeof(GLfloat));
   1568 
   1569   DoEnableVertexAttribArray(0);
   1570   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
   1571   DoVertexAttribDivisorANGLE(0, 1);
   1572   EXPECT_CALL(
   1573       *gl_,
   1574       DrawElementsInstancedANGLE(GL_TRIANGLES,
   1575                                  kValidIndexRangeCount,
   1576                                  GL_UNSIGNED_SHORT,
   1577                                  BufferOffset(kValidIndexRangeStart * 2),
   1578                                  kNumVertices / 2))
   1579       .Times(1)
   1580       .RetiresOnSaturation();
   1581   DrawElementsInstancedANGLE cmd;
   1582   cmd.Init(GL_TRIANGLES,
   1583            kValidIndexRangeCount,
   1584            GL_UNSIGNED_SHORT,
   1585            kValidIndexRangeStart * 2,
   1586            kNumVertices / 2);
   1587   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1588   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1589 }
   1590 
   1591 // Per-instance data is twice as large, but divisor is twice
   1592 TEST_P(GLES2DecoderGeometryInstancingTest,
   1593        DrawElementsInstancedANGLELargeDivisorSucceeds) {
   1594   SetupTexture();
   1595   SetupIndexBuffer();
   1596   SetupVertexBuffer();
   1597   SetupExpectationsForApplyingDefaultDirtyState();
   1598   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1599 
   1600   DoEnableVertexAttribArray(0);
   1601   DoVertexAttribPointer(0, 4, GL_FLOAT, 0, 0);
   1602   DoVertexAttribDivisorANGLE(0, 2);
   1603   EXPECT_CALL(
   1604       *gl_,
   1605       DrawElementsInstancedANGLE(GL_TRIANGLES,
   1606                                  kValidIndexRangeCount,
   1607                                  GL_UNSIGNED_SHORT,
   1608                                  BufferOffset(kValidIndexRangeStart * 2),
   1609                                  kNumVertices))
   1610       .Times(1)
   1611       .RetiresOnSaturation();
   1612   DrawElementsInstancedANGLE cmd;
   1613   cmd.Init(GL_TRIANGLES,
   1614            kValidIndexRangeCount,
   1615            GL_UNSIGNED_SHORT,
   1616            kValidIndexRangeStart * 2,
   1617            kNumVertices);
   1618   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1619   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1620 }
   1621 
   1622 TEST_P(GLES2DecoderGeometryInstancingTest,
   1623        DrawElementsInstancedANGLELargeFails) {
   1624   SetupTexture();
   1625   SetupIndexBuffer();
   1626   SetupVertexBuffer();
   1627   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1628 
   1629   DoEnableVertexAttribArray(0);
   1630   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1631   DoVertexAttribDivisorANGLE(0, 1);
   1632   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1633       .Times(0)
   1634       .RetiresOnSaturation();
   1635   DrawElementsInstancedANGLE cmd;
   1636   cmd.Init(GL_TRIANGLES,
   1637            kValidIndexRangeCount,
   1638            GL_UNSIGNED_SHORT,
   1639            kValidIndexRangeStart * 2,
   1640            kNumVertices + 1);
   1641   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1642   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1643   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1644 
   1645   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1646       .Times(0)
   1647       .RetiresOnSaturation();
   1648   cmd.Init(GL_TRIANGLES,
   1649            kInvalidIndexRangeCount,
   1650            GL_UNSIGNED_SHORT,
   1651            kInvalidIndexRangeStart * 2,
   1652            kNumVertices);
   1653   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1654   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1655   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1656 }
   1657 
   1658 TEST_P(GLES2DecoderGeometryInstancingTest,
   1659        DrawElementsInstancedANGLEInvalidPrimcountFails) {
   1660   SetupTexture();
   1661   SetupIndexBuffer();
   1662   SetupVertexBuffer();
   1663   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1664 
   1665   DoEnableVertexAttribArray(0);
   1666   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1667   DoVertexAttribDivisorANGLE(0, 1);
   1668   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1669       .Times(0)
   1670       .RetiresOnSaturation();
   1671   DrawElementsInstancedANGLE cmd;
   1672   cmd.Init(GL_TRIANGLES,
   1673            kValidIndexRangeCount,
   1674            GL_UNSIGNED_SHORT,
   1675            kValidIndexRangeStart * 2,
   1676            -1);
   1677   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1678   EXPECT_EQ(GL_INVALID_VALUE, GetGLError());
   1679   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1680 }
   1681 
   1682 // Per-index data is twice as large, but values of indices are smaller
   1683 TEST_P(GLES2DecoderGeometryInstancingTest,
   1684        DrawElementsInstancedANGLELargeIndexSucceeds) {
   1685   SetupTexture();
   1686   SetupIndexBuffer();
   1687   SetupVertexBuffer();
   1688   SetupExpectationsForApplyingDefaultDirtyState();
   1689   DoVertexAttribPointer(1, 4, GL_FLOAT, 0, 0);
   1690 
   1691   DoEnableVertexAttribArray(0);
   1692   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1693   DoVertexAttribDivisorANGLE(0, 1);
   1694   EXPECT_CALL(
   1695       *gl_,
   1696       DrawElementsInstancedANGLE(GL_TRIANGLES,
   1697                                  kValidIndexRangeCount,
   1698                                  GL_UNSIGNED_SHORT,
   1699                                  BufferOffset(kValidIndexRangeStart * 2),
   1700                                  kNumVertices))
   1701       .Times(1)
   1702       .RetiresOnSaturation();
   1703   DrawElementsInstancedANGLE cmd;
   1704   cmd.Init(GL_TRIANGLES,
   1705            kValidIndexRangeCount,
   1706            GL_UNSIGNED_SHORT,
   1707            kValidIndexRangeStart * 2,
   1708            kNumVertices);
   1709   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1710   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1711 }
   1712 
   1713 TEST_P(GLES2DecoderGeometryInstancingTest,
   1714        DrawElementsInstancedANGLENoDivisor0Fails) {
   1715   SetupTexture();
   1716   SetupIndexBuffer();
   1717   SetupVertexBuffer();
   1718   DoVertexAttribPointer(1, 2, GL_FLOAT, 0, 0);
   1719 
   1720   DoEnableVertexAttribArray(0);
   1721   DoVertexAttribPointer(0, 2, GL_FLOAT, 0, 0);
   1722   DoVertexAttribDivisorANGLE(0, 1);
   1723   DoVertexAttribDivisorANGLE(1, 1);
   1724   EXPECT_CALL(*gl_, DrawElementsInstancedANGLE(_, _, _, _, _))
   1725       .Times(0)
   1726       .RetiresOnSaturation();
   1727   DrawElementsInstancedANGLE cmd;
   1728   cmd.Init(GL_TRIANGLES,
   1729            kValidIndexRangeCount,
   1730            GL_UNSIGNED_SHORT,
   1731            kValidIndexRangeStart * 2,
   1732            kNumVertices);
   1733   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1734   EXPECT_EQ(GL_INVALID_OPERATION, GetGLError());
   1735   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1736 }
   1737 
   1738 TEST_P(GLES2DecoderWithShaderTest, DrawArraysClearsAfterTexImage2DNULL) {
   1739   SetupAllNeededVertexBuffers();
   1740   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1741   // Create an uncleared texture with 2 levels.
   1742   DoTexImage2D(
   1743       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1744   DoTexImage2D(
   1745       GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1746   // Expect 2 levels will be cleared.
   1747   SetupClearTextureExpectations(kServiceTextureId,
   1748                                 kServiceTextureId,
   1749                                 GL_TEXTURE_2D,
   1750                                 GL_TEXTURE_2D,
   1751                                 0,
   1752                                 GL_RGBA,
   1753                                 GL_RGBA,
   1754                                 GL_UNSIGNED_BYTE,
   1755                                 2,
   1756                                 2);
   1757   SetupClearTextureExpectations(kServiceTextureId,
   1758                                 kServiceTextureId,
   1759                                 GL_TEXTURE_2D,
   1760                                 GL_TEXTURE_2D,
   1761                                 1,
   1762                                 GL_RGBA,
   1763                                 GL_RGBA,
   1764                                 GL_UNSIGNED_BYTE,
   1765                                 1,
   1766                                 1);
   1767   SetupExpectationsForApplyingDefaultDirtyState();
   1768   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1769       .Times(1)
   1770       .RetiresOnSaturation();
   1771   DrawArrays cmd;
   1772   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   1773   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1774   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1775 
   1776   // But not again
   1777   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1778       .Times(1)
   1779       .RetiresOnSaturation();
   1780   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1781   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1782 }
   1783 
   1784 TEST_P(GLES2DecoderWithShaderTest, DrawElementsClearsAfterTexImage2DNULL) {
   1785   SetupAllNeededVertexBuffers();
   1786   SetupIndexBuffer();
   1787   DoBindTexture(GL_TEXTURE_2D, client_texture_id_, kServiceTextureId);
   1788   // Create an uncleared texture with 2 levels.
   1789   DoTexImage2D(
   1790       GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1791   DoTexImage2D(
   1792       GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1793   // Expect 2 levels will be cleared.
   1794   SetupClearTextureExpectations(kServiceTextureId,
   1795                                 kServiceTextureId,
   1796                                 GL_TEXTURE_2D,
   1797                                 GL_TEXTURE_2D,
   1798                                 0,
   1799                                 GL_RGBA,
   1800                                 GL_RGBA,
   1801                                 GL_UNSIGNED_BYTE,
   1802                                 2,
   1803                                 2);
   1804   SetupClearTextureExpectations(kServiceTextureId,
   1805                                 kServiceTextureId,
   1806                                 GL_TEXTURE_2D,
   1807                                 GL_TEXTURE_2D,
   1808                                 1,
   1809                                 GL_RGBA,
   1810                                 GL_RGBA,
   1811                                 GL_UNSIGNED_BYTE,
   1812                                 1,
   1813                                 1);
   1814   SetupExpectationsForApplyingDefaultDirtyState();
   1815 
   1816   EXPECT_CALL(*gl_,
   1817               DrawElements(GL_TRIANGLES,
   1818                            kValidIndexRangeCount,
   1819                            GL_UNSIGNED_SHORT,
   1820                            BufferOffset(kValidIndexRangeStart * 2)))
   1821       .Times(1)
   1822       .RetiresOnSaturation();
   1823   DrawElements cmd;
   1824   cmd.Init(GL_TRIANGLES,
   1825            kValidIndexRangeCount,
   1826            GL_UNSIGNED_SHORT,
   1827            kValidIndexRangeStart * 2);
   1828   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1829   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1830 
   1831   // But not again
   1832   EXPECT_CALL(*gl_,
   1833               DrawElements(GL_TRIANGLES,
   1834                            kValidIndexRangeCount,
   1835                            GL_UNSIGNED_SHORT,
   1836                            BufferOffset(kValidIndexRangeStart * 2)))
   1837       .Times(1)
   1838       .RetiresOnSaturation();
   1839   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1840   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1841 }
   1842 
   1843 TEST_P(GLES2DecoderWithShaderTest, DrawClearsAfterTexImage2DNULLInFBO) {
   1844   const GLuint kFBOClientTextureId = 4100;
   1845   const GLuint kFBOServiceTextureId = 4101;
   1846 
   1847   SetupAllNeededVertexBuffers();
   1848   // Register a texture id.
   1849   EXPECT_CALL(*gl_, GenTextures(_, _))
   1850       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   1851       .RetiresOnSaturation();
   1852   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   1853 
   1854   // Setup "render to" texture.
   1855   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   1856   DoTexImage2D(
   1857       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1858   DoBindFramebuffer(
   1859       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   1860   DoFramebufferTexture2D(GL_FRAMEBUFFER,
   1861                          GL_COLOR_ATTACHMENT0,
   1862                          GL_TEXTURE_2D,
   1863                          kFBOClientTextureId,
   1864                          kFBOServiceTextureId,
   1865                          0,
   1866                          GL_NO_ERROR);
   1867 
   1868   // Setup "render from" texture.
   1869   SetupTexture();
   1870 
   1871   SetupExpectationsForFramebufferClearing(GL_FRAMEBUFFER,       // target
   1872                                           GL_COLOR_BUFFER_BIT,  // clear bits
   1873                                           0,
   1874                                           0,
   1875                                           0,
   1876                                           0,       // color
   1877                                           0,       // stencil
   1878                                           1.0f,    // depth
   1879                                           false);  // scissor test
   1880 
   1881   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
   1882                                          false,   // Framebuffer has depth
   1883                                          false,   // Framebuffer has stencil
   1884                                          0x1111,  // color bits
   1885                                          false,   // depth mask
   1886                                          false,   // depth enabled
   1887                                          0,       // front stencil mask
   1888                                          0,       // back stencil mask
   1889                                          false);  // stencil enabled
   1890 
   1891   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1892       .Times(1)
   1893       .RetiresOnSaturation();
   1894   DrawArrays cmd;
   1895   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   1896   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1897   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1898 
   1899   // But not again.
   1900   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1901       .Times(1)
   1902       .RetiresOnSaturation();
   1903   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1904   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1905 }
   1906 
   1907 TEST_P(GLES2DecoderWithShaderTest, DrawWitFBOThatCantClearDoesNotDraw) {
   1908   const GLuint kFBOClientTextureId = 4100;
   1909   const GLuint kFBOServiceTextureId = 4101;
   1910 
   1911   // Register a texture id.
   1912   EXPECT_CALL(*gl_, GenTextures(_, _))
   1913       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   1914       .RetiresOnSaturation();
   1915   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   1916 
   1917   // Setup "render to" texture.
   1918   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   1919   DoTexImage2D(
   1920       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   1921   DoBindFramebuffer(
   1922       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   1923   DoFramebufferTexture2D(GL_FRAMEBUFFER,
   1924                          GL_COLOR_ATTACHMENT0,
   1925                          GL_TEXTURE_2D,
   1926                          kFBOClientTextureId,
   1927                          kFBOServiceTextureId,
   1928                          0,
   1929                          GL_NO_ERROR);
   1930 
   1931   // Setup "render from" texture.
   1932   SetupTexture();
   1933 
   1934   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   1935       .WillOnce(Return(GL_FRAMEBUFFER_UNSUPPORTED))
   1936       .RetiresOnSaturation();
   1937   EXPECT_CALL(*gl_, DrawArrays(_, _, _)).Times(0).RetiresOnSaturation();
   1938   DrawArrays cmd;
   1939   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   1940   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1941   EXPECT_EQ(GL_INVALID_FRAMEBUFFER_OPERATION, GetGLError());
   1942 }
   1943 
   1944 TEST_P(GLES2DecoderWithShaderTest, DrawClearsAfterRenderbufferStorageInFBO) {
   1945   SetupTexture();
   1946   DoBindRenderbuffer(
   1947       GL_RENDERBUFFER, client_renderbuffer_id_, kServiceRenderbufferId);
   1948   DoBindFramebuffer(
   1949       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   1950   DoRenderbufferStorage(
   1951       GL_RENDERBUFFER, GL_RGBA4, GL_RGBA, 100, 50, GL_NO_ERROR);
   1952   DoFramebufferRenderbuffer(GL_FRAMEBUFFER,
   1953                             GL_COLOR_ATTACHMENT0,
   1954                             GL_RENDERBUFFER,
   1955                             client_renderbuffer_id_,
   1956                             kServiceRenderbufferId,
   1957                             GL_NO_ERROR);
   1958 
   1959   SetupExpectationsForFramebufferClearing(GL_FRAMEBUFFER,       // target
   1960                                           GL_COLOR_BUFFER_BIT,  // clear bits
   1961                                           0,
   1962                                           0,
   1963                                           0,
   1964                                           0,       // color
   1965                                           0,       // stencil
   1966                                           1.0f,    // depth
   1967                                           false);  // scissor test
   1968 
   1969   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   1970   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
   1971                                          false,   // Framebuffer has depth
   1972                                          false,   // Framebuffer has stencil
   1973                                          0x1111,  // color bits
   1974                                          false,   // depth mask
   1975                                          false,   // depth enabled
   1976                                          0,       // front stencil mask
   1977                                          0,       // back stencil mask
   1978                                          false);  // stencil enabled
   1979 
   1980   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   1981       .Times(1)
   1982       .RetiresOnSaturation();
   1983   DrawArrays cmd;
   1984   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   1985   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   1986   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   1987 }
   1988 
   1989 TEST_P(GLES2DecoderManualInitTest, DrawArraysClearsAfterTexImage2DNULLCubemap) {
   1990   InitState init;
   1991   init.gl_version = "opengl es 2.0";
   1992   init.has_alpha = true;
   1993   init.has_depth = true;
   1994   init.request_alpha = true;
   1995   init.request_depth = true;
   1996   InitDecoder(init);
   1997 
   1998   static const GLenum faces[] = {
   1999       GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
   2000       GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
   2001       GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
   2002   };
   2003   SetupCubemapProgram();
   2004   DoBindTexture(GL_TEXTURE_CUBE_MAP, client_texture_id_, kServiceTextureId);
   2005   // Fill out all the faces for 2 levels, leave 2 uncleared.
   2006   for (int ii = 0; ii < 6; ++ii) {
   2007     GLenum face = faces[ii];
   2008     int32 shm_id =
   2009         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryId;
   2010     uint32 shm_offset =
   2011         (face == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) ? 0 : kSharedMemoryOffset;
   2012     DoTexImage2D(face,
   2013                  0,
   2014                  GL_RGBA,
   2015                  2,
   2016                  2,
   2017                  0,
   2018                  GL_RGBA,
   2019                  GL_UNSIGNED_BYTE,
   2020                  shm_id,
   2021                  shm_offset);
   2022     DoTexImage2D(face,
   2023                  1,
   2024                  GL_RGBA,
   2025                  1,
   2026                  1,
   2027                  0,
   2028                  GL_RGBA,
   2029                  GL_UNSIGNED_BYTE,
   2030                  shm_id,
   2031                  shm_offset);
   2032   }
   2033   // Expect 2 levels will be cleared.
   2034   SetupClearTextureExpectations(kServiceTextureId,
   2035                                 kServiceTextureId,
   2036                                 GL_TEXTURE_CUBE_MAP,
   2037                                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
   2038                                 0,
   2039                                 GL_RGBA,
   2040                                 GL_RGBA,
   2041                                 GL_UNSIGNED_BYTE,
   2042                                 2,
   2043                                 2);
   2044   SetupClearTextureExpectations(kServiceTextureId,
   2045                                 kServiceTextureId,
   2046                                 GL_TEXTURE_CUBE_MAP,
   2047                                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
   2048                                 1,
   2049                                 GL_RGBA,
   2050                                 GL_RGBA,
   2051                                 GL_UNSIGNED_BYTE,
   2052                                 1,
   2053                                 1);
   2054   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   2055   SetupExpectationsForApplyingDefaultDirtyState();
   2056   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   2057       .Times(1)
   2058       .RetiresOnSaturation();
   2059   DrawArrays cmd;
   2060   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   2061   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2062 }
   2063 
   2064 TEST_P(GLES2DecoderWithShaderTest,
   2065        DrawClearsAfterRenderbuffersWithMultipleAttachments) {
   2066   const GLuint kFBOClientTextureId = 4100;
   2067   const GLuint kFBOServiceTextureId = 4101;
   2068 
   2069   // Register a texture id.
   2070   EXPECT_CALL(*gl_, GenTextures(_, _))
   2071       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   2072       .RetiresOnSaturation();
   2073   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   2074 
   2075   // Setup "render to" texture.
   2076   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   2077   DoTexImage2D(
   2078       GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, 0);
   2079   DoBindFramebuffer(
   2080       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   2081   DoFramebufferTexture2D(GL_FRAMEBUFFER,
   2082                          GL_COLOR_ATTACHMENT0,
   2083                          GL_TEXTURE_2D,
   2084                          kFBOClientTextureId,
   2085                          kFBOServiceTextureId,
   2086                          0,
   2087                          GL_NO_ERROR);
   2088 
   2089   DoBindRenderbuffer(
   2090       GL_RENDERBUFFER, client_renderbuffer_id_, kServiceRenderbufferId);
   2091   DoBindFramebuffer(
   2092       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   2093   DoRenderbufferStorage(GL_RENDERBUFFER,
   2094                         GL_DEPTH_COMPONENT16,
   2095                         GL_DEPTH_COMPONENT,
   2096                         1,
   2097                         1,
   2098                         GL_NO_ERROR);
   2099   DoFramebufferRenderbuffer(GL_FRAMEBUFFER,
   2100                             GL_DEPTH_ATTACHMENT,
   2101                             GL_RENDERBUFFER,
   2102                             client_renderbuffer_id_,
   2103                             kServiceRenderbufferId,
   2104                             GL_NO_ERROR);
   2105 
   2106   SetupTexture();
   2107   SetupExpectationsForFramebufferClearing(
   2108       GL_FRAMEBUFFER,                             // target
   2109       GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,  // clear bits
   2110       0,
   2111       0,
   2112       0,
   2113       0,       // color
   2114       0,       // stencil
   2115       1.0f,    // depth
   2116       false);  // scissor test
   2117 
   2118   AddExpectationsForSimulatedAttrib0(kNumVertices, 0);
   2119   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
   2120                                          true,    // Framebuffer has depth
   2121                                          false,   // Framebuffer has stencil
   2122                                          0x1111,  // color bits
   2123                                          true,    // depth mask
   2124                                          false,   // depth enabled
   2125                                          0,       // front stencil mask
   2126                                          0,       // back stencil mask
   2127                                          false);  // stencil enabled
   2128 
   2129   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   2130       .Times(1)
   2131       .RetiresOnSaturation();
   2132   DrawArrays cmd;
   2133   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   2134   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2135   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2136 }
   2137 
   2138 TEST_P(GLES2DecoderWithShaderTest,
   2139        DrawingWithFBOTwiceChecksForFBOCompleteOnce) {
   2140   const GLuint kFBOClientTextureId = 4100;
   2141   const GLuint kFBOServiceTextureId = 4101;
   2142 
   2143   SetupAllNeededVertexBuffers();
   2144 
   2145   // Register a texture id.
   2146   EXPECT_CALL(*gl_, GenTextures(_, _))
   2147       .WillOnce(SetArgumentPointee<1>(kFBOServiceTextureId))
   2148       .RetiresOnSaturation();
   2149   GenHelper<GenTexturesImmediate>(kFBOClientTextureId);
   2150 
   2151   // Setup "render to" texture that is cleared.
   2152   DoBindTexture(GL_TEXTURE_2D, kFBOClientTextureId, kFBOServiceTextureId);
   2153   DoTexImage2D(GL_TEXTURE_2D,
   2154                0,
   2155                GL_RGBA,
   2156                1,
   2157                1,
   2158                0,
   2159                GL_RGBA,
   2160                GL_UNSIGNED_BYTE,
   2161                kSharedMemoryId,
   2162                kSharedMemoryOffset);
   2163   DoBindFramebuffer(
   2164       GL_FRAMEBUFFER, client_framebuffer_id_, kServiceFramebufferId);
   2165   DoFramebufferTexture2D(GL_FRAMEBUFFER,
   2166                          GL_COLOR_ATTACHMENT0,
   2167                          GL_TEXTURE_2D,
   2168                          kFBOClientTextureId,
   2169                          kFBOServiceTextureId,
   2170                          0,
   2171                          GL_NO_ERROR);
   2172 
   2173   // Setup "render from" texture.
   2174   SetupTexture();
   2175 
   2176   // Make sure we check for framebuffer complete.
   2177   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_FRAMEBUFFER))
   2178       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   2179       .RetiresOnSaturation();
   2180 
   2181   SetupExpectationsForApplyingDirtyState(false,   // Framebuffer is RGB
   2182                                          false,   // Framebuffer has depth
   2183                                          false,   // Framebuffer has stencil
   2184                                          0x1111,  // color bits
   2185                                          false,   // depth mask
   2186                                          false,   // depth enabled
   2187                                          0,       // front stencil mask
   2188                                          0,       // back stencil mask
   2189                                          false);  // stencil enabled
   2190 
   2191   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   2192       .Times(1)
   2193       .RetiresOnSaturation();
   2194   DrawArrays cmd;
   2195   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   2196   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2197   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2198 
   2199   // But not again.
   2200   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   2201       .Times(1)
   2202       .RetiresOnSaturation();
   2203   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2204   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2205 }
   2206 
   2207 TEST_P(GLES2DecoderManualInitTest, DrawClearsDepthTexture) {
   2208   InitState init;
   2209   init.extensions = "GL_ANGLE_depth_texture";
   2210   init.gl_version = "opengl es 2.0";
   2211   init.has_alpha = true;
   2212   init.has_depth = true;
   2213   init.request_alpha = true;
   2214   init.request_depth = true;
   2215   init.bind_generates_resource = true;
   2216   InitDecoder(init);
   2217 
   2218   SetupDefaultProgram();
   2219   SetupAllNeededVertexBuffers();
   2220   const GLenum attachment = GL_DEPTH_ATTACHMENT;
   2221   const GLenum target = GL_TEXTURE_2D;
   2222   const GLint level = 0;
   2223   DoBindTexture(target, client_texture_id_, kServiceTextureId);
   2224 
   2225   // Create a depth texture.
   2226   DoTexImage2D(target,
   2227                level,
   2228                GL_DEPTH_COMPONENT,
   2229                1,
   2230                1,
   2231                0,
   2232                GL_DEPTH_COMPONENT,
   2233                GL_UNSIGNED_INT,
   2234                0,
   2235                0);
   2236 
   2237   // Enable GL_SCISSOR_TEST to make sure we disable it in the clear,
   2238   // then re-enable it.
   2239   DoEnableDisable(GL_SCISSOR_TEST, true);
   2240 
   2241   EXPECT_CALL(*gl_, GenFramebuffersEXT(1, _)).Times(1).RetiresOnSaturation();
   2242   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, _))
   2243       .Times(1)
   2244       .RetiresOnSaturation();
   2245 
   2246   EXPECT_CALL(*gl_,
   2247               FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER_EXT,
   2248                                       attachment,
   2249                                       target,
   2250                                       kServiceTextureId,
   2251                                       level))
   2252       .Times(1)
   2253       .RetiresOnSaturation();
   2254   EXPECT_CALL(*gl_, CheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER_EXT))
   2255       .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE))
   2256       .RetiresOnSaturation();
   2257 
   2258   EXPECT_CALL(*gl_, ClearStencil(0)).Times(1).RetiresOnSaturation();
   2259   SetupExpectationsForStencilMask(-1, -1);
   2260   EXPECT_CALL(*gl_, ClearDepth(1.0f)).Times(1).RetiresOnSaturation();
   2261   SetupExpectationsForDepthMask(true);
   2262   SetupExpectationsForEnableDisable(GL_SCISSOR_TEST, false);
   2263 
   2264   EXPECT_CALL(*gl_, Clear(GL_DEPTH_BUFFER_BIT)).Times(1).RetiresOnSaturation();
   2265 
   2266   SetupExpectationsForRestoreClearState(0.0f, 0.0f, 0.0f, 0.0f, 0, 1.0f, true);
   2267 
   2268   EXPECT_CALL(*gl_, DeleteFramebuffersEXT(1, _)).Times(1).RetiresOnSaturation();
   2269   EXPECT_CALL(*gl_, BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0))
   2270       .Times(1)
   2271       .RetiresOnSaturation();
   2272 
   2273   SetupExpectationsForApplyingDefaultDirtyState();
   2274   EXPECT_CALL(*gl_, DrawArrays(GL_TRIANGLES, 0, kNumVertices))
   2275       .Times(1)
   2276       .RetiresOnSaturation();
   2277   DrawArrays cmd;
   2278   cmd.Init(GL_TRIANGLES, 0, kNumVertices);
   2279   EXPECT_EQ(error::kNoError, ExecuteCmd(cmd));
   2280   EXPECT_EQ(GL_NO_ERROR, GetGLError());
   2281 }
   2282 
   2283 }  // namespace gles2
   2284 }  // namespace gpu
   2285